//<!--
//JavaScriptFunctions.h
//Global file for JavaScript functions, Client side validation

//Global Variables
var errMsg;

//Function hasQuotes 
//Checkes for single or double quotes in the argument passed
//returns true if single or double quotes were found false otherwise
function hasQuote(str)
{
	var atPos;

	atPos = str.indexOf('\'');
	if(atPos!=-1)
		return true;
	
	atPos = str.indexOf('\"');
	if(atPos!=-1)
		return true;
		
	return false
}



//Function trim similiar to Visual Basic Trim()
//Removes Leading and trailing spaces and tabs from the argument passed
//returns a string
//trim all the required fields using this function
function trim(str)
{
	var x;
	var ch;
	
	for(x=0;x<str.length;x++)
	{
		ch=str.substr(x,1);
		if(ch==' ' || ch=='\t')
		{
			str=str.substr(x+1,str.length-1);
		}
		else
			break;
	}
	
	for(x=str.length-1;x>=0;x=x-1)
	{
		ch=str.substr(x,1);
		if(ch==' ' || ch=='\t')
		{
			str=str.substr(0,str.length-1);
		}
		else
			break;
	}
	
	return str;
}

function hasdecimal(str)
{
	var x, ch, cnt, dotpos;
	cnt=0;
	dotpos=0;
	
	if(str.length==1)
		{
			ch=parseInt(str);
			if(ch==str)
			{
				return true;
			}
		}
	
	for(x=0;x<str.length;x++)
	{ 
	     if(str.substr(x,1)=='.')
			{
				cnt=cnt+1;
				dotpos=x;
			}
	}     
	if(cnt>1)
		{
			return false;
		}
	
	if(str.substr(dotpos+1,1)=='')
		{
			return false;
		}

	for(x=0;x<str.length;x++)
		{
			if(x!=dotpos)
			{
			  ch=parseInt(str.substr(x,1));
			  if(ch!=str.substr(x,1))
			  {
				return false;
			  }
			}
		}
	
	return true;
}

//Function hasSpace
//Checkes for spaces and tabs inside the passed argument
//Returns true if spaces or tabs are found
function hasSpace(str)
{
	var x, ch;

	for(x=0;x<str.length;x++)
	{
		ch=str.substr(x,1);
		if(ch==' ' || ch=='\t')
		{
			return true;
		}
	}
	
	return false;
}

//Function isDate
//Checkes whether the passed argument is a date
//Returns true if the passed argument is a date
function isDate(str)
{
	var newDate;
	var varMonth;
	var varDay;
	var varYear;
	varMonth = str.substring(0, str.indexOf("/"));
	varMonth -= 1;
	varDay = str.substring(str.indexOf("/") + 1, str.lastIndexOf("/"));
	varYear = str.substring(str.lastIndexOf("/") + 1);
	newDate = new Date(varYear, varMonth, varDay);
	if (varDay != newDate.getDate() || varMonth != newDate.getMonth() || varYear != newDate.getFullYear())
		return false;
	else
		return true;
}

//Function hasSpChar
//Checks for special characters inside the passed argument
//Returns true if special characters are found
//Checks for following special characters
//` ~ ! # $ % ^ & * ( ) + = [ ] { } | \ ; : " ' , < . > / ?
function hasSpChar(str)
{
	var x, ch;

	for(x=0;x<str.length;x++)
	{
		ch=str.substr(x,1);
		//if(ch=='`' || ch=='~' || ch=='!' || ch=='!' || ch=='#' || ch=='$' || ch=='%' || ch=='^' || ch=='&' || ch=='*' || ch=='(' || ch==')' || ch=='=' || ch=='+' || ch=='[' || ch==']' || ch=='{' || ch=='}' || ch=='\' || ch=='|' || ch=='\;' || ch==':' || ch=='"' || ch==''' || ch==',' || ch=='<' || ch=='.' || ch=='>' || ch=='/' || ch=='?')
		{
			return true;
		}
	}
	
	return false;
}


//Function isValid which handles the front end side FORM validations
//Example call :isValid(document.forms[0].email.value,"e-mail","E-mail",true,false,3,false);
//1st parameter=Value of the field on the form
//2nd parameter=type of the field(Can be "text", "email", "digit", "number" , "decimal")
//3rd parameter=description of the field
//4th parameter=true for required, false for not required
//5th parameter=true for space, false no space
//6th parameter=n for length, null for no length validation
//7th parameter=true for quotes allowed, false no quotes not allowed
//8th parameter=max length, null for no length validation
//retruns false if the argument passed is not proper

function isValid(vValue,vType,vDescription,vRequired,vCanHaveSpace,vCanHaveQuote,vLength,vMaxlength)
{
	var atPos,tStr,allZero,x,ch;

	//Trim the value first
	vValue=trim(vValue);

	//Killer Quotes validation
	if(vCanHaveQuote==false && hasQuote(vValue)==true)
	{
		errMsg = errMsg + "Quotes in " + vDescription + " are not allowed.\n";
		return false;
	}

	
	//E-mail validation 
	if(vType=="e-mail" && vRequired==true)
	{
		atPos = vValue.indexOf('@');
		if (atPos < 1 || atPos == (vValue.length - 1) || hasSpace(vValue)==true)
		{
			errMsg = errMsg + vDescription + " is not a valid E-mail address.\n";
			return false;
		}
	}
	if(vType=="e-mail" && vRequired==false && vValue!="")
	{
		atPos = vValue.indexOf('@');
		if (atPos < 1 || atPos == (vValue.length - 1) || hasSpace(vValue)==true)
		{
			errMsg = errMsg + vDescription + " is not a valid E-mail address.\n";
			return false;
		}
	}

	//date validation
	if(vType == "date" && vRequired == true)
	{
		if(vValue == "//")
		{
			errMsg = errMsg + vDescription + " cannot be left blank.\n";
			return false;
		}
		if (!isDate(vValue))
		{
			errMsg = errMsg + vDescription + " is not a valid date.\n";
			return false;
		}
	}
	if(vType == "date" && vRequired == false && vValue != "//")
	{
		if (!isDate(vValue))
		{
			errMsg = errMsg + vDescription + " is not a valid date.\n";
			return false;
		}
	}
	if(vType == "date" && vLength != null && Date.parse(vValue) < Date.parse(vLength) && vRequired == true)
	{
		errMsg = errMsg + vDescription + " cannot be less than " + vLength + ".\n";
		return false;
	}
	if(vType == "date" && vValue!="//" && vLength != null && Date.parse(vValue) < Date.parse(vLength) && vValue != "" && vRequired == false)
	{
		errMsg = errMsg + vDescription + " cannot be less than " + vLength + ".\n";
		return false;
	}
	if(vType=="date" && vMaxlength != null && Date.parse(vValue) > Date.parse(vMaxlength) && vRequired==true)
	{
		errMsg = errMsg + vDescription + " cannot be greater than " + vMaxLength + ".\n";
		return false;
	}
	if(vType=="date" && vValue!="//" && vMaxlength != null && Date.parse(vValue) > Date.parse(vMaxlength) && vValue!="" && vRequired==false)
	{
		errMsg = errMsg + vDescription + " cannot be greater than " + vMaxLength + ".\n";
		return false;
	}


	//number validation
	if(vType=="number" && vRequired==true)
	{
		num=parseInt(vValue);
		if(num!=vValue || hasSpace(vValue)==true || num < 0)
		{
			errMsg = errMsg + vDescription + " is not a valid number.\n";
			return false;
		}
	}
	if(vType=="number" && vRequired==false && vValue!="")
	{
		num=parseInt(vValue);
		if(num!=vValue || hasSpace(vValue)==true || num < 0)
		{
			errMsg = errMsg + vDescription + " is not a valid number.\n";
			return false;
		}
	}
 
 //Decimal Validation
 	if(vType=="decimal" && vRequired==true)
 		{
 			if (hasdecimal(vValue)==false)
 			{
				errMsg = errMsg + vDescription + " is not a valid number.\n";
				return false;
			}
 		}
 	
	//Digits validation
	if(vType=="digit" && vRequired==true)
	{
		tStr=vValue;
		allZero=true;
		
		for(x=0;x<vValue.length;x++)
		{
			ch=tStr.substr(x,1);
			if(ch!='0')
				allZero=false;
			if(ch<'0' || ch>'9' || hasSpace(vValue)==true)
			{
				errMsg = errMsg + vDescription + " can only contain digits.\n";
				return false;
			}
		}
		
		if(allZero==true || hasSpace(vValue)==true)
		{
			errMsg = errMsg + vDescription + " can only contain digits.\n";
			return false;
		}
	}
	if(vType=="digit" && vRequired==false && vValue!="")
	{
		tStr=vValue;
		allZero=true;
		
		for(x=0;x<vValue.length;x++)
		{
			ch=tStr.substr(x,1);
			if(ch!='0')
				allZero=false;
			if(ch<'0' || ch>'9' || hasSpace(vValue)==true)
			{
				errMsg = errMsg + vDescription + " can only contain digits.\n";
				return false;
			}
		}
		
		if(allZero==true || hasSpace(vValue)==true)
		{
			errMsg = errMsg + vDescription + " can only contain digits.\n";
			return false;
		}
	}
	
	if(vType=="text" && vCanHaveSpace==false && vRequired==true)
	{
		if(hasSpace(vValue)==true || vValue.length==0 || vValue=="")
		{
			errMsg = errMsg + vDescription + " cannot be blank.\n";
			return false;
		}
	}

	if(vType=="text" && vCanHaveSpace==true && vRequired==true)
	{
		if(vValue.length==0 || vValue=="")
		{
			errMsg = errMsg + vDescription + " cannot be blank.\n";
			return false;
		}
	}

	if(vLength != null && vValue.length < vLength && vRequired==true)
	{
		errMsg = errMsg + vDescription + " min. required length is: " + vLength + ".\n";
		return false;
	}

	if(vLength != null && vValue.length < vLength && vValue!="" && vRequired==false)
	{
		errMsg = errMsg + vDescription + " min. required length is: " + vLength + ".\n";
		return false;
	}
	
	if(vMaxlength != null && vValue.length+1 >= vMaxlength && vRequired==true)
	{
		errMsg = errMsg + vDescription + " max. required length is: " + vMaxlength + ".\n";
		return false;
	}

	if(vMaxlength != null && vValue.length+1 >= vMaxlength && vValue!="" && vRequired==false)
	{
		errMsg = errMsg + vDescription + " max. required length is: " + vMaxlength + ".\n";
		return false;
	}
	return true;
}
//-->
