// javascript validation library 

// written by neelmani chitransi

// for origin softech pvt ltd.

// all right reserved.

// do not modify this script

// this script use is intended to only use on the softwar developed by originsoftech.

// library path url=http://www.originsoftech.biz/library/javascript/jlib.js

	//	formname: name of the form 

	// selectionbox: id of the check box click on which all the check box of the form will get selected 

	//

	function CheckAll(formname,selectionbox)
	{

		var elements = document.forms[formname].elements.length;

		for(j=0;j<elements;j++)

			{
				var x= document.forms[formname].elements[j];

				//alert(x.type);

				if (x.type=="checkbox")

				{

					if(x.name != selectionbox)

					{

						if (x.checked==true)

						{

							x.checked=false;

						}

						else

						{

							x.checked=true;

						}

					}

				}

			}

	}

	

function ValidateForm(formname,checkarray)

	{

			var emailfilter=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i

			var usPhone =/^\d{3}-\d{3}-\d{4}$/

			errmsgshow='';

			errorcolor="#ff0000";

			var elements = document.forms[formname].elements.length;

			for(j=0;j<elements;j++)

			{

				var x= document.forms[formname].elements[j];

				if (x.type!="submit" && x.type!="reset" && x.type!="button")

				{

						x.style.backgroundColor="#ffffff";

				}

			}

			a=checkarray.length;

			for(i=0;i<a;i++)

			{	

				var msg1='';

				msg1=checkarray[i];

				brk=msg1.split('|');

				try

				{

						curid=document.getElementById(brk[0]); // brk[0] the name of the field to be checked 

						errtype=brk[2];

						errmsg=brk[1];

						if(brk.length>3)

						{

							limitlow=brk[3];

							limithigh=brk[4];

						}

						if(brk.length>3)

						{

							textlow=brk[3];

							texthigh=brk[4];

						}

						switch(errtype)

						{

							case "blank":

									{

										if (curid.value.length<=0)

										{

											errmsgshow+=errmsg +"\n";

											curid.style.backgroundColor=errorcolor;

										}

									}

									break;

							case "checked":

									{

										if (!curid.checked)

										{

											errmsgshow+=errmsg +"\n";

											curid.style.backgroundColor=errorcolor;

										}

									}

									break;

							case "email":

									{

										var returnval=emailfilter.test(curid.value)

										if (returnval==false){

										errmsgshow+=errmsg +"\n";

										curid.style.backgroundColor=errorcolor;

										}

									}

									break;

							case "usphone":

									{

										var returnval=usPhone.test(curid.value)

										if (returnval==false){

										errmsgshow+=errmsg +"\n";

										curid.style.backgroundColor=errorcolor;

										}

									}

									break;

							case "number":

									{

										if (isNaN(curid.value)){

										errmsgshow+=errmsg +"\n";

										curid.style.backgroundColor=errorcolor;

										}

									}

									break;

							case "float":

									{

										if (isNaN(curid.value)){

										errmsgshow+=errmsg +"\n";

										curid.style.backgroundColor=errorcolor;

										}

									}

									break;

							case "text":

									{

										if (!isNaN(curid.value)){

										errmsgshow+=errmsg +"\n";

										curid.style.backgroundColor=errorcolor;

										}

									}

									break;

		

							case "negative":

									{

										if (curid.value<0){

										errmsgshow+=errmsg +"\n";

										curid.style.backgroundColor=errorcolor;

										}

									}

									break;

							case "numberlimit":

									{

										if ( ! (curid.value>=limitlow && curid.value<=limithigh))

										{

										errmsgshow+=errmsg +"\n";

										curid.style.backgroundColor=errorcolor;

										}

									}

									break;
									
							case "numlowlimit":

									{
										//alert(curid.value +"===="+ limitlow);
										val=parseInt(curid.value,0);	
										if ((val<limitlow))

										{

										errmsgshow+=errmsg +"\n";

										curid.style.backgroundColor=errorcolor;

										}

									}

									break;		

							case "match":

									{

										if ( !(curid.value==document.getElementById(brk[3]).value))

										{

										errmsgshow+=errmsg +"\n";

										curid.style.backgroundColor=errorcolor;

										}

									}

									break;
							case "notfound":

									{
								if(curid.value.length>0)
								{
										if(brk[3].search(':'))
										{	found=false;			 
											option= brk[3].split(':');
											arrlength =option.length;
											
											for(j=0;j<arrlength;j++)
											{
												//alert(curid.value +'      ' + option[j]);
												//alert(curid.value.search(option[j]));
												if ((curid.value.search(option[j])!=-1)&&(curid.value.length>0) )
												{
													found=true;
													//alert('found');
													break;
												}
												
											}
											if(found==false)
											{
													errmsgshow+=errmsg +"\n";
													curid.style.backgroundColor=errorcolor;
											}
										}
										else
										{
											if ((curid.value.search(brk[3])==-1)&&(curid.value.length>0) )
	
											{
	
												errmsgshow+=errmsg +"\n";
		
												curid.style.backgroundColor=errorcolor;
	
											}
										}
								}

							}

									break;

							case "textlengthlimit":

									{

										if ( !(curid.value.length >= textlow && curid.value.length<=texthigh))

										{

										errmsgshow+=errmsg +"\n";

										curid.style.backgroundColor=errorcolor;

										}

									}

									break;

							case "dateUS":

									{

										//mm/dd/yyyy

										var validformat=/^\d{2}\/\d{2}\/\d{4}$/ //Basic check for format validity

										if (!validformat.test(curid.value))

										{

											errmsgshow+=errmsg +"\n";

											curid.style.backgroundColor=errorcolor;

										}

										else

										{ //Detailed check for valid date ranges

											var monthfield=curid.value.split("/")[0]

											var dayfield=curid.value.split("/")[1]

											var yearfield=curid.value.split("/")[2]

											var dayobj = new Date(yearfield, monthfield-1, dayfield)

											if ((dayobj.getMonth()+1!=monthfield)||(dayobj.getDate()!=dayfield)||(dayobj.getFullYear()!=yearfield))

											{

												errmsgshow+=errmsg +"\n";

												curid.style.backgroundColor=errorcolor;

												break;

											}

										}

									}

									break;

							case "dateUK":

									{

										//dd/mm/yyyy

										var validformat=/^\d{2}\/\d{2}\/\d{4}$/ //Basic check for format validity

										if (!validformat.test(curid.value))

										{

											errmsgshow+=errmsg +"\n";

											curid.style.backgroundColor=errorcolor;

										}

										else

										{ //Detailed check for valid date ranges

											var monthfield=curid.value.split("/")[1]

											var dayfield=curid.value.split("/")[0]

											var yearfield=curid.value.split("/")[2]

											var dayobj = new Date(yearfield, monthfield-1, dayfield)

											if ((dayobj.getMonth()+1!=monthfield)||(dayobj.getDate()!=dayfield)||(dayobj.getFullYear()!=yearfield))

											{

												errmsgshow+=errmsg +"\n";

												curid.style.backgroundColor=errorcolor;

												break;

											}

										}

									}

									break;

							case "dateEU":

									{	// yyyy/mm/dd

										var validformat=/^\d{4}\/\d{2}\/\d{2}$/ //Basic check for format validity

										if (!validformat.test(curid.value))

										{

											errmsgshow+=errmsg +"\n";

											curid.style.backgroundColor=errorcolor;

										}

										else

										{ //Detailed check for valid date ranges

											var monthfield=curid.value.split("/")[1]

											var dayfield=curid.value.split("/")[2]

											var yearfield=curid.value.split("/")[0]

											var dayobj = new Date(yearfield, monthfield-1, dayfield)

											if ((dayobj.getMonth()+1!=monthfield)||(dayobj.getDate()!=dayfield)||(dayobj.getFullYear()!=yearfield))

											{

												errmsgshow+=errmsg +"\n";

												curid.style.backgroundColor=errorcolor;

												break;

											}

										}

									}

									break;					

						}

				}

				catch(err)

				{

						 txt="There is an error on this page.\n\n";

						  txt+="Error description: " + err.description +"\n\n"+msg1+ "\n\n";

						  txt+="Click OK to continue.\n\n";

						  alert(txt);



				}

			}

			if (errmsgshow.length >0)

			{

				errors="Following errors encountred Please correct Them \n";

				errors+=errmsgshow;

				alert (errors);

				return false;

			}

			else

			{

				return true;

			}

			//	alert (errmsgshow);

	}

// this function has option to set color



function ValidateFormWithColor(formname,checkarray,errorcolor,clearcolor)

	{

			var emailfilter=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i

			var usPhone =/^\d{3}-\d{3}-\d{4}$/

			errmsgshow='';

			//errorcolor="#ff0000";

			var elements = document.forms[formname].elements.length;

			for(j=0;j<elements;j++)

			{

				var x= document.forms[formname].elements[j];

				if (x.type!="submit" && x.type!="reset" && x.type!="button")

				{

						x.style.backgroundColor=clearcolor;

				}

			}

			a=checkarray.length;

			for(i=0;i<a;i++)

			{	

				var msg1='';

				msg1=checkarray[i];

				brk=msg1.split('|');

				try

				{

						curid=document.getElementById(brk[0]); // brk[0] the name of the field to be checked 

						errtype=brk[2];

						errmsg=brk[1];

						if(brk.length>3)

						{

							limitlow=brk[3];

							limithigh=brk[4];

						}

						if(brk.length>3)

						{

							textlow=brk[3];

							texthigh=brk[4];

						}

						switch(errtype)

						{

							case "blank":

									{

										if (curid.value.length<=0)

										{

											errmsgshow+=errmsg +"\n";

											curid.style.backgroundColor=errorcolor;

										}

									}

									break;

							case "checked":

									{

										if (!curid.checked)

										{

											errmsgshow+=errmsg +"\n";

											curid.style.backgroundColor=errorcolor;

										}

									}

									break;

							case "email":

									{

										var returnval=emailfilter.test(curid.value)

										if (returnval==false){

										errmsgshow+=errmsg +"\n";

										curid.style.backgroundColor=errorcolor;

										}

									}

									break;

							case "usphone":

									{

										var returnval=usPhone.test(curid.value)

										if (returnval==false){

										errmsgshow+=errmsg +"\n";

										curid.style.backgroundColor=errorcolor;

										}

									}

									break;

							case "number":

									{

										if (isNaN(curid.value)){

										errmsgshow+=errmsg +"\n";

										curid.style.backgroundColor=errorcolor;

										}

									}

									break;

							case "float":

									{

										if (isNaN(curid.value)){

										errmsgshow+=errmsg +"\n";

										curid.style.backgroundColor=errorcolor;

										}

									}

									break;

							case "text":

									{

										if (!isNaN(curid.value)){

										errmsgshow+=errmsg +"\n";

										curid.style.backgroundColor=errorcolor;

										}

									}

									break;

		

							case "negative":

									{

										if (curid.value<0){

										errmsgshow+=errmsg +"\n";

										curid.style.backgroundColor=errorcolor;

										}

									}

									break;

							case "numberlimit":

									{

										if ( ! (curid.value>=limitlow && curid.value<=limithigh))

										{

										errmsgshow+=errmsg +"\n";

										curid.style.backgroundColor=errorcolor;

										}

									}

									break;

							case "match":

									{

										if ( !(curid.value==document.getElementById(brk[3]).value))

										{

										errmsgshow+=errmsg +"\n";

										curid.style.backgroundColor=errorcolor;

										}

									}

									break;

							case "textlengthlimit":

									{

										if ( !(curid.value.length >= textlow && curid.value.length<=texthigh))

										{

										errmsgshow+=errmsg +"\n";

										curid.style.backgroundColor=errorcolor;

										}

									}

									break;

							case "dateUS":

									{

										//mm/dd/yyyy

										var validformat=/^\d{2}\/\d{2}\/\d{4}$/ //Basic check for format validity

										if (!validformat.test(curid.value))

										{

											errmsgshow+=errmsg +"\n";

											curid.style.backgroundColor=errorcolor;

										}

										else

										{ //Detailed check for valid date ranges

											var monthfield=curid.value.split("/")[0]

											var dayfield=curid.value.split("/")[1]

											var yearfield=curid.value.split("/")[2]

											var dayobj = new Date(yearfield, monthfield-1, dayfield)

											if ((dayobj.getMonth()+1!=monthfield)||(dayobj.getDate()!=dayfield)||(dayobj.getFullYear()!=yearfield))

											{

												errmsgshow+=errmsg +"\n";

												curid.style.backgroundColor=errorcolor;

												break;

											}

										}

									}

									break;

							case "dateUK":

									{

										//dd/mm/yyyy

										var validformat=/^\d{2}\/\d{2}\/\d{4}$/ //Basic check for format validity

										if (!validformat.test(curid.value))

										{

											errmsgshow+=errmsg +"\n";

											curid.style.backgroundColor=errorcolor;

										}

										else

										{ //Detailed check for valid date ranges

											var monthfield=curid.value.split("/")[1]

											var dayfield=curid.value.split("/")[0]

											var yearfield=curid.value.split("/")[2]

											var dayobj = new Date(yearfield, monthfield-1, dayfield)

											if ((dayobj.getMonth()+1!=monthfield)||(dayobj.getDate()!=dayfield)||(dayobj.getFullYear()!=yearfield))

											{

												errmsgshow+=errmsg +"\n";

												curid.style.backgroundColor=errorcolor;

												break;

											}

										}

									}

									break;

							case "dateEU":

									{	// yyyy/mm/dd

										var validformat=/^\d{4}\/\d{2}\/\d{2}$/ //Basic check for format validity

										if (!validformat.test(curid.value))

										{

											errmsgshow+=errmsg +"\n";

											curid.style.backgroundColor=errorcolor;

										}

										else

										{ //Detailed check for valid date ranges

											var monthfield=curid.value.split("/")[1]

											var dayfield=curid.value.split("/")[2]

											var yearfield=curid.value.split("/")[0]

											var dayobj = new Date(yearfield, monthfield-1, dayfield)

											if ((dayobj.getMonth()+1!=monthfield)||(dayobj.getDate()!=dayfield)||(dayobj.getFullYear()!=yearfield))

											{

												errmsgshow+=errmsg +"\n";

												curid.style.backgroundColor=errorcolor;

												break;

											}

										}

									}

									break;					

						}

				}

				catch(err)

				{

						 txt="There is an error on this page.\n\n";

						  txt+="Error description: " + err.description +"\n\n"+msg1+ "\n\n";

						  txt+="Click OK to continue.\n\n";

						  alert(txt);



				}

			}

			if (errmsgshow.length >0)

			{

				errors="Following errors encountred Please correct Them \n";

				errors+=errmsgshow;

				alert (errors);

				return false;

			}

			else

			{

				return true;

			}

			//	alert (errmsgshow);

	}

	

// this function will return the resul in an htmlformat to a target control where you 

// want to display the error message

//

//

function ValidateFormDiv(formname,checkarray,errdiv)

	{

			var emailfilter=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i

			var usPhone =/^\d{3}-\d{3}-\d{4}$/

			errmsgshow='';

			errorcolor="#ff0000";

			var elements = document.forms[formname].elements.length;

			for(j=0;j<elements;j++)

			{

				var x= document.forms[formname].elements[j];

				if (x.type!="submit" && x.type!="reset" && x.type!="button")

				{

						x.style.backgroundColor="#ffffff";

				}

			}

			a=checkarray.length;

			for(i=0;i<a;i++)

			{	

				var msg1='';

				msg1=checkarray[i];

				brk=msg1.split('|');

				try

				{

						curid=document.getElementById(brk[0]); // brk[0] the name of the field to be checked 

						errtype=brk[2];

						errmsg=brk[1];

						if(brk.length>3)

						{

							limitlow=brk[3];

							limithigh=brk[4];

						}

						if(brk.length>3)

						{

							textlow=brk[3];

							texthigh=brk[4];

						}

						switch(errtype)

						{

							case "blank":

									{

										if (curid.value.length<=0)

										{

											errmsgshow+=errmsg +"<br />";

											curid.style.borderColor=errorcolor;

										}

									}

									break;

							case "checked":

									{

										if (!curid.checked)

										{

											errmsgshow+=errmsg +"<br />";

											curid.style.borderColor=errorcolor;

										}

									}

									break;

							case "email":

									{

										var returnval=emailfilter.test(curid.value)

										if (returnval==false){

										errmsgshow+=errmsg +"<br />";

										curid.style.borderColor=errorcolor;

										}

									}

									break;

							case "usphone":

									{

										var returnval=usPhone.test(curid.value)

										if (returnval==false){

										errmsgshow+=errmsg +"<br />";

										curid.style.borderColor=errorcolor;

										}

									}

									break;

							case "number":

									{

										if (isNaN(curid.value)){

										errmsgshow+=errmsg +"<br />";

										curid.style.borderColor=errorcolor;

										}

									}

									break;

							case "float":

									{

										if (isNaN(curid.value)){

										errmsgshow+=errmsg +"<br />";

										curid.style.borderColor=errorcolor;

										}

									}

									break;

							case "text":

									{

										if (!isNaN(curid.value)){

										errmsgshow+=errmsg +"<br />";

										curid.style.borderColor=errorcolor;

										}

									}

									break;

		

							case "negative":

									{

										if (curid.value<0){

										errmsgshow+=errmsg +"<br />";

										curid.style.borderColor=errorcolor;

										}

									}

									break;

							case "numberlimit":

									{

										if ( ! (curid.value>=limitlow && curid.value<=limithigh))

										{

										errmsgshow+=errmsg +"<br />";

										curid.style.borderColor=errorcolor;

										}

									}

									break;

							case "match":

									{

										if ( !(curid.value==document.getElementById(brk[3]).value))

										{

										errmsgshow+=errmsg +"<br />";

										curid.style.borderColor=errorcolor;

										}

									}

									break;

							case "textlengthlimit":

									{

										if ( !(curid.value.length >= textlow && curid.value.length<=texthigh))

										{

										errmsgshow+=errmsg +"<br />";

										curid.style.borderColor=errorcolor;

										}

									}

									break;

							case "dateUS":

									{

										//mm/dd/yyyy

										var validformat=/^\d{2}\/\d{2}\/\d{4}$/ //Basic check for format validity

										if (!validformat.test(curid.value))

										{

											errmsgshow+=errmsg +"<br />";

											curid.style.borderColor=errorcolor;

										}

										else

										{ //Detailed check for valid date ranges

											var monthfield=curid.value.split("/")[0]

											var dayfield=curid.value.split("/")[1]

											var yearfield=curid.value.split("/")[2]

											var dayobj = new Date(yearfield, monthfield-1, dayfield)

											if ((dayobj.getMonth()+1!=monthfield)||(dayobj.getDate()!=dayfield)||(dayobj.getFullYear()!=yearfield))

											{

												errmsgshow+=errmsg +"<br />";

												curid.style.borderColor=errorcolor;

												break;

											}

										}

									}

									break;

							case "dateUK":

									{

										//dd/mm/yyyy

										var validformat=/^\d{2}\/\d{2}\/\d{4}$/ //Basic check for format validity

										if (!validformat.test(curid.value))

										{

											errmsgshow+=errmsg +"<br />";

											curid.style.borderColor=errorcolor;

										}

										else

										{ //Detailed check for valid date ranges

											var monthfield=curid.value.split("/")[1]

											var dayfield=curid.value.split("/")[0]

											var yearfield=curid.value.split("/")[2]

											var dayobj = new Date(yearfield, monthfield-1, dayfield)

											if ((dayobj.getMonth()+1!=monthfield)||(dayobj.getDate()!=dayfield)||(dayobj.getFullYear()!=yearfield))

											{

												errmsgshow+=errmsg +"<br />";

												curid.style.borderColor=errorcolor;

												break;

											}

										}

									}

									break;

							case "dateEU":

									{	// yyyy/mm/dd

										var validformat=/^\d{4}\/\d{2}\/\d{2}$/ //Basic check for format validity

										if (!validformat.test(curid.value))

										{

											errmsgshow+=errmsg +"<br />";

											curid.style.borderColor=errorcolor;

										}

										else

										{ //Detailed check for valid date ranges

											var monthfield=curid.value.split("/")[1]

											var dayfield=curid.value.split("/")[2]

											var yearfield=curid.value.split("/")[0]

											var dayobj = new Date(yearfield, monthfield-1, dayfield)

											if ((dayobj.getMonth()+1!=monthfield)||(dayobj.getDate()!=dayfield)||(dayobj.getFullYear()!=yearfield))

											{

												errmsgshow+=errmsg +"<br />";

												curid.style.borderColor=errorcolor;

												break;

											}

										}

									}

									break;					

						}

				}

				catch(err)

				{

						 txt="There is an error on this page.\n\n";

						  txt+="Error description: " + err.description +"\n\n"+msg1+ "\n\n";

						  txt+="Click OK to continue.\n\n";

						  alert(txt);



				}

			}

			if (errmsgshow.length >0)

			{

				errors="Following errors encountred Please correct Them \n";

				errors+=errmsgshow;

				//alert (errors);

			document.getElementById(errdiv).innerHTML=errors;

				return false;

			}

			else

			{

				return true;

			}

			//	alert (errmsgshow);

	}



function addOption(id1,id2)

{

	var idsrc=document.getElementById(id1);

	var iddest=document.getElementById(id2);

	if(idsrc.value.length<2)

	{

		alert('Please enter a value');

	}

	else

	{

		if(iddest.value.length>0)

		{

			str1=iddest.value;

			if(str1.search(idsrc.value)>=0)

			{

				iddest.value=iddest.value.replace(idsrc.value+'|','');

			}

			else

			{

				iddest.value=iddest.value+idsrc.value+'|';

			}

		}

		else

		{	

				iddest.value=idsrc.value+'|';

		}

	}

}