/*
Strip whitespace from the beginning and end of a string
Input : a string
*/
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 selectOptCustEmail() {
	temp = document.getElementById('optExsCustEmail');
	temp.checked = 'checked';
}

function selectOptCustPhone() {
	temp = document.getElementById('optExsCustPhone');
	temp.checked = 'checked';
}

function checkCustHistory(element) {
	if (document.getElementById('optNewCust').checked == true) {
	}
	else {
		tempPhone = document.getElementById('txtExsCustPhone');
		tempEmail = document.getElementById('txtExsCustEmail');
		if (tempPhone.value == "" && tempEmail.value == "")
		{
			alert('Please enter your email address / phone number');
			return false;
		}
	}
	element.submit();
}

function validateSearchForm(form)
{
	if(form.searchkey.value == "")
	{
		return false;
	}
	else
	{
		return true;
	}
}

function checkPaymentConfirmation(step)
{
	if(step == 1)
	with (window.document.frmPaymentConfirmation1) {
		if (isEmpty(txtPaymentConfirmationOrderNumber, 'Enter Order Number')) {
			return false;
		} else {
			return true;
		}
	}
}

function checkOrderTracking(step)
{
	if(step == 1)
	with (window.document.frmOrderTracking1) {
		if (isEmpty(txtOrderTrackingOrderNumber, 'Enter Order Number')) {
			return false;
		} else {
			return true;
		}
	}
}

/**
 * DHTML email validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */

function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Invalid E-mail ID")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid E-mail ID")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

 		 return true					
	}

function ValidateForm(){
	var emailID=document.frmSample.txtEmail
	
	if ((emailID.value==null)||(emailID.value=="")){
		alert("Please Enter your Email ID")
		emailID.focus()
		return false
	}
	if (echeck(emailID.value)==false){
		emailID.value=""
		emailID.focus()
		return false
	}
	return true
 }