function trim(str)
{
   return str.replace(/^\s*|\s*$/g,"");
}

function radioVal(btn) {
var cnt = -1;
for (var i=btn.length-1; i > -1; i--) {
   if (btn[i].checked) {cnt = i; i = -1;}
   }
if (cnt > -1) return btn[cnt].value;
else return "";
}

function antispam(name,domain) {
    	document.location = "mailto:" + name + "@" + domain;
	}

//howManyChecked('myform','cb_industry',6,1,'Industry');
function howManyChecked(whichForm,whichCheckBoxArray,myMax,myMin,whichQuestion)
/*
  This function takes 5 paramaters:
  whichForm -- the NAME of the form to be validated, a string
  whichCheckBoxArray -- the NAME of the checkbox to be checked, a string
  myMax -- the most you want the user to be able to check, an integer
  myMin -- the least you want the user to be able to check, an integer
  whichQuestion -- a short description of the question, a string
  
  example use:
  howManyChecked('myform','cb_industry',6,1,'Industry');
*/
{
	var _countChecked = 0;
	var err = 0;
	/* iterate through all the elements in the checkbox array */
	for(i=1;i<document[whichForm][whichCheckBoxArray].length;i++)
	{
		/* and check to see if each is checked */
		if(document[whichForm][whichCheckBoxArray][i].checked==true)
			/* if it is, increment a counter */
			{ _countChecked++; }
	}
	/* is the count too high? */
	if(_countChecked > myMax)
		{ alert('Limit '+myMax+' checks for '+whichQuestion);
			err = 1;}
	/* of is the count too low */
	else if(_countChecked < myMin)
		{ alert('You must fill out at least '+myMin+' entry(s) for '+whichQuestion);
			err = 1;}
	if (err == 1) { 
	return true; 
	}
	else { 
	return false; 
	}
}

function validateRegister(form)
{
checkFName = trim(form.txtFName.value)
checkLName = trim(form.txtLName.value)
checkGender = trim(radioVal(form.radGender))
checkEmail = trim(form.txtEmail.value)
aliaspos = checkEmail.indexOf("@")
dotpos = checkEmail.lastIndexOf(".")
checkUsername = trim(form.txtUsername.value)
checkPassword1 = trim(form.txtPassword1.value)
checkPassword2 = trim(form.txtPassword2.value)
checkPhone1 = trim(form.txtPhone1.value)

checkAddress = trim(form.txtAddress.value)
checkCity= trim(form.txtCity.value)
checkState = trim(form.txtState.value)
checkCountry = form.cbbCountry.selectedIndex
checkMState = form.cbbState.selectedIndex
checkPostcode= trim(form.txtPostcode.value)

if (checkFName.length < 1){
alert("Please enter your first name")
return false
}
else if (checkLName.length < 1){
alert("Please enter your last name")
return false
}
else if (checkGender.length < 1){
alert("Please choose your gender")
return false
}
else if (checkEmail.length < 1){
alert("Please enter your email")
return false
}
else if (aliaspos == -1 || dotpos == -1)
{ alert ("Not a valid email")
return false
}
else if (checkUsername.length < 1){
alert("Please enter your user name")
return false
}

else if (checkPassword1.length < 1){
alert("Please enter your password")
return false
}
else if (checkPassword1 != checkPassword2){
alert("Both passwords are not matched. Please renter your password")
return false
}
else if (checkPhone1.length < 1){
alert("Please enter your phone 1")
return false
}
else if (checkAddress.length < 1){
alert("Please enter your street")
return false
}
else if (checkCity.length < 1){
alert("Please enter your city")
return false
}
else if (checkCountry == 0){
alert("Please select your country")
return false
}
else if (checkPostcode.length < 1){
alert("Please enter your postcode")
return false
}
else if (form.cbbCountry.options[checkCountry].value=='Malaysia'){
	if (checkMState == 0){
	alert("Please select your state")
	return false
	}
} 
else if (form.cbbCountry.options[checkCountry].value!='Malaysia'){
	if (checkState.length < 1){
	alert("Please enter your state")
	return false
	}
}
return true
}

function validateProfile(form)
{
checkFName = trim(form.txtFName.value)
checkLName = trim(form.txtLName.value)
checkGender = trim(radioVal(form.radGender))
checkEmail = trim(form.txtEmail.value)
aliaspos = checkEmail.indexOf("@")
dotpos = checkEmail.lastIndexOf(".")
checkPassword = trim(form.txtPassword.value)
checkPhone1 = trim(form.txtPhone1.value)

checkAddress = trim(form.txtAddress.value)
checkCity= trim(form.txtCity.value)
checkState = trim(form.txtState.value)
checkCountry = form.cbbCountry.selectedIndex
checkMState = form.cbbState.selectedIndex
checkPostcode= trim(form.txtPostcode.value)

if (checkFName.length < 1){
alert("Please enter your first name")
return false
}
else if (checkLName.length < 1){
alert("Please enter your last name")
return false
}
else if (checkGender.length < 1){
alert("Please choose your gender")
return false
}
else if (checkEmail.length < 1){
alert("Please enter your email")
return false
}
else if (aliaspos == -1 || dotpos == -1)
{ alert ("Not a valid email")
return false
}
else if (checkPassword.length < 1){
alert("Please enter your password")
return false
}
else if (checkPhone1.length < 1){
alert("Please enter your phone 1")
return false
}
else if (checkAddress.length < 1){
alert("Please enter your street")
return false
}
else if (checkCity.length < 1){
alert("Please enter your city")
return false
}
else if (checkCountry == 0){
alert("Please select your country")
return false
}
else if (checkPostcode.length < 1){
alert("Please enter your postcode")
return false
}
else if (form.cbbCountry.options[checkCountry].value=='Malaysia'){
	if (checkMState == 0){
	alert("Please select your state")
	return false
	}
} 
else if (form.cbbCountry.options[checkCountry].value!='Malaysia'){
	if (checkState.length < 1){
	alert("Please enter your state")
	return false
	}
}
return true
}

function validateDelivery(form)
{
//Billing
checkBName = trim(form.txtBName.value)
checkBEmail = trim(form.txtBEmail.value)
Baliaspos = checkBEmail.indexOf("@")
Bdotpos = checkBEmail.lastIndexOf(".")
checkBPhone1 = trim(form.txtBPhone1.value)

checkBAddress= trim(form.txtBAddress.value)
checkBCity = trim(form.txtBCity.value)
checkBState = trim(form.txtBState.value)
checkBCountry = form.cbbBCountry.selectedIndex
checkMBState = form.cbbBState.selectedIndex
checkBPostcode = trim(form.txtBPostcode.value)

//Shipping
checkSName = trim(form.txtSName.value)
checkSEmail = trim(form.txtSEmail.value)
Saliaspos = checkSEmail.indexOf("@")
Sdotpos = checkSEmail.lastIndexOf(".")
checkSPhone1 = trim(form.txtSPhone1.value)

checkSAddress= trim(form.txtSAddress.value)
checkSCity = trim(form.txtSCity.value)
checkSState = trim(form.txtSState.value)
checkSCountry = form.cbbSCountry.selectedIndex
checkMSState = form.cbbSState.selectedIndex
checkSPostcode = trim(form.txtSPostcode.value)


if (checkBName.length < 1){
alert("Please enter your name for billing")
return false
}
else if (checkBEmail.length < 1){
alert("Please enter your email for billing")
return false
}
else if (Baliaspos == -1 || Bdotpos == -1)
{ alert ("Not a valid email for billing")
return false
}
else if (checkBPhone1.length < 1){
alert("Please enter your phone for billing")
return false
}
else if (checkBAddress.length < 1){
alert("Please enter your address for billing")
return false
}
else if (checkBCity.length < 1){
alert("Please enter your city for billing")
return false
}
else if (checkBCountry == 0){
alert("Please select your country for billing")
return false
}
else if (checkBPostcode.length < 1){
alert("Please enter your postcode for billing")
return false
}
else if (checkSName.length < 1){
alert("Please enter your name for shipping")
return false
}
else if (checkSEmail.length < 1){
alert("Please enter your email for shipping")
return false
}
else if (Saliaspos == -1 || Sdotpos == -1)
{ alert ("Not a valid email for shipping")
return false
}
else if (checkSPhone1.length < 1){
alert("Please enter your phone for shipping")
return false
}
else if (checkSAddress.length < 1){
alert("Please enter your address for shipping")
return false
}
else if (checkSCity.length < 1){
alert("Please enter your city for shipping")
return false
}
else if (checkSCountry == 0){
alert("Please select your country for shipping")
return false
}
else if (checkSPostcode.length < 1){
alert("Please enter your postcode for shipping")
return false
}

if (form.cbbBCountry.options[checkBCountry].value=='Malaysia'){
	if (checkMBState == 0){
	alert("Please select your state")
	return false
	}
} 
else if (form.cbbBCountry.options[checkBCountry].value!='Malaysia'){
	if (checkBState.length < 1){
	alert("Please enter your state")
	return false
	}
}

if (form.cbbSCountry.options[checkSCountry].value=='Malaysia'){
	if (checkMSState == 0){
	alert("Please select your state")
	return false
	}
} 
else if (form.cbbSCountry.options[checkSCountry].value!='Malaysia'){
	if (checkSState.length < 1){
	alert("Please enter your state")
	return false
	}
}

return true
}


function validateLogin(form)
{
checkUsername = trim(form.txtUsername.value)
checkPassword = trim(form.txtPassword.value)


if (checkUsername.length < 1){
alert("Please enter your user name")
return false
}

else if (checkPassword.length < 1){
alert("Please enter your password")
return false
}
return true
}

function validateSearch(form)
{
checkSearch = trim(form.txtSearch.value)

if (checkSearch.length < 1){
alert("Please enter keyword to search")
return false
}
return true
}

function validateTC(form)
{
if (form.chbTC.checked==false) {
alert("Please accept terms and conditions before checkout")
return false
}
return true
}

function copyBilling(form)
{
if (form.chbSame.checked==true) {
form.txtSName.value = trim(form.txtBName.value)
form.txtSEmail.value = trim(form.txtBEmail.value)
form.txtSPhone1.value = trim(form.txtBPhone1.value)
form.txtSFax.value = trim(form.txtBFax.value)

form.txtSAddress.value = trim(form.txtBAddress.value)
form.txtSCity.value = trim(form.txtBCity.value)
form.txtSState.value = trim(form.txtBState.value)
form.cbbSCountry.selectedIndex = form.cbbBCountry.selectedIndex
form.cbbSState.selectedIndex = form.cbbBState.selectedIndex
form.txtSPostcode.value = trim(form.txtBPostcode.value)
}
else {
form.txtSName.value = ""
form.txtSEmail.value = ""
form.txtSPhone1.value = ""
form.txtSFax.value = ""

form.txtSAddress.value = ""
form.txtSCity.value = ""
form.txtSState.value = ""
//form.cbbSCountry.selectedIndex = form.cbbBCountry.selectedIndex
form.cbbSCountry.selectedIndex = 0
form.cbbSState.selectedIndex = 0
form.txtSPostcode.value = ""
}
}

	function showLayer(layerName)
{
    if (document.getElementById) // Netscape 6 and IE 5+
    {
 var targetElement = document.getElementById(layerName);
 targetElement.style.display = 'block';
    }
}

function hideLayer(layerName)
{
    if (document.getElementById)
    {
 var targetElement = document.getElementById(layerName);
 targetElement.style.display = 'none';
    }
}
