
function display_biker(biker_id,is_biker)
{
	var biker = document.getElementById(biker_id);
	
	if (is_biker)
	{
		biker.style.display = 'block';
	}
	else
	{
		biker.style.display = 'none';
	}
}

//this function is for the dependent list
function get_models_for_make(make) {
	var httpxml;
	try {
		// Firefox, Opera 8.0+, Safari
		httpxml=new XMLHttpRequest();
	}
	catch (e) {
		// Internet Explorer
		try {
			httpxml=new ActiveXObject("Msxml2.XMLHTTP");
    	} catch (e) {
    			try {
					httpxml=new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {
					alert("Your browser does not support AJAX!");
					return false;
				}
    	}
	}
		
	var url="/getmodels.php";
	url=url+"?make="+make;
  
	httpxml.onreadystatechange=stateck;
	httpxml.open("GET",url,true);
	httpxml.send(null);
  

	function stateck() {
		if(httpxml.readyState==4) {
    
			var myarray=eval(httpxml.responseText);
    
			//Before adding new we must remove previously loaded elements
			while(0 < document.emaillistform.bike_model.options.length) {
				document.emaillistform.bike_model.remove(0);
			}

			var defaultoptn = document.createElement("OPTION");
			defaultoptn.text = "Select Model";
			defaultoptn.value = "initial";
			document.emaillistform.bike_model.options.add(defaultoptn);
			for (i=0;i<myarray.length;i++) {
				var optn = document.createElement("OPTION");
				optn.text = myarray[i];
				optn.value = myarray[i];
				document.emaillistform.bike_model.options.add(optn);
			}
			var makeIndex = read_cookie('makeindex');
			var lastFetched = read_cookie('lastfetched');
			if(makeIndex==lastFetched) {
				var modelIndex = read_cookie('modelindex');
				if(modelIndex!=null) {
					document.emaillistform.bike_model.selectedIndex = modelIndex;
				}
			}
			create_cookie('lastfetched', makeIndex, 0);
		}
	}
}

function radio_unselected(radiovar,numChoices) {
	for(i=0; i<numChoices; i++) {
		if(radiovar[i].checked==true) {
			return false;
		}
	}
	return true;
}

function isValidEmail(str) {
	return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
}

//checks to make sure all questions are answered and submits the form if so
function validate_questions(form) {
	if (radio_unselected(form.own_bike,2)) {
		alert("You forgot to tell us if you own a bike?");
	} else {
		if (form.own_bike[0].checked) {
			if (radio_unselected(form.bike_type,3)) {
				alert("You forgot to tell us what kind of bike you ride! Street, dirt or both?");
			} else if ((form.bike_make.value=="initial"||form.bike_make.value==null) && form.bike_type[0].checked==false) {
				alert("You forgot to tell us your bike make!");
			} else if ((form.bike_model.value=="initial"||form.bike_model.value==null) && form.bike_type[0].checked==false) {
				alert("You forgot to tell us your bike model!");
			} else if ( radio_unselected(form.engine_status,2) ) {
				alert("You forgot to tell us if your engine is modded or not!");
			} else if ( radio_unselected(form.upgrade_engine,2) ) {
				alert("You forgot to tell us if you're interested in engine upgrades!");
			} else if ( radio_unselected(form.suspension_status,2) ) {
				alert("You forgot to tell us if your suspension is modded or not!");
			} else if ( radio_unselected(form.upgrade_suspension,2) ) {
				alert("You forgot to tell us if you're interested in suspension upgrades!");
			} else if ( radio_unselected(form.bike_service,3) )  {
				alert("You forgot to tell us who works on your bike!");
			} else if ( radio_unselected(form.racer,2) ) {
				alert("You forgot to tell us if you race or not!");
			} else if( (form.shirt_size.value=="Select"||form.shirt_size.value==null) ) {
				alert("You forgot to tell us what size free shirt you want!");
			} else if( radio_unselected(form.receive_SMS,2) ) {
				alert("You forgot to tell us if you want to receive SMS messages!");
			} else if((form.sms_number.value==""||form.sms_number.value==null) && (form.receive_SMS[0].checked==true) ) {
				alert("You forgot to tell us your mobile number for the SMS messages!");
				form.sms_number.focus();
			} else if((!(new RegExp(/\d{10}/).test(form.sms_number.value)) || (form.sms_number.value.length != 10)) && (form.receive_SMS[0].checked==true)) {
				alert("Your mobile number must be a 10 digit number, no punctuation.");
				form.sms_number.focus();
			} else if( radio_unselected(form.monthly_drawing,2) ) {
				alert("You forgot to tell us if you want to be in the monthly free give away drawing!");
			} else {
				//clear out bike_make and bike_model if street bike is selected (no street bikes supported yet)9/3/09
				if(form.bike_type[0].checked==true) {
					form.bike_make.value = "";
					form.bike_model.value = "";
				}
				
				//alert("valid input");
				form.submit();
			}
		} else {
			if( (form.shirt_size.value=="Select"||form.shirt_size.value==null) ) {
				alert("You forgot to tell us what size free shirt you want!");
			} else if( radio_unselected(form.receive_SMS,2) ) {
				alert("You forgot to tell us if you want to receive SMS messages!");
			} else if((form.sms_number.value==""||form.sms_number.value==null) && (form.receive_SMS[0].checked==true) ) {
				alert("You forgot to tell us your mobile number for the SMS messages!");
				form.sms_number.focus();
			} else if((!(new RegExp(/\d{10}/).test(form.sms_number.value)) || (form.sms_number.value.length != 10)) && (form.receive_SMS[0].checked==true)) {
				alert("Your mobile number must be a 10 digit number, no punctuation.");
				form.sms_number.focus();
			} else if( radio_unselected(form.monthly_drawing,2) ) {
				alert("You forgot to tell us if you want to be in the monthly free give away drawing!");
			} else {	
				//clear out bike_make and bike_model if street bike is selected (no street bikes supported yet)9/3/09
				if(form.bike_type[0].checked==true) {
					form.bike_make.value = "";
					form.bike_model.value = "";
				}
				
				//alert("valid input");
				form.submit();
			}
		}
	}
	
	
	
	
}

//synchonrize the shipping/billing information
function copyAddress(theForm) {
   theForm.ship_name.value          = theForm.name.value;
   theForm.ship_address1.value      = theForm.address1.value;
   theForm.ship_address2.value      = theForm.address2.value;
   theForm.ship_city.value          = theForm.city.value;
   theForm.ship_state.value 		= theForm.state.value;
   theForm.ship_zip.value           = theForm.zip.value;
   theForm.ship_email.value         = theForm.email.value;
   theForm.ship_phone.value         = theForm.phone.value;
}

function display_cvv() {
	window.open('cvv.html','cvv_window','toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=600,height=300,left = 250,top = 250');
}

function validate_cc(form) {
	//alert('cc_validate called');
	
	if( form.name.value==""||form.name.value==null ) {
		alert("You forgot to fill in your name!");
		form.name.focus();
	} else if(form.address1.value==""||form.address1.value==null) {
		alert("You forgot to fill in your address!");
		form.address1.focus();
	} else if(form.city.value==""||form.city.value==null) {
		alert("You forgot to fill in your city!");
		form.city.focus();
	} else if(form.state.value==""||form.state.value==null||form.state.value=="initial") {
		alert("You forgot to select your state!");
		form.state.focus();
	} else if(form.zip.value==""||form.zip.value==null) {
		alert("You forgot to fill in your zip code!");
		form.zip.focus();
	} else if(!isValidEmail(form.email.value) || form.email.value=="") {
		alert("Please enter a valid email address!");
		form.email.focus();
	} else if(form.ship_name.value==""||form.ship_name.value==null) {
		alert("You forgot to tell us who to ship too!");
		form.ship_name.focus();
	} else if(form.ship_address1.value==""||form.ship_address1.value==null) {
		alert("You forgot to tell us what address to ship too!");
		form.ship_address1.focus();
	} else if(form.ship_city.value==""||form.ship_city.value==null) {
		alert("You forgot to tell us what city to ship too!");
		form.ship_city.focus();
	} else if(form.ship_state.value==""||form.ship_state.value==null||form.ship_state.value=="initial") {
		alert("You forgot to tell us what state to ship too!");
		form.ship_state.focus();
	} else if(form.ship_zip.value==""||form.ship_zip.value==null) {
		alert("You forgot to tell us what zipcode to ship too!");
		form.ship_zip.focus();
	} else if(!isValidEmail(form.ship_email.value)||form.ship_email.value==null) {
		alert("Please enter a valid email address for the shipping location too!");
		form.ship_email.focus();
	} else if (form.card_name.value == '') {
    	alert('You did not enter the name on the credit card.  You must complete all fields to continue.');
    	form.card_name.focus();
	} else if (form.card_number.value == '') {
    	alert('You did not enter a valid credit card number.  You must complete all fields to continue.');
    	form.card_number.focus();
	} else if (form.card_type.value == 'xx') {
    	alert('You did not enter a valid credit card type.  You must complete all fields to continue.');
    	form.card_type.focus();
	} else if (form.cvv2.value == '') {
    	alert('You did not enter a cvv2 code.  You must complete all fields to continue.');
    	form.cvv2.focus();
	} else  {
		//alert('submitting the form goes here');
		form.submit();
	}
}

function create_cookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function read_cookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}


