/*
#########################################################################################

	Validation Script Version - 1.1

	Written By	 			: Gagan Bagga
	Last Updated On 		: 24-Mar-2005
	Purpose					: To validate the form elements
	Functions & Parameters	: 

		Name of the Function		Parameters							Return Type

	1.	validate_form()				Referernce of the form				Boolean
	2.	lr_trim()														String
	3.	chkSplCharacters()			Reference of an text element	
	4.	chkSplFormat()				Reference of an text element

#########################################################################################
*/

var bValid = 1;

function lr_trim()
{
	return this.replace(/^\s+/,'').replace(/\s+$/,'');
}
String.prototype.trim = lr_trim;

function chkSplCharacters(cntrl)
{
	var splChars = "~!@#$%^*()+=[]\;{}|\"?";
    for (var i = 0; i < cntrl.value.length; i++)
    {
		if (splChars.indexOf(cntrl.value.charAt(i)) != -1)
		{
			alert ("Following special characters are not allowed.\n\n" + splChars);
			cntrl.select();
            bValid = 0;
            break;
        }
		if (cntrl.value.indexOf("@#$") != -1)
		{
			alert ("Invalid combination of special characters (\'@#$\')");
			cntrl.select();
            bValid = 0;
            break;
        }
	}
}

function chkSplFormat(cntrl)
{
	var format = cntrl.getAttribute("format").toLowerCase();
	switch(format)
	{
		case 'email':
			var addr = cntrl.value;
			if (!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(addr))
			{
				alert("Invalid Email ID ! Please re-ernter.\n e.g. user@jam.com ");
				cntrl.select();
				bValid = 0;
			}
			break;
		case 'phone/fax':
			var phoneFax = cntrl.value;
			var validChars = ", +-0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
			for(i=0;i < phoneFax.length;i++)
				if(validChars.indexOf(phoneFax.charAt(i)) == -1)
				{
					alert("Please enter a valid " + cntrl.getAttribute("label"));
					cntrl.select();
					bValid = 0;
					break;
				}
			break;
		case 'numeric' :
			val = cntrl.value;
			if(parseFloat(val,10) != (val*1))
			{
				alert("Please enter only numeric value for " + cntrl.getAttribute("label"));
				cntrl.select();
				bValid = 0;
			}
			break;
		case 'currency' :
			val = cntrl.value;
			if(parseFloat(val,10) != (val*1))
			{
				alert("Please enter only numeric value for " + cntrl.getAttribute("label"));
				cntrl.select();
				bValid = 0;
			}			
			if(val <= 0)
			{
				alert("Please enter a " + cntrl.getAttribute("label") + " of at least 1");
				cntrl.select();
				bValid = 0;
			}
			break;
		case 'wholeinteger' :
			val = cntrl.value;
			if(parseFloat(val,10) != (val*1))
			{
				alert("Please enter only numeric value for \"" + cntrl.getAttribute("label") + "\"");
				cntrl.select();
				bValid = 0;
			}			
			if(val <= 0)
			{
				alert("Zero (0) / negative value is not allowed for \"" + cntrl.getAttribute("label") + "\"");
				cntrl.select();
				bValid = 0;
			}
			if(val.indexOf(".") != -1)
			{
				alert("Only whole numbers are allowed for \"" + cntrl.getAttribute("label") + "\"");
				cntrl.select();
				bValid = 0;
			}
			break;
		case 'username':
			var splChars = "~!@#$%^&*()-+=[]\\\;,<>/{}|\":?";
			for (var i = 0; i < cntrl.value.length; i++)
			{
				if (splChars.indexOf(cntrl.value.charAt(i)) != -1)
				{
					alert ("None of the special characters are allowed for " + cntrl.getAttribute("label"));
					cntrl.select();
			        bValid = 0;
			        break;
			    }
			}
			break;
		case 'password':
			var splChars = "~!@#$%^&*()-+=[]\\\;,.<>/{}|\":? ";
			for (var i = 0; i < cntrl.value.length; i++)
			{
				if (splChars.indexOf(cntrl.value.charAt(i)) != -1)
				{
					alert ("Any of the special characters or blank spaces are not allowed for " + cntrl.getAttribute("label"));
					cntrl.select();
			        bValid = 0;
			        break;
			    }
			}
			break;		
	}
}

function doComparision(cntrl1, cntrl2)
{
	if(cntrl1.value != cntrl2.value)
	{
		alert("\"" + cntrl2.getAttribute("label") + "\" and \"" + cntrl1.getAttribute("label") + "\" do not match");
		cntrl1.value="";
		cntrl2.value="";
		cntrl2.focus();
		bValid = 0;
	}
}

function validate_form(oFrm)
{
	var aElements = oFrm.elements;
	bValid = 1;
	for(var i=0;i < aElements.length;i++)
	{
		if(!(aElements[i].disabled || aElements[i].getAttribute("readonly") || aElements[i].getAttribute("with-no-check") != null))
		{
			if(aElements[i].type=="text" || aElements[i].type=="textarea" || aElements[i].type=="password")
			{
				if(aElements[i].type != "password")
					aElements[i].value = aElements[i].value.trim();
				if(aElements[i].value.length > 0)
				{
					if(aElements[i].getAttribute("format") == null)
						chkSplCharacters(aElements[i]);
					else if(aElements[i].getAttribute("format").toLowerCase() != "none")
						chkSplFormat(aElements[i]);
					if(aElements[i].getAttribute("compareWith") != null && bValid==1)
						doComparision(aElements[i], eval(aElements[i].getAttribute("compareWith")));
				}
				else if(aElements[i].getAttribute("required") != null)
				{
					alert("Please enter your \"" + aElements[i].getAttribute("label") + "\"");
					aElements[i].focus();
					bValid = 0;
				}
				if(aElements[i].type=="textarea" && aElements[i].getAttribute("maxlength") != null && bValid==1)
					if(aElements[i].value.length > aElements[i].getAttribute("maxlength"))
					{
						alert(aElements[i].getAttribute("label") + " should not exceed the maximum length of " + aElements[i].getAttribute("maxlength") + " characters");
						aElements[i].focus();
						bValid = 0;
					}
				if(aElements[i].getAttribute("minlength") != null && bValid==1)
					if(aElements[i].value.length < aElements[i].getAttribute("minlength"))
					{
						alert(aElements[i].getAttribute("label") + " should be at least of " + aElements[i].getAttribute("minlength") + " characters");
						aElements[i].focus();
						bValid = 0;
					}
			}
			else if(aElements[i].type=="file")
			{
				if(aElements[i].value.trim() == "")
				{
					if(aElements[i].getAttribute("required") != null)
					{
						alert("Please enter your \"" + aElements[i].getAttribute("label") + "\"");
						aElements[i].select();
						bValid = 0;
						
					}
				}
				else
				{
					var vFileName = aElements[i].value.trim();
					iDotPos = vFileName.lastIndexOf(".")
					if(iDotPos <= 0)
					{
						alert("You must specified the valid value for the field \"" + aElements[i].getAttribute("label") + "\"");
						aElements[i].select();
						bValid = 0;
					}
					else
					{
						if(aElements[i].getAttribute("filter") != null)
						{
							var aValidFileTypes = aElements[i].getAttribute("filter").split("/");
							var bValidFile = false;
							for(var ctr = 0; ctr < aValidFileTypes.length; ctr++)
							{
								var vFileExt = vFileName.substring(iDotPos);
								if(vFileExt.toLowerCase() == aValidFileTypes[ctr])
								{
									bValidFile = true; break;
								}
							}
							if(!bValidFile)
							{
								alert("System allows only the following file types\n\n{" + aElements[i].getAttribute("filter") + "}");
								aElements[i].select();
								bValid = 0;
							}
						}
					}					
				}
				
			}
			else if(aElements[i].type == "select" || aElements[i].type == "select-one") // If it is a combox box
			{
				if(aElements[i][aElements[i].selectedIndex].value=="" && aElements[i].getAttribute("required") != null)
				{
					alert("Please select your \"" + aElements[i].getAttribute("label") + "\"");
					aElements[i].focus();
					bValid = 0;
				}
			}
			else if(aElements[i].type == "select-multiple") // If it is a list box
			{
				if(aElements[i].length==0 && aElements[i].getAttribute("required") != null)
				{
					alert("Please enter your \"" + aElements[i].getAttribute("label") + "\"");
					aElements[i].focus();
					bValid = 0;
				}
			}
			else if(aElements[i].type == "radio") // If it is a list box
			{
				var objRadioGroup = oFrm.elements[aElements[i].name];
				var bChecked = false;
				if (objRadioGroup.length == null)
				{
					if (objRadioGroup.checked)
						bChecked = true;
				}
				else
				{
					for(var j=0; j< objRadioGroup.length; j++)
					{
						if(objRadioGroup[j].checked)
						{
							bChecked = true;
							break;
						}
					}
				}
				if(!bChecked && aElements[i].getAttribute("required") != null)
				{
					alert("Please select your \"" + aElements[i].getAttribute("label") + "\"");
					aElements[i].focus();
					bValid = 0;
				}
			}			
		}
		if(!bValid) 
			return false;
	}
	return true;
}
