
//build cart url
function AddToCart(productid,radioGroup,producttype) {
    coursedateid=getSelectedValue(radioGroup,'Select a Course Date');
    if(coursedateid.length>0)
        window.location.href='cart.php?action=add&p='+productid+'&c='+coursedateid;
    else
        if (producttype!='video' && producttype!='dvd')
            alert("Please select a Course Date");
        else
            window.location.href='cart.php?action=add&p='+productid+'&c='+coursedateid;
}

function getSelectedValue(radioObj,msg) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
    alert(msg);
    return "";
}

function ValidateSignup(theForm) {
    if(ValidateEmail(theForm)) {
        alert("Thank you for signing up for our Newsletter!");
        return true;
    } else {
        return false;
    }
}

function ValidateEmail(theForm)
{
  var strEmail=theForm.email.value;
  var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
  if (filter.test(strEmail)) {
  		testresults=true
  }
  else{
  		alert('Please enter a valid email address!')
  		testresults=false
  }
  return testresults;
}

function ValidateEmail2(txtEmail)
{
  var strEmail=txtEmail.value;
  var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
  if (filter.test(strEmail))
  		testresults=true
  else{
  		alert('Please enter a valid email address!');
        txtEmail.focus();
  		testresults=false
  }
  return testresults;
}

/*
Strip whitespace from the beginning and end of a string
Input : a string
*/
function trim(str)
{
    str= '' + str;
    if (str.length!=0)
    	return str.replace(/^\s+|\s+$/g,'');
    else return str;
}

/*
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)
	}

    if (textBox.value.length != 0)
	    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;
		}
	}	
}