var xmlDocAirRates = "https://spreadsheets.google.com/feeds/list/0ArSi-WJzp-jxdER1UnI5ZHZ2Y29YTEdiSzh0dnl5WGc/od6/public/basic?alt=rss";
var baseo2Net = "o2NetDev";
var xsltDoc = "/xslt/homePage2011_hudson.xslt";
var cookieName = "homePageCityStart";

var cityStartLoad = "CHICAGO DOWNTOWN";

var linkStr = '';

var departArrive = "SELDEP";
var cityId = "";
var portType = "1";
var monthNum = "";
var dateNum = "";
var hourNum = "";
var minuteNum = "";
var ampm = "";

$(function (){
	getPrice();
	$('.date-pick').datePicker().val(new Date().asString()).trigger('change');
	$("#time2").timePicker({
	  startTime: "1:00",  // Using string. Can take string or Date object.
	  endTime: new Date(0, 0, 0, 23, 30, 0),  // Using Date object.
	  show24Hours: false,
	  separator:':',
	  step: 5
	  });
	dateChange();
	changePort();
	changeDepartArrive();
	changeCity();
	changeTime();
	executeBookTrip();
	
	var sSite = window.location;
	sSite = sSite + ' ';
	if(sSite.indexOf('orbisdesign.net') != -1){
		rssFeedXml(window.xmlDocAirRates);
	}	
	dailDealNav();
	
	
});
$(window).load(function () {
	getPrice();
	stylizeDate();
	footerNavHome();
});
function dailDealNav(){
	var htmlStr = "";
	htmlStr += '<div id="dailyDealTab">';
  htmlStr += '<a href="/SpecialOffers_8237.aspx">';
  htmlStr += '<img src="/images/content/dailyDealTab.png">';
  htmlStr += '</a>';
  htmlStr += '</div>';
	$("#main").append(htmlStr);
}
function footerNavHome(){
	var footerNav = document.getElementById('FooterImage_1');
	var HTMLDisp = '';
	HTMLDisp = HTMLDisp + '<div id=\"FooterImage_1\">'
	HTMLDisp = HTMLDisp + '<a href=\"Privacy_7558.aspx\">Privacy</a>';
	HTMLDisp = HTMLDisp + ' | ';
	HTMLDisp = HTMLDisp + '<a href=\"Legal_1375.aspx\">Usage & Regulations of Our Website</a>';
	HTMLDisp = HTMLDisp + '</div>'
	//seeAlso custImport.css
	HTMLDisp = HTMLDisp + '<div id=\"twitterLink\">';
	HTMLDisp = HTMLDisp + '<a class=\"followLinks\" href=\"' + twitterLink + '\"><img src=\"/images/trans.gif\" width=\"130\" height=\"25\"></a>';
	HTMLDisp = HTMLDisp + '</div>';
	HTMLDisp = HTMLDisp + '<div id=\"facebookLink\">';
	HTMLDisp = HTMLDisp + '<a class=\"followLinks\" href=\"' + facebookLink + '\"><img src=\"/images/trans.gif\" width=\"130\" height=\"25\"></a>';
	HTMLDisp = HTMLDisp + '</div>';	
	footerNav.innerHTML = HTMLDisp;	
}
//try{Typekit.load();}catch(e){}
function executeBookTrip(){
	$("#bookTrip").click(function(){
		window.location = linkStr;
	});	
	$("#bookTripBottom").click(function(){
		window.location = linkStr;
	});	
}
function changeCity(){
	$('#chooseCity').change(function() {
	  getPrice();
	})	
}
function changeTime(){
	$('#time2').change(function() {
	  hourNum = parseHour(document.getElementById('time2').value);
	  hourNum = getMonth(window.hourNum);
	  
	  minuteNum = parseMinute(document.getElementById('time2').value);
	  minuteNum = getMonth(window.minuteNum) + 1;
	  minuteNum = parseInt(minuteNum)/5;
	  
	  ampm = parseAmPm(document.getElementById('time2').value);

	  getPrice();
	})	
	hourNum = parseHour(document.getElementById('time2').value);
	hourNum = getMonth(window.hourNum);
	
	minuteNum = parseMinute(document.getElementById('time2').value);
	minuteNum = getMonth(window.minuteNum) + 1;
	minuteNum = parseInt(minuteNum)/5;
	
	ampm = parseAmPm(document.getElementById('time2').value);

}
function parseHour(num){
	num = num.substring(0,2);
	return num;
}
function parseMinute(num){
	num = num.substr(3,2);
	return num;
}
function parseAmPm(num){
	num = num.substr(6,2);
	if(num == 'AM'){
		return 1;
	}else{
		return 2;
	}
}
function changePort(){
	var $radios = $('input:radio[name=airportOption]');
	
	$("#portOhare").click(function(){
		$("#imgPortOhare").attr("src","/images/interface/portOhare_selected.png");
		$("#imgPortMidway").attr("src","/images/interface/portMidway_unselected.png");
		$radios.filter('[value=ohare]').attr('checked', true);
		portType = "1";
		getPrice();
	});
		
	$("#portMidway").click(function(){
		$("#imgPortOhare").attr("src","/images/interface/portOhare_unselected.png");
		$("#imgPortMidway").attr("src","/images/interface/portMidway_selected.png");
		$radios.filter('[value=midway]').attr('checked', true);
		portType = "0";
		getPrice();
	});	
	
	/*
	$('#portOhare').mouseenter(function() {
	  $("#imgPortOhare").attr("src","/images/interface/portOhare_hover.png");
	});
	$('#portOhare').mouseleave(function() {
	  $("#imgPortOhare").attr("src","/images/interface/portOhare_selected.png");
	});
	*/
}
function changeDepartArrive(){
	var $radios = $('input:radio[name=tripType]');
	
	$("#depart").click(function(){
		$("#imgDepart").attr("src","/images/interface/depart_selected.png");
		$("#imgArrive").attr("src","/images/interface/arrive_unselected.png");
		$radios.filter('[value=depart]').attr('checked', true);
		departArrive = "SELDEP";
		getPrice();
	});
	$("#arrive").click(function(){
		$("#imgDepart").attr("src","/images/interface/depart_unselected.png");
		$("#imgArrive").attr("src","/images/interface/arrive_selected.png");
		$radios.filter('[value=arrive]').attr('checked', true);
		departArrive = "SELARV";
		getPrice();
	});
}
function Select_Value_Set(SelectName) {
	var Value = window.cityStartLoad;
	var selectobject=document.getElementById(SelectName)
	for (var i=0; i<selectobject.length; i++){
		if(selectobject.options[i].text == Value){
			document.getElementById(SelectName).options[i].selected = true;
		}
	}	
	getPrice();
}
function stylizeDate(){
	var dateNumParse = document.getElementById('date1').value;
	//alert(dateNumParse);
	dateNumParse = dateNumParse.substring(0,dateNumParse.indexOf('/'));
	var monthNameParse = document.getElementById('date1').value;
	monthNameParse = monthNameParse.substring(3,monthNameParse.indexOf('/',3)); 
	var yearNumParse = document.getElementById('date1').value;
	yearNumParse = yearNumParse.substring(6);
	
	$("#monthName").html(months[getMonth(monthNameParse)]);
	$("#dateNum").html(dateNumParse);
	$("#yearNum").html(yearNumParse);
	
	monthNum = 	getMonth(monthNameParse);
	dateNum = getMonth(dateNumParse);
	getPrice();
}
function dateChange(){
	$('#date1').change(function() {
	  stylizeDate();
	});	
}

var months = new Array('JANUARY', 'FEBRUARY', 'MARCH', 'APRIL', 'MAY', 'JUNE', 'JULY', 'AUGUST', 'SEPTEMBER', 'OCTOBER', 'NOVEMBER', 'DECEMBER');

function getMonth(num){
	if(num.substring(0,1) == "0"){
		num = num.substring(1);
	}
	num = parseInt(num);
	num = num - 1;
	return num;
}

function getPrice(){
	var selObj = document.getElementById('chooseCity');
	var selIndex = selObj.selectedIndex;
	var price = selObj.options[selIndex].value;
	var airPortSelected = getCheckedValue(document.bookTrip.airportOption);
	var prices = price.split("|");
	var finalPrice;

	switch(airPortSelected){
		case 'ohare':
			finalPrice = prices[0];
			break;
		case 'midway':
			finalPrice = prices[1];
			break;
		default:
			finalPrice = "NO PRICE";
	}
	cityId = prices[2];
	
	bookTripLink();
	
	$("#priceOut").html(finalPrice);
}
function bookTripLink(){
	linkStr = "http://americancoachlimousine.hudsonltd.net/res?";
	
	//	Departur / Arrive
	linkStr += window.departArrive;
	linkStr += '=ONEWAY';
	//	City
	linkStr += '&selectCity=' + window.cityId;
	//	Port Type
	linkStr += '&selectAir=' + window.portType;
	//	Date: month
	linkStr += '&selectMonth=' + window.monthNum;
	//	Date: Date
	linkStr += '&selectDate=' + window.dateNum;
	//	hour
	linkStr += '&selectHour=' + window.hourNum;
	//	minute
	linkStr += '&selectMinute=' + window.minuteNum;
	//	AM/PM
	linkStr += '&selectAmPm=' + window.ampm;
	
	//	SET
	document.getElementById('vars').value = linkStr;
	
	/*
	SELARV=ONEWAY
	SELDEP=ONEWAY
	selectCity=7
	selectAir=1    	ohare
	selectAir=0		midway
	
	selectMonth=	0-11
	selectDate=		1-31
	selectHour=		1-12
	selectMinute=	0,5,10,...55
	selectAmPm=		AM,PM
	*/
	
}
function PostLogon (type) {
	var f=document.RES_PART2;
	f.LINKNAME.value= type;
	f.action = "https://americancoachlimousine.hudsonltd.net/res";
	f.submit();
}


function getCheckedValue(radioObj) {
	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;
		}
	}
	return "";
}
function createDivTag(divParentName,newDivIdName){
	var divParent = document.getElementById(divParentName);		
	var newdiv = document.createElement('div');
	var divIdName = newDivIdName;

	newdiv.setAttribute('id',divIdName);
	newdiv.innerHTML = '<img src=\"/images/trans.gif\" />';
	divParent.appendChild(newdiv);
}
function rssFeedXml(feedStr){																	//http://sites.google.com/a/orbisdesign.com/bonsai-documentation/advanced/savexmlfeed
	var localFile = "";
	var saveXmlStr = "";

	saveXmlStr += '<img src="';
	saveXmlStr += "/" + baseo2Net + "/applications/transformDoc/saveXmlFeed.aspx?feed=";
	saveXmlStr += feedStr;
	saveXmlStr += '">';
	createDivTag('footer','saveXml');
	document.getElementById('saveXml').innerHTML = saveXmlStr;	
	localFile += "/xml/content/feeds/";
	feedStr = feedStr.substring(feedStr.indexOf('feeds')+5,feedStr.indexOf('?'));
	feedStr = feedStr.replace(/\//g,"");
	localFile += feedStr;
	localFile += ".xml";
	return(localFile);
}


/* ~~~~~~~~~~[ Promo ]~~~~~~~~~~~ */
function promotion(totalPromos){
	if(getCookie('promotion') == null || getCookie('promotion') == undefined){
		SetGetCookie('promotion',1);
	}else{
		if(parseFloat(getCookie('promotion')) >= parseFloat(totalPromos)){
			SetGetCookie('promotion',1);
		}else{
			SetGetCookie('promotion',(parseFloat(getCookie('promotion'))+1));
		}
	}

	var promoShow = 'promo' + getCookie('promotion');
	if(getCookie('americanCoachLimousine.com_coupon') != null){
		var checkAd = document.getElementById(promoShow).innerHTML;
		if(checkAd.indexOf('promo_coupon') != -1){
			//Check cookie indicated they've already got the coupon, so skip this ad
			promotion(totalPromos);
		}else{
			document.getElementById(promoShow).style.display = 'block';	
		}
		
	}else{
		document.getElementById(promoShow).style.display = 'block';
	}
}



/* ~~~~~~~~~~[ coupon ]~~~~~~~~~~~ */
if(getQueryVariable('clearCoupon') == 'true'){
	delete_cookie ('americanCoachLimousine.com_coupon');
}	
function delete_cookie(cookie_name){
	var cookie_date = new Date ( );  // current date & time
	cookie_date.setTime ( cookie_date.getTime() - 1 );
	document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
}
function coupon(){
	var divParent = document.getElementById('col-a');		
	var newdiv = document.createElement('div');
	var divIdName = 'coupondiv';
	var htmlStr = "";

	newdiv.setAttribute('id',divIdName);

	htmlStr += '<div id="startCoupon">';
	htmlStr += '<div style="width:20px;position:absolute;margin:-45px 0 0 280px;"><a href="javascript:void(0);" onMouseDown="closeCoupon();"><img src="/images/trans.gif" width="15" height="15" /></a></div>';
	htmlStr += '<form name="couponSubmit">';
	htmlStr += '<div id="couponFirstName"><input type="text" name="First_Name" /></div>';
	htmlStr += '<div id="couponLastName"><input type="text" name="Last_Name" /></div>';
	htmlStr += '<div id="couponEmail"><input type="text" name="email" /></div>';
	htmlStr += '<div id="couponCheck"><input type="checkbox" name="Notify" value="true" checked="true" /></div>';
	htmlStr += '<div id="couponSubmit"><input type="button" value="Get Coupon" onClick="goCoupon();" /></div>';
	htmlStr += '<div class="couponHidden"><input type="test" name="Print_Coupon_Link" value="" /></div>';
	htmlStr += '<img name="ProcessForm" src="/images/trans.gif" width="1" height="1" border="0" />';
	htmlStr += '</form>';
	htmlStr += '</div>';

	newdiv.innerHTML = htmlStr;
	divParent.appendChild(newdiv);
	
	$import('/javascriptO2/date.js');
	$import('/javascript/requestvars_coupon.js');
	$import('/javascriptO2/FormFunctions.js');
	$import('/javascriptO2/FormProcess.js');
	$import('/javascriptO2/RequestVars.js');
	$import('/JavaScript/FormValidationVars_coupon.js');
}
function closeCoupon(){
	$("div#startCoupon").css("display","none");
}
var who = '';
var couponId;
var promoCode = '';

function goCoupon(){
	var thisMonth = Date.today().getMonth() + 1;
	var thisDate = Date.today().getDate();
	var thisYear = Date.today().getFullYear();
	var thisform = 'couponSubmit';
	var baseSite = "americancoach.orbisdesign.net";
	if(baseURL().indexOf('orbisdesign.net') == -1){
		baseSite = 		"www.americancoachlimousine.com";
	}	
	var printLink = "";	
	
	who = document.couponSubmit.First_Name.value + ' ' + document.couponSubmit.Last_Name.value;
	
	couponId = guid();
	promoCode = 'WS-' + thisMonth + '-' + thisDate + '-' + thisYear; 
	
	printLink = '<a href="' + baseSite + '/Coupon^8176.aspx?who=' + window.who + '&id=' + window.couponId + '&pcode=' + window.promoCode + '">Print Coupon</a>';
	
	document.couponSubmit.Print_Coupon_Link.value = printLink;
	
	var string = "";
	var els = document.forms[thisform].elements;
	for(var no=0;no<els.length;no++){
	    string+="Name: "+els[no].name + ", Value: "+els[no].value + "\n";
	}
	var ValidateVarsArray = ValidateVars.split('|');
	with (thisform){
		for (var i=0;i<ValidateVarsArray.length-1;i++) {
			var ValidateVarPieces = ValidateVarsArray[i].split('=');
			if(typeof(eval('document.' + thisform + '.' + ValidateVarPieces[1])) != 'undefined'){			
				if(eval(ValidateVarPieces[0] + '(document.' + thisform + '.' + ValidateVarPieces[1] + ',"' + ValidateVarPieces[2] + '")')==false){
					eval('document.' + thisform + '.' + ValidateVarPieces[1]).focus();
					return false;
				}else{
					//Everything is good 
					setTimeout('goCouponGetCoupon()',1000);
				}
			}else{
				alert('FormValidationVars ERROR: \r Trying to validate >>>' + ValidateVarPieces[1] + '<<< which is not in this form \r \r Check that the validation var is spelled correctly  \r Or \r Remove the validation check \r Or \r Add the form element that is missing');
				return false;
			}
		}	
	}
	
	GetAllInputs(thisform);
	ProcessInputs();
}
function goCouponGetCoupon(){
	window.location = '/Coupon_8176.aspx?who=' + window.who + '&id=' + window.couponId + '&pcode=' + window.promoCode;
}
function S4() {
   return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
}
function guid() {
   return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
}
function getQueryVariable(variable) {
	var query = window.location.search.substring(1);
	var vars = query.split("&");
	for (var i=0;i<vars.length;i++) {
		var pair = vars[i].split("=");
		if (pair[0] == variable) {
			return pair[1];
		}
  	}
	return "999";
}

