


String.prototype.trim = function() {
    return this.replace(/(^\s*)|(\s*$)/g, "");
}

/**
 * returns string with all leading and trailing characters 
 * eliminated.
 */
function trim(str){
  var s = new String(str);
  //trailing spaces
  while (s.length>0 && isSpaceCharacter(""+s.charAt(s.length-1))){
    s = s.substring(0,s.length-1);
  }
  //leading spaces
  while (s.length>0 && isSpaceCharacter(""+s.charAt(0))){
    s = s.substring(1);
  }
  return s
}

var spaces = " \t\r\n"+String.fromCharCode(160);

function isSpaceCharacter(ch){
  return spaces.indexOf(ch) >-1;
}
/**
 * Returns value of selected option in listbox
 * If there is no options selected - returns null.
 * If multiple options selected - returns first.
 */

function selected(sel){
  if(!sel.options){
     return null;
  }
  for(var i = 0; i< sel.options.length; i++){
     if(sel.options[i].selected){
        return sel.options[i].value;
     }
  }

  return null;
}

/**
 * Returns value of checked radiobutton in radiobuttons group
 * If there is no radiobuttons checked - returns null.
 */

function selectedRadio(sel){
  if(!sel){
     return null;
  }
  if(!sel.length){
     if(!sel.checked){
        return null;
     }
     return true;
  }
  for(var i = 0; i< sel.length; i++){
     if(sel[i].checked){
        return sel[i].value;
     }
  }
  return null;
}

/**
 * Returns true if one of options in radiogroup is checked;
 */
function isChecked(sel){
  return selectedRadio(sel) != null;
}
/**
 * Validates that input's value is correct email address
 */
function validateEmail(elem) {
    var str = "";
    if (elem.value) {
        str = new String(elem.value);
    } else {
        str = new String(elem);
    }

    if (window.RegExp) {
        var reg1str = "(@.*@)|(\\.\\.)|(@\\.)|(\\.@)|(^\\.)";
        var reg2str = "^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,4}|[0-9]{1,4})(\\]?)$";
        var reg3str = "\"([^\"]+)\"\\s+<{1}([A-z]{1}[A-z0-9\\x2d\\.]*[A-z0-9]{1}@[A-z0-9\\x2d\\.]*[A-z-9]{1}\\.[A-z]{2,5})>{1}";
        var reg1 = new RegExp(reg1str);
        var reg2 = new RegExp(reg2str);
        var reg3 = new RegExp(reg3str);
        if (!reg1.test(str) && (reg2.test(str) || reg3.test(str))) return true;
        return false;
    } else {
        if (str.indexOf("@") >= 0) return true;
        return false;
    }
}

function isEmpty(elem){
  return trim(elem.value).length==0;
}

function isNumber(d,isinteger,ispositive){
 var data=new String(d);
 var numStr=".-0123456789";
 var th;  var counter=0;  var ret=false;
 for(var i=0; i < data.length; i++){
  th = data.substring(i, i+1); if(numStr.indexOf(th) != -1) counter++;
 }
 if(counter == data.length){
   if(data.indexOf(".")!=data.lastIndexOf(".")) return false;
   if(data.indexOf("-")>0) return false;
   if(isinteger&&data.indexOf(".")!=-1) return false;
   if(ispositive&&data.indexOf("-")!=-1) return false;
   return true;
 }
 return(false);
}

function validateForm(f){
  if (f.login.type!="hidden") {
    if (isEmpty(f.login)) {
      alert("Please enter login name");
      f.login.focus();
      return false;
    }

    if (!validateEmail(f.login)) {
      alert("Please enter correct email address");
      f.login.focus();
      return false;
    }

    if (isEmpty(f.email)) {
      alert("Please enter email address");
      f.email.focus();
      return false;
    }

    if (!validateEmail(f.email)) {
      alert("Please enter correct email address");
      f.email.focus();
      return false;
    }

    if(f.login.value!=f.email.value){
      alert("Emails do not match");
      f.email.focus();
      return false;
    }
  }

  if (isEmpty(f.password)) {
    alert("Please enter password");
    f.password.focus();
    return false;
  }

  if(trim(f.password.value).length<4){
      alert("Password must be at least 4 characters long");
      f.password.focus();
      return false;
  }

  if (isEmpty(f.password2)) {
    alert("Please confirm password");
    f.password2.focus();
    return false;
  }

  if(f.password.value!=f.password2.value){
      alert("Passwords do not match");
      f.password2.focus();
      return false;
  }

  if (isEmpty(f.firstname)) {
    alert("Please enter first name");
    f.firstname.focus();
    return false;
  }

  if (isEmpty(f.lastname)) {
    alert("Please enter last name");
    f.lastname.focus();
    return false;
  }

  if (isEmpty(f.address)) {
    alert("Please enter address");
    f.address.focus();
    return false;
  }

  if (isEmpty(f.city)) {
    alert("Please enter city");
    f.city.focus();
    return false;
  }

  if (isEmpty(f.zip)) {
    alert("Please enter zip/postal code");
    f.zip.focus();
    return false;
  }

  if (f.Country.value=="AA") {
    alert("Please select country");
    f.Country.focus();
    return false;
  }

  if (isEmpty(f.phone)) {
    alert("Please enter phone");
    f.phone.focus();
    return false;
  }


  if(f.elements['accept'] && !f.elements['accept'].checked){
      alert("You cannot register \nif you do not accept our terms and conditions");
      f.elements['accept'].focus();
      return false;
  }

  return true;

}

function insertOptions(x){
    var oOption = document.createElement("OPTION");
    form1.Country.options.add(oOption,0);
    oOption.innerText = "All Countries";
    oOption.value = "AA";
    if(x){
        form1.Country.options[0].selected=true;
    }
}

