function lTrim(str)
{
   var re;
   re  = /^\s*/;
   str = (str.replace(re,''));
   return str;
}



function rTrim(str)
{
   var len = str.length;
   var cnt = len-1;
   while(str.charAt(cnt) == " ")
   {
      cnt = cnt - 1;
      if (cnt <=0)
         break;
   }
   str = str.slice(0,cnt+1);
   return str;
}


function trimString(str)
{
   if (str)
   {
      return lTrim(rTrim(str));
   }
   else
   {
      return "";
   }
}



function isNumber(str)
{
   var strReg = /^([0-9])+$/;
   str = trimString(str);
   if(str.length > 0)
      return(strReg.test(str));
   return true;
}

function validateData()
{
    var errorCount = 0;
    var firstNameError = false;
    var lastNameError = false;
    var companyError = false;
    var agencyError = false;
    var phoneError = false;
    var emailError = false;
    var descriptionError = false;
    var budgetError = false;
    var timeframeError = false;

    // Reset error message
    document.getElementById("errs").innerHTML = "";
    document.getElementById("agency_label").style.color = "";
    document.getElementById("agency_label").style.fontWeight = "";
    document.getElementById("business").style.color = "";
    document.getElementById("business").style.fontWeight = "";
    document.getElementById("first_name_label").style.color = "";
    document.getElementById("first_name_label").style.fontWeight = "";
    document.getElementById("last_name_label").style.color = "";
    document.getElementById("last_name_label").style.fontWeight = "";
    document.getElementById("email_label").style.color = "";
    document.getElementById("email_label").style.fontWeight = "";    
    document.getElementById("telephone").style.color = "";
    document.getElementById("telephone").style.fontWeight = "";    
    document.getElementById("comments").style.color = "";
    document.getElementById("comments").style.fontWeight = "";    
    
    document.contactform.first_name.value = trimString(document.contactform.first_name.value);
    if(document.contactform.first_name.value=="")
    {
       document.getElementById("first_name_label").style.color = "#bd3824";
       document.getElementById("first_name_label").style.fontWeight = "bold";
       document.contactform.first_name.focus();
       errorCount++;
    } else {
      var check = 0;
      var str = trimString(document.contactform.first_name.value);
        var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]\*\`\&\$\/\{\}]/ ;
        if (str.match(illegalChars)) {
         firstNameError = true;
      }
    }    

    document.contactform.last_name.value = trimString(document.contactform.last_name.value);
	if(document.contactform.last_name.value=="")
    {
       document.getElementById("last_name_label").style.color = "#bd3824";
       document.getElementById("last_name_label").style.fontWeight = "bold";
       document.contactform.last_name.focus();
       errorCount++;
    } else {
      var check = 0;
      var str = trimString(document.contactform.last_name.value);
        var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]\*\`\&\$\/\{\}]/ ;
        if (str.match(illegalChars)) {
         lastNameError = true;
      }
    }    
    
    document.contactform.description.value = trimString(document.contactform.description.value);
    if(document.contactform.description.value=="")
    {
       document.getElementById("comments").style.color = "#bd3824";
       document.getElementById("comments").style.fontWeight = "bold";
       document.contactform.description.focus();
       errorCount++;
    }    

   document.getElementById("00N60000001hnaf").value = trimString(document.getElementById("00N60000001hnaf").value);
   if (document.getElementById("00N60000001hnaf").value!="")
   {
      var str = trimString(document.getElementById("00N60000001hnaf").value);
        var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]\*\`\&\$\/\{\}]/ ;
        if (str.match(illegalChars)) {
         agencyError = true;
      }
   }

   document.contactform.company.value = trimString(document.contactform.company.value);
   if (document.contactform.company.value!="")
   {
      var str = trimString(document.contactform.company.value);
        var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]\*\`\&\$\/\{\}]/ ;
        if (str.match(illegalChars)) {
         companyError = true;
      }
   }

   document.contactform.phone.value = trimString(document.contactform.phone.value);
   if(document.contactform.phone.value=="")
   {
       document.getElementById("telephone").style.color = "#bd3824";
       document.getElementById("telephone").style.fontWeight = "bold";
       document.contactform.phone.focus();
       errorCount++;
   }
   else
   {      
      var st = document.contactform.phone.value;
      var tempst=/^\d{3}-\d{3}-\d{4}$/;
      if(tempst.test(st)==false)
      {
          phoneError=true;
      }
   }
   
   document.contactform.email.value = trimString(document.contactform.email.value);
   if (document.contactform.email.value=="")
   {
      document.getElementById("email_label").style.color = "#bd3824";
      document.getElementById("email_label").style.fontWeight = "bold";
      document.contactform.email.focus();
      errorCount++;
   } else {
      var check = 0;
      var str = trimString(document.contactform.email.value);
      var emailFilter=/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        if (!(emailFilter.test(str))) {
         emailError = true;
      }
   }
   
   if (errorCount > 0)
   {
       document.getElementById("errs").innerHTML="<p style='color: #bd3824; padding: 0'> <b>Please complete all required fields before submitting your request for information.<br>  Thank you! </b></p><br>";   
       return false;
   }

   if (firstNameError) {
      document.getElementById("first_name_label").style.color = "#bd3824";
      document.getElementById("first_name_label").style.fontWeight = "bold";
      document.getElementById("errs").innerHTML = "<p style='color: #bd3824; padding: 0'> <b>The first name contains one or more illegal characters: ( ) < > , ; : \ \" [ ] * ` & $ / { } </b></p>";
      document.contactform.first_name.focus();
      return false;
   }

   if (lastNameError) {
      document.getElementById("last_name_label").style.color = "#bd3824";
      document.getElementById("last_name_label").style.fontWeight = "bold";
      document.getElementById("errs").innerHTML = "<p style='color: #bd3824; padding: 0'> <b>The last name contains one or more illegal characters: ( ) < > , ; : \ \" [ ] * ` & $ / { } </b></p>";
      document.contactform.last_name.focus();
      return false;
   }

   if (descriptionError) {
      document.getElementById("comments").style.color = "#bd3824";
      document.getElementById("comments").style.fontWeight = "bold";
      document.getElementById("errs").innerHTML = "<p style='color: #bd3824; padding: 0'> <b>The description contains one or more illegal characters: ( ) < > , ; : \ \" [ ] * ` & $ / { } </b></p>";
      document.contactform.description.focus();
      return false;
   }

   if (agencyError) 
   {
      document.getElementById("agency_label").style.color = "#bd3824";
      document.getElementById("agency_label").style.fontWeight = "bold";
      document.getElementById("errs").innerHTML = "<p style='color: #bd3824; padding: 0'> <b>The agency name contains one or more illegal characters: ( ) < > , ; : \ \" [ ] * ` & $ / { } </b></p>";
      document.contactform.agency.focus();
      return false;
   }

   if (companyError) 
   {
      document.getElementById("business").style.color = "#bd3824";
      document.getElementById("business").style.fontWeight = "bold";
      document.getElementById("errs").innerHTML = "<p style='color: #bd3824; padding: 0'> <b>The business name contains one or more illegal characters: ( ) < > , ; : \ \" [ ] * ` & $ / { } </b></p>";
      document.contactform.company.focus();
      return false;
   }

   if (phoneError)
   {
       document.getElementById("telephone").style.color = "#bd3824";
       document.getElementById("telephone").style.fontWeight = "bold";
       document.getElementById("errs").innerHTML="<p style='color: #bd3824; padding: 0'> <b>Please provide a valid phone no. (EX:333-333-3333)</b></p>";
       document.contactform.phone.focus();       
       return false;
   } 
   
   if (emailError) 
   {
      document.getElementById("email_label").style.color = "#bd3824";
      document.getElementById("email_label").style.fontWeight = "bold";
      document.getElementById("errs").innerHTML = "<p style='color: #bd3824; padding: 0'> <b>Please enter a valid email address. </b></p>";
      document.contactform.email.focus();
      return false;
   }
   
   return true;
}

function limitTextArea() {
	var limitNum = 32000;
	if (document.contactform.description.value.length > limitNum) {
		document.contactform.description.value = document.contactform.description.value.substring(0, limitNum);
	}
}