/* ****************************************************

	Application: YHA Outbound Travel
	
	File: formvalidation.js
	
	Type: Javascript Source
	
	Desc: global js code library
	
	Comments: bits obtained from various sources...
	
	Author: Danielle Moulton <danielle@redsquare.com.au>
	Date:  01/08/00
	Version: 1.0.0
	
	***** Modification Details *****
	
	DATE  		NAME	MODIFICATION
	01/03/04	Sara	commented out the duplicated isValidEmail function	
**************************************************** */

//checks if the checkbox is selected or at least one of the array is selected
function checkBoxSelected(theForm,FieldName)
{
objBox = eval(theForm[FieldName]);
if (objBox.length)
{
	for (i=0; i < objBox.length; i++)
	{
		if (objBox[i].checked)
			return true;
	}
} else {
	if (objBox.checked)
		return true;
}
	return false;
}


function isSpace(inChar) 
{
 	return (inChar == ' ' | inChar == '\t' || inChar == '\n');
}
	
// trims a string
function trim(tmpStr) 
{
 var atChar;
 if (tmpStr.length > 0) atChar = tmpStr.charAt(0);
while (isSpace(atChar)) 
 {
   tmpStr = tmpStr.substring(1, tmpStr.length);
   atChar = tmpStr.charAt(0);
 }
 if (tmpStr.length > 0) atChar = tmpStr.charAt(tmpStr.length-1);
 while (isSpace(atChar)) 
 {
   tmpStr = tmpStr.substring(0,( tmpStr.length-1));
   atChar = tmpStr.charAt(tmpStr.length-1);
 }
//  alert("here'"+tmpStr+"'");
 return tmpStr;
}

//checks if an email is valid	
function isValidEmail(theForm, strFieldName)
{
	var str1 = theForm[strFieldName].value;
	return  isValidEmail_1(str1);
}

function isValidEmailAd(objField)
{
	var str1 = trim(objField.value);
	return  isValidEmail_1(str1);
}


function isValidEmail_1(str)
{
	str += '';
	namestr= '';
	domainstr = '';
	namestr = str.substring(0,str.indexOf("@")); // get all the info before the "@"
	domainstr = str.substring(str.indexOf("@")+1,str.length); // get all the info after the "@"
	tailstr = str.substring(str.indexOf(".")+1,str.length) // after the first "." how many chars are they
	blankstrInd = str.indexOf(" "); // check if email contain blanks
	if( (namestr.length == 0) || (domainstr.indexOf(".") <= 0) || (domainstr.length == 0) || (domainstr.indexOf("@") != -1) || tailstr.length == 0 || (blankstrInd > 0)) return false;
	
	return true;
}


//checks if a textbox is empty
function isEmpty(theForm, strFieldName)
{	
	var strS = trim(theForm[strFieldName].value);
	if (strS.length == 0 || strS == "")	return true;
	return false;
}

// checks if at least one of a set of radios is selected 	
function isEmptyRadio(theForm, strFieldName)
{
 	for (i=0; i<theForm[strFieldName].length; i++)
	{
		if (theForm[strFieldName][i].checked)
			return false;
	}
	return true;
}

function isEmptySelect(theForm, strFieldName)
{
	var strS = "";
	iIndex = theForm[strFieldName].selectedIndex;
	if (iIndex != null)
	{
		strS = theForm[strFieldName][iIndex].value;
	}	
	if (trim(strS) == "") return true;
	return false;
}

//returns -1 if fields is required and is null, returns -2 if it exceeds maxlength, returns -3 if type is invalid, otherwise returns 0
function checkTextBox(field,brequired,maxlength,type)
{	
	var strS = trim(field.value);
	if (brequired)
	{	
		if (strS.length == 0 || strS == "")	return -1;
	}
	if (maxlength) 
	{
		if (maxlength > 0)
			if (strS.length > maxlength) return -2;
	}
	if (type)
	{
		if (type == "email") 
		{
			if (strS.length != 0 && strS != "")
			{
				if (!isValidEmail_1(strS)) return -3;
			}	
		}	
	}
	return 0;
}

//checks if a textbox is empty
function isEmptyText(objField)
{	
	var strS = trim(objField.value);
	if (strS.length == 0 || strS == "")	return true;
	return false;
}

//***************************************************************************
function isValidPostcode(objField)
{
	string = objField.value;
	if (!isValidInteger(objField))
		return false;
	if (parseInt(string.length,10) != 4)
		return false;
	
	return true;
}

//***************************************************************************
function isValidPostcode2(objField)
{
	string = objField.value;
	if (!isValidInteger(objField))
		return false;
	if (parseInt(string.length,10) > 6)
		return false;
	
	return true;
}

//***************************************************************************
// validate phone numbers
function validatePhone(objField) {

    var Chars = "0123456789() ";
	string = objField.value; 
    for (var i = 0; i < string.length; i++) 
	{
       if (Chars.indexOf(string.charAt(i)) == -1)
	    return false;
    }
    return true;
}
//***************************************************************************
function isValidInteger(objField) {
	string = objField.value;
    var Chars = "0123456789";
 
    for (var i = 0; i < string.length; i++) 
	{
       if (Chars.indexOf(string.charAt(i)) == -1)
	    return false;
    }
 
	if(string.length == 0)
		return false;
	else
	    return true;
}
//************************************************************************
/*function isValidEmail(objField)
{
	str = objField.value;
	str += '';
	namestr= '';
	domainstr = '';
	namestr = str.substring(0,str.indexOf("@")); // get all the info before the "@"
	domainstr = str.substring(str.indexOf("@")+1,str.length); // get all the info after the "@"
	tailstr = str.substring(str.indexOf(".")+1,str.length) // after the first "." how many chars are they
	if( (namestr.length == 0) || (domainstr.indexOf(".") <= 0) || (domainstr.length == 0) || (domainstr.indexOf("@") != -1) || tailstr.length == 0) return false;
	
	return true;
}*/

//************************************************************************
function y2k(number) { return (number < 1000) ? number + 1900 : number; }

//************************************************************************
function isValidDate(objField,sep) 
{
	myDate = objField.value;
	//set default
	if (!sep) sep = "/";
	if (trim(myDate) == "")
		return false;
		
 	var dateArrayFrom = myDate.split(sep);
	var date = dateArrayFrom[0];
	var month = dateArrayFrom[1];
	var year = dateArrayFrom[2];
	 
    var test = new Date(year,month-1,date);
 	if (year == y2k(test.getYear()) && (month-1 == test.getMonth()) && (date == test.getDate())) 
   	{
       reason = '';
       return true;
    } else {
       reason = 'Valid format but an invalid date.';
       return false;
    }
}

function isEmptySelect2(objField)
{
	var strS = "";
	iIndex = objField.selectedIndex;
	if (iIndex != null)
	{
		strS = objField.options[iIndex].value;
	}	
	if (trim(strS) == "") return true;
	return false;
}
