//<!--
//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 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 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=='?'|| 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")
//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
//retruns false if the argument passed is not proper

function isValid(vValue,vType,vDescription,vRequired,vCanHaveSpace,vLength,vCanHaveQuote)
{
	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;
		}
	}

	//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;
		}
	}
	if(vType=="fnumber" && vRequired==true)
	{
		num=parseFloat(vValue);
		
		if(num!=vValue || hasSpace(vValue)==true || num < 0)
		{
			errMsg = errMsg + vDescription + " is not a valid number.\n";
			return false;
		}
	}
	if(vType=="fnumber" && vRequired==false && vValue!="")
	{
		num=parseFloat(vValue);
		
		if(num!=vValue || hasSpace(vValue)==true || num < 0)
		{
			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!="" && vCanHaveSpace==true)
	{ 
		tStr=vValue;
		allZero=true;
		
		for(x=0;x<vValue.length;x++)
		{
			ch=tStr.substr(x,1);
			if(ch!='0')
				allZero=false;
			if(ch=='a' || ch=='b' || ch=='c' || ch=='d'  || ch=='e' || ch=='f'   || ch=='g' || ch=='h'   || ch=='i' || ch=='j'   || ch=='k' || ch=='l'   || ch=='m' || ch=='n'   || ch=='o' || ch=='q'   || ch=='r' || ch=='s'   || ch=='t' || ch=='u' || ch=='v' || ch=='w' || ch=='x' || ch=='y' || ch=='z')
			{
				errMsg = errMsg + vDescription + " can only contain digits.\n";
				return false;
			}
		}
	
	}
	
	
	
	
	
	
	if(vType=="digit" && vRequired==false && vValue!="" && vCanHaveSpace==false)
	{
		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 + " has to be entered.\n";
			return false;
		}
	}

	if(vType=="text" && vCanHaveSpace==true && vRequired==true)
	{
		if(vValue.length==0 || vValue=="")
		{
			errMsg = errMsg + vDescription + " has to be entered.\n";
			return false;
		}
	}

	if(vLength != null && vValue.length < vLength && vRequired==true)
	{
		errMsg = errMsg + vDescription + " required length is: " + vLength + ".\n";
		return false;
	}

	if(vLength != null && vValue.length < vLength && vValue!="" && vRequired==false)
	{
		errMsg = errMsg + vDescription + " required length is: " + vLength + ".\n";
		return false;
	}
	
	return true;
}
//-->


function isValid1(vValue,vType,vDescription,vRequired,vCanHaveSpace,vLength,vCanHaveQuote)
{
	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;
		}
	}

	//number validation
	if(vType=="number" && vRequired==true)
	{
		num=parseFloat(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=parseFloat(vValue);
		
		if(num!=vValue || hasSpace(vValue)==true || num < 0)
		{
			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!="" && vCanHaveSpace==true)
	{ 
		tStr=vValue;
		allZero=true;
		
		for(x=0;x<vValue.length;x++)
		{
			ch=tStr.substr(x,1);
			if(ch!='0')
				allZero=false;
			if(ch=='a' || ch=='b' || ch=='c' || ch=='d'  || ch=='e' || ch=='f'   || ch=='g' || ch=='h'   || ch=='i' || ch=='j'   || ch=='k' || ch=='l'   || ch=='m' || ch=='n'   || ch=='o' || ch=='q'   || ch=='r' || ch=='s'   || ch=='t' || ch=='u' || ch=='v' || ch=='w' || ch=='x' || ch=='y' || ch=='z')
			{
				errMsg = errMsg + vDescription + " can only contain digits.\n";
				return false;
			}
		}
	
	}
	
	
	
	
	
	
	if(vType=="digit" && vRequired==false && vValue!="" && vCanHaveSpace==false)
	{
		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 + " has to be entered.\n";
			return false;
		}
	}

	if(vType=="text" && vCanHaveSpace==true && vRequired==true)
	{
		if(vValue.length==0 || vValue=="")
		{
			errMsg = errMsg + vDescription + " has to be entered.\n";
			return false;
		}
	}

	if(vLength != null && vValue.length < vLength && vRequired==true)
	{
		errMsg = errMsg + vDescription + " required length is: " + vLength + ".\n";
		return false;
	}

	if(vLength != null && vValue.length < vLength && vValue!="" && vRequired==false)
	{
		errMsg = errMsg + vDescription + " required length is: " + vLength + ".\n";
		return false;
	}
	
	return true;
}



 function addToCombo(objCombo,pValue,pText){		
	emptyTheCombo(objCombo);		
	var opt		= new Option();
	opt.value	= pValue;
	opt.text	= pText;
	objCombo.add(opt);
	objCombo.options[objCombo.length-1].selected=true;	
}
function emptyTheCombo(objCombo){	
	var objCombo_length = objCombo.length;
	for(var i=0;i<objCombo_length;i++){
		objCombo.options[0] = null;
	}	
	
}

	function disableSave(){		
		if(typeof(document.all.id_save_button_img_1) != 'undefined' && typeof(document.all.id_save_button_img_2) != 'undefined' ){		
			document.all.id_save_button_img_1.style.display = 'none';
			document.all.id_save_button_img_2.style.display = 'inline';
		}		
	}

//-->
