function checkForEnterKey(e,buttonName) {
  //the purpose of this function is to allow the enter key to 
  //point to the correct button to click.
  var characterCode;
    if (e && e.which){
      e = e;
      characterCode = e.which;
    }
    else {
      e = event;
      characterCode = e.keyCode;
    }  
        
    if (characterCode == 13){
      var btn = document.getElementById(buttonName);
      
      if (btn != null){ //If we find the button click it
        btn.click();
        return false;
      }          
    }
        else
      return true;
}

var isEmailAvail = true;

function submitForm (button) {
  var recurse = function (element) {
    if (element.tagName == 'FORM') return element;
    else {
      if (element.parentNode != null) return recurse(element.parentNode);
      else return null;
    }
  }
  var form = recurse(button);
  if (form != null) form.submit();
}

function imgswp(elementId,imgname){
  document.getElementById(elementId).src = imgname;
}

function imageover(imageid,imgname){
  document.getElementById(imageid).src = 'templates/images/'+imageid+'_color.png';
}

function imagerestore(imageid){
  document.getElementById(imageid).src = 'templates/images/'+imageid+'.png';
}

function imageover2(imageid,menutext){
  document.getElementById(imageid).src = 'templates/images/'+imageid+'_color.png';
  document.getElementById("menutext").innerHTML = menutext;
}

function imagerestore2(imageid){
  document.getElementById(imageid).src = 'templates/images/'+imageid+'.png';
  document.getElementById("menutext").innerHTML = "";
}

function switchcity(ppage,pid){
  //alert(pid);
  $('div#Jcity').load(ppage+'?region='+pid);
  //$('div#Jcity').load('<? echo HTTP_SERVER; ?>getcity.php');
}

function form_submit(){
  var frm = document.forms["form_signup_homeowner"];
  err_st = "";
//your name
  cn = frm.contactname.value;
  cn = cn.replace(/^\s+|\s+$/g,"");//trim
  if (cn.length == 0)
    err_st+= "Please enter your Name\r\n";
  else if (cn.length > 40)
    err_st+= "Max length for Your Name is 40\r\n";
  else if (cn.match("[^A-Za-z\\s]"))  
    err_st+= "Only Alphabetic Characters and Space Allowed for Your Name\r\n";
//end your name    

//postalcode
  pc = frm.postalcode.value;
  pc = pc.replace(/^\s+|\s+$/g,"");//trim
  if (pc.length == 0)
    err_st+= "Please enter your Postal Code\r\n";
  else if (pc.length != 6)
    err_st+= "6 Chars for Postal Code\r\n";
  else if (pc.match("[^A-Za-z0-9]"))  
    err_st+= "Only Alphabetic Characters and Numeric Allowed for Postal Code\r\n";
//end postalcode  

//salesref
  sr = frm.salesref.value;
  sr = sr.replace(/^\s+|\s+$/g,"");//trim
  if (sr.length > 20)
    err_st+= "Max 20 Alphanumeric Characters for Referer Id\r\n"; 
    else if (sr.match("[^A-Za-z0-9]"))
    err_st+= "Only Alphabetic Characters and Numeric Allowed for Referer Id\r\n";     
//end salesref 

//emailaddess
  ea = frm.emailaddress.value;
  ea = ea.replace(/^\s+|\s+$/g,"");//trim
  if (ea.length == 0)
    err_st+= "Please enter your Email Address\r\n";
  else if (!validateEmail(ea))  
    err_st+= "Email Address is invalid\r\n";    
//end emailaddress  
//password
  ps = frm.password.value;
  ps = ps.replace(/^\s+|\s+$/g,"");//trim
  if (ps.length == 0)
    err_st+= "Please enter your Password\r\n";
  else if (ps.length < 6)
    err_st+= "Password needs at least 6 Chars\r\n";
  else if (ps.length > 12)
    err_st+= "Password can not have more than 12 Chars\r\n";
  else if (ps.match("[^A-Za-z0-9]"))  
    err_st+= "Only Alphabetic Characters and Numeric Allowed for Password\r\n";
//end password
//password2
  ps2 = frm.password2.value;
  ps2 = ps2.replace(/^\s+|\s+$/g,"");//trim
  if (ps != ps2)
    err_st+= "Password doesn't match\r\n";
  
//end password2

  if (err_st != "")
    jAlert (err_st);
  else  
    frm.submit();  
}

function sform_submit(){
  var frm = document.forms["form_signup_tradesmen"];
  err_st = "";
//company name
  cn = frm.companyname.value;
  cn = cn.replace(/^\s+|\s+$/g,"");//trim
  if (cn.length == 0)
    err_st+= "Please enter the Company Name\r\n";
  else if (cn.length > 80)
    err_st+= "Max length for Company Name is 80\r\n";
//end companytname    
//address
  ad = frm.address.value;
  ad = ad.replace(/^\s+|\s+$/g,"");//trim
  if (ad.length == 0)
    err_st+= "Please enter Company Address\r\n";  
//end address    
//postalcode
  pc = frm.postalcode.value;
  pc = pc.replace(/^\s+|\s+$/g,"");//trim
  if (pc.length == 0)
    err_st+= "Please enter your Postal Code\r\n";
  else if (pc.length != 6)
    err_st+= "6 Chars for Postal Code\r\n";
  else if (pc.match("[^A-Za-z0-9]"))  
    err_st+= "Only Alphabetic Characters and Numeric Allowed for Postal Code\r\n";
//end postalcode
//salesref
  sr = frm.salesref.value;
  sr = sr.replace(/^\s+|\s+$/g,"");//trim
  if (sr.length > 20)
    err_st+= "Max 20 Alphanumeric Characters for Referer Id\r\n"; 
    else if (sr.match("[^A-Za-z0-9]"))
    err_st+= "Only Alphabetic Characters and Numeric Allowed for Referer Id\r\n";           
//end salesref 

//emailaddess
  ea = frm.emailaddress.value;
  ea = ea.replace(/^\s+|\s+$/g,"");//trim
  if (ea.length == 0)
    err_st+= "Please enter your Email Address\r\n";
  else if (!validateEmail(ea))  
    err_st+= "Email Address is invalid\r\n";    
//end emailaddress  
//password
  ps = frm.password.value;
  ps = ps.replace(/^\s+|\s+$/g,"");//trim
  if (ps.length == 0)
    err_st+= "Please enter your Password\r\n";
  else if (ps.length < 6)
    err_st+= "Password needs at least 6 Chars\r\n";
  else if (ps.length > 12)
    err_st+= "Password can not have more than 12 Chars\r\n";
  else if (ps.match("[^A-Za-z0-9]"))  
    err_st+= "Only Alphabetic Characters and Numeric Allowed for Password\r\n";
//end password
//password2
  ps2 = frm.password2.value;
  ps2 = ps2.replace(/^\s+|\s+$/g,"");//trim
  if (ps2.length == 0)
    err_st+= "Please re enter your Password\r\n";
  else if (ps != ps2)
    err_st+= "Password doesn't match\r\n";
  
//end password2
  if (err_st != "")
    jAlert (err_st);
  else {
    ct = document.getElementById("city").value;
  //alert ();
    if (ct != "")
      frm.jqcity.value = ct;
    frm.submit();
  }
}

function mmform_submit(){
  var frm = document.forms["form_signup_micromember"];
  err_st = "";
//your name
  cn = frm.yourname.value;
  cn = cn.replace(/^\s+|\s+$/g,"");//trim
  if (cn.length == 0)
    err_st+= "Please enter the Your Name\r\n";
  else if (cn.length > 40)
    err_st+= "Max length for Your Name is 40\r\n";
//end yourname    
//organization name
/*  cn = frm.orgname.value;
  cn = cn.replace(/^\s+|\s+$/g,"");//trim
  if (cn.length == 0)
    err_st+= "Please enter the Company Name\r\n";
  else if (cn.length > 80)
    err_st+= "Max length for Company Name is 80\r\n";*/
//end orgtname    
//address
  ad = frm.address.value;
  ad = ad.replace(/^\s+|\s+$/g,"");//trim
  if (ad.length == 0)
    err_st+= "Please enter Address\r\n";  
  //else if (ad.length > 50)
  //  err_st+= "Max length for Address is 50\r\n";
//end address    
//postalcode
  pc = frm.postalcode.value;
  pc = pc.replace(/^\s+|\s+$/g,"");//trim
  if (pc.length == 0)
    err_st+= "Please enter your Postal Code\r\n";
  else if (pc.length != 6)
    err_st+= "6 Chars for Postal Code\r\n";
  else if (pc.match("[^A-Za-z0-9]"))  
    err_st+= "Only Alphabetic Characters and Numeric Allowed for Postal Code\r\n";
//end postalcode
//phone
  pn = frm.phone.value;
  pn = pn.replace(/^\s+|\s+$/g,"");//trim
  
  if (pn.match("[^0-9]"))  
    err_st+= "Only Numeric Allowed for Phone Number\r\n";
//end phone 
//workphonenumber
  wp = frm.workphone.value;
  wp = wp.replace(/^\s+|\s+$/g,"");//trim
  
  if (wp.match("[^0-9]"))  
    err_st+= "Only Numeric Allowed for Work Phone Number\r\n";
//end workphonenumber 
//mfId
  sr = frm.mfId.value;
  sr = sr.replace(/^\s+|\s+$/g,"");//trim
  if (sr.length > 40)
    err_st+= "Max 40 Alphanumeric Characters for Preferred Name\r\n"; 
  else if (sr.match("[^A-Za-z0-9]"))
    err_st+= "Only Alphabetic Characters and Numeric Allowed for Preferred Name\r\n";           
//end mfId 
//Name on Cheque
  noc = frm.nameonchq.value;
  noc = noc.replace(/^\s+|\s+$/g,"");//trim
  if (noc.length == 0)
    err_st+= "Please enter the Name on cheque\r\n";
  else if (noc.length > 40)
    err_st+= "Max length for Name on cheque is 40\r\n";
//end Name on Cheque    
//address  on Cheque
  adc = frm.addronchq.value;
  adc = adc.replace(/^\s+|\s+$/g,"");//trim
  if (adc.length == 0)
    err_st+= "Please enter Address on cheque\r\n";  
  // else if (ad.length > 50)
  //  err_st+= "Max length for Address on cheque is 50\r\n";
//end address  on Cheque   
//hearaboutus
  var c_value = "^^";
  for (var i=0; i < frm.survey.length; i++){
    if (frm.survey[i].checked) {
      c_value = c_value + frm.survey[i].value + "^^";
    }
  }
  if (c_value == "^^")
    err_st+= "Please tell us how did you hear about us!\r\n";
  else {
    //cv = c_value.substr(0,c_value.length-3); 
    frm.strsurvey.value = c_value;
  }    

//end hearaboutus   
//emailaddess
  eax = frm.emailaddress.value;
  eax = eax.replace(/^\s+|\s+$/g,"");//trim
  if (eax.length == 0)
    err_st+= "Please enter your Email Address\r\n";
  else if (!validateEmail(eax))  
    err_st+= "Email Address is invalid\r\n";    
//end emailaddress  
//password
  ps = frm.password.value;
  ps = ps.replace(/^\s+|\s+$/g,"");//trim
  if (ps.length == 0)
    err_st+= "Please enter your Password\r\n";
  else if (ps.length < 6)
    err_st+= "Password needs at least 6 Chars\r\n";
  else if (ps.length > 12)
    err_st+= "Password can not have more than 12 Chars\r\n";
  else if (ps.match("[^A-Za-z0-9]"))  
    err_st+= "Only Alphabetic Characters and Numeric Allowed for Password\r\n";
//end password
//password2
  ps2 = frm.password2.value;
  ps2 = ps2.replace(/^\s+|\s+$/g,"");//trim
  if (ps2.length == 0)
    err_st+= "Please re enter your Password\r\n";
  else if (ps != ps2)
    err_st+= "Password doesn't match\r\n";
  
//end password2
  if (err_st != "")
    jAlert (err_st);
  else {
    ct = document.getElementById("city").value;
  //alert ();
    if (ct != "")
      frm.jqcity.value = ct;
    frm.submit();
  }
}

function pform_submit(){
  var frm = document.forms["form_credit_process"];
  err_st = "";
//company name
  cn = frm.ordName.value;
  cn = cn.replace(/^\s+|\s+$/g,"");//trim
  if (cn.length == 0)
    err_st+= "Please enter the Company Name\r\n";
  else if (cn.length > 32)
    err_st+= "Max length for Company Name is 32\r\n";
//end companytname    
//address
  ad = frm.ordAddress1.value;
  ad = ad.replace(/^\s+|\s+$/g,"");//trim
  if (ad.length == 0)
    err_st+= "Please enter Company Address\r\n";  
    else if (ad.length > 64)
      err_st+= "Max length for Company Address is 64\r\n";
//end address    
//postalcode
  pc = frm.ordPostalCode.value;
  pc = pc.replace(/^\s+|\s+$/g,"");//trim
  if (pc.length == 0)
    err_st+= "Please enter your Postal Code\r\n";
  else if (pc.length != 6)
    err_st+= "6 Chars for Postal Code\r\n";
  else if (pc.match("[^A-Za-z0-9]"))  
    err_st+= "Only Alphabetic Characters and Numeric Allowed for Postal Code\r\n";
//end postalcode

//emailaddess
  ea = frm.ordEmailAddress.value;
  ea = ea.replace(/^\s+|\s+$/g,"");//trim
  if (ea.length == 0)
    err_st+= "Please enter your Email Address\r\n";
  else if (!validateEmail(ea))  
    err_st+= "Email Address is invalid\r\n";    
//end emailaddress

//phonenumber
  pn = frm.ordPhoneNumber.value;
  pn = pn.replace(/^\s+|\s+$/g,"");//trim
  
  if (pn.match("[^0-9]"))  
    err_st+= "Only Numeric Allowed for Phone Number\r\n";
  if (pn.length == 0)
    err_st+= "Please enter Contact Phone Number\r\n";  
    else if (pn.length > 16)
      err_st+= "Max length for Phone Number is 16\r\n";        
//end phonenumber 

//card name
  //check only when using credit card
    if (frm.trnCardOwner.disabled == false) {
    co = frm.trnCardOwner.value;
    co = co.replace(/^\s+|\s+$/g,"");//trim
    if (co.length == 0 )
      err_st+= "Please enter the Name of the Card Owner as it appears on the credit card\r\n";
				else if (co.length < 4)
      err_st+= "Min length for the Name of the Card Owner is 4\r\n";
    else if (co.length > 64)
      err_st+= "Max length for the Name of the Card Owner is 64\r\n";
    }
//end card name

//card number
  //check only when using credit card
    if (frm.trnCardNumber.disabled == false) {
    cr = frm.trnCardNumber.value;
    cr = cr.replace(/^\s+|\s+$/g,"");//trim
  
    if (cr.match("[^0-9]"))  
      err_st+= "Only Numeric Allowed for Card Number\r\n";
    if (cr.length == 0)
      err_st+= "Please enter the Card Number\r\n";  
      else if (cr.length > 20)
        err_st+= "Max length for Card Number is 20\r\n";  
    }
//end card number

//cvv
//check only when using credit card
    if (frm.trnCardCvd.disabled == false) {
    cv = frm.trnCardCvd.value;
    cv = cv.replace(/^\s+|\s+$/g,"");//trim
  
    if (cv.match("[^0-9]"))  
      err_st+= "Only Numeric Allowed for CVV\r\n";
    if (cv.length == 0)
      err_st+= "Please enter the CVV Number\r\n";  
      else if (cv.length > 4)
      err_st+= "Max length for CVV Number is 4\r\n";        
    }
//end cvv

  if (err_st != "")
    jAlert (err_st);
  else {
    ct = document.getElementById("city").value;
  //alert ();
    if (ct != "")
      frm.ordCity.value = ct;
    frm.submit();
  }
}

function validateEmail(email){
  var splitted = email.match("^(.+)@(.+)$");
  if(splitted == null) return false;
  if(splitted[1] != null )
  {
    var regexp_user=/^\"?[\w-_\.]*\"?$/;
    if(splitted[1].match(regexp_user) == null) return false;
  }
  if(splitted[2] != null)
  {
    var regexp_domain=/^[\w-\.]*\.[A-Za-z]{2,4}$/;
    if(splitted[2].match(regexp_domain) == null) 
    {
   var regexp_ip =/^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/;
   if(splitted[2].match(regexp_ip) == null) return false;
    }// if
    return true;
  }
  return false;
}

function checkemailavail(email){
 
  avail = document.getElementById("login_id_check").innerHTML;
  jAlert (avail);
  if (avail == "noexist")
  return true;
  else
  return false;
}

function form_submit2(){//login check
  var frm = document.forms["frm_login"];
  err_st = "";

//emailaddess
  ea = frm.loginid.value;
  ea = ea.replace(/^\s+|\s+$/g,"");//trim
  if (ea.length == 0)
    err_st+= "Please enter your Email Address\r\n";  
  
//end emailaddress  
//password
  ps = frm.password.value;
  ps = ps.replace(/^\s+|\s+$/g,"");//trim
  if (ps.length == 0)
    err_st+= "Please enter your Password\r\n";  
//end password

  if (err_st != "")
    jAlert (err_st);
  else {  
    frm.is_submit.value = "yes";
    frm.submit();
  }
}

function form_submit3(){//using check forgot pass and sending temp pass
  var frm = document.forms["frm_resetpass"];
  err_st = "";

//emailaddess
  ea = frm.loginid.value;
  ea = ea.replace(/^\s+|\s+$/g,"");//trim
  if (ea.length == 0)
    err_st+= "Please enter your Email Address\r\n";  
  else if (!validateEmail(ea))  
    err_st+= "Email Address is invalid\r\n";      
//end emailaddress  

  if (err_st != "")
    jAlert (err_st);
  else {  
    frm.is_submit.value = "yes";
    frm.submit();
  }
}

function form_submit4(){//when check after login using temp pass and set new pass
  var frm = document.forms["reset_homeowner_pass"];
  err_st = "";

//password
  ps = frm.password.value;
  ps = ps.replace(/^\s+|\s+$/g,"");//trim
  if (ps.length == 0)
    err_st+= "Please enter your Password\r\n";
  else if (ps.length < 6)
    err_st+= "Password needs at least 6 Chars\r\n";
  else if (ps.length > 12)
    err_st+= "Password can not have more than 12 Chars\r\n";
  else if (ps.match("[^A-Za-z0-9]"))  
    err_st+= "Only Alphabetic Characters and Numeric Allowed for Password\r\n";
//end password
//password2
  ps2 = frm.password2.value;
  ps2 = ps2.replace(/^\s+|\s+$/g,"");//trim
  if (ps2.length == 0)
    err_st+= "Please re enter your Password\r\n";
  else if (ps != ps2)
    err_st+= "Password doesn't match\r\n";
  
//end password2

  if (err_st != "")
    jAlert (err_st);
  else {     
    frm.submit();
  }
}

function form_submit5(){
  var frm = document.forms["form_update_homeowner"];
  err_st = "";
//contact name
  cn = frm.contactname.value;
  cn = cn.replace(/^\s+|\s+$/g,"");//trim
  if (cn.length == 0)
    err_st+= "Please enter your Name\r\n";
  else if (cn.length > 40)
    err_st+= "Max length for your Name is 40\r\n";
  else if (cn.match("[^A-Za-z\\s]"))  
    err_st+= "Only Alphabetic Characters and Space Allowed for your Name\r\n";
//end contact name    

//postalcode
  pc = frm.postalcode.value;
  pc = pc.replace(/^\s+|\s+$/g,"");//trim
  if (pc.length == 0)
    err_st+= "Please enter your Postal Code\r\n";
  else if (pc.length != 6)
    err_st+= "6 Chars for Postal Code\r\n";
  else if (pc.match("[^A-Za-z0-9]"))  
    err_st+= "Only Alphabetic Characters and Numeric Allowed for Postal Code\r\n";
//end postalcode  

  if (err_st != "")
    alert (err_st);
  else    
    frm.submit(); 
}

function form_submit6(){//when check after login using temp pass and set new pass
  var frm = document.forms["reset_micromember_pass"];
  err_st = "";

//password
  ps = frm.password.value;
  ps = ps.replace(/^\s+|\s+$/g,"");//trim
  if (ps.length == 0)
    err_st+= "Please enter your Password\r\n";
  else if (ps.length < 6)
    err_st+= "Password needs at least 6 Chars\r\n";
  else if (ps.length > 12)
    err_st+= "Password can not have more than 12 Chars\r\n";
  else if (ps.match("[^A-Za-z0-9]"))  
    err_st+= "Only Alphabetic Characters and Numeric Allowed for Password\r\n";
//end password
//password2
  ps2 = frm.password2.value;
  ps2 = ps2.replace(/^\s+|\s+$/g,"");//trim
  if (ps2.length == 0)
    err_st+= "Please re enter your Password\r\n";
  else if (ps != ps2)
    err_st+= "Password doesn't match\r\n";
  
//end password2

  if (err_st != "")
    jAlert (err_st);
  else {     
    frm.submit();
  }
}

function form_submit7(){//when check after login using temp pass and set new pass
  var frm = document.forms["activation"];
  err_st = "";

//activationCode
  ps = frm.activationCode.value;
  ps = ps.replace(/^\s+|\s+$/g,"");//trim
  if (ps.length == 0)
    err_st+= "Please enter your Password\r\n";

  if (err_st != "")
    jAlert (err_st);
  else {     
    frm.submit();
  }
}

function form_submit8(){
  var frm = document.forms["form_micromember_chginfo"];
  err_st = "";
//your name
  cn = frm.yourname.value;
  cn = cn.replace(/^\s+|\s+$/g,"");//trim
  if (cn.length == 0)
    err_st+= "Please enter the Your Name\r\n";
  else if (cn.length > 20)
    err_st+= "Max length for Your Name is 40\r\n";
//end yourname    
//organization name
/*  cn = frm.orgname.value;
  cn = cn.replace(/^\s+|\s+$/g,"");//trim
  if (cn.length == 0)
    err_st+= "Please enter the Company Name\r\n";
  else if (cn.length > 80)
    err_st+= "Max length for Company Name is 80\r\n";
//end orgtname */   
//address
  ad = frm.address.value;
  ad = ad.replace(/^\s+|\s+$/g,"");//trim
  if (ad.length == 0)
    err_st+= "Please enter Address\r\n";  
  //else if (ad.length > 50)
 //   err_st+= "Max length for Address is 50\r\n";
//end address    
//postalcode
  pc = frm.postalcode.value;
  pc = pc.replace(/^\s+|\s+$/g,"");//trim
  if (pc.length == 0)
    err_st+= "Please enter your Postal Code\r\n";
  else if (pc.length != 6)
    err_st+= "6 Chars for Postal Code\r\n";
  else if (pc.match("[^A-Za-z0-9]"))  
    err_st+= "Only Alphabetic Characters and Numeric Allowed for Postal Code\r\n";
//end postalcode
//phone
  pn = frm.phone.value;
  pn = pn.replace(/^\s+|\s+$/g,"");//trim
  
  if (pn.match("[^0-9]"))  
    err_st+= "Only Numeric Allowed for Phone Number\r\n";
//end phone 
//workphonenumber
  wp = frm.workphone.value;
  wp = wp.replace(/^\s+|\s+$/g,"");//trim
  
  if (wp.match("[^0-9]"))  
    err_st+= "Only Numeric Allowed for Work Phone Number\r\n";
//end workphonenumber 
//Name on Cheque
  noc = frm.nameonchq.value;
  noc = noc.replace(/^\s+|\s+$/g,"");//trim
  if (noc.length == 0)
    err_st+= "Please enter the Name on cheque\r\n";
  else if (noc.length > 40)
    err_st+= "Max length for Name on cheque is 40\r\n";
//end Name on Cheque    
//address  on Cheque
  adq = frm.addronchq.value;
  adq = adq.replace(/^\s+|\s+$/g,"");//trim
  if (adq.length == 0)
    err_st+= "Please enter Address on cheque\r\n";  
  //else if (ad.length > 50)
  //  err_st+= "Max length for Address on cheque is 50\r\n";
//end address  on Cheque   
  if (err_st != "")
    jAlert (err_st);
  else {
    ct = document.getElementById("city").value;
  //alert ();
    if (ct != "")
      frm.jqcity.value = ct;
    frm.submit();
  }
}

function tellfriend_submit(){
  var frm = document.forms["form_tellfriend"];
    err_st = "";
//emailaddess
  ye = frm.youremail.value;
  ye = ye.replace(/^\s+|\s+$/g,"");//trim
  if (ye.length == 0)
    err_st+= "Please enter your Email Address\r\n";
  else if (!validateEmail(ye))  
    err_st+= "Your Email Address is invalid\r\n";    
//end emailaddress  
//friendemailaddess
  fe = frm.friendemail.value;
  fe = fe.replace(/^\s+|\s+$/g,"");//trim
  if (fe.length == 0)
    err_st+= "Please enter your friend's Email Address\r\n";
  else if (!validateEmail(fe))  
    err_st+= "Your Friend's Email Address is invalid\r\n";    
//end friendemailaddress  
  if (err_st != "")
    jAlert (err_st);
  else {
      frm.submit();  
    }
}

function tform_submit(){
  var frm = document.forms["form_update_tradesmen"];
  err_st = "";
//company name
  cn = frm.companyname.value;
  cn = cn.replace(/^\s+|\s+$/g,"");//trim
  if (cn.length == 0)
    err_st+= "Please enter the Company Name\r\n";
  else if (cn.length > 80)
    err_st+= "Max length for Company Name is 80\r\n";
//end companytname    
//address
  ad = frm.address.value;
  ad = ad.replace(/^\s+|\s+$/g,"");//trim
  if (ad.length == 0)
    err_st+= "Please enter Company Address\r\n";  
//end address    
//postalcode
  pc = frm.postalcode.value;
  pc = pc.replace(/^\s+|\s+$/g,"");//trim
  if (pc.length == 0)
    err_st+= "Please enter your Postal Code\r\n";
  else if (pc.length != 6)
    err_st+= "6 Chars for Postal Code\r\n";
  else if (pc.match("[^A-Za-z0-9]"))  
    err_st+= "Only Alphabetic Characters and Numeric Allowed for Postal Code\r\n";
//end postalcode  
//emailaddess
  ea = frm.companyemail.value;
  ea = ea.replace(/^\s+|\s+$/g,"");//trim
  if (ea.length == 0)
    err_st+= "Please enter your Email Address\r\n";
  else if (!validateEmail(ea))  
    err_st+= "Email Address is invalid\r\n";    
//end emailaddress  
//phonenumber
  pn = frm.mainphone.value;
  pn = pn.replace(/^\s+|\s+$/g,"");//trim
  if (pn.length == 0)
    err_st+= "Please enter your Primary Contact Number\r\n";
    else if (pn.length != 10)
      err_st+= "Need 10 digits on Primary Contact Phone Number\r\n";
  else if (pn.match("[^0-9]"))  
    err_st+= "Only Numeric Allowed for Phone Number\r\n";
//end phonenumber  
//faxnumber
  fx = frm.fax.value;
  fx = fx.replace(/^\s+|\s+$/g,"");//trim
    if (fx.match("[^0-9]"))  
    err_st+= "Only Numeric Allowed for Fax Number\r\n";
    else if (fx.length >0 && fx.length != 10)
      err_st+= "Need 10 digits on Fax Number\r\n";
//end faxnumber  
//web url
  wu = frm.weburl.value;
  wu = wu.replace(/^\s+|\s+$/g,"");//trim
    if (wu.length > 0){
    var v = new RegExp();
    
    v.compile("^[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$");
    if (!v.test(wu))
      err_st+= "Web URL Pattern does not Match\r\n";
//end web url
//tfurl
    }
  tfu = frm.tfurl.value;
  tfu = tfu.replace(/^\s+|\s+$/g,"");//trim
  if (tfu.match("[^A-Za-z0-9]"))  
    err_st+= "Only Alphabetic Characters and Numeric Allowed for Tradefinder link\r\n";
  else if (tfu.length <3)
    err_st+= "Tradefinder link needs at least 3 Chars\r\n";
  else if (tfu.length > 30)
    err_st+= "Tradefinder link can not have more than 30 Chars\r\n";    
//end tfurl
//trades
  cmt = frm.companytrades.value;
  cmt = cmt.replace(/^\s+|\s+$/g,"");//trim
  if (cmt.length == 0)
    err_st+= "Company Trades Needed\r\n";
//end trades
//service range
  sr = frm.servicerange.value;
  sr = sr.replace(/^\s+|\s+$/g,"");//trim
  if (sr.length == 0)
    err_st+= "Service Range Needed\r\n";
//end service range
//full address and postalcode check
  ct = document.getElementById("city").value;
  prov = frm.province.value;
  partaddress = ad+" "+ct+" "+prov;
  postc = pc;//postal code
//end of address check
  //initialize();
  //alert(fulladdress);
  fulladdress = partaddress+" "+postc;
  
    
    //alert(frm.mlat.value);
    //alert(frm.mlon.value);
    //window.setTimeout("alert("+frm.mlon.value+")",3000);
    
     
  if (err_st != "")
    jAlert (err_st);
  else {
    //ct = document.getElementById("city").value;
    //alert ();
    if (ct != "")
      frm.jqcity.value = ct;
      geocoder.getLocations(fulladdress, addAddressToMap);
        
      if (is_set)    
        frm.submit();
      else
        window.setTimeout("tform_submit()",1000);
  }
}

function showAddress(address) { 
  geocoder.getLatLng( 
    address, 
    function(point) { 
      if (!point) { 
        jAlert(address + " not found"); 
      } else { 
        map.setCenter(point, 15); 
        var marker = new GMarker(point); 
        map.addOverlay(marker); 
        //marker.openInfoWindowHtml(address); 
      } 
    } 
  ); 
}

function img_validate(){
  var isfiletype = false;
  var extensions = new Array("jpg","jpeg","gif","png");
  var image_file = document.form_uploadimg.fileField.value;
  var image_length = document.form_uploadimg.fileField.value.length;
  var image_hasfile = document.form_uploadimg.maxfiles.value;  
  var pos = image_file.lastIndexOf('.') + 1;
  var ext = image_file.substring(pos, image_length);
  var final_ext = ext.toLowerCase();
  if (image_hasfile >=5)
     jAlert("You are Only Allowed to Upload 5 Files!");
  else{  
    for (i = 0; i < extensions.length; i++){
      if(extensions[i] == final_ext){
        isfiletype = true;
        loading_item = parseInt(image_hasfile)+1;
        loading_itemid = "imagetn_"+loading_item;
        //alert(loading_itemid);
        Jimage.document.getElementById(loading_itemid).style.display = "block";
        document.form_uploadimg.submit();
      }
    }
  
    if (isfiletype == false)
      jAlert("You Must Upload an Image File with One of the Following Extensions: "+ extensions.join(', ') +".");
  }
}

function toolargesize(fsize){
  mfsize = parseFloat(fsize/(1024.00*1024));
  mfsize = Math.round(mfsize*100)/100;
  jAlert("Your Image Size is "+mfsize+" MB, The Maximum Size Allowed is 2 MB!");
}

function urltaken(name,hd){
  jAlert("Your Tradefinder Link "+name+" has been taken, Please try another one!");
  window.location = hd+'myttoolbox.php?stage=3';
}

function project_submit(stage){
  var frm = document.forms["form_postjob"];
  var imgfrm = document.forms["form_uploadimg"];
  var maxbids = 3;
  var minbids = 3;
  var maxdays = 7;
  var mindays = 7;
  err_st = "";
  alert_st = "";
//contact name
  cn = frm.contactname.value;
  cn = cn.replace(/^\s+|\s+$/g,"");//trim
  if (cn.length == 0)
    err_st+= "Please enter your Contact Name\r\n";
  else if (cn.length > 80)
    err_st+= "Max length for First Name is 80\r\n";
  else if (cn.match("[^A-Za-z\\s]"))  
    err_st+= "Only Alphabetic Characters and Space Allowed for Contact Name\r\n";
//end contactname    
//project address
  pa = frm.projectaddress.value;
  pa = pa.replace(/^\s+|\s+$/g,"");//trim
  if (pa.length == 0)
    err_st+= "Please enter the Project Address\r\n";
//end project address    
//phonenumber
  pn = frm.phone.value;
  pn = pn.replace(/^\s+|\s+$/g,"");//trim
  if (pn.length == 0)
    err_st+= "Please enter your Primary Phone Number\r\n";
  else if (pn.match("[^0-9]"))  
    err_st+= "Only Numeric Allowed for Phone Number\r\n";
//end phonenumber  
//emailaddess
  ea = frm.emailaddress.value;
  ea = ea.replace(/^\s+|\s+$/g,"");//trim
  if (ea.length == 0)
    err_st+= "Please enter your Email Address\r\n";
  else if (!validateEmail(ea))  
    err_st+= "Email Address is invalid\r\n";    
//end emailaddress  
//project title
  pt = frm.projecttitle.value;
  pt = pt.replace(/^\s+|\s+$/g,"");//trim
  if (pt.length == 0)
    err_st+= "Please enter the Project Title\r\n";
//end project title
//postalcode
  pc = frm.postalcode.value;
  pc = pc.replace(/^\s+|\s+$/g,"");//trim
  if (pc.length == 0)
    err_st+= "Please enter your Postal Code\r\n";
  else if (pc.length != 6)
    err_st+= "6 Chars for Postal Code\r\n";
  else if (pc.match("[^A-Za-z0-9]"))  
    err_st+= "Only Alphabetic Characters and Numeric Allowed for Postal Code\r\n";
//end postalcode  
//category
  jc = frm.jobcode.value;
  jc = jc.replace(/^\s+|\s+$/g,"");//trim
  if (jc.length == 0)
    err_st+= "Please enter the Project Category\r\n";
//end category
//job details
  jd = frm.jobdetails.value;
  jd = jd.replace(/^\s+|\s+$/g,"");//trim
  if (jd.length == 0)
    err_st+= "Please enter the Project Details\r\n";
//end job details
//bids number
  bn = frm.bidsnumber.value;
  bn = bn.replace(/^\s+|\s+$/g,"");//trim
  if (bn.length == 0)
    err_st+= "Please enter Bids Number\r\n";
  else if (bn.match("[^0-9]"))  
    err_st+= "Only Numeric Allowed for Bids Number\r\n";
  else if (bn > maxbids)
    err_st+= "You can not have More Than 3 Bids\r\n";
  else if (bn < minbids)
    err_st+= "You Need at Least 3 Bids\r\n";  
//end of bids number
//bids days
  bd = frm.daysopen.value;
  bd = bd.replace(/^\s+|\s+$/g,"");//trim
  if (bd.length == 0)
    err_st+= "Please enter Bids Open Days\r\n";
  else if (bd.match("[^0-9]"))  
    err_st+= "Only Numeric Allowed for Bids Open Days\r\n";
  else if (bd > maxdays)
    err_st+= "You can not have More Than 7 Open Days\r\n";
  else if (bd < mindays)
    err_st+= "You Need at Least 7 Open Day\r\n";  
//end of bids days
//alert for images
  nimg = imgfrm.maxfiles.value;
  if (nimg == 0 || nimg == "")
    alert_st = "We noticed you did not attach any images, to have a better response, we always recommend to have them uploaded before you post a project";
//end of alert images
  frm.dostage.value = stage;
  ct = document.getElementById("city").value;
  //full address and postalcode check
  
  prov = frm.province.value;
  partaddress = pa+" "+ct+" "+prov;
  postc = pc;//postal code;
//end of address check
  //initialize();
  //alert(fulladdress);
  fulladdress = partaddress+" "+postc;
 
    
  if (ct != "")
    frm.jqcity.value = ct;
        
  if (err_st != "")
    jAlert (err_st);
  else {
    //alert (fulladdress); 
    geocoder.getLocations(fulladdress, addAddressToMap);
    if (alert_st != ""){
      //if (confirm(alert_st)){
      //  frm.submit();
      //}
      if (stage == "pubprj")
        alert_st+= "\r\n\r\nYou are not allowed to change any information after you post a project!";
      jConfirm(alert_st, 'Confirmation Dialog', function(r) {
        if (r)
          frm.submit(); 
      });
    }
    else{
      frm.imgtf.value = imgfrm.imgarray.value;
      if (is_set)
        frm.submit();
      else
        window.setTimeout("project_submit('"+stage+"')",1000);
    }
  }
}

function setdivBackground(divname,divcolor,groupname,amount){
  for (i=0;i<amount;i++){
    tempname = groupname+i;
    tlayer = "projectlister"+i;
    //alert(tempname);
    document.getElementById(tempname).style.backgroundColor = "";
    document.getElementById(tlayer).style.display = "none";
  }
  projectfolder = "projectfolder"+divname;
  projectlister = "projectlister"+divname;
  document.getElementById(projectfolder).style.backgroundColor = divcolor;
  document.getElementById(projectlister).style.display = "block";
}

var pItemSel = "";

function setBk2(mid){
  //alert(mid);
  setBk(mid);
  pItemSel = mid;
  //alert(pItemSel);
}

function setBk(mid){
  //alert(mid);
  var divs = document.getElementsByTagName("div");
  for (var i = 0;i<divs.length; i++){
    if (divs[i].className == "projectitems")
      divs[i].style.backgroundColor = "";
  }
  document.getElementById(mid).style.backgroundColor = "#DEEBFF";
  //pItemSel = mid;
  //alert(pItemSel);
}

function editprj(hd){  
  pItemSel = document.theform2.pitem_id.value; 
    if (pItemSel == "")
    jAlert ("Project Id is missing, can't continue!");
  else {
    document.theform2.action = hd+"myhtoolbox.php?stage=1b";
    document.theform2.submit();
  }
}

function postprj(hd,sfile){  
  pItemSel = document.theform2.pitem_id.value; 
  if (pItemSel == "")
    jAlert ("Project Id is missing, can't continue!");
  else {
    if (sfile == 0)
      alert_st = "We noticed you did not attach any images, to have a better response, we always recommend to have them uploaded before you post a project";
    else
      alert_st = "";
      
    alert_st+= "\r\n\r\nYou are not allowed to change any information after you post a project!";
        
    jConfirm(alert_st, 'Confirmation Dialog', function(r) {
      if (r) {
        document.theform2.action = hd+"myhtoolbox.php?stage=1e";
        document.theform2.submit();
      }
    });        
  }
}

function bidview(hd){
  if (pItemSel == "")
    jAlert ("You need to select one of the projects to start!");
  else{
    document.theform.pid.value = pItemSel;    
    document.theform.action = hd+"myttoolbox.php?stage=3b";
    document.theform.submit();
  }
}

function bidpreview(hd){
  if (pItemSel == "")
    jAlert ("You need to select one of the projects to start!");
  else{
    document.theform.pid.value = pItemSel;    
    document.theform.action = hd+"myttoolbox.php?stage=3a";
    document.theform.submit();
  }
}

function wonmove(hd){
  if (pItemSel == "")
    jAlert ("You need to select one of the projects to start!");
  else{
    document.theform.pid.value = pItemSel; 
        document.theform.method.value = "wonmove";
    document.theform.action = hd+"myttoolbox.php?stage=3";
    document.theform.submit();
  }
}

function activemove(hd){
  if (pItemSel == "")
    jAlert ("You need to select one of the projects to start!");
  else{
    document.theform.pid.value = pItemSel; 
        document.theform.method.value = "activemove";
    document.theform.action = hd+"myttoolbox.php?stage=3";
    document.theform.submit();
  }
}

function expiredmove(hd){
  if (pItemSel == "")
    jAlert ("You need to select one of the projects to start!");
  else{
    document.theform.pid.value = pItemSel; 
        document.theform.method.value = "expiredmove";
    document.theform.action = hd+"myttoolbox.php?stage=3";
    document.theform.submit();
  }
}

function emailContact(hd){
  if (pItemSel == "")
    jAlert ("You need to select one of the referees to start!");
  else{
    document.theform.pid.value = pItemSel;    
    document.theform.action = hd+"mymmtoolbox.php?stage=3a";
    document.theform.submit();
  }
}

function pview(hd){
  if (pItemSel == "")
    jAlert ("You need to select one of the projects to start!");
  else{
    document.theform.pitem_id.value = pItemSel;    
    document.theform.action = hd+"myhtoolbox.php?stage=1a";
    document.theform.submit();
  }
}

function pedit(hd){
  if (pItemSel == "")
    jAlert ("You need to select one of the projects to start!");
  else{
    document.theform.pitem_id.value = pItemSel;    
    document.theform.action = hd+"myhtoolbox.php?stage=1b";
    document.theform.submit();
  }
}

function ppublish(hd){
  if (pItemSel == "")
    jAlert ("You need to select one of the projects to start!");
  else{
    document.theform.pitem_id.value = pItemSel;    
    document.theform.action = hd+"myhtoolbox.php?stage=1e";
    document.theform.submit();
  }
}

function pdelete(hd){
  if (pItemSel == "")
    jAlert ("You need to select one of the projects to start!");
  else{
    jConfirm('Do you really want to delete this project?', 'Confirmation Dialog', function(r) {
    if (r){
      document.theform.pitem_id.value = pItemSel;    
      document.theform.action = hd+"myhtoolbox.php?stage=1c";      
      document.theform.submit();
      }
    });

    /*if (jConfirm("Do you really want to delete this project?")){
      document.theform.pitem_id.value = pItemSel;    
      document.theform.action = hd+"myhtoolbox.php?stage=1c";
      //alert(document.theform.pitem_id.value);
      document.theform.submit();*/   
  }
}


//gmap library
var map;
var geocoder;
//var m_lat = 0;
//var m_lon = 0;
var is_response;
var is_set;

function initialize() {
  map = new GMap2(document.getElementById("map_canvas"));
  //map.setCenter(new GLatLng(34, 0), 1);
  geocoder = new GClientGeocoder();
  is_set = false;
  is_response = false;
}

  // addAddressToMap() is called when the geocoder returns an
  // answer.  It adds a marker to the map with an open info window
  // showing the nicely formatted version of the address and the country code.

function addAddressToMap(response) {
  
  map.clearOverlays();
  if (!response || response.Status.code != 200) {
    jAlert ("Sorry, we were unable to geocode that address");
    is_set = true;
  } else {
    place = response.Placemark[0];
    point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
    m_lat = place.Point.coordinates[1];
    m_lon = place.Point.coordinates[0];
    // marker = new GMarker(point);
    // map.addOverlay(marker);
    // marker.openInfoWindowHtml(place.Point.coordinates[0]+
    //  ';' + place.Point.coordinates[1]);
    //var frm = document.forms["form_update_tradesmen"];
    //document.getElementById("mlat").value = m_lat;
    //document.getElementById("mlon").value = m_lon;        
    //alert (m_lat+";"+m_lon);
    //return point;
    is_response = true;
    if (is_response)
      dotimeout(m_lat,m_lon);
  }  
}

    // showLocation() is called when you click on the Search button
    // in the form.  It geocodes the address entered into the form
    // and adds a marker to the map at that location.
function showLocation() {
  var address = document.forms[0].q.value;
  geocoder.getLocations(address, addAddressToMap);
}

   // findLocation() is used to enter the sample addresses into the form.
function findLocation(address) {
  document.forms[0].q.value = address;
  showLocation();
}

function dotimeout(lat,lon) {
  document.getElementById("mlat").value = lat;
  document.getElementById("mlon").value = lon;  
  is_set = true;
  //alert (lat+";"+lon);
}

function clickBid(project,istokenenuf){
  if (istokenenuf < 0)
    jAlert ("You do not have enough credits to bid on this project, Please click on the 'Get More Credits' tab to purchase credits!");
  else
    document.form_bids.submit();        
}

function prselect(id){
  for (i=0; i<document.prform.p_select.length; i++){
    if (document.prform.p_select[i].checked == true)
      document.prform.p_select[i].checked = false;
  }
  document.getElementById("p_select_"+id).checked = true; 
  pItemSel = id;  
} 

function switchhfaq(hd,id){
  imgname = document.getElementById("ha"+id).src;
    //andiv = document.getElementById("hq"+id).style.display;
    //alert(hd+";"+imgname);
    if (imgname == hd+"images/row-expand.gif"){
      document.getElementById("ha"+id).src = hd+"images/row-collapse.gif";
        document.getElementById("ha"+id).title = "click to close";
        document.getElementById("hq"+id).style.display = "block";
    }
    else if (imgname == hd+"images/row-collapse.gif"){
      document.getElementById("ha"+id).src = hd+"images/row-expand.gif"; 
        document.getElementById("ha"+id).title = "click to open";
        document.getElementById("hq"+id).style.display = "none";
    }
}

function switchtfaq(hd,id){
  imgname = document.getElementById("ta"+id).src;  
    //andiv = document.getElementById("tq"+id).style.display;
    //alert(hd+";"+imgname);
    if (imgname == hd+"images/row-expand.gif"){
      document.getElementById("ta"+id).src = hd+"images/row-collapse.gif";
      document.getElementById("ta"+id).title = "click to close";
      document.getElementById("tq"+id).style.display = "block";
    }
    else if (imgname == hd+"images/row-collapse.gif"){
      document.getElementById("ta"+id).src = hd+"images/row-expand.gif"; 
      document.getElementById("ta"+id).title = "click to open";
      document.getElementById("tq"+id).style.display = "none";
    }
}

function switchhfaqheader(hd,this_id){
  imgname = document.getElementById("haheader").src;  
  if (imgname == hd+"images/row-expand.gif"){
    document.getElementById("haheader").src = hd+"images/row-collapse.gif";
    document.getElementById(this_id).title = "click to close quick list";
    document.getElementById("haheadertext").innerHTML = "Click to close quick list";
    document.getElementById("faqhomeownercontent").style.display = "block";
  }
  else if (imgname == hd+"images/row-collapse.gif"){
    document.getElementById("haheader").src = hd+"images/row-expand.gif";
    document.getElementById(this_id).title = "click to view quick list";
    document.getElementById("haheadertext").innerHTML = "Click to view quick list";
    document.getElementById("faqhomeownercontent").style.display = "none";
  }
}

function switchtfaqheader(hd,this_id){
  imgname = document.getElementById("taheader").src;  
  if (imgname == hd+"images/row-expand.gif"){
    document.getElementById("taheader").src = hd+"images/row-collapse.gif";
    document.getElementById(this_id).title = "click to close quick list";
    document.getElementById("taheadertext").innerHTML = "Click to close quick list";
    document.getElementById("faqtradesmencontent").style.display = "block";
  }
  else if (imgname == hd+"images/row-collapse.gif"){
    document.getElementById("taheader").src = hd+"images/row-expand.gif";
    document.getElementById(this_id).title = "click to view quick list";
    document.getElementById("taheadertext").innerHTML = "Click to view quick list";
    document.getElementById("faqtradesmencontent").style.display = "none";
  }
}

function switchMenuContent(currentid,totalid){
  for (i=0;i<totalid;i++){
    document.getElementById("sidemenuContent"+i).style.display = "none";
  }
  document.getElementById("sidemenuContent"+currentid).style.display = "block";
}

function bookmarksite(){
  if (document.all)
    window.external.AddFavorite(location.href, document.title);
  else if (window.sidebar)
    window.sidebar.addPanel(document.title, location.href, "");
}

function showmoreCate(ncates){
  for (i=5;i<ncates;i++){      
    document.getElementById("cate_"+i).style.display = "block";  
  }  
  document.getElementById("ulcate").style.listStyleType = "disc";
}

function openselcity(type){
  if (type == "emergency"){
		  //document.getElementById("selcity_emergency").style.display = "block";
				//document.getElementById("selcity_discount").style.display = "none";
				$('#selcity_discount').slideUp('slow', function(){});
				$('#selcity_emergency').slideDown('slow', function(){});
		}
		else if (type == "discount"){
	   //document.getElementById("selcity_emergency").style.display = "none";
				//document.getElementById("selcity_discount").style.display = "block";	
				$('#selcity_emergency').slideUp('slow', function(){});
				$('#selcity_discount').slideDown('slow', function(){});
		}
}

function closeselcity(la){
  $(document.getElementById(la)).slideUp('slow', function(){});
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
 //-->

