var company = "My Video Store";
var xmlHttp;
var stateChanged_flag;
var newFunction;
var newTimerTimout;

function createTimer(func, timeout, vars) {
  var newFunc = "";

  var newTimerTimout = timeout;
  
  var innerFunc = "if(newTimerTimeout > 0) { newFunc = " + func + "("
  for(var i=0;i<vars.length;i++) {
    thisVar  = vars[i];
    innerFunc += ((i > 0) ? ", " : "") + "'" + thisVar + "'";
  }
  innerFunc += "); newTimerTimeout = newTimerTimeout - 1; setTimeout(\"newFunction()\",1000); } return true;";

  alert(innerFunc);
  //var newFunction = new function newFunction(innerFunc);
  setTimeout("newFunction()", 1000);
}

function GetXmlHttpObject() {
  var objXMLHttp = null;
  if (window.XMLHttpRequest) {
    objXMLHttp = new XMLHttpRequest();
  }
  else if (window.ActiveXObject) {
    objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
  }
  return objXMLHttp;
}

function validateEmail(email){
  if(email.length <= 0){
    return true;
  }
  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;
}

/*** CHECK MOBILE ***/
function validateMobile(input){
  var newPhone = input.replace(/ /g, "");
  //var re = new RegExp('^[0][4][0-9]{8}$');
  //var re2 = new RegExp('^[0][6][0-9]{11}$');

  //var re = /^(0)(2|4|3|7|8)(\ |-){0,1}[0-9]{2}(\ |-){0,1}[0-9]{2}(\ |-){0,1}[0-9]{1}(\ |-){0,1}[0-9]{3}$/;
  //var re2 = /^(0)(3|4|6|7|9)(\ |-){0,1}[0-9]{2}(\ |-){0,1}[0-9]{2}(\ |-){0,1}[0-9]{1}(\ |-){0,1}[0-9]{2} (\ |-){0,1}[0-9]{2} (\ |-){0,1}[0-9]{2}$/;

  //if(newPhone.length==0){
   // return true;
  //}
  //else if(newPhone.match(re) || newPhone.match(re2)){
    //return true;
  //}
  //return false;
  if(newPhone.length==10 || newPhone.length==11)
      return true;
  else
      return false;
}

function validateUsername(input) {
  var re = new RegExp('^[0-9A-Za-z\.\-\_]+$');
  if(input.match(re)){
    return true;
  }
  return false;
}

function validatePW(pw) {
  if(pw.match(/[a-zA-Z]/) && pw.match(/[0-9]/) && pw.length > 7)
    return true;
  else
    return false;
}

function trim(stringToTrim) {
  return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function isInt(number) {
  return ((number % 1 == 0) ? true : false);
}

function validatePhone(input){
  var newPhone = input.replace(/ /g, "");
  //var regexp = /^\({0,1}((0|\+61)(2|4|3|7|8)){0,1}\){0,1}(\ |-){0,1}[0-9]{2}(\ |-){0,1}[0-9]{2}(\ |-){0,1}[0-9]{1}(\ |-){0,1}[0-9]{3}$/;
  var regexp = /^(0)(2|4|3|7|8)(\ |-){0,1}[0-9]{2}(\ |-){0,1}[0-9]{2}(\ |-){0,1}[0-9]{1}(\ |-){0,1}[0-9]{3}$/;
  var regexp2 = /^(0)(3|4|6|7|9)(\ |-){0,1}[0-9]{2}(\ |-){0,1}[0-9]{2}(\ |-){0,1}[0-9]{1}(\ |-){0,1}[0-9]{2}$/;
  if(newPhone.match(regexp) || newPhone.match(regexp2)){
    return true;
  }
  return false;
}

function validateCVV(input) {
  if(isInt(input)&&input>=1&&input<=999) {
    return true;
  }
  else {
    return false;
  }
}

function checkABN(abn) {
/*
  if(abn.length == 11) {
    weights = Array(10,1,3,5,7,9,11,13,15,17,19);
    calc = 0;
    for(i=0;i<11;i++) {
      calc = calc + (((i==0) ? -1 : 0) + (abn.charAt(i) * 1)) * weights[i];
    }
    calc = calc % 89;

    return (calc == 0) ? true : false;
  }
  else {
    return false;
  }
    */
   return true;
}

function validatePostcode(postcode) {
  var regexp = /^[0-9]{4}$/;
  if(postcode.match(regexp)){
    return true;
  }
  return false;
}

/** returns true if the string only contains characters A-Z or a-z **/
function isAlpha(str){
  var re = /[^a-zA-Z]/g
  if (re.test(str)) return false;
  return true;
}

/** returns true if the string only contains characters 0-9 **/
function isNumeric(str){
  var re = /[\D]/g
  if (re.test(str)) return false;
  return true;
}

/** returns true if the string only contains characters A-Z, a-z or 0-9 **/
function isAlphaNumeric(str){
  var re = /[^a-zA-Z0-9]/g
  if (re.test(str)) return false;
  return true;
}
