/*
 * 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 != ""){
    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) {
  document.formObj.disabled = true;
}

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();
  }
}