/*
Author: Pathfinder Solutions, India
Desc: The Form validation script for any type of form
*/

	
	function fnEmailCheck (emailStr) {
	var emailPat=/^(.+)@(.+)$/
	
	var specialChars="\\(\\)<>@,;:~^\\\\\\\"\\.\\[\\]"
	
	var validChars="\[^\\s" + specialChars + "\]"
	
	var quotedUser="(\"[^\"]*\")"
	
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	
	var atom=validChars + '+'
	
	var word="(" + atom + "|" + quotedUser + ")"
	
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
	
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
	
	
	var matchArray=emailStr.match(emailPat)
	if (matchArray==null) 
	 {
		 //alert("Email address seems incorrect (check @ and .'s)") // VIKRNT
	 	 return false
	 }
	var user=matchArray[1]
	var domain=matchArray[2]
	
	// See if "user" is valid
	if (user.match(userPat)==null) {
		// user is not valid
		//alert("The username doesn't seem to be valid.") // VIKRANT
		return false
	}
	
	/* if the e-mail address is at an IP address (as opposed to a symbolic
	   host name) make sure the IP address is valid. */
	var IPArray=domain.match(ipDomainPat)
	if (IPArray!=null) {
		// this is an IP address
	   for (var i=1;i<=4;i++) {
		 if (IPArray[i]>255) {
			 //alert("Destination IP address is invalid!") // VIKRANT
	  return false
		 }
		}
		return true
	}
	
	// Domain is symbolic name
	var domainArray=domain.match(domainPat)
	if (domainArray==null) {
	 //alert("The domain name doesn't seem to be valid.") // VIKRANT
		return false
	}
	
	var atomPat=new RegExp(atom,"g")
	var domArr=domain.match(atomPat)
	var len=domArr.length
	if (domArr[domArr.length-1].length<2 ||
		domArr[domArr.length-1].length>4) {
	   // the address must end in a two letter or three letter word.
	   //alert("The address must end in a three-letter domain, or two letter country.") //VIKRANT
	   return false
	}
	
	// Make sure there's a host name preceding the domain.
	if (len<2) {
	   var errStr="This address is missing a hostname!"
	   //alert(errStr) // VIKRANT
	   return false
	}
	// If we've got this far, everything's valid!
	return true;
	}
	
	function validateUSDate(strValue)
	{
		var objRegExp = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/
		
		//check to see if in correct format
		if(!objRegExp.test(strValue))
			return false; //doesn't match pattern, bad date
		else
		{
			var strSeparator = strValue.substring(2,3) 
			
			var arrayDate = strValue.split(strSeparator); 
			//create a lookup for months not equal to Feb.
			var arrayLookup = { '01' : 31,'03' : 31, 
								'04' : 30,'05' : 31,
								'06' : 30,'07' : 31,
								'08' : 31,'09' : 30,
								'10' : 31,'11' : 30,'12' : 31}
			var intDay = parseInt(arrayDate[1],10); 
		
			//check if month value and day value agree
			alert("dada"+arrayLookup[arrayDate[0]]);
			if(arrayLookup[arrayDate[0]] != null)
			{
			  if(intDay <= arrayLookup[arrayDate[0]] && intDay != 0)
				return true; //found in lookup table, good date
			}
		
			//check for February (bugfix 20050322)
			//bugfix  for parseInt kevin
			//bugfix  biss year  O.Jp Voutat
			var intMonth = parseInt(arrayDate[0],10);
			if (intMonth == 2)
			{ 
				var intYear = parseInt(arrayDate[2]);
				if (intDay > 0 && intDay < 29)
				{
					return true;
				}
				else if (intDay == 29)
				{
					if ((intYear % 4 == 0) && (intYear % 100 != 0) || (intYear % 400 == 0))
					{
						// year div by 4 and ((not div by 100) or div by 400) ->ok
						return true;
					}   
				}
			}
		}  
		return false; //any other values, bad date
	}
	function ValidateCurrency(str)
	{
		if (str <= 0)
			return false;
		return /^[$]?\d{1,10}(\.\d{1,4})?$/.test(str);
	}
	function fnValidatePhone(strPhoneNumber)
	{
		var objRegExp  = /^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/
        var validPhone = objRegExp.test(strPhoneNumber);
        if (!validPhone)
        {
          return false;
        }
		return true;
	}
	
	function fnValidateString(string)
	{
		var strLen = string.length
		var iChars = "*!|,.\":<>[]{}`\';()@&$#%_-~^+?/=\\";
		for (var i = 0; i < strLen; i++) 
		{
			if (iChars.indexOf(string.charAt(i)) != -1) 
			{
				return false;
			}
		}
		if (string != "")
		{
			var pattern =new RegExp(/\d/);
			if (pattern.test(string))
				return false;
		}
		return true;
	}
	
	function fnValidateBlankSpace(string)
	{
		if(string == " ")
			return true;
		else
			return false;
	}
	
	function fnValidateName(string)
	{
		strNumVal = ((/[0-9]/.test(string)));
		strCharVal = ((/[a-zA-Z]/.test(string)));
		//strInvalideChar = ((/[`_~!@#$%&,.:;<>{}=\/\"\/\*\/\^\/\+\/\|\/\/]/.test(string)));
		
		/*if( strNumVal == true || strInvalideChar == true)
		{
			return false;
		}*/
		if( strCharVal == true || strNumVal == true)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	function fnValidateOnlyName(string)
	{
		strCharVal = ((/[a-zA-Z]/.test(string)));
		if( strCharVal == true)
		{
			return true;
		}
		else
		{
			if(string == "")
				return true;
			else
				return false;
		}
	}
	
	function fnValidateSpecialChar(string)
	{
		var iChars = /[`_\-~!@#$%&,.:;<>{}()=\/\"\/\*\/\^\/\+\/\|\/\/]/
        var SpecialChar = iChars.test(string);
		if (SpecialChar) 
		{
			return false;
		}
		return true;
	}
	
	function fnValidateChatId(string)
	{
		var iChars = /[`\~!@#$%&,:;<>{}()=\/\"\/\*\/\^\/\+\/\|\/\/]/
        var SpecialChar = iChars.test(string);
		if (SpecialChar) 
		{
			return false;
		}
		return true;
	}
	
	function fnValidatePhoneNo(phone)
	{
		strNumVal = ((/[0-9]/.test(phone)));
		strCharVal = ((/[a-zA-Z]/.test(phone)));
		strInvalideChar = ((/[`_~!@#$%&,.:;<>{}=\/\"\/\*\/\^\/\+\/\|\/\/]/.test(phone)));
		
		if( strCharVal == true || strInvalideChar == true)
		{
			return false;
		}
		if( strNumVal == true)
		{
			return true;
		}
		return false;
	}
	
	function trim(stringToTrim) 
	{
		return stringToTrim.replace(/^\s+|\s+$/g,"");
	}
	
	function fnIsNumber(intNumber)
	{
		if(isNaN(intNumber) == true)	
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	//  End -->
	
	/*
	FUNCTION: fnValidateInput
	PARAMS: form, input type
	*/
	function fnValidateInputType(theElement, strType)
	{
		var theForm = theElement, z = 0;
		var intChkCount = 0;
		for(z=0; z<theForm.length;z++)
		{
			if(theForm[z].type == strType)
			{
				switch (strType)
				{
					case "radio": if (theForm[z].checked == true)
								  {
									  intChkCount = intChkCount+1;
								  }
								  break;

					case "checkbox":  if (theForm[z].checked == true)
									  {
										  intChkCount = intChkCount+1;
									  }
									  break;
				}
			}
		  }
		  return intChkCount;
	}

	
	
	var lightbox = 1;
	function fnValidateForm(obj)
	{
		var frm = obj;
		var len = frm.elements.length;
		var errormsg = "";
		
		var noserrors = 0
		if(lightbox == 1)
			var linebreak = "<br /><img src='images/error_small.jpg' alt='error' border='0'  /> ";
		else
			var linebreak = "\n";
			
		for(var i=0 ; i<len ; i++) 
		{		
			var eletype = frm.elements[i].type;

			if( (eletype.toLowerCase() != "submit") && (eletype.toLowerCase() != "hidden"))
			{
				if(frm.elements[i].alt != "");
				{
					var arrValue = new Array();
					
					try
					{
						var strValue = "";
						
						if(frm.elements[i].type == 'textarea')
						{
							strValue = frm.elements[i].title;
						}
						else
						{
							strValue = frm.elements[i].alt;
						}
						
						if(frm.elements[i].type == 'select-one')
						{
							strValue = frm.elements[i].title;
						}

						var eleValue =  trim(frm.elements[i].value);

						if(frm.elements[i].type == 'checkbox')
						{
							eleValue = frm.elements[i].checked;
						}
						
						if(frm.elements[i].type == 'radio')
						{
							eleValue = frm.elements[i].checked;
						}
						
						arrValue = strValue.split(/:/);
						var vallength = arrValue.length
						if(vallength > 0)
						{
							for(var j=0 ; j<vallength ; j++) 
							{
								//alert(arrValue[j]);
								var formula = trim(arrValue[j]);
								var arrFormula = formula.split("-");
								if(arrFormula[0] == "M") // MANDATORY FIELD VALIDATION
								{
									if(eleValue == "")
									{
										errormsg = errormsg +  linebreak + arrFormula[1];
										noserrors++;
									}
								}	
								
								if(arrFormula[0] == "E") // EMAIL VALIDATION
								{
									if(eleValue != "")
									{
										if(!fnEmailCheck(eleValue))
										{
											errormsg = errormsg +  linebreak + arrFormula[1];
											noserrors++;
										}
									}
								}	
								
								if(arrFormula[0] == "CHAR") // EMAIL VALIDATION
								{
									if(eleValue != "")
									{
										if(!fnValidateString(eleValue))
										{
											errormsg = errormsg +  linebreak + arrFormula[1];
											noserrors++;
										}
									}
								}
								
								if(arrFormula[0] == "BLANK") // EMAIL VALIDATION
								{
									if(eleValue == "")
									{
										if(!fnValidateBlankSpace(eleValue))
										{
											errormsg = errormsg +  linebreak + arrFormula[1];
											noserrors++;
										}
									}
								}

								if(arrFormula[0] == "PH") // PHONE VALIDATION
								{
									if(eleValue != "")
									{
										if(!fnValidatePhoneNo(eleValue))
										{
											errormsg = errormsg +  linebreak + arrFormula[1];
											noserrors++;
										}
									}
								}

								if(arrFormula[0] == "SEL") // SELECT BOX VALIDATION
								{
									
									if(eleValue != "")
									{
										if(eleValue == "")
										{
											errormsg = errormsg +  linebreak + arrFormula[1];
											noserrors++;
										}
									}
								}	

								if(arrFormula[0] == "CHK") // CHECK BOX FIELD VALIDATION
								{
									if(eleValue == false)
									{
										errormsg = errormsg +  linebreak + arrFormula[1];
										noserrors++;
									}
								}	

								if(arrFormula[0] == "RDO") // CHECK BOX FIELD VALIDATION
								{
									if(eleValue == false)
									{
										errormsg = errormsg +  linebreak + arrFormula[1];
										noserrors++;
									}
								}	

								if(arrFormula[0] == "NUM") // MANDATORY FIELD VALIDATION
								{
									if(eleValue != "")
									{
										if(!fnIsNumber(eleValue))
										{
											errormsg = errormsg +  linebreak + arrFormula[1];	
											noserrors++;
										}
									}
								}	
								
								if(arrFormula[0] == "MIN") // MINIMUM LIMIT
								{
									if(eleValue != "")
									{
										var string = eleValue;
										var limit = string.length;
										if(limit < parseInt(arrFormula[1]))
										{
											errormsg = errormsg + linebreak + arrFormula[2];
											noserrors++;
										}
									}
								}
								
								if(arrFormula[0] == "MAX") // MAXIMUM LIMIT
								{
									var string = eleValue;
									var limit = string.length;
									if(limit > parseInt(arrFormula[1]))
									{
										errormsg = errormsg +  linebreak + arrFormula[2];	
										noserrors++;
									}
								}
								
								if(arrFormula[0] == "BET") // BETWEEN LIMIT
								{
									var string = eleValue;
									var limit = string.length;
									if(limit < parseInt(arrFormula[1]) && limit > parseInt(arrFormula[2]))
									{
										errormsg = errormsg +  linebreak + arrFormula[3];
										noserrors++;
									}
								}	
								
								if(arrFormula[0] == "COMP") // COMPARISION BETWEEN TWO FIELDS
								{
									var compvalue = trim(document.getElementById(arrFormula[2]).value)
									if(compvalue)
									{
										if(arrFormula[1] == "==")
										{
											if(eleValue != compvalue)
											{
												errormsg = errormsg +  linebreak + arrFormula[3];
												noserrors++;
											}
										}
										if(arrFormula[1] == "!=")
										{
											if(eleValue == compvalue)
											{
												errormsg = errormsg +  linebreak + arrFormula[3];
												noserrors++;
											}
										}
									}
								}//
								
								if(arrFormula[0] == "CMPVALUE") // DUPLICATE VALIDATION
								{
									if(arrFormula[1] != eleValue)
									{
										errormsg = errormsg +  linebreak + arrFormula[2];
										noserrors++;
									}
								}
								if(arrFormula[0] == "MAXNUM") // DUPLICATE VALIDATION
								{
									if(eleValue < arrFormula[1])
									{
										errormsg = errormsg +  linebreak + arrFormula[2];
										noserrors++;
									}
								}
								
								if(arrFormula[0] == "CURR") // DUPLICATE VALIDATION
								{
									if(!ValidateCurrency(eleValue))
									{
										errormsg = errormsg +  linebreak + arrFormula[1];
										noserrors++;
									}
								}
								
								if(arrFormula[0] == "COMPOR") //COMPAIR TWO FIELDS
								{
									if(document.getElementById(arrFormula[1]).value != "" && document.getElementById(arrFormula[2]).value != "")
									{
										errormsg = errormsg +  linebreak + arrFormula[3];
										noserrors++;
									}
									if(document.getElementById(arrFormula[1]).value == "" && document.getElementById(arrFormula[2]).value == "")
									{
										errormsg = errormsg +  linebreak + arrFormula[3];
										noserrors++;
									}
								}
								if(arrFormula[0] == "DEPEND") //COMPAIR TWO FIELDS
								{
									if(eleValue != "" && document.getElementById(arrFormula[1]).value == "")
									{
										errormsg = errormsg +  linebreak + arrFormula[2];
										noserrors++;
									}
									else if(eleValue == "" && document.getElementById(arrFormula[1]).value != "")
									{
										errormsg = errormsg +  linebreak + arrFormula[2];
										noserrors++;
									}
								}
								if(arrFormula[0] == "INPUTFIELD") //VALIDATE FIELD TYPE
								{
									if(fnValidateInputType(obj, arrFormula[1]) == 0)
									{
										errormsg = errormsg +  linebreak + arrFormula[2];
										noserrors++;
									}
								}
								if(arrFormula[0] == "SPECHAR") //VALIDATE FIELD TYPE
								{
									if(!fnValidateSpecialChar(eleValue))
									{
										errormsg = errormsg +  linebreak + arrFormula[1];
										noserrors++;
									}
								}
								
								if(arrFormula[0] == "NAME") // NAME VALIDATION
								{
									if(!fnValidateName(eleValue))
									{
										errormsg = errormsg +  linebreak + arrFormula[1];
										noserrors++;
									}
								}
								
								if(arrFormula[0] == "ONLYNAME") // NAME VALIDATION
								{
									if(!fnValidateOnlyName(eleValue))
									{
										errormsg = errormsg +  linebreak + arrFormula[1];
										noserrors++;
									}
								}
								
								
								if(arrFormula[0] == "CHATID") //VALIDATE FIELD TYPE
								{
									if(!fnValidateChatId(eleValue))
									{
										errormsg = errormsg +  linebreak + arrFormula[1];
										noserrors++;
									}
								}
								if(arrFormula[0] == "URL") //VALIDATE FIELD TYPE
								{
									if(!fnValidateUrl(eleValue) && eleValue != "")
									{
										errormsg = errormsg +  linebreak + arrFormula[1];
										noserrors++;
									}
								}
								if(arrFormula[0] == "SN") //VALIDATE FIELD TYPE
								{
									if(!fnValidateSiteNAme(eleValue) && eleValue != "")
									{
										errormsg = errormsg +  linebreak + arrFormula[1];
										noserrors++;
									}
								}
							}//
						}
					}
					catch(e)
					{ 
						//
					}
				}	
			}					 
		}
		
		
		if(errormsg == "") 
		{
			return true;			
		}
		else 
		{
			if(lightbox == 1)	
			{
				fnLightBoxError(errormsg, noserrors);
			}
			else
			{
				alert(errormsg);	
			}
			return false;
		}
		
	}

function fnLightBoxError(errormsg, noserrors)
{
	var lightheight = 240;
	var forheight = 0;
	if(noserrors > 3)
	{
		forheight = (noserrors - 3);
		subheight = (25 * forheight);
		lightheight = lightheight + subheight;
	}
	var warning = "<div id='errormessage' align='left' ><div style='border-bottom:#329845 1px solid'><img src='images/errorheadernew.JPG' alt='warning' border='0'  /></div><br />";
	warning =  warning + errormsg;
	warning = warning + "<br /><br /><br /><center><input type='button' value='Ok' class='commit' style='width:40px;' onclick='Lightbox.hideBox()' /></center></div>";
	
	/*var warning = "<div id='errormessage' align='left'><img src='images/warning_small.jpg' alt='warning' border='0'  /><br />";
	warning =  warning + errormsg;
	warning = warning + "<br /><br /><br /><center><input type='button' value='Ok' class='commit' style='width:40px;' onclick='Lightbox.hideBox()' /></center></div>";*/
	Lightbox.showBoxString(warning,420,lightheight);
}

function fnValidateUrl(strUrl)
{
     var tomatch= /http:\/\/[A-Za-z0-9\.-]{3,}\.[A-Za-z]/
     if (tomatch.test(strUrl))
     {
         return true;
     }
     else
     {
         return false; 
     }
}

function fnValidateSiteNAme(strSiteName)
{
     var tomatch= /www.[A-Za-z0-9\.-]{3,}\.[A-Za-z]/
     if (tomatch.test(strSiteName))
     {
         return true;
     }
     else
     {
         return false; 
     }
}

