function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}
function rtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
}


function validateForm(form){
	var emailID=document.frmMain1.email
    var error;
	error = ""
	with(form){
		if(trim(fname.value) == ""){
            error += "Please enter a value for First Name.\n";
        }	
		if(trim(lname.value) == ""){
            error += "Please enter a value for Last Name.\n";
        }
		if(trim(address.value) == ""){
            error += "Please enter a value for Address.\n";
        }
        if(trim(city.value) == ""){
            error += "Please enter a value for City.\n";
        }
		if(trim(country.value) == ""){
            error += "Please select a Country.\n";
        }
        if(trim(state.value) == "" && (country.value == "CA" || country.value == "US")){
            error += "Please enter a value for State/Province.\n";
        }
        if(trim(zipcode.value) == "" && (country.value == "CA" || country.value == "US")){
            error += "Please enter a value for Zipcode/Postal Code.\n";
        }
		if(trim(phone.value) == ""){
            error += "Please enter a value for Phone\n";
        }
	if ((emailID.value==null)||(emailID.value=="")){
		alert("Please enter an email address.");
		emailID.focus();
		return false;
	}
	if (echeck(emailID.value)==false){
		emailID.value="";
		emailID.focus();
		return false;
	}
		if(trim(mediaType.value) == ""){
            error += "Please enter a value for Media Type.\n";
        }
		if(breakSeal[0]){
			if(!breakSeal[0].checked && !breakSeal[1].checked){
            error += "Please choose whether we may break the media seals.\n";
            }
        }
		if(!legalRequired[0].checked && !legalRequired[1].checked){
            error += "Please choose whether data recovery is a legal requirement.\n";
        }
		if(trim(problemDesc.value) == ""){
            error += "Please enter a brief description of the problem.\n";
        }
		if(trim(referredBy.value) == ""){
            error += "Please tell us how you found T.I.M.\n";
        }
   }
   if (error != ""){
		alert(error);
		return false;
    }
    return true;
}

function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("Invalid email address");
		   return false;
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid email address");
		   return false;
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Invalid email address");
		    return false;
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid email address");
		    return false;
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid email address");
		    return false;
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid email address");
		    return false;
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Invalid email address");
		    return false;
		 }

 		 return true;					
	}