/*
 * general.js
 * General javascripts used
 *
 * @copyright Copyright 2007-2009 Tenchi Marketing Pte Ltd
 * @version $Id: general.js 2009-06-23
 */


/*
 * Create custom DOB popup calendar
 */
function cus_popup_dob_calendar(inputObject, anchorname, dateFormat, cur_date, divname) {
  var cal = new CalendarPopup(divname);
  cal.showNavigationDropdowns();
  cal.showCalendar(anchorname);
  cal.hideCalendar(0);
  cal.setYearSelectStartOffset(100);
  cal.setCssPrefix("Green");
  cal.offsetX = -100;
  cal.offsetY = 20;
  cal.select(inputObject, anchorname, dateFormat);
}


/*
 * Check if the text length of the textbox have exceeded the maximum length
 */
function check_length(textboxname,max) {
  var maxwords = max;
  var message = document.getElementById(textboxname);
  var len = message.value.length;
  if (len > maxwords) {
    alert("Message limited to " + maxwords + " words.");
  }
  document.getElementById('wordcount').innerHTML = maxwords - len;
}


/*
 * Check the extention of the textbox if the file extention is that of images
 */
function checkExt(formObj){ 
  var filename = formObj.value;
  if (filename == "") return true;
  var filelength = parseInt(filename.length) - 3;
  var fileext = filename.substring(filelength,filelength + 3);
   // Check file extenst/ion
  if (fileext.toLowerCase() != "gif" && fileext.toLowerCase() != "jpg" && fileext.toLowerCase() != "png" && fileext != ""){
    formObj.focus();
    formObj.value = "";
    alert ("You can only upload gif or jpg images.");
    return false;
  }
  else {
    return true;
  }
}

/*
 * Disable the submit button when clicked so will not cause accidental double click
 */
function submit_button_clicked(formObj) {
  msg("Please wait...");
}

function checkNum(formObj){
  var val = formObj.value;
  if (val == "") return true;
  if (isNumeric(val)) return true;
  else {
    formObj.value = "";
    alert ("You can only enter numeric values!");
    return false;
  } 
}

function isNumeric(input){
  return (input - 0) == input && input.length > 0;
}

function addToCart(id) {
  var textbox = document.getElementById(id);
  var value = textbox.value;
  if (value > 0) {
    var hidden_qty = document.getElementById('hidden_qty');
    hidden_qty.value = value;
    var hidden_id = document.getElementById('hidden_id');
    hidden_id.value = id;
    document.mainform.submit();
  }
}

function setvalue(id, val, form) {
  var formobj = document.getElementById(id);
  formobj.value = val;
  if (form != "") {
    form.submit();
  }
}

function showprint(id, type) {
  var URL = "showprint.php?id=" + id + "&type=" + type;
  window.open(URL, "newwindow", "toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=1,resizable=0,width=800,height=800");
}

function urlgo(url) {
  location.href = url;
}


/*
 * For showing and hidding the tooltip divider
 */
function showhidden(id) {
  var item = document.getElementById(id);
  item.style.visibility = 'visible';
}

function hidehidden(id) {
  var item = document.getElementById(id);
  item.style.visibility = 'hidden';
}

function getcheckboxvalue(cbox) {
  for (var i=0; i < cbox.length; i++) {
    if (cbox[i].checked) { return cbox[i].value;}
  }
  return '';
}

function buttonstyle(btn, type)
{
  if (type.toLowerCase() == "over") btn.style.backgroundColor = "#f5989d";
  else if (type.toLowerCase() == "out") btn.style.backgroundColor = "#990000";
}


function popUp(URL) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=1280,height=100,left = 0,top = 334');");
}


String.prototype.replaceAll = function(pcFrom, pcTo) {
  var i = this.indexOf(pcFrom);
  var c = this;

  while (i > -1){
    c = c.replace(pcFrom, pcTo);
    i = c.indexOf(pcFrom);
  }
  return c;
}


