/* form_validation.js
   * CREATED BY: Martin Paczynski
   * Copyright (c) 2001-2003 Martin Paczynski. All Rights Reserved.
   * Originally published and documented at http://www.paczynski.net/
   * License to use is granted if and only if this entire copyright notice
   * is included.
   * This program is free software; you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published by
   * the Free Software Foundation; either version 2 of the License, or
   * (at your option) any later version.
  */
// LAST MODIFIED: 07/07/03


//**************** General email verifier


function replaceChars(entry) {
out = "'"; // replace this
add = "`"; // with this
temp = "" + entry; // temporary holder

while (temp.indexOf(out)>-1) {
pos= temp.indexOf(out);
temp = "" + (temp.substring(0, pos) + add + 
temp.substring((pos + out.length), temp.length));
}
document.subform.text.value = temp;
}

// The script will validate any 2 or 3 letter suffix (e.g. "com", "uk") but not anything longer or shorter
// that is not listed in the Array below
var validEmailExt = new Array("info","aero","museum","name");

function checkIfEmail(formEl){
	this.formEl = formEl;
	formElV=formEl.value
	
	var pat = new RegExp("^(\\w|-|\\w\\.\\w)+@(\\w|-|\\w\\.\\w)+\\.\\w{2,3}$") ;
	patmatch = pat.test(formElV);
	if(patmatch)
		{return true;}
	else
		{
		for(a=0;a<validEmailExt.length;a++)
			{
			pat.compile("^(\\w|-|\\w\\.\\w)+@(\\w|-|\\w\\.\\w)+\\."+ validEmailExt[a] +"$");
			patmatch = pat.test(formElV);
			if(patmatch)
				{return true;}
			else
				{continue;}
			}
		}
	return false;	
}


//************** Check if text field is filled. minL is the minimum length allowed. maxL is the maximum length allowed

function checkText(formEl,minL,maxL){
	this.formEl = formEl;
	formElV = formEl.value;
	minL = minL?minL:1;
	maxL = maxL?maxL:formElV.length;
	if(minL<=formElV.length && formElV.length<=maxL)
		{return true}
	else
		{return false}

}


//************* Checks if value is a valid number

function checkIsNumber(formEl,minS,maxS,integ){
	this.formEl = formEl;
	formElV = parseFloat(formEl.value);
	
	tmpMin = parseFloat(minS);
	tmpMax = parseFloat(maxS);
			
	if((!minS&&minS!=0)||tmpMin!=minS)
		{minS = -(1/0);}
	if((!maxS&&maxS!=0)||tmpMax!=maxS)
		{maxS = (1/0);}
	
	if(integ)
		{
		tmpNo = parseInt(formElV);
		if(tmpNo != formElV)
			{return false;}
		}
	else
		{
		tmpNo = parseFloat(formElV);
		if(tmpNo != formElV)
			{return false;}
		}
	
	if(Math.max(minS,formElV) != formElV)
		{return false;}
	else if(Math.min(maxS,formElV) != formElV)
		{return false;}
	else
		{return true;}
	
}


//************ Check if a radio button is checked within a group

function checkRadioChecked(formEl, position){
	this.formEl = formEl;
	
	if(!formEl.length)
		{
		return  formEl.checked;
		}
	
	for(i=0;i<formEl.length;i++)
		{
		 if(formEl[i].checked){return position?i:true;}
		 }
	return position?-1:false;
}

//************ Check for checked checkboxes and return number checked

function checkCheckboxChecked(formEl){
	this.formEl = formEl;
	
	if(!formEl.length)
		{
		return  formEl.checked;
		}
	
	checkCounter = new Array();
	for(i=0;i<formEl.length;i++)
		{
		 if(formEl[i].checked){checkCounter[checkCounter.length]=i}
		}
		
	if(checkCounter.length>0)
		{return checkCounter;}
	else
		{return null;}
}


//************ Check if an option is selected

function checkOptionSelected(formEl){
	this.formEl = formEl;
	tmpSelObj = new Array();
	if(formEl.type.indexOf("multiple")!=-1)
		{tmpCounter = 0;
		for(a=0;a<formEl.length;a++)
			{
			if(formEl.options[a].selected)
				{
				tmpSelObj[tmpCounter]=a;
				tmpCounter++;
				}
			}
		if(tmpCounter)
			{return tmpSelObj}
		else
			{return false}
		}
	else if(formEl.selectedIndex>0)
		{
		tmpSelObj[0] = formEl.selectedIndex;
		return tmpSelObj;
		}
	else
		{return false}
}


//********** Check if entry contains between minL (min. length) and maxL (max. lenght) digits in it, total,
//********** disregarding '-','.','(' and any other symbols used when writing a telephone number. If the number
//********** must be exactly a certain length, only input minL requirements.  If both minL and maxL are left blank,
//********** the default length is exactly 11 digits (standard USA phone number with area code.

function checkPhone(formEl,minL,maxL){
	this.formEl = formEl;
	formElV = formEl.value;

	tmpMin = parseInt(minL);
	tmpMax = parseInt(maxL);
			
	if((!minL&&minL!=0)||isNaN(tmpMin))
		{tmpMin = 11;}
	if((!maxL&&maxL!=0)||isNaN(tmpMax))
		{tmpMax = tmpMin;}
	
	tempNo = formElV.split('');
	tempCounter=0;
	for(a=0;a<tempNo.length;a++)
		{
		if(tempNo[a].match(/\d/)){tempCounter++}
		}
	if(tempCounter<tmpMin||tempCounter>tmpMax){return false}
	else{return true}
}

//*********** Check if date

function checkDate(formEl){
	this.formEl = formEl;
	formElV = formEl.value;
	pat = /(\d{1,2})\D+(\d{1,2})\D+(\d{4}|\d{2})/;
	
	result = pat.exec(formElV);
	
	if(result)
		{
		if(result.length!=4)
			{return false;}
		
		if((result[1]>12)&&(result[2]>12))
			{return false;}
		else
			{return true;}
			
		}
	else
		{return false;}
}


//************ Check if value if valid name for a DOM complient element

function checkValElName(formEl){
	this.formEl = formEl;
	formElV=formEl.value
	var pat = new RegExp("^(_|[a-zA-Z])+\\\w*$") ;
	patmatch = pat.test(formElV);
	return patmatch;
}


//***********  Check for "sameness" of values passed.  If you wish you can pass two (2) form elements to the function to see if they are the same 
//***********  OR a value (directly or a variable) and a form element to see if that element's value is the same as that value
//***********  OR you can use this function for comparing two variables or whathave you.
//***********  WARNING: THIS WILL NOT WORK WITH PASSWORD FIELDS AS JAVASCRIPT CANNOT READ THE VALUE OF THESE


function checkSame(formEl1,formEl2){
	if(formEl1.value || formEl1.value=='' || formEl1.value ==0)
		{v1 = formEl1.value; }
	else
		{v1 = formEl1;}
		
		
	if(formEl2.value || formEl2.value=='' || formEl2.value ==0)
		{v2 = formEl2.value;}
	else
		{v2 = formEl2;}

	if(v1==v2)
		{return true;}
	else
		{return false;}
}





//*************** Check SSN

function checkSSN(formEl){
	this.formEl = formEl;
	formElV = formEl.value;
	ssn = (formElV.match(/\d{3}-\d{2}-\d{4}/))?1:0
	return ssn
}




//************* Rudimentary check of "URL"

function checkURL(formEl){
	this.formEl = formEl;
	tmpURL = formEl.value;
	if(tmpURL.match(/^(\w|-)\.\w{2}/))
		{return true}
	else
		{return false}
}



//************* Fill all empty fields with "N/A"

function fillAllElements(thisForm){
var dFt = thisForm;
for(a=0; a<dFt.length; a++)
	{
	if(dFt.elements[a].value=="")
		{dFt.elements[a].value="N/A";}
	}
}


//************ Simply a function that alerts what needs to be entered and then places focus on that element

function retFalse(say,what){
	alert("Please enter "+say);
	what.focus();
	return false;
}




//************ Determine is a passed "select" object contains the value passed and returns the indexes of those options. 

function optionValueExists(elem, value){
		if(arguments.length==0 || typeof value != 'string' || !elem.type.toString().match(/select/))
			{return false;}
		
		var tmpArr = new Array();
		
		for(a=0;a<elem.length;a++)
			{
			if(elem[a].value == value)
				{tmpArr[tmpArr.length] = a;}
			}
			
		return tmpArr.length==0?null:tmpArr;
		
	}
	
	
//************* Function is used to compare the number of arguements passed to a function with the number expected.
//************* If too few are passed, function return negative number. If too many, function returns positive number.
//************* If passed equal to expected, function returns zero.

function checkArgs(args){
		var passed = args.length;
		var needed = args.callee.length;
		return (passed - needed);
	}

