// JavaScript Document
function ValidateForm(whatform)
{	
	if (whatform.txtFirstName.value==""){
		alert('Please enter your first name.');
		whatform.txtFirstName.focus();
		return false;
	}
	if (whatform.txtLastName.value==""){
		alert('Please enter your last name.');
		whatform.txtLastName.focus();
		return false;
	}
	if (whatform.txtHomeContact.value=="")
	{
		alert('Please enter a contact number.');
		whatform.txtHomeContact.focus();
		return false;
	}
	if (whatform.txtEmailAdd.value=="")	
	{
		alert('Please enter email address.');
		whatform.txtEmailAdd.focus();
		return false;
	}
				
	if (whatform.txtSubject.value==""){
		alert('Please enter the feedback subject.');
		whatform.txtSubject.focus();
		return false;
	}
	if (whatform.txtMessage.value==""){
		alert('Please enter a message.');
		whatform.txtMessage.focus();
		return false;
	}
	else	
	{
		return confirm("Are you sure you want to send this message?");
			
	}				
}	

// START FUNCTION TRIM
// This function will trim leading and/or trailing spaces from a string
// arg = the value you wish to have trimmed..
// func = "left" for Ltrim(), "right" for RTrim() or "both" for Trim()

//===================================
function trim(arg,func) {
//===================================
	var trimvalue = "";
	arglen = arg.length;
	if (arglen < 1) return trimvalue;

	if (func == "left" || func== "both") {
		i = 0;
		pos = -1;
		while (i < arglen) {
			if (arg.charCodeAt(i) != 32 && !isNaN(arg.charCodeAt(i))) {
				pos = i;
				break;
			}
			i++;
		}
	}
	if (func == "right" || func== "both") {
		var lastpos = -1;
		i = arglen;
		while (i >= 0) {
			if (arg.charCodeAt(i) != 32 && !isNaN(arg.charCodeAt(i))) {
				lastpos = i;
				break;
			}
			i--;
		}
	}
	if (func == "left") {
			trimvalue = arg.substring(pos,arglen-1);
		}

	if (func == "right") {
		trimvalue = arg.substring(0,lastpos+1);
	}

	if (func == "both") {
		trimvalue = arg.substring(pos,lastpos + 1);
	}
	return trimvalue;
}

// END FUNCTION TRIM

function checkEmail(pObj) {
	var objectName	= pObj.name;
	var objectValue	= pObj.value;

	var newstr	= "";
	var at		= false;
	var dot		= false;
	var AtSym	= objectValue.indexOf('@');
	var Period	= objectValue.lastIndexOf('.');
	var Space	= objectValue.indexOf(' ');
	var Length	= objectValue.length - 1  ;
	    
	// check the data
	if (trim(objectValue,"both") == "") return;
    
    if	((AtSym	<	1) ||				// '@' cannot be in first position
		(Period	<=	AtSym+1) ||		// Must be atleast one valid char btwn '@' and '.'
		(Period	==	Length ) ||		// Must be atleast one valid char after '.'
		(Space	!=	-1)) {
		dot =  false;
		at = false;
    } 
    else {
		dot	= true;
		at	= true;
    }        
    // parse the remainder of the string
    for (var i = 0; i < objectValue.length; i++) {
		ch = objectValue.substring(i, i + 1)
		if	(
			(ch	>=	"A"	&&	ch	<=	"Z") ||
			(ch	>=	"a"	&&	ch	<=	"z") ||
			(ch	>=	"0"	&&	ch	<=	"9") ||
			(ch	==	"@") ||
			(ch	==	".") ||
			(ch	==	"_") ||
			(ch	==	"-")
			) {
				newstr += ch;
		}
    }
    if ((at == true) && (dot == true)) {
        return newstr;
    }
    else {
      // prompt for an error
      alert ("Please enter a valid email address.");
      pObj.focus();
      pObj.select();
//      return objectValue;
      return false;
    }
}	
function ValidateInput(strValue){
	if(strValue.match(/([\<])([^\>]{1,})*([\>])/i) != null) {
    	return false;
    } else {
    	return true;
    }
}
function Check(whatform) {
	for(i=0; i<whatform.elements.length; i++){
		if ((whatform.elements[i].type=="text") || (whatform.elements[i].type=="textarea")) {
			if (ValidateInput(whatform.elements[i].value)==false) {
				return false;
			}
		}		
	}
	return ValidateForm(whatform)
}
