// JavaScript Document
function ValidateNewsletter(){
	var x = document.Newsletter;
	
	if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(x.email.value))){
		alert('Please enter a valid email address to subscribe to our newsletter.');
		x.email.focus();
		return false;
	}
}

function ValidatePhotos(){
	var x = document.SendPhotos;
	
	if (x.first_name.value == ""){
		alert('Please enter your first name.');
		x.first_name.focus();
		return false;
	} else if (x.last_name.value == ""){
		alert('Please enter your last name.');
		x.last_name.focus();
		return false;
	} else if (x.city.value == ""){
		alert('Please enter your city.');
		x.city.focus();
		return false;
	} else if (x.state.selectedIndex==0){
		alert('Please select your state.');
		x.state.focus();
		return false;
	} else if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(x.email.value))){
		alert('Please enter a valid email address.');
		x.email.focus();
		return false;
	} else if (x.photo.value == ""){
		alert('Please choose a photo to upload. Accepted file types are .JPG and .GIF.');
		x.photo.focus();
		return false;
	} else if ( (Right(x.photo.value, 4) != ".gif") && (Right(x.photo.value, 4) != ".jpg") ){
		alert('The photo you are trying to upload is not valid. Accepted file types are .JPG and .GIF. Your file type is "' + Right(x.photo.value, 4) + '". Please choose a new file.');
		x.photo.value="";
		x.photo.focus();
		return false;
	}
}


function ValidateShipping() {
	var x = document.Shipping;
	
	if (x.first_name.value == ""){
		alert('Please enter your first name.');
		x.first_name.focus();
		return false;
	} else if (x.last_name.value == ""){
		alert('Please enter your last name.');
		x.last_name.focus();
		return false;
	} else if (x.city.value == ""){
		alert('Please enter your city.');
		x.city.focus();
		return false;
	} else if (x.state.selectedIndex==0){
		alert('Please select your state.');
		x.state.focus();
		return false;
	} else if (x.zip.value == ""){
		alert('Please enter your zip code.');
		x.zip.focus();
		return false;	
	} else if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(x.email.value))){
		alert('Please enter a valid email address.');
		x.email.focus();
		return false;
	}
}

function ValidateBilling() {
	var x = document.Billing;
	
	if (x.first_name.value == ""){
		alert('Please enter your first name.');
		x.first_name.focus();
		return false;
	} else if (x.last_name.value == ""){
		alert('Please enter your last name.');
		x.last_name.focus();
		return false;
	} else if (x.city.value == ""){
		alert('Please enter your city.');
		x.city.focus();
		return false;
	} else if (x.state.selectedIndex==0){
		alert('Please select your state.');
		x.state.focus();
		return false;
	} else if (x.zip.value == ""){
		alert('Please enter your zip code.');
		x.zip.focus();
		return false;	
	}
}

function addShip(){
	var x = document.Billing;
	
	x.first_name.value = x.ShipFirstName.value;
	x.last_name.value  = x.ShipLastName.value;
	x.address.value    = x.ShipAddress.value;
	x.address_2.value   = x.ShipAddress2.value;
	x.city.value       = x.ShipCity.value;
	x.zip.value        = x.ShipZip.value;
	
	for(i=0; i<x.state.options.length; i++) {
		//alert(x.state.options[i].value + "--" + x.ShipCity.value);
		if(x.state.options[i].value == x.ShipState.value){
			x.state.selectedIndex = i;
		}
	}
}

function checkOrder(formElement) {
	CheckCardNumber(formElement)
	/*
	if(CheckCardNumber(formElement)) {
		formElement.submit();
	} 
	*/
}


// Tools
function Left(str, n){
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}

function Right(str, n){
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}