/*
Strip whitespace from the beginning and end of a string
Input : a string
*/
function querySt(ji) {
	hu = window.location.search.substring(1);
	gy = hu.split("&");
	for (i=0;i<gy.length;i++) {
	ft = gy[i].split("=");
		if (ft[0] == ji) {
		return ft[1];
		}
	}
}

function trim(str)
{
	return str.replace(/^\s+|\s+$/g,'');
}

/*
Make sure that textBox only contain number
*/
function checkNumber(textBox)
{
	while (textBox.value.length > 0 && isNaN(textBox.value)) {
		textBox.value = textBox.value.substring(0, textBox.value.length - 1)
	}
	
	textBox.value = trim(textBox.value);
/*	if (textBox.value.length == 0) {
		textBox.value = 0;		
	} else {
		textBox.value = parseInt(textBox.value);
	}*/
}

/*
	Check if a form element is empty.
	If it is display an alert box and focus
	on the element
*/
function isEmpty(formElement, message) {
	formElement.value = trim(formElement.value);
	
	_isEmpty = false;
	if (formElement.value == '') {
		_isEmpty = true;
		alert(message);
		formElement.focus();
	}
	
	return _isEmpty;
}

/*
	Set one value in combo box as the selected value
*/
function setSelect(listElement, listValue)
{
	for (i=0; i < listElement.options.length; i++) {
		if (listElement.options[i].value == listValue)	{
			listElement.selectedIndex = i;
		}
	}	
}

function getObject(id) { 
	return document.getElementById(id);
  return (ns4) ? document.layers[id] : (ie4) ? document.all[id] : (ie5||ns5) ? document.getElementById(id) : null; 
}

function addToCart(ID)
{	
		//alert(ID);		
		var error='';
		var pdcr='N/A';
		var pdsz='N/A'
		var qty=getObject('txtQuantity'+ID).value;		
		if(getObject('cboColor_'+ID)){
			pdcr=getObject('cboColor_'+ID).value;
			if(pdcr==''){
				error +='Select product color. \n';
			}
		}
		if(getObject('cboSize_'+ID)){
			pdsz=getObject('cboSize_'+ID).value;
			if(pdsz==''){
				error +='Select product size. \n';
			}
		}
		if(qty=='' || qty==0){
				qty=1;
			
		}
				
		
		if(error!=''){
			 msg='Please select the following field(s): \n'+error;
			alert(msg);
			return false;
		}
		else{
			
			window.location.href='?cart&action=add&p='+ID+'&qty='+qty+'&size='+pdsz+'&color='+pdcr;
		}
		
}

function deleteFromCart(Id)
{
	if (confirm('Do you really want to delete this product?')) {
		window.location.href = '?cart&action=delete&cid=' + Id;
	}
}

function selectRow(rowId)
{
	_row = getObject(rowId);
	check= getObject('optId'+rowId);
	if(check.checked==true){
		_row.style.background='#D4D4D4';
	}
	else{
		_row.style.background='';
	}	
	
}

function dosearch()
{
	//with (window.document.frmSearch) {
	var query=window.document.frmSearch.txtSearch.value;
	
	if(query=="") {
		alert('Please enter keyword');
		window.document.frmSearch.txtSearch.focus();
		return false ;
	}
	else{
		window.location.href='?search&q='+query;		
	}
	return false ;
	
}

function mailList()
{
	var query=window.document.frmMailList.email.value;
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	if(query=="" || query=="Enter email address") {
		alert('Please enter your email address.');
		window.document.frmMailList.email.focus();
		return false ;
	}else if(reg.test(query) == false){
		alert('Please enter valid email address.');
		return false ;
	}
	else{		
		return true;
	}
	return false ;
	
}

function valid() {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   var address = document.frmCreatAccount.txtUserEmail.value;
   if(reg.test(address) == false) {
	   return false;
   }
   return true;
}

