// 2/4/2005 Ryan Spatz - made every field execpt company required.  Changed the way the form is referenced to keep
// exceptions from being thrown.
// 06/01/2007 Lan Wallin - rename Query field to Comment (during spamslam changes)
// 07/21/2009 Lan Wallin - changes for customer service dept (David Martin), change most code to use document.getElementById 

function checkYear(year) {
  year = parseInt(year, 10);
  if (year < 20)
     year = year + 2000;

  if (year < 100)
     year = year + 1900;

  return year;
}

function notEmpty(name, value) {
  if ((value != null) && (value != ""))
     return true;
  else {
     alert('The ' + name + ' field has been left empty\nPlease fill in all fields before submitting');
     return false;
  }
}

/*
function radioNotEmpty(name, value1, value2) {
  if ((value1) || (value2))
     return true;
  else {
     alert('The ' + name + ' field has been left empty\nPlease fill in all fields before submitting');
     return false;
  }
}
*/

function radioNotEmpty(name, radioobj) 
{
	numbuttons = radioobj.length;
	for(i=0; i<numbuttons; i++)
	{
    	if(radioobj[i].checked)
			return true;
    }
     alert('The ' + name + ' field has been left empty\nPlease fill in all fields before submitting');
     return false;
}

function stripCharsNotInBag (s, bag) {
    var i;
    var returnString = "";

    for (i = 0; i < s.length; i++) {
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) != -1) returnString += c;
    }

    return returnString;
}

function checkSerial(radioobj) {
	numbuttons = radioobj.length;
	Owned = "No";
	for(i=0; i<numbuttons; i++)
	{
    	if(radioobj[i].checked)
			Owned = radioobj[i].value;
    }
	if ((Owned == "Yes") && (notEmpty("Serial Number", document.getElementById('contactform').Serial_Number.value)))
  	{
		if (document.getElementById('contactform').Serial_Number.value == stripCharsNotInBag (document.getElementById('contactform').Serial_Number.value, '0123456789'))
			return true;
		else 
		{
			alert('The Serial Number field must be all numbers, no letters or special characters');
			return false;
		}
	}

	if (Owned == "No")
		return true;
}

function echeck(str) {
/*
* DHTML email validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
*/

		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 E-mail address, please re-enter");
		   return false;
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid E-mail address, please re-enter");
		   return false;
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		   alert("Invalid E-mail address, please re-enter");
		   return false;
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		   alert("Invalid E-mail address, please re-enter");
		   return false;
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		   alert("Invalid E-mail address, please re-enter");
		   return false;
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		   alert("Invalid E-mail address, please re-enter");
		   return false;
		 }
		
		 if (str.indexOf(" ")!=-1){
		   alert("Invalid E-mail address, please re-enter");
		   return false;
		 }

 		 return true;
	}

function ValidateForm(){
	var emailID=document.frmSample.txtEmail;
	
	if ((emailID.value==null)||(emailID.value=="")){
		alert("Please Enter your Email ID");
		emailID.focus();
		return false
	}
	if (echeck(emailID.value)==false){
		emailID.value="";
		emailID.focus();
		return false;
	}
	return true;
 }

function validate() 
{
	if ((notEmpty("Department", document.getElementById('contactform').Department[document.getElementById('contactform').Department.selectedIndex].value)))
	{
		if(document.getElementById('contactform').Department[document.getElementById('contactform').Department.selectedIndex].value == "service@exmark.com")
		{	
			if ((notEmpty("First Name", document.getElementById('contactform').FirstName.value)) &&
				(notEmpty("Last Name", document.getElementById('contactform').LastName.value)) &&
				(notEmpty("Address", document.getElementById('contactform').Address.value)) &&
				(notEmpty("City", document.getElementById('contactform').City.value)) &&
				(notEmpty("State", document.getElementById('contactform').State[document.getElementById('contactform').State.selectedIndex].value)) &&
				(notEmpty("Zip Code", document.getElementById('contactform').Zip.value)) &&
				(radioNotEmpty("Contact Method", document.getElementById('contactform').Contact_Method)) &&
				(notEmpty("E-mail", document.getElementById('contactform').Email.value)) &&
				(echeck(document.getElementById('contactform').Email.value)==true) &&
				(notEmpty("Phone", document.getElementById('contactform').Phone.value)) &&
				(radioNotEmpty("MowerUsage", document.getElementById('contactform').MowerUsage)) &&
				(radioNotEmpty("Currently_Owned", document.getElementById('contactform').Currently_Owned)) &&
				(checkSerial(document.getElementById('contactform').Currently_Owned) == true) &&
				(notEmpty("Comment", document.getElementById('contactform').Comment.value)))
				return true;
			else
				return false;
		}
		else
		{
			if ((notEmpty("First Name", document.getElementById('contactform').FirstName.value)) &&
				(notEmpty("Last Name", document.getElementById('contactform').LastName.value)) &&
				(notEmpty("Address", document.getElementById('contactform').Address.value)) &&
				(notEmpty("City", document.getElementById('contactform').City.value)) &&
				(notEmpty("State", document.getElementById('contactform').State[document.getElementById('contactform').State.selectedIndex].value)) &&
				(notEmpty("Zip Code", document.getElementById('contactform').Zip.value)) &&
				(notEmpty("E-mail", document.getElementById('contactform').Email.value)) &&
				(echeck(document.getElementById('contactform').Email.value)==true) &&
				(notEmpty("Phone", document.getElementById('contactform').Phone.value)) &&
				(radioNotEmpty("MowerUsage", document.getElementById('contactform').MowerUsage)) &&
				(checkSerial(document.getElementById('contactform').Currently_Owned) == true) &&
				(notEmpty("Comment", document.getElementById('contactform').Comment.value)))
				return true;
			else
				return false;
		}
	}
	else
		return false;
}

