function validatePhone(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');     

   if (fld.value == "") {
        error = "Please enter your 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;
}


function validateDate(fld){
	var error = "";
	if (!fld.value.match(/^[0-9]{4}\-(0[1-9]|1[012])\-(0[1-9]|[12][0-9]|3[01])/)) {
        error = "Dates must be in a YYYY-MM-DD format with the dashes.\n";
        fld.style.background = 'Yellow';
    } 
	if (fld.value.length > 10) {
	 	error = "Dates must be in a YYYY-MM-DD format with the dashes.\n";
        fld.style.background = 'Yellow';
	}
	if(fld.value == '0000-00-00' || fld.value == ""){
        error = "";
		fld.style.background = 'White';
    }
    return error;
}

	
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 = "Please enter your 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;
}

function validatePassword(fld) {
    var error = "";
    var illegalChars = /[\W_]/; // allow only letters and numbers 
 
    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "You didn't enter a password.\n";
    } else if ((fld.value.length < 7) || (fld.value.length > 15)) {
        error = "The password is the wrong length. \n";
        fld.style.background = 'Yellow';
    } else if (illegalChars.test(fld.value)) {
        error = "The password contains illegal characters.\n";
        fld.style.background = 'Yellow';
    } else if (!((fld.value.search(/(a-z)+/)) && (fld.value.search(/(0-9)+/)))) {
        error = "The password must contain at least one numeral.\n";
        fld.style.background = 'Yellow';
    } else {
        fld.style.background = 'White';
    }
   return error;
}   

function validateUsername(fld) {
    var error = "";
    var illegalChars = /\W/; // allow letters, numbers, and underscores
 
    if (fld.value == "") {
        fld.style.background = 'Yellow'; 
        error = "You didn't enter a username.\n";
    } else if ((fld.value.length < 5) || (fld.value.length > 15)) {
        fld.style.background = 'Yellow'; 
        error = "The username is the wrong length.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = 'Yellow'; 
        error = "The username contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    } 
    return error;
}


function validateEmpty(fld,errortxt) {
    var error = "";
  
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = errortxt+"\n";
    } else {
        fld.style.background = 'White';
    }
    return error;   
}

function validateChecked(fld,errortxt){
    var error="";
	
  	if (!fld.checked) {
        fld.style.background = 'Yellow'; 
        error = errortxt+"\n";
    } else {
        fld.style.background = 'White';
    }
    return error; 
}

function validateFormOnSubmit(theForm) {
var reason = "";

  reason += validateEmpty(theForm.fname,"Please enter your First Name");
  reason += validateEmpty(theForm.lname,"Please enter your Last Name");
  //reason += validateUsername(theForm.username);
  //reason += validatePassword(theForm.pwd);
  reason += validateEmail(theForm.email);
  reason += validatePhone(theForm.phone);
      
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

  return true;
}

function validateUpload(theForm) {
var reason = "";

  reason += validateEmpty(document.getElementById('status'),"Selct Status for this List");
      
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

  return true;
}

function validateLeadEntry(theForm) {
var reason = "";

  reason += validateEmpty(theForm.fname,"Please enter your First Name");
  reason += validateEmpty(theForm.lname,"Please enter your Last Name");

  if (theForm.email.value == "" && theForm.phone.value == "")
  {
  reason += "You must enter either a Phone Number or an Email Address.\n";
  theForm.phone.style.background = 'Yellow'; 
  theForm.email.style.background = 'Yellow';
  }
  else
  {
	  if (theForm.email.value != "")
	  {
	  reason += validateEmail(theForm.email);
	  }
	  else
	  {
	  theForm.email.style.background = 'White';
	  }
	  
	  if (theForm.phone.value != "")
	  {
	  reason += validatePhone(theForm.phone);
	  }
	  else
	  {
	  theForm.phone.style.background = 'White';
	  }
  }
  reason += validateDate(theForm.lead_date);
  reason += validateDate(theForm.sale_date);
  reason += validateDate(theForm.contact_date);
  reason += validateDate(theForm.appt_date);
      
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

  return true;
}

function ValidateEmailRemove(theForm) {

var reason = "";
reason += validateEmail(theForm.email);

  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

  return true;
}


//iframe_framework
function iframe_insert(){
var reason = "";

var client_id = encodeURI(document.getElementById('ssp_client_id').value);
var fname = "";
var lname = "";
var company_name = "";
var company_url = "";
var email = "";
var phone = "";
var comments = "";

if(document.getElementById('ssp_fname')) {
var fname = encodeURI(document.getElementById('ssp_fname').value);
reason += validateEmpty(document.getElementById('ssp_fname'),"Please enter your First Name");
}

if(document.getElementById('ssp_lname')) {
var lname = encodeURI(document.getElementById('ssp_lname').value);
reason += validateEmpty(document.getElementById('ssp_lname'),"Please enter your Last Name");
}

if(document.getElementById('ssp_company_name'))
var company_name = encodeURI(document.getElementById('ssp_company_name').value);

if(document.getElementById('ssp_company_url'))
var company_url = encodeURI(document.getElementById('ssp_company_url').value);

var email = encodeURI(document.getElementById('ssp_email').value);
reason += validateEmail(document.getElementById('ssp_email'));

if(document.getElementById('ssp_phone')) {
var phone = encodeURI(document.getElementById('ssp_phone').value);
reason += validatePhone(document.getElementById('ssp_phone'));
}

if(document.getElementById('ssp_comments'))
var comments = encodeURI(document.getElementById('ssp_comments').value);

var url = "http://www.simplesalespro.com/sql/sql_lead_webform_insert.php?client_id="+client_id+"&fname="+fname+"&lname="+lname+"&company_name="+company_name+"&company_url="+company_url+"&email="+email+"&phone="+phone+"&comments="+comments;


if (reason != "") {
alert("Some fields need correction:\n" + reason);
}


else {

document.getElementById('ssp_iframe_html').innerHTML = "<iframe frameborder='0' src='"+url+"' width='0' height='0' scrolling='no'></iframe>";
document.getElementById('ssp_insert_response').innerHTML = "Thank You "+fname+"<br>Please check your email."
document.getElementById('ssp_submit').innerHTML = "";
}

}


