var NN = false;
var IE = false;
var deletecode = 127;
var deletekey = 46;
var backspacecode = 8;
var dash = 45;
var dot = 46;
var underscore = 95;
var colon = 58;
var space = 32;
if (navigator.appName == 'Netscape')
  NN = true;
else
  IE = true;
var version = parseInt(navigator.appVersion);

function getWindow(url) {
  newWindow = window.open("http://" + url);
}

function getKeyCode(evt) {
  if (NN)
    return evt.which;
  else
    return window.event.keyCode;
}

function getTargetObject(evt) {
  if (NN)
    return evt.target;
  else
    return window.event.srcElement;
}

function isValidCode(code, type) {
  if (type == 'c' || type == 'm' || type == 'e')
    return true;
  if (code == deletecode || code == backspacecode || code == 0 || code == deletekey)
    return true;
  if (type == 'n' && code <= 57 && code >= 48)
    return true;
  if (type == 'a' && (code <= 90 && code >= 48 || code == space || code <= 122 && code >= 97))     return true;
  if (type == 't' && (code >= 40 && code <= 41 || code == space || isValidCode(code, 'n')))
    return true;
  if (type == 'i' && (isValidCode(code, 'a') || isValidCode(code, 't')))
    return true;
  if (type == 'p' && (isValidCode(code, 'n') || code == 46))
    return true;
  if (type == 'd' && (isValidCode(code, 'n') || code == 45))
    return true;
  if (type == 'k' && (isValidCode(code, 'n') || code >= 65 && code <= 90 || code <= 122 && code >= 97 || code == dash))
    return true;
  if (type == 'f' && (isValidCode(code, 'k') || code == underscore || code == dot || code == colon))
    return true;
  if (type == 'z' && (code == colon || isValidCode(code, 'n')))
    return true;
  return false;
}

function checkcode(evt, type) {
  var code = getKeyCode(evt);
  if (!isValidCode(code, type)) {
    if (NN)
      return false;
    else {
      window.event.returnValue=false;
      return false;
    }
  }
  return true;
}

function isNumber(data) {
  var isnumber = /\D/;
  if (window.RegExp)
    return !isnumber.test(data);
}

function isEmpty(data) {
  var nonempty = new RegExp('.+');
  if (window.RegExp)
    return !nonempty.test(data);
}

function isEmail(data) {
  var isemail = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,8}|[0-9]{1,3})(\]?)$/;
  var notemail = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/;
  if (window.RegExp) {
    if (!notemail.test(data) && isemail.test(data))
      return true;
    else
      return false;
  }
}

function getEnglishValue(obj) {
  var result = '';
  for (i = 0; i < obj.value.length; i++) {
    if (obj.value.charCodeAt(i) < 127)
      result += obj.value.substr(i, 1);
  }
  obj.value = result;
}

function changelang(url, lang) {
  form = document.forms["content"];
  form.action = url;
  form.lang.value = lang;
  form.submit();
}

function changelink(link) {
  location.href = "/index/content/" + link;
}
function isDomainBody(data) {
  var isdomainbody = /^[A-Za-z0-9]{1}[A-Za-z0-9\-]{1,63}[A-Za-z0-9]{1}$/;
  if (window.RegExp)
    return isdomainbody.test(data);
}
function isDomainName(data) {
  var isdomainname = /^[A-Za-z0-9]{1}[A-Za-z0-9\-]{0,63}[A-Za-z0-9]{1}[.]{1}[A-Za-z0-9]{1}[A-Za-z0-9\-]{1,63}[A-Za-z0-9]{1}[.]{1}[A-Za-z0-9]{1}[A-Za-z0-9\-]{0,63}[A-Za-z0-9]{1}$/;
  var iscountrydomainname = /^[A-Za-z0-9]{1}[A-Za-z0-9\-]{0,63}[A-Za-z0-9]{1}[.]{1}[A-Za-z0-9]{1}[A-Za-z0-9\-]{1,63}[A-Za-z0-9]{1}[.]{1}[A-Za-z0-9]{1}[A-Za-z0-9\-]{0,63}[A-Za-z0-9]{1}[.]{1}[A-Za-z0-9]{2,63}$/;
  if (window.RegExp) {
    if (isdomainname.test(data))
      return true;
    return iscountrydomainname.test(data);
  }
}
function getNewChildWindow(url, name, width, height) {
  newWindow = window.open(url, name, 'dependent,width=' + width + ',height=' + height + ',scrollbars=yes,toolbar=no,location=no,menubar=no, statusbar=no,left=50,screenX=50,top=50,screenY=50');
}
function isDate(data) {
  var isdate = new RegExp('[0-9]{1,2}[-]{1}[0-9]{1,2}[-]{1}[0-9]{4}');
  if (!isdate.test(data))
    return false;
  var entries = data.split("-");
  var month = parseInt(entries[1], 10) - 1;
  var date = new Date(parseInt(entries[2], 10), month, parseInt(entries[0], 10));
//  var date = new Date();
//  date.setFullYear(parseInt(entries[2], 10));
//  date.setMonth(month);
//  date.setDate(parseInt(entries[0], 10));

  if (month != date.getMonth())
    return false;
  return date.getTime();
}
function isID(data) {
  var isid = new RegExp('[A-Z,a-z]{1}[0-9]{6}[(]{1}[0-9]{1}[)]{1}');
  if (window.RegExp)
    return isid.test(data);
}
function isIP(data) {
  var isip = new RegExp('[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}');
  if (window.RegExp) {
    if (!isip.test(data))
      return false;
    var entries = data.split(".");
    var i = 0;
    for (i = 0; i < entries.length; i++)
      if (parseInt(entries[i], 10) > 255)
        return false; 
    return true;
  }
}

function order(thisform) {
  if (getAmountById("totalcharge") == 0) {
    alert(noorder);
    return;
  }
  thisform.action = '/index/content/order';
  thisform.submit();
}
function changeid(obj) {
  thisform = obj.form;
  thisform.inputref.value = obj.options[obj.selectedIndex].value;
  thisform.submit();
}
function changeover(obj) {
  obj.src = obj.src.substring(0, obj.src.lastIndexOf(".gif")) + "over.gif";
}
function changeout(obj) {
  obj.src = obj.src.substring(0, obj.src.lastIndexOf("over.gif")) + ".gif";
}
function getAmountById(id) {
  thisamount = document.getElementById(id).innerHTML;
  if (thisamount.charAt(0) == "-") {
    factor = -1;
    thisamount = thisamount.substring(1);
  }
  else
    factor = 1;
  if (thisamount.charAt(1) == ",")
    thisamount = thisamount.charAt(0) + thisamount.substring(2);
  amount = 0;
  if (thisamount != '')
    amount = parseFloat(thisamount);
  return amount * factor;
}
function formatAmount(charge) {
  charge = Math.round(charge * 100)/100;
  if (charge < 0)
    abscharge = charge * -1;
  else
    abscharge = charge;
  if (abscharge > 999)
    chargetext = abscharge.toString().charAt(0) + "," + abscharge.toString().substring(1);
  else
    chargetext = abscharge.toString();
  if (charge < 0)
    chargetext = "-" + chargetext;
  return chargetext;
}
function isTime(data) {
  var istime = new RegExp('[0-9]{1,2}[:]{1}[0-9]{1,2}[:]{1}[0-9]{1,2}');
  if (!istime.test(data))
    return false;
  var entries = data.split(":");
  var hh = parseInt(entries[0], 10);
  var mm = parseInt(entries[1], 10);
  var ss = parseInt(entries[2], 10);
  if (hh >= 24 || mm > 60 || ss > 60)
    return false;
  return true;
}
function setImage(obj) {
  image = obj.getAttribute('src');
  var pos = image.indexOf("over.gif");
  if (pos >= 0)
    image = image.substr(0, pos) + ".gif";
  else
    image = image.substr(0, image.indexOf(".gif")) + "over.gif";
  obj.setAttribute('src', image);
}
