// JavaScript Document
// Form interaction controls

$(document).ready(function(){
	$("#exsisting_customer").hide();
	$("#dealerInfo").hide();
	
});
function exsistingOpen(){
	$("#exsisting_customer").show('fast');		
} 
function exsistingClose(){
	$("#exsisting_customer").hide('fast');	
} 
function dealerOpen(){
	$("#dealerInfo").show('fast');		
} 
function dealerClose(){
	$("#dealerInfo").hide('fast');	
} 

// form Validation
function validate_form(theForm) {
var reason = "";

  reason += validateUsername(theForm.full_name);
  reason += validateEmail(theForm.email_address);
  //reason += validatePhoneNum(theForm.phone_number);
  reason += validateContactProfession(theForm.profession);
  reason += validateType(theForm.project_type);
  reason += validateContactPref(theForm.c_contact_pref);  
  // reason += validateEmpty(theForm.from);
      
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

  return true;
}
//Phone
function validatePhoneNum(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');    

   if (fld.value == "") {
        error = "You didn't enter a phone number.\n";
        fld.style.background = 'Yellow';
    } else if (isNaN(parseInt(stripped))) {
        error = "The phone number contains illegal characters.\n";
        fld.style.background = 'Yellow';
    } else if (!(stripped.length == 10)) {
        error = "The phone number is the wrong length. Make sure you included an area code.\n";
        fld.style.background = 'Yellow';
    } else {
        fld.style.background = 'White';
    }
    return error;
}

//Email
function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}

function validateEmail(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;   
    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "You didn't enter an email address.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = 'Yellow';
        error = "Please enter a valid email address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'Yellow';
        error = "The email address contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

//Name
function validateUsername(fld) {
    var error = "";
 
    if (fld.value == "") {
        fld.style.background = 'Yellow'; 
        error = "You didn't enter your name.\n";
    } 
      else {
        fld.style.background = 'White';
    }
    return error;
}
//Contact Method
function validateContactPref(fld) {
    var error = ""; 
    if (fld.value == "") {
        fld.style.background = 'Yellow'; 
        error = "Please select a prefered contact method.\n";
    } 
      else {
        fld.style.background = 'White';
    }
    return error;
}
// Radio Button Validation
// copyright Stephen Chapman, 15th Nov 2004,14th Sep 2005
//Profession
function validateContactProfession(fld) {
    var error = ""; 
	var cnt = -1;
    for (var i=fld.length-1; i > -1; i--) {
    	if (fld[i].checked) {cnt = i; i = -1;}
    }
    if (cnt > -1){ 
		//nothing
	}else{
        error = "Please select a profession.\n";
	}
	return error;
}
function validateType(fld) {
    var error = ""; 
	var cnt = -1;
    for (var i=fld.length-1; i > -1; i--) {
    	if (fld[i].checked) {cnt = i; i = -1;}
    }
    if (cnt > -1){ 
	//nothing
	}else{
        error = "Please select the type of project.\n";
	}
	return error;
}
      