/**
 * December 23, 2008
 * @author TJ Horlacher, Ambria Technology...tj@ambriatech.com
 * licensed to Boondockers, All rights reserved
 * 
 * v1.0
 */

var calPop = "";

////////////
//init for calendarPopup by Matt Kruse <matt@mattkruse.com> at http://www.mattkruse.com/
function init_Popup(){
	if (calPop == "") { //only init once
		calPop = new CalendarPopup("divCalPopup");
		var today = new Date();	//with (yesterday) setDate(getDate() - 1);
		calPop.setCssPrefix("sched_cal");
		getCalendarStyles()	
	}
}
function do_calPop() {
	calPop.select(document.forms[0].purdate,'anchor_calpop','MM/dd/yyyy');
}
function hideOtherPopupWindows() {
	//extended original calpop from Matt by embedding
	//this call in calendarPopup.js - func -> hidePopupWindows
	//calendarPopup attaches listener to mouseup event...
	//closes calendarPopup and does anything here on event mouseup

	//alert("hi");
}

function do_sendRec() {
	var sSend = "";
	
	////////////
	//check required fields
	if ( !validateForm() ){return false;}

	////////////
	//disable submit button to prevent multiple submissions
	$('sendBtn').disabled = true;
	
	////////////
	//pickup input values
	var FormItms = document.getElementById("divForm")
	var inputs = FormItms.getElementsByTagName("input");
	//var inputs = $$('#FormItms input'); //easier but does not yet work in google chrome
	//instead...
	for(var i=0;i<inputs.length;i++)
	{
		var input = inputs[i];
		if (input.id != "sendBtn" && input.id != "bb" && input.id != "single" && input.id != "twin" && input.id != "turbo" ) {		
			//clean up the string
			sVal = input.value.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
			sSend += "&" + input.id + "=" + sVal;
		}
	}

	///////////
	//next pickup the checkbox's
	//...loop above does not work consistently work in all browsers	
	//bb
	if ( $F('bb') == "on" ) {	sSend += "&bb=on";
	} else { sSend += "&bb=off"; }

	//single
	if ( $F('single') == "on" ) {	sSend += "&single=on";
	} else { sSend += "&single=off"; }

	//twin
	if ( $F('twin') == "on" ) {	sSend += "&twin=on";
	} else { sSend += "&twin=off"; }
	
	//turbo
	if ( $F('turbo') == "on" ) {	sSend += "&turbo=on";
	} else { sSend += "&turbo=off"; }


	///////////
	//next pickup the select items (combobox's)
	//...loop above does not work in IE
	//country
	sVal = $F('country');
	sSend += "&country=" + sVal;

	//state
	sVal = $F('state');
	sSend += "&state=" + sVal;
	
	//item purchached
	sVal = $F('item');
	sSend += "&item=" + sVal;
	
	//elevation
	sVal = $F('elevation');
	sSend += "&elevation=" + sVal;	

	//riding
	sVal = $F('riding');
	sSend += "&riding=" + sVal;	

	//miles
	sVal = $F('miles');
	sSend += "&miles=" + sVal;	


	///////////
	//next pickup the textareas
	//var inputs = $$('#FormItms textarea'); //easier but does not yet work in google chrome
	//instead...
	var inputs = FormItms.getElementsByTagName("textarea");
	for(var i=0;i<inputs.length;i++)
	{
		var input = inputs[i];
		//clean up the string
		sVal = input.value.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
		sSend += "&" + input.id + "=" + sVal;
	}
	
	////////////
	//post the request
	new Ajax.Request( 'scripts/reg-functions.php', {
		asynchronous: true,
		method: 'post', 
		postBody: 'task=emailRec' +  sSend,
		onComplete: function(request){			
			if (request.responseText.indexOf("|") == "7") {
				//result is coming in like 'success|response' or 'failure|response'
				var aVal = request.responseText.split('|');
				
				if (aVal[0] == "success") {
					//notify reg was sent
					do_Alert("Boondockers Registration Sent", aVal[1]);
					$('buttons').innerHTML = "Registration Sent Successfully";
					return false;
				}
				else {
					//notify reg val error
					$('sendBtn').disabled = false;
					do_valAlert("Invalid Email", aVal[1], "email");
					return false;
				}
			} else {
				$('sendBtn').disabled = false;
				if (request.responseText == "") {
					do_GenAlert("Unknown Error", "An unknown error occured. Please contact Boondockers for assistance.");
				}
				else {
					do_GenAlert("Unknown Error", request.responseText);
				}
			}
		}
	});
}

function validateForm(){
	if ($F('fname') == "") {
			do_valAlert("Missing Information","Please enter your first and last name.","fname");
			return false;
	}
	if ($F('lname') == "") {
			do_valAlert("Missing Information","Please enter your first and last name.","lname");
			return false;
	}
	if ($F('address1') == "") {
			do_valAlert("Missing Information","Please enter your full contact address.","address1");
			return false;
	}
	if ($F('country') == "") {
			do_valAlert("Missing Information","Please enter your full contact address.","country");
			return false;
	}
	if ($F('city') == "") {
			do_valAlert("Missing Information","Please enter your full contact address.","city");
			return false;
	}
	if ($F('state') == "") {
			do_valAlert("Missing Information","Please enter your full contact address.","state");
			return false;
	}
	if ($F('zip') == "") {
			do_valAlert("Missing Information","Please enter your full contact address.","zip");
			return false;
	}
	if ($F('email') == "") {
			do_valAlert("Missing Information","Please enter your email address.","email");
			return false;
	}
	if ($F('item') == "") {
			do_valAlert("Missing Information","Please enter the item you purchased.","item");
			return false;
	}
	if ($F('purdate') == "") {
			do_valAlert("Missing Information","Please enter date you purchased the item.","purdate");
			return false;
	}
	if ($F('where') == "") {
			do_valAlert("Missing Information","Please enter where you purchased the item.","where");
			return false;
	}
	if ($F('make') == "") {
			do_valAlert("Missing Information","Please enter complete the vehicle information section.","make");
			return false;
	}
	if ($F('model') == "") {
			do_valAlert("Missing Information","Please enter complete the vehicle information section.","model");
			return false;
	}
	if ($F('year') == "") {
			do_valAlert("Missing Information","Please enter complete the vehicle information section.","year");
			return false;
	}
	if ($F('engine') == "") {
			do_valAlert("Missing Information","Please complete the vehicle information section.","engine");
			return false;
	}
	
	//everything validated ok
	return true;
}

function formatPhoneNumber(input){
	if((/^(\d{3})(\d{3})(\d{4})$/).test($(input).value)){
		$('input').value = '('+RegExp.$1+') '+RegExp.$2+'-'+RegExp.$3;
	} else{ $('input').value = $(input).value }
}

function valRideOther() {
	if ($F('riding') == "Other") {
		$('other').disabled = false;
		$('other').style.backgroundColor = "#ffff66"
		//$('other').focus();
	} else {
		$('other').value = '';
		$('other').disabled = true;
		$('other').style.backgroundColor = "#cccccc"
	}
}

function valItem() {
	if ($F('item') != "") {
		$('itemdesc').disabled = false;
		$('itemdesc').style.backgroundColor = "#ffff66"
		//$('itemdesc').focus();
	} else {
		$('itemdesc').value = '';
		$('itemdesc').disabled = true;
		$('itemdesc').style.backgroundColor = "#cccccc"		
	}
}

function valState() {
	if ($F('country') == "United States of America") {
		$('selectState').innerHTML = '<input name="city" type="text" class="txt" id="city" style="width:243" /> \
									<label for="state" style="padding-left:4px;padding-right:3px;">State</label> \
									<select name="state" id="state" size="1" style="width:148"> \
									<option value="">Select State</option> \
									<option value="AK">Alaska</option> \
									<option value="AL">Alabama</option> \
									<option value="AR">Arkansas</option> \
									<option value="AZ">Arizona</option> \
									<option value="CA">California</option> \
									<option value="CO">Colorado</option> \
									<option value="CT">Connecticut</option> \
									<option value="DC">District of Columbia</option> \
									<option value="DE">Delaware</option> \
									<option value="FL">Florida</option> \
									<option value="GA">Georgia</option> \
									<option value="HI">Hawaii</option> \
									<option value="IA">Iowa</option> \
									<option value="ID">Idaho</option> \
									<option value="IL">Illinois</option> \
									<option value="IN">Indiana</option> \
									<option value="KS">Kansas</option> \
									<option value="KY">Kentucky</option> \
									<option value="LA">Louisiana</option> \
									<option value="MA">Massachusetts</option> \
									<option value="MD">Maryland</option> \
									<option value="ME">Maine</option> \
									<option value="MI">Michigan</option> \
									<option value="MN">Minnesota</option> \
									<option value="MO">Missouri</option> \
									<option value="MS">Mississippi</option> \
									<option value="MT">Montana</option> \
									<option value="NC">North Carolina</option> \
									<option value="ND">North Dakota</option> \
									<option value="NE">Nebraska</option> \
									<option value="NH">New Hampshire</option> \
									<option value="NJ">New Jersey</option> \
									<option value="NM">New Mexico</option> \
									<option value="NV">Nevada</option> \
									<option value="NY">New York</option> \
									<option value="OH">Ohio</option> \
									<option value="OK">Oklahoma</option> \
									<option value="OR">Oregon</option> \
									<option value="PA">Pennsylvania</option> \
									<option value="PR">Puerto Rico</option> \
									<option value="RI">Rhode Island</option> \
									<option value="SC">South Carolina</option> \
									<option value="SD">South Dakota</option> \
									<option value="TN">Tennessee</option> \
									<option value="TX">Texas</option> \
									<option value="UT">Utah</option> \
									<option value="VA">Virginia</option> \
									<option value="VT">Vermont</option> \
									<option value="WA">Washington</option> \
									<option value="WI">Wisconsin</option> \
									<option value="WV">West Virginia</option> \
									<option value="WY">Wyoming</option></select> \
									<label for="zip" style="padding-left:4px;padding-right:3px;">Zip</label> \
									<input name="zip" type="text" class="txt" id="zip" style="width:105">';
	} else if ($F('country') == "Canada") {
		$('selectState').innerHTML = '<input name="city" type="text" class="txt" id="city" style="width:243" /> \
									<label for="state" style="padding-left:4px;padding-right:3px;">State</label> \
									<select name="state" id="state" size="1" style="width:148"> \
									<option value="">Select Province</option> \
									<option value="AB">Alberta</option> \
									<option value="BC">British Columbia</option> \
									<option value="MB">Manitoba</option> \
									<option value="NB">New Brunswick</option> \
									<option value="NF">Newfoundland</option> \
									<option value="NT">Northwest Territories</option> \
									<option value="NS">Nova Scotia</option> \
									<option value="NU">Nunavut</option> \
									<option value="ON">Ontario</option> \
									<option value="PE">Prince Edward Island</option> \
									<option value="QC">Quebec</option> \
									<option value="SK">Saskatchewan</option> \
									<option value="YT">Yukon Territory</option></select> \
									<label for="zip" style="padding-left:4px;padding-right:3px;">Zip</label> \
									<input name="zip" type="text" class="txt" id="zip" style="width:105">';
	} else if ($F('country') == "") {
		$('selectState').innerHTML = '<input name="city" type="text" class="txt" id="city" style="width:243" /> \
									<label for="state" style="padding-left:4px;padding-right:3px;">State</label> \
									<select name="state" id="state" size="1" style="width:148"> \
									<option value="">No Country</option></select> \
									<label for="zip" style="padding-left:4px;padding-right:3px;">Zip</label> \
									<input name="zip" type="text" class="txt" id="zip" style="width:105">';
	} else {
		$('selectState').innerHTML = '<input name="city" type="text" class="txt" id="city" style="width:243" /> \
									<label for="state" style="padding-left:4px;padding-right:3px;">State</label> \
									<input name="state" id="state" size="1" style="width:148;background-color:#FFFF66;border-width: 1px;border-color: #CCCCCC;border-style: solid;-moz-border-radius: 3px 3px;"> \
									<label for="zip" style="padding-left:4px;padding-right:3px;">Zip</label> \
									<input name="zip" type="text" class="txt" id="zip" style="width:105">';
	}
}

ModalPopups.SetDefaults( 
	{
		shadow: true, 
		shadowSize: 5, 
		shadowColor: "#333333", 
		backgroundColor: "#4f4f4f", 
		borderColor: "#999999", 
		titleBackColor: "#104c87", 
		titleFontColor: "#ffffff", 
		popupBackColor: "#ebf0fb", 
		popupFontColor: "#4f4f4f", 
		footerBackColor: "#ebf0fb", 
		footerFontColor: "#4f4f4f", 
		okButtonText: "OK", 
		yesButtonText: "Yes", 
		noButtonText: "No", 
		cancelButtonText: "Cancel", 
		fontFamily: "Tahoma, Helvetica, sans-serif", 
		fontSize: "9pt"
	}
);
function do_Alert(sTitle, sTxt) {
	ModalPopups.Alert("simpAlert",  
        "<span style=color:white>" + sTitle + "</span>",
        "<div style='padding:25px;font-size:12px:'><img src=\"images/announce_48.png\" style='vertical-align:middle;' width=48 height=48><b><span style='padding-left:10px'>" + sTxt + "</span><br/>" +
        "</b></div>"
    );
}
function do_GenAlert(sTitle, sTxt) {
	ModalPopups.Alert("simpAlert",  
        "<span style=color:white>" + sTitle + "</span>",
        "<div style='padding:25px;font-size:12px:'><img src=\"images/warning_48.png\" style='vertical-align:middle;' width=48 height=48><b><span style='padding-left:10px'>" + sTxt + "</span><br/>" +
        "</b></div>"
    );
}
function do_valAlert(sTitle, sTxt, sFrom) {  
    ModalPopups.Alert("valAlert",  
        "<span style=color:white>" + sTitle + "</span>",
        "<div style='padding:25px;font-size:12px'><img src=\"images/warning_48.png\" style='vertical-align:middle;' width=48 height=48><b><span style='padding-left:10px'>" + sTxt + "</span><br/>" +
        "</b></div>",
        {  
			onOk: "valAlertOk(\"" + sFrom + "\");"
        }
    );
}
function valAlertOk(sFrom) {
	ModalPopups.Close("valAlert");
	$(sFrom).focus();
	new Effect.Highlight($(sFrom),{ startcolor: '#ffff66', endcolor: '#ff9900' }); return false;
}

