function inputCustomInfo(frm) {
   var input_array = new Array('firstname','lastname','address','city','postcode','phone');
   var error_array = new Array('First Name','Last Name','an Address','a city','a valid postcode','a phone number');

   if (!(validEmail(frm.elements['email'].value))) { 
      alert('Please enter a valid email address in the form of email@host.tag.');
      frm.elements['email'].focus();
      return false;
   }

   if (frm.elements['password'].value.length == 0) {
      alert('Please enter a password 6-10 characters long using only letters or numbers.');
      frm.elements['password'].focus();
      return false;
   }

   if (frm.elements['password2'].value.length == 0) {
      alert('Please enter a Re-Password.');
      frm.elements['password2'].focus();
      return false;
   }

   if (frm.elements['password'].value != frm.elements['password2'].value) {
      alert('Your password confirmation does not match the original password');
      frm.elements['password2'].focus();
      return false;
   }

   for (var i=0; i<input_array.length; i++) {
      var source = input_array[i];          
      if (frm.elements[source].value.length == 0) {
         alert('Please enter '+error_array[i]);
         frm.elements[source].focus();
         return false;
      }
   }
  
   return true;
}

function validEmail(email) {
  invalidChars = " /:,;";
  if (email == "") {
    return false;
  }
  for (i=0; i<invalidChars.length;i++) {
    badChar = invalidChars.charAt(i);
    if (email.indexOf(badChar,0) > -1) {
      return false;
    }
  }
  atPos = email.indexOf("@",1);
  if (atPos == -1) {
    return false;
  }
  if (email.indexOf("@",atPos+1) > -1) {
    return false;
  }
  periodPos = email.indexOf(".",atPos);
  if (periodPos == -1) {
    return false;
  }
  if (periodPos+3 > email.length) {
    return false;
  }
  return true;
}

function validSearch(frm) {
   var input_array = new Array('makes_list','models_list','minprice','maxprice','other_model','counties_list');
   flag = 0;
   for (var i=0; i<input_array.length; i++) {
      var source = input_array[i];       
      //if (document.forms["quick_search"].elements[source].value.length > 0) {
         flag = 1;
      //}
   }	 
   if (flag == 0){
      alert('Please, choose criterion for search.');
      return false;
   }
   return true;
}

function validAdvSearch() {
   var input_array = new Array('makes_list','models_list','minprice','maxprice','counties_list');
   flag = 0;
   for (var i=0; i<input_array.length; i++) {
      var source = input_array[i];  
      if (document.adv_search.elements[source].value.length > 0) {
         flag = 1;
      }
   }
   for (var i=0; i < document.adv_search.elements.length; i++){
      if (document.adv_search.elements[i].type == 'checkbox' & document.adv_search.elements[i].checked == true 
          & document.adv_search.elements[i].value.length >0) {
         flag = 1;
         
      }
      if (document.adv_search.elements[i].name == 'transmission' & document.adv_search.elements[i].checked== true && document.adv_search.elements[i].value>0){
         flag = 1;  
      }   
   }    
   if (flag == 0){
      alert('Please, choose criterion for search.');
      return false;
   }
   return true;
}


function viewform() {
 var w = 520;
 var h = 300;
 var scroll = 'no';
 var settings;
 var LeftPosition;
 var TopPosition;
 var win;

 LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
 TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
 
 settings = 'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll;
 settings = settings + ',resizable=no,maximize=no,status=no,toolbar=no,directories=no,menubar=no,location=no';

 win = window.open("compare.php", 'pre', settings);
 win.name="Advert Comparision Table";
 win.focus();
}

function checkDelete(form) {
 if ( confirm("Are you sure you want to delete this advert?") ) {
   return true;
 } else {
   return false;
 }
}
function onAddAdvert(form) {
  var error;

  if (!form.get_vin.value) {
     alert('Advert No. must be specified');
     form.get_vin.focus();
     return false;
  }

  if (!form.get_year.value) {
     alert('Year must be specified');
     form.get_year.focus();
     return false;
  }

  if (!form.get_mileage.value) {
     alert('Mileage must be specified');
     form.get_mileage.focus();
     return false;
  }

  if (!form.get_engine.value) {
     alert('Engine size must be specified');
     form.get_engine.focus();
     return false;
  }

  if (!form.get_price.value) {
     alert('Price must be specified');
     form.get_price.focus();
     return false;
  }

  regex = new RegExp("^\\d+$");
  if (!regex.test(form.get_year.value)) {
     alert('Invalid Year format. Example: 2000');
     form.get_year.focus();
     return false;
  }

  regex = new RegExp("^((\\d+(\\.\\d*)?)|((\\d*\\.)?\\d+))$");
  if (!regex.test(form.get_mileage.value)) {
     alert('Invalid Mileage format. Example: 1000');
     form.get_mileage.focus();
     return false;
  }

  if (!regex.test(form.get_engine.value)) {
     alert('Invalid Engine Size format. Example 2500');
     form.get_engine.focus();
     return false;
  }

  if (!regex.test(form.get_price.value)) {
     alert('Invalid Price format. Example 10500');
     form.get_price.focus();
     return false;
  }
  return true;
}

function SelectAll() {
   var s=document.adv_search.elements["get_ref_county[]"];
   for (i=0; i<s.options.length; i++) {
      s.options[i].selected=true;
   }
}
function UnSelectAll() {
   var s=document.adv_search.elements["get_ref_county[]"];
   for (i=0; i<s.options.length; i++) {
      s.options[i].selected=false;
   }
}
function InvertSelection() {
   var s=document.adv_search.elements["get_ref_county[]"];
   for (i=0; i<s.options.length; i++) {
      s.options[i].selected=!s.options[i].selected;
   }
}
function setCookie(name, value, expires, path, domain, secure) {
   var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
         ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            ((secure) ? "; secure" : "");
   if ((name + "=" + escape(value)).length <= 4000)
      document.cookie = curCookie;
}

function getCookie(name) {
   var prefix = name + "=";
   var cookieStartIndex = document.cookie.indexOf(prefix);
   if (cookieStartIndex == -1) return null;
   var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex + prefix.length);
   if (cookieEndIndex == -1) cookieEndIndex = document.cookie.length;
   return unescape(document.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex));
}

function OnSelectCounty(name) {
   var strSelectedCounty=new String(getCookie("SelectedCounty"));
   if(strSelectedCounty.length>0){
      arrSelectedCounty=strSelectedCounty.split(",");
      strSet=new String("");
      var i=0;
      for(i=0; i<arrSelectedCounty.length; i++)
         if(arrSelectedCounty[i]!=parseInt(document.ThisForm.elements[name].value) && arrSelectedCounty!="null")
            strSet=strSet.concat(arrSelectedCounty[i], ",");
      if (document.ThisForm.elements[name].checked)
         strSet=strSet.concat(document.ThisForm.elements[name].value);
      else strSet=strSet.substr(0, strSet.length-1); 
   }
   else strSet=document.ThisForm.elements[name].value;
   setCookie("SelectedCounty", strSet);
}

function OnPageLoad() {
   var i=0;
   var j=0;
   var strSelectedCounty=new String(getCookie("SelectedCounty"));
   var arrSelectedCounty=strSelectedCounty.split(",");
   for(i=0; i<document.ThisForm.elements.length; i++)
      for(j=0; j<arrSelectedCounty.length; j++)
         if(document.ThisForm.elements[i].type=="checkbox" && parseInt(document.ThisForm.elements[i].value)==arrSelectedCounty[j]) {
            document.ThisForm.elements[i].checked=true;
         }
}
