var popWin;
var submitted = false;      // to avoid double-click

/** function used by viewlet launcher */
function popup_window( url, id, width, height )
{
 //extract the url parameters if any, and pass them to the called html
 var tempvar=document.location.toString(); // fetch the URL string
 var passedparams = tempvar.lastIndexOf("?");
 if(passedparams > -1)
    url += tempvar.substring(passedparams);
  popup = window.open( url, id, 'toolbar=no,scrollbars=no,location=no,statusbar=no,menubar=no,resizable=no,width=' + width + ',height=' + height + '' );
  popup.focus();
}

function popPublish(locale,product) {
  window.open('/'+locale+'/publish.html?product='+encodeURI(product),'product_comment',"toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, copyhistory=yes, width=526, height=640");
//'scrollbar=yes, status=no, menubar=no, resize=yes, width=508');
//alert('haha');
}

function popSize(locale) {
  window.open('/'+locale+'/size_spec.html','size_spec','scrollbar=false,status=false,menubar=false,resize=false,width=400,height=540');
}

function calcPrice(price, ratedis, rate) {
  cents = Math.round(price * rate * 100) % 100;
  if (cents<10) cents = '0' + cents;
  ratedis.innerHTML = Math.floor(price * rate) + '.' + cents;
}

function showPrice(cur, price, ratedis) {
  var rateUS = 0.1282;
  var rateCA = 0.1955;
  var rateAU = 0.2171;
  var rateEU = 0.1196;
  var rateSG = 0.2252;
  var rateJP = 15.4516;
  var rateNT = 4.471;
  var rateRM = 0.4873;
  var rateRB = 1.06;
  if (ratedis!=null) {
    if (cur=='US$') {
      rate = rateUS;
      cents = Math.round(price * rate * 100) % 100;
      if (cents<10) cents = '0' + cents;
      ratedis.innerHTML = Math.floor(price * rate) + '.' + cents;
    } else if (cur=='CAD$') {
      rate = rateCA;
      cents = Math.round(price * rate * 10) % 10;
      cents = cents + '0';
      ratedis.innerHTML = Math.floor(price * rate) + '.' + cents;
    } else if (cur=='AUD$') {
      rate = rateAU;
      cents = Math.round(price * rate * 10) % 10;
      cents = cents + '0';
      ratedis.innerHTML = Math.floor(price * rate) + '.' + cents;
    } else if (cur=='EUR') {
      rate = rateEU;
      cents = Math.round(price * rate * 100) % 100;
      if (cents<10) cents = '0' + cents;
      ratedis.innerHTML = Math.floor(price * rate) + '.' + cents;
    } else if (cur=='S$') {
      rate = rateSG;
      cents = Math.round(price * rate * 10) % 10;
      cents = cents + '0';
      ratedis.innerHTML = Math.floor(price * rate) + '.' + cents;
    } else if (cur=='JPY') {
      rate = rateJP;
      ratedis.innerHTML = Math.floor(price * rate);
    } else if (cur=='NT$') {
      rate = rateNT;
      cents = '00';
      ratedis.innerHTML = Math.floor(price * rate) + '.' + cents;
    } else if (cur=='RM') {
      rate = rateRM;
      cents = Math.round(price * rate * 100) % 100;
      if (cents<10) cents = '0' + cents;
      ratedis.innerHTML = Math.floor(price * rate) + '.' + cents;
    } else if (cur=='RMB') {
      rate = rateRB;
      cents = Math.round(price * rate * 10) % 10;
      cents = cents + '0';
      ratedis.innerHTML = Math.floor(price * rate) + '.' + cents;
    } else {
      ratedis.innerHTML = '';
      return;
    }
  }
}

function imgWidth(margin) {
  var w = 772;
  if (document.layers) {
    w = window.innerWidth;
  } else if (document.all) {
    w = document.body.clientWidth;
  }
  if (w<772) w = 772;
  return w - margin;
}

function validatePoll(errMsg) {
  var valid = false;
  if (document.pollAction.n_2_3_poll_option_id) {
    with (document.pollAction) {
      for (var i=0; i<n_2_3_poll_option_id.length; i++) {
        if (n_2_3_poll_option_id[i].checked) {
          valid = true;
          break;
        }
      }
    }
  }
  if (document.pollAction.poll_option_id) {
    with (document.pollAction) {
      for (var i=0; i<poll_option_id.length; i++) {
        if (poll_option_id[i].checked) {
          valid = true;
          break;
        }
      }
    }
  }
  if (!valid) alert(errMsg);
  return valid;
}

function validateSearch(inputObj, errMsg) {
  var valid = false;
  with (inputObj) {
    if (value.length>2) valid = true;
  }
  if (!valid) {
    inputObj.focus();
    alert(errMsg);
  }
  return valid;
}

function votePoll() {
  try {
    with (document.pollAction) {
      var url = action;
      url = url + '?act_type=' + act_type.value;
      url = url + '&poll_id=' + poll_id.value;
      for (var i=0; i<poll_option_id.length; i++) {
        if (poll_option_id[i].checked) {
          url = url + '&poll_option_id=' + poll_option_id[i].value;
          break;
        }
      }
    }
    var features = 'width=280,height=440,toolbar=no,location=no,status=no,menubar=no,resizable=no';
    popWin = window.open(url, 'Poll', features);
    popWin.focus();
    document.pollAction.reset();
  } catch (exception) {}
}

function tipsImage(img, prefix, suffix) {
  var rand = Math.random();
  rand = Math.ceil(rand * 9.0);
  var id = '0'+rand;
  id = id.substring(id.length-2, id.length);
  img.src = prefix + id + suffix;
}

function addToFavorites(url, desc) {
  if (window.external) {
    external.AddFavorite(url, desc);
  }
}

function validateCheckOut(checkoutform, err1, err2, err3) {
  if (submitted) return false;
  with (checkoutform) {
    // check card number
    if (payment_ref1.value.indexOf('-')!=-1) {
      alert(err1);
      payment_ref1.focus();
      return false;
    }
    // check card number
    if (payment_ref1.value.length<16 || !luhnCheck(payment_ref1.value)) {
      alert(err2);
      payment_ref1.focus();
      return false;
    }
    // check month
    if (payment_ref2.selectedIndex==0) {
      alert(err3);
      payment_ref2.focus();
      return false;
    }
    // check year
    if (payment_ref3.selectedIndex==0) {
      alert(err3);
      payment_ref3.focus();
      return false;
    }
  }
  return true;
}

function luhnCheck(cardnum) {
  if (! isNum(cardnum)) {
    return false;
  }
  var no_digit = cardnum.length;
  var oddoeven = no_digit & 1;
  var sum = 0;
  for (var count = 0; count < no_digit; count++) {
    var digit = parseInt(cardnum.charAt(count));
    if (!((count & 1) ^ oddoeven)) {
      digit *= 2;
      if (digit > 9) digit -= 9;
    }
    sum += digit;
  }
  if (sum % 10 == 0) return true;
  else return false;
}

function isNum(argvalue) {
  argvalue = argvalue.toString();
  if (argvalue.length == 0) return false;
  for (var n = 0; n < argvalue.length; n++)
    if (argvalue.substring(n, n+1) < "0" || argvalue.substring(n, n+1) > "9")
    return false;

  return true;
}

function popWait(url) {
  var x = screen.availWidth/2-170;
  var y = screen.availHeight/2-160;

  var features = 'top='+y+',left='+x+',screenX='+x+',screenY='+y+',width=340,height=160,toolbar=no,location=no,status=no,menubar=no,resizable=no';
  popWin = window.open(url, 'Wait', features);
  popWin.focus();
}

function closeWait() {
  if (popWin!=null) {
    popWin.close();
  }
}


var preloadFlag = false;

function newImage(arg) {
  if (document.images) {
    rslt = new Image();
    rslt.src = arg;
    return rslt;
  }
}

function changeImages() {
  if (document.images && (preloadFlag == true)) {
    for (var i=0; i<changeImages.arguments.length; i+=2) {
      document[changeImages.arguments[i]].src = changeImages.arguments[i+1];
    }
  }
}

function preloadImages() {
  if (document.images) {
    a = newImage("/img/zh_HK/amCart-over.gif");
    a = newImage("/img/zh_HK/amHelp-over.gif");
    a = newImage("/img/zh_HK/amLogout-over.gif");
    a = newImage("/img/zh_HK/amMyAc-over.gif");
    a = newImage("/img/zh_HK/cmdBack-over.gif");
    a = newImage("/img/zh_HK/cmdBuy-over.gif");
    a = newImage("/img/zh_HK/cmdCheckOut-over.gif");
    a = newImage("/img/zh_HK/cmdContinue-over.gif");
    a = newImage("/img/zh_HK/cmdContinueShopping-over.gif");
    a = newImage("/img/zh_HK/cmdDismiss-over.gif");
    a = newImage("/img/zh_HK/cmdFinish-over.gif");
    a = newImage("/img/zh_HK/cmdLogin-over.gif");
    a = newImage("/img/zh_HK/cmdPlaceMyOrder-over.gif");
    a = newImage("/img/zh_HK/cmdRegisterAsMember-over.gif");
    a = newImage("/img/zh_HK/cmdSearch-over.gif");
    a = newImage("/img/zh_HK/cmdSend-over.gif");
    a = newImage("/img/zh_HK/cmdUpdate-over.gif");
    a = newImage("/img/zh_HK/cmdUpdateSubtotal-over.gif");
    a = newImage("/img/zh_HK/cmdVote-over.gif");
    a = newImage("/img/zh_HK/mmContactUs-over.gif");
    a = newImage("/img/zh_HK/mmOrderTracking-over.gif");
    a = newImage("/img/zh_HK/mmProducts-over.gif");
    a = newImage("/img/zh_HK/mmSpecialOffer-over.gif");
    a = newImage("/img/en_US-over.gif");
    a = newImage("/img/zh_HK-over.gif");
    a = newImage("/img/en_US/amCart-over.gif");
    a = newImage("/img/en_US/amHelp-over.gif");
    a = newImage("/img/en_US/amLogout-over.gif");
    a = newImage("/img/en_US/amMyAc-over.gif");
    a = newImage("/img/en_US/cmdBack-over.gif");
    a = newImage("/img/en_US/cmdBuy-over.gif");
    a = newImage("/img/en_US/cmdCheckOut-over.gif");
    a = newImage("/img/en_US/cmdContinue-over.gif");
    a = newImage("/img/en_US/cmdContinueShopping-over.gif");
    a = newImage("/img/en_US/cmdDismiss-over.gif");
    a = newImage("/img/en_US/cmdFinish-over.gif");
    a = newImage("/img/en_US/cmdLogin-over.gif");
    a = newImage("/img/en_US/cmdPlaceMyOrder-over.gif");
    a = newImage("/img/en_US/cmdRegisterAsMember-over.gif");
    a = newImage("/img/en_US/cmdSearch-over.gif");
    a = newImage("/img/en_US/cmdSend-over.gif");
    a = newImage("/img/en_US/cmdUpdate-over.gif");
    a = newImage("/img/en_US/cmdUpdateSubtotal-over.gif");
    a = newImage("/img/en_US/cmdVote-over.gif");
    a = newImage("/img/en_US/mmContactUs-over.gif");
    a = newImage("/img/en_US/mmOrderTracking-over.gif");
    a = newImage("/img/en_US/mmProducts-over.gif");
    a = newImage("/img/en_US/mmSpecialOffer-over.gif");
    preloadFlag = true;
  }
}

function getCookie(c_name) {
  if (document.cookie.length>0) {
    c_start=document.cookie.indexOf(c_name + "=");
    if (c_start!=-1) {
      c_start=c_start + c_name.length+1;
      c_end=document.cookie.indexOf(";",c_start);
      if (c_end==-1) c_end=document.cookie.length;
      return unescape(document.cookie.substring(c_start,c_end));
    }
  }
  return "";
}

function setCookie(c_name,value,expiredays) {
  var exdate=new Date();
  exdate.setDate(exdate.getDate()+expiredays);
  document.cookie=c_name+ "=" +escape(value)+ ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function loadChatImg() {
  // for future
}

