function undef(o)
{
  return typeof(o) == 'undefined' || o === '' || o == null
}

function def(o)
{
  return !undef(o)
}

function moveItem(srcList, destList){
	for(i = 0; i< srcList.options.length;){
		if(srcList.options[i].selected == true){
			addItemTo(destList, srcList.options[i].text, srcList.options[i].value);
			deleteItemFrom(srcList, srcList.options[i].value);
			continue;
		}
		i++;
	}
}

function moveItemTo(itemname, srcList, destList){
	for(i = 0; i< srcList.options.length;){
		if(srcList.options[i].value == itemname){
			//alert(srcList.options[i].text);			
			addItemTo(destList, srcList.options[i].text, srcList.options[i].value);
			deleteItemFrom(srcList, srcList.options[i].value);
			break;
		}
		i++;
	}
}

function addItemTo(list, itemName, itemValue){
	list.options.add(new Option(itemName, itemValue));
}

function deleteItemFrom(list, itemValue){
	for(j = 0; j< list.options.length;j++){
		if(list.options[j].value == itemValue){
			list.remove(j);
		}
	}
}

function clearItems(ctrl){
 ctrl.options.length = 0;
}

function setCheckboxChecked(name, value, checked){
	var ctls = document.getElementsByName(name);
	
	for(i = 0; i< ctls.length; i++){
		if(ctls[i].value == value){
			ctls[i].checked = checked;
		}
	}
}
function setSelect(selectctl, value){
	if(undef(selectctl))
		return;

	for(i = 0; i< selectctl.options.length;i++){
		if(selectctl.options[i].value == value){
			selectctl.selectedIndex= i;
		}
	}
}


function getSelectValue(selectctl){
	if(undef(selectctl))
		return;

	return selectctl.options[selectctl.selectedIndex].value;
}

function callOnLoad(init)
{
    if (window.addEventListener)
    {
        window.addEventListener("load", init, false);
    }
    else if (window.attachEvent)
    {
        window.attachEvent("onload", init);
    }
    else
    {
        window.onload = init;
    }
}

Object.prototype.equals = function (obj){
	if(this == obj)return true;
	if(typeof(obj)=="undefined"||obj==null||typeof(obj)!="object")return false;
	var length = 0; var length1=0;
	for(var ele in this) length++;for(var ele in obj) length1++;
	if(length!=length1) return false;
	if(obj.constructor==this.constructor){
		for(var ele in this){
			if(typeof(this[ele])=="object") {if(!this[ele].equals(obj[ele]))return false;}
			else if(typeof(this[ele])=="function"){if(!this[ele].toString().equals(obj[ele].toString())) return false;}
			else if(this[ele]!=obj[ele]) return false;
		}
		return true;
	}
	return false;
}

String.prototype.equals = function (str){
	if(this==str)return true;
	return false;
}

Function.prototype.equals = function (func){
	if(this.toString().equals(func.toString()))return true;
	return false;
}

Boolean.prototype.equals = function (bool){
	if(this==bool)return true;
	if (bool instanceof Boolean){
	    return this.toString().equals(bool.toString());
	} 
	return false;
}

Object.prototype.clone = function (){
	var newObj = new Object();
	for(elements in this){
		newObj[elements] = this[elements];
	}
	return newObj;
}

Object.prototype.cloneAll = function (){
	function clonePrototype(){}
	clonePrototype.prototype = this;
	var obj = new clonePrototype();
	for(var ele in obj){
		if(typeof(obj[ele])=="object") obj[ele] = obj[ele].cloneAll();
	}
	return obj;
}

Object.prototype.toString = function (){
	var str="";
	for(elements in this){
		str += elements + this[elements];
	}
	return str;
}


/**
 * trim
 */
String.prototype.trim = function() {
    return this.replace(/(^\s+)|(\s+$)/g, "");
}
/**
 * case insensitive equals
 */
String.prototype.iEquals = function(str) {
    return this.toLowerCase() == str.toLowerCase();
}
/**
 * compare string, returns -1, 0, 1
 */
String.prototype.compareTo = function(str) {
    if (this == str) {
        return 0;
    } else if (this < str) {
        return -1;
    } else {
        return 1;
    }
}

/**
 * compare string, returns -1, 0, 1 insensitive
 */
String.prototype.iCompareTo = function(str) {
    return this.toLowerCase().compareTo(str.toLowerCase());
}
/**
 * test if string starts with specifical string
 */
String.prototype.startsWith = function(str) {
    return this.substr(0, str.length) == str;
}
/**
 * test if string starts with specifical string insensitive
 */
String.prototype.iStartsWith = function(str) {
    return this.substr(0, str.length).iEquals(str);
}
/**
 * test if string ends with specifical string
 */
String.prototype.endsWith = function(str) {
    return this.substr(this.length - str.length) == str;
}
/**
 * test if string ends with specifical string insensitive
 */
String.prototype.iEndsWith = function(str) {
    return this.substr(this.length - str.length).iEquals(str);
}
/**
 * generate style using <span></span>
 */
String.prototype.style = function(style) {
    return "<span style=\"".concat(style, "\">", this, "</span>");
}
String.prototype.toInt = function(style) {
    return parseInt(this);
}

function checkCreditCard(ctrl){
    if(def(ctrl)){
        var creditCardNo = ctrl.value;
	    if(creditCardNo.length > 0){
	        if(creditCardNo.substring(0,1)=='3'||creditCardNo.substring(0,1)=='6'){
	            alert("We're sorry, we do not accept this form of payment.  Please enter a Visa or Mastercard to proceed.");
	            ctrl.value='';
	        }
	    }
    }
}

/***********************************************************
**
*TODO:changeCategory
*Function changeCategory()
*Author : fd.meng
**
***********************************************************/
function changeCategory(categoryid) {
	window.location = "listProducts.do?type=listProducts&categoryid="+categoryid; 		
}