function MM_swapImgRestore() { //v3.0
 var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

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];}}
}

function MM_findObj(n, d) { //v4.0
 var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
 d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
 if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
 for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
 if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
 var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
 if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

//
//  check whether str is number
//
function isNumber(field, errorMsg) {
  str = String(field.value);

  if (str.length == 0) {
    if (errorMsg != null && errorMsg != '') alert(errorMsg);
    return false;
  }

  refString="1234567890";

  for (i = 0; i < str.length; i++)  {
    tempChar = str.substring(i, i + 1);
    if (refString.indexOf(tempChar) == -1) {
      if (errorMsg != null && errorMsg != '') alert(errorMsg);
      field.focus();
      return false;
    }
  }
  return true;
}

function isSpace(str, pos) {
    curChar = str.charAt(pos);
    return ( curChar == ' ' || curChar == '\x09' || curChar == '\xA0' ||  // xA0 - 160 - code for &nbsp;
             curChar == '\x0D' || curChar == '\x0A' );
}

//
//  removes leading and trailing spaces/tabs/CRs/LFs from string strToTrim
//
function strTrim(strToTrim) {
    strToTrim = String(strToTrim);
    if (strToTrim == "")
        return "";
    iStart = 0;
    while ((isSpace(strToTrim, iStart)) && 
            iStart < strToTrim.length - 1)
        iStart++;
    iEnd = strToTrim.length - 1;
    while ((isSpace(strToTrim, iStart)) &&
            iEnd >= iStart)
        iEnd--;
    return strToTrim.substr(iStart, iEnd - iStart + 1);
}

function isEmpty(field, errorMsg) {
    value = field.value;
    if (String(field.type).indexOf('select') != -1)
        if (value == "-1")
            value = "";

    if (strTrim(value) == "") {
        if (errorMsg != '')
            alert(errorMsg);
        field.focus();
        return true;
    }
    else
        return false;
}

function CheckSearch() {

 if(document.f1.what.value=="")
 {
  window.alert('Enter the search criteria, please!');
  document.f1.what.focus();
  return false;
 }

}

function CheckFriend() {

 if(document.sfriend.f1.value=="")
 {
  window.alert('Enter your email address!');
  document.sfriend.f1.focus();
  return false;
 }

 if(document.sfriend.f2.value=="")
 {
  window.alert('Enter your friend email address!');
  document.sfriend.f2.focus();
  return false;
 }

}

function CheckLogin() {

 if(document.lform.us.value=="")
 {
  window.alert('Enter your username, please!');
  document.lform.us.focus();
  return false;
 }

 if(document.lform.ps.value=="")
 {
  window.alert('Enter your password, please!');
  document.lform.ps.focus();
  return false;
 }

}


function CheckForgot() {

 if(document.ForgotForm.u2.value=="" && document.ForgotForm.e2.value=="")
 {
  window.alert('Please specify your username or e-mail!');
  document.ForgotForm.u2.focus();
  return false;
 }
}

function checkEmail(field, errorMsg) {
  if (!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(field.value)) {
    if (errorMsg != '')
        alert(errorMsg);
    field.focus();
    return false;
  }
  return true;
}
function CheckRegister() {

 if(document.mainForm.NewUsername.value=="")
 {
  window.alert('Enter your username, please!');
  document.mainForm.NewUsername.focus();
  return false;
 }

 if(document.mainForm.p1.value=="")
 {
  window.alert('Enter your password, please!');
  document.mainForm.p1.focus();
  return false;
 }

 if(document.mainForm.p2.value=="")
 {
  window.alert('Confirm your password, please!');
  document.mainForm.p2.focus();
  return false;
 }

 if(document.mainForm.p1.value != "" && document.mainForm.p2.value != "" && document.mainForm.p1.value != document.mainForm.p2.value)
 {
  window.alert('Enter and confirm your password again!');
  document.mainForm.p1.value="";
  document.mainForm.p2.value="";
  document.mainForm.p1.focus();
  return false;
 }
/*
 if(document.mainForm.FirstName.value=="")
 {
  window.alert('Enter your First Name, please!');
  document.mainForm.FirstName.focus();
  return false;
 }

 if(document.mainForm.city.value=="")
 {
  alert('Enter your city, please!');
  document.mainForm.city.focus();
  return false;
 }

 if(document.mainForm.state.value=="")
 {
  alert('Enter your state, please!');
  document.mainForm.state.focus();
  return false;
 }

 if(document.mainForm.zip.value=="")
 {
  alert('Enter your zip code, please!');
  document.mainForm.zip.focus();
  return false;
 }

 if(document.mainForm.phone.value=="")
 {
  window.alert('Enter your Phone, please!');
  document.mainForm.phone.focus();
  return false;
 }
*/
 if (!checkEmail(document.mainForm.email, 'Please specify valid e-mail address!')) {
    return false;
 }

 return true;
/*
 if(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(document.mainForm.email.value))
 {
  return true;
 }
 
 alert("Invalid E-mail Address! Please re-enter.");
 document.mainForm.email.focus();
 return false;
*/ 

}

function CheckProfile2() {
 if(document.mainForm.FirstName.value=="")
 {
  window.alert('Enter your First Name, please!');
  document.mainForm.FirstName.focus();
  return false;
 }

 if(document.mainForm.city.value=="")
 {
  alert('Enter your city, please!');
  document.mainForm.city.focus();
  return false;
 }

 if(document.mainForm.state.value=="")
 {
  alert('Enter your state, please!');
  document.mainForm.state.focus();
  return false;
 }

 s = String(document.mainForm.zip.value);
 if(s.length != 5)
 {
  alert('Please specify correct zip code!');
  document.mainForm.zip.focus();
  return false;
 }

 if(!isNumber(document.mainForm.zip, "Please specify correct zip code!")) return false;

 if(document.mainForm.phone.value=="")
 {
  window.alert('Enter your Phone, please!');
  document.mainForm.phone.focus();
  return false;
 }
 return true;
}

function CheckProfile() {

 if(document.mainForm.p1.value != "" && document.mainForm.p2.value != "" && document.mainForm.p1.value != document.mainForm.p2.value)
 {
  window.alert('Passwords does not match! Enter and confirm your password again!');
  document.mainForm.p1.value="";
  document.mainForm.p2.value="";
  document.mainForm.p1.focus();
  return false;
 }

 return CheckProfile2();
/*
 if(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(document.mainForm.email.value))
 {
  return true;
 }
 
 alert("Invalid E-mail Address! Please re-enter.");
 document.mainForm.email.focus();
 return false;
*/
}

function CheckDealerProfile() {

 if (isEmpty(document.mainForm.BusinessName, 'Please specify your dealership name!'))
  return false;
 
 if (isEmpty(document.mainForm.address, 'Please specify your dealership address!'))
  return false;

 return CheckProfile();
}

function CheckOffer() {

 if(document.mainForm.CategoryID.value=="0")
 {
  alert('Select the category in which your offer will appear!');
  document.mainForm.CategoryID.focus();
  return false;
 }

 if(document.mainForm.year.value=="")
 {
  alert('Select the vehicle year, please!');
  document.mainForm.year.focus();
  return false;
 }

 if(document.mainForm.make.value=="" || document.mainForm.make.value=="All")
 {
  alert('Select the vehicle make, please!');
  document.mainForm.make.focus();
  return false;
 }

 if(document.mainForm.model.value=="" || document.mainForm.model.value=="All")
 {
  alert('Enter the vehicle model, please!');
  document.mainForm.model.focus();
  return false;
 }
/*
 if(strTrim(document.mainForm.VehicleColor.value) == "")
 {
  alert('Enter the vehicle color, please!');
  document.mainForm.VehicleColor.focus();
  return false;
 }
*/
 if (!isEmpty(document.mainForm.Price, '')) {
    if (!isNumber(document.mainForm.Price, 'Please specify correct vehicle price!')) {
        return false;
    } 
 }

 if (!isEmpty(document.mainForm.mileage, '')) {
    if (!isNumber(document.mainForm.mileage, 'Please specify correct vehicle mileage!')) {
        return false;
    } 
 }

 return true;

}

function CheckOffer2() {

 if (!CheckOffer()) {
     return false;
 }

 if (!CheckProfile2()) {
    return false;
 }
 return true;

}

function checkSearchForm(form) {
    if (form.within.value != 'All') {
        zipStr = String(form.zip.value);
        if ((form.zip.value == '') ||
            (zipStr.length != 5) ||
            (!isNumber(form.zip))) {

            alert("Please specify correct zip code!");
            form.zip.select();
            return false;
        }
    }
}

function confirmRenewAll() {
    if (confirm('Would you like to renew all your ads for extra 30 days?')) {
        document.location.href = 'renewAll.php';
    }
}

function confirmRemove(id) {
    if (confirm('Would you like to remove this ad?')) {
        document.location.href = 'delete.php?id=' + id;
    }
}

function registerPrivate(button) {
  form.type.value = 'private';
  button.form.submit();
}

function isChecked(form, fieldName, errorMsg) {
    firstElem = null;
    for (i = 0; i < form.elements.length; i++) {
        field = form.elements[i];
        if ((field.name == fieldName) && (field.type == "checkbox")) {
            if (firstElem == null) firstElem = field;
            if (field.checked) return true;
        }
    }
    alert(errorMsg);
    if (firstElem != null)
        firstElem.focus();
    return false;
}
