/*
TP Naming Conventions:
- Prefixes
	- arr: Array
	- fun: Function
*/

//TP
function funChkboxRange(chkboxarray, min, minerror, max, maxerror)
{
	var chkd=0;

	//Cycle thru checkbox array
	//since first index=0, length is always 1 more than last index.
	for(var i=0;i<chkboxarray.length;i++)
	{
		if(chkboxarray[i].checked)
		{
			chkd++;
		}
	}

	/*
	Creates Array
	- var arrName = new Array()
		arrName[0]="item1"
		arrName[1]="item2"
		arrName[2]="item3"
	- var arrName=new Array("item1","item2","item3")
	- var arrName = ["item1","item2","item3"]
	*/
	var arrError = new Array();

	//if min exists & chkd is less than min, add minerror to arrError array.
	if(min && chkd<min)
	{
		arrError.push(minerror);
	}

	//if max exists & chkd is more than max, add maxerror to arrError array.
	if(max && chkd>max)
	{
		arrError.push(maxerror);
	}

	return arrError;
}

/*
This function checks whether the passed parameter is null or blank.
In this we pass the value of the fields as a parameter.
If the str is blank or null, it will return true and otherwise false.
*/
function funIsEmpty(feild, errmsg)
{
	var str= feild.value;
	alert(str);
	/*
	http://www.webreference.com/js/column5/index.html
	Regular Expression Key:
		/ - pattern must begin and end with / in Javascript
		Modifiers Before or after
			g	Do global pattern matching.
			i	Do case-insensitive pattern matching.
			m*	Treat the string as multiple lines.
			s*	Treat the string as a single line.
			x*	Ignore whitespace within a pattern.
			* Modifiers that are not supported by Navigator 4.0x and Internet Explorer 4.0.
			Examples
				/JavaScript/i matches both "javascript" and "JavaScript"
		Rule 2: | Seperates alternatives
		Rule 4: Assertions
			^	Matches at the beginning of the string.
			$	Matches at the end of the string.
			\b	Matches a word boundary (between \w and \W), when not inside [].
			\B	Matches a non-word boundary.
		Rule 5: Quantifiers
			{m,n}	Must occur at least m times, but not more than n times.
			{n,}	Must occur at least n times.
			{n}	Must occur exactly n times.
			*	Must occur 0 or more times (same as {0,}).
			+	Must occur 1 or more times (same as {1,}).
			?	Must occur 0 or 1 time (same as {0,1}).
		Rule 6: Special Characters
			\n	Linefeed
			\r	Carriage return
			\t	Tab
			\v	Vertical tab
			\f	Form-feed
			\d	A digit (same as [0-9])
			\D	A non-digit (same as [^0-9])
			\w	A word (alphanumeric) character (same as [a-zA-Z_0-9])
			\W	A non-word character (same as [^a-zA-Z_0-9])
			\s	A whitespace character (same as [ \t\v\n\r\f])
			\S	A non-whitespace character (same as [^ \t\v\n\r\f])
				/abc/gi 
	*/

	//Replace str value with value after run thru regex.
	//regex deletes whitespace before the first & after the last alphanumeric character
	var trim = str.replace(/^\s+|\s+$/g,"");		
	
	if(trim == null || trim.length==0)
	{
		return errmsg;
	}
	else
	{
		return true;
	}
}

//this Keyword - http://www.quirksmode.org/js/this.html

/*
This Function is to find out that whther the value of the field is numeric or not.
Parameter:	any value(In this case its a value contained in any field.)
Returns:	false if user has not entered the number, true otherwise.
*/
function funOnlyNbr(feild)
{
	var str=feild.value;

	var pat = /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/;

	if(!str.match(pat))
	{
		var error='<a href=#'+feild+'>'+feild+'should consist of numbers only.</a>';
		return error;
	}
	else
	{
		return true;
	}
}

/*
Function to find out whether the passed id is valid or not.
Paramter:	Email Id. In this case vfalue of a field in which email is entered.
Return:		It returns true is the mail is not valid and false in opposite situation.
*/
function isValidEmail(feild)
{
	var str=feild.value;

	var pat = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;

	if(!str.match(pat))
	{
		var error='<a href=#'+feild+'>'+feild+'is not a valid email address containing a \'@\' and extension (.com, .org, etc).</a>';
		return error;
	}
	else
	{
		return true;
	}
}

/*
TP
Vars - 	AllFeildsInfo: format -> feildid,errormsg;feildid2,errormsg2
	arrFeildInfo=feildid,errormsg
*/
function funChkEach(allfeildsinfo,funct)
{
	alert(allfeildsinfo);
	alert(document.getElementById(allfeildsinfo).value);
	if(document.getElementById(allfeildsinfo))
	{
		var arrAllFeildsInfo=document.getElementById(allfeildsinfo).value.split(';');
		var arrErr= new Array();

		for(var i=0; i<arrAllFeildsInfo.length; i++)
		{
			alert(arrAllFeildsInfo[i]);
			var arrFeildInfo=arrAllFeildsInfo[i].split(',');
			arrErr.concat(funct(document.getElementById(arrFeildInfo[0]), arrFeildInfo[1]));
		}
	}

	return arrErr;
}

//TP
function funChkUsual()
{
	var arrErr= new Array();

	//Concatenate arrays - http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_concat

	arrErr=arrErr.concat(funChkEach('reqTypeTxt',funIsEmpty));
	//arrErr=arrErr.concat(chkEach(onlyLtr,));
	//arrErr=arrErr.concat(chkEach(onlyNbr,onlyNbr));
	//arrErr=arrErr.concat(chkEach(validEmail,isValidEmail));

	//arrErr=[1,2];
	return arrErr;
}

//TP
/*
function chkForm()
{
	var arrErr= new Array();
	arrErr=arrErr.concat(chkUsual());

	if(arrErr)
	{
		for(var i=0;i<arrErr.length;i++)
		{
			document.getElementById('faults').innerHTML+=arrErr[i];
		}

		return false;
	}
}
*/
