var imageX='plus';	// used for changing the image for the 'more options' link/hidden div

/**
	trim function for strings
*/
String.prototype.trim = function()
{
	return this.replace(/^\s+|\s+$/g, "");

};// end trim()


function checkForExpand()
{
	if (document.forms[0].rentFavCar.checked == true)
	{
		rentFavCarCheckboxClick(document.forms[0].rentFavCar);
		expand('oneClickEmail');
		//alert(document.forms[0].rentFavCar.checked);
	}
}

/*
 * Responsible for any processing required prior to
 * submitting the form, including setting form
 * variables and executing the Omniture tracking.
 *
 * Intended for the non-member section button
 * ("show me the best rates").
 */
function prepareToSubmitNewResForm()
{
	try
	{
		// check the bcd # and trim all spaces
		if (document.forms[0].discountNumber && document.forms[0].discountNumber.value != null)
		{	
			document.forms[0].discountNumber.value = document.forms[0].discountNumber.value.trim();
		}

		formUsed='newResForm';
		// in case the default value isn't create
		// document.forms[0].action.value = 'create';

		//If action is null,use value of actionRef
		document.forms[0].actionRef.value = 'create';

		// set differentReturnLocation value
		if (document.forms[0].chkReturn.checked || (document.forms[0].dolDesc.value != "city, state, country, zip or airport" && document.forms[0].dolDesc.value != ""))
		{
			document.forms[0].differentReturnLocation.value='yes';
		}
		else
		{
			document.forms[0].differentReturnLocation.value='no';
		}

		doTrackingEvent('Get Rates');
				
		document.reservationForm.action = GetFormPostURL("/budgetWeb/home/home.ex?action=create");				
		
		setTimeout('document.reservationForm.submit();', 100);
		
	}
	catch(e)
	{
		//alert("An error occurred: " + e.message + " : " + document.reservationForm.action);
	}
}

/*
 * Responsible for any processing required prior to
 * submitting the form, including setting form
 * variables and executing the Omniture tracking.
 *
 * Intended for members, using the member section
 * button ("rent my fav car").
 */
function submitMemberNewResForm()
{
	try
	{

		// check the bcd # and trim all spaces
		if (document.forms[0].discountNumber)
		{		
		document.forms[0].discountNumber.value = document.forms[0].discountNumber.value.trim();		
		}


		formUsed='memberNewResForm';
		// set the action value to indicate member submit
		// document.forms[0].action.value = 'rapidRes';
		
		// set differentReturnLocation value
		if (document.forms[0].chkReturn.checked|| (document.forms[0].dolDesc.value != "city, state, country, zip or airport" && document.forms[0].dolDesc.value != ""))    
		{
			document.forms[0].differentReturnLocation.value='yes';
		}
		else
		{
			document.forms[0].differentReturnLocation.value='no';
		}

		doTrackingEvent('One Click');				
		
		//document.reservationForm.action = GetFormPostURL("/budgetWeb/reservation/initializer.ex?action=rapidRes");
		document.reservationForm.action = GetFormPostURL("/budgetWeb/home/home.ex?action=create");

		setTimeout('document.reservationForm.submit();', 100);
	}
	catch(e)
	{
		//alert("An error occurred: " + e.message + " : " + document.reservationForm.action);
	}
}

function doResSubmit()
{
	theForm=document.forms[0];

	if (checkForm())
	{
		if(theForm.rentFavCar != null && theForm.rentFavCar.checked)
		{			
			submitMemberNewResForm();
		}
		else
		{			
			prepareToSubmitNewResForm();
		}
	}
}


// 6/13/05 added in response to tracker 487, issues 6b
// validation of empty pick-up and/or return location text fields
function checkEmptyLocations()
{	
    updateHiddenCheckbox();
   
    return true; 

} // end function

/*
 * Responsible for all tracking tasks.
 */
function doTrackingEvent(ev)
{
	if(preventDuplicateProcessing())
	{				
		s.linkTrackVars="events,eVar1,eVar4,eVar6,eVar7,eVar13,eVar17,eVar18,eVar20";
		s.linkTrackEvents="scOpen";

		f = document.forms[0];
		puTime = f.puHourMinute.options[f.puHourMinute.selectedIndex].value;
		doTime = f.doHourMinute.options[f.doHourMinute.selectedIndex].value;
		puDay = f.puDay.options[f.puDay.selectedIndex].value;

		if(puDay < 10)
		{
			puDay = '0'+puDay;
		}

		doDay = f.doDay.options[f.doDay.selectedIndex].value;
		if(doDay < 10)
		{
			doDay = '0'+doDay;
		}

		puMo = f.puMonth.options[f.puMonth.selectedIndex].value;
		puMo++;
		if(puMo < 10)
		{
			puMo = '0'+puMo;
		}

		doMo = f.doMonth.options[f.doMonth.selectedIndex].value;
		doMo++;
		if(doMo < 10)
		{
			doMo = '0'+doMo;
		}

		puTxt = f.puMonth.options[f.puMonth.selectedIndex].text;
		doTxt = f.doMonth.options[f.doMonth.selectedIndex].text;
		puYr = puTxt.substring(puTxt.length-4);
		doYr = doTxt.substring(doTxt.length-4);

		s_linkName='Home page button: '+ev;
		s.eVar1 = f.pulDesc.value;

		if(f.chkReturn.checked)
		{
			s.eVar4='false';
		}
		else
		{
			s.eVar4='true';
		}	

		s.eVar6 = puMo+puDay+puYr+':'+puTime;
		s.eVar7 = doMo+doDay+doYr+':'+doTime;
		s.eVar20 = f.discountNumber.value;

		if(f.customerNumber!=null)
		{
			s.eVar13 = f.customerNumber.value;
		}

		if(f.USResident && f.USResident.checked)
		{
			s.eVar17 = 'United States';
		}
		else
		{
			s.eVar17 = f.countryOfResidence.options[f.countryOfResidence.selectedIndex].text;
		}

		//alert(s.eVar13+': '+s.eVar20+': '+s.eVar17);

		// case 0803-6988
		// track the BCD number
		if (f.discountNumber != null)
		{
			s.prop40 = ((f.discountNumber.value === "" || f.discountNumber.value === "undefined") ? (s.pageName) : (f.discountNumber.value + ":" + s.pageName));
			//alert("s.prop40 = " + s.prop40);
		}
		// end case 0803-6988

		s.eVar18=ev;
		s.events='scOpen';
		
		//alert("pre s.tl call");
		s.tl(this,"o",s_linkName);
		//alert('Omniture values:\ns.eVar1='+s.eVar1+'\ns.eVar4='+s.eVar4+'\ns.eVar6='+s.eVar6+'\ns.eVar7='+s.eVar7+'\ns.eVar13='+s.eVar13+'\ns.eVar18='+s.eVar18);		
				
	}
}

function checkForm()
{		
	return preventDoubleSubmit();
}

/**
 *	Turn off fields laying under hidden layers.
*/
function updateHiddenCheckbox()
{
	// handle one click
	var memberBox = document.getElementById('memberBox');

	if (memberBox && memberBox.style.display == 'none')
	{		
		// clear out checkbox/email
		var rentFavCar = document.getElementById('rentFavCar');
		
		rentFavCar.checked = false;		
	}
}

function checkEmail()
{
    if(formUsed != 'findResForm' )
    {
        if (objectExists('rentFavCar') && document.forms[0].rentFavCar.value == '')
        {
            alert("Whoops! You forgot to enter your e-mail address.");
            return false;
        }
    }

    return preventDoubleSubmit();
}

function checkDates()
{	
    var f = document.forms[0];

    now = new Date();
    puDay = f.puDay.options[f.puDay.selectedIndex].value;
    puMo = f.puMonth.options[f.puMonth.selectedIndex].value;
    puTxt = f.puMonth.options[f.puMonth.selectedIndex].text;
    puYr = puTxt.substring(puTxt.length-4);
    puTime = f.puHourMinute.options[f.puHourMinute.selectedIndex].value;
    puHour = puTime.substring(0,2);
    puMin = puTime.substring(2,4);
    puDate = new Date(puYr,puMo,puDay,puHour,puMin,0,0);

    if (puDate < now)
    {
    	alert("Whoops! Looks like that pick-up time has already passed. Please select a new time. Budget.com is set to U.S. Mountain Time.");
        return false;
    }

    doDay = f.doDay.options[f.doDay.selectedIndex].value;
    doMo = f.doMonth.options[f.doMonth.selectedIndex].value;
    doTxt = f.doMonth.options[f.doMonth.selectedIndex].text;
    doYr = doTxt.substring(doTxt.length-4);
    doTime = f.doHourMinute.options[f.doHourMinute.selectedIndex].value;
    doHour = doTime.substring(0,2);
    doMin = doTime.substring(2,4);
    doDate = new Date(doYr,doMo,doDay,doHour,doMin,0,0);

    if (doDate < now)
    {
	alert("Whoops! Looks like that return time has already passed. Please select a new time. Budget.com is set to U.S. Mountain Time.");
        return false;
    }

    return true;
}

function rememberMeCheckboxClick()
{
	if (document.forms[0].rememberMe.checked == true)
	{
		//s_linkName='Email Subscribe';
		//s.events='event1';
		//s.tl(document.forms[0].rememberMe,'o',s_linkName);
	}
}

function rentFavCarCheckboxClick(checkBox)
{
	if (checkBox.checked == true)
	{
		//alert(document.forms[0].rentFavCar.checked)
		//s_linkName='continue';
		//s.events='event1';
		//s.tl(document.forms[0].rentFavCar,'o',s_linkName);
	}
}

function MM_goToURL()  //v3.0
{
	var i;
	var args = MM_goToURL.arguments; 
	document.MM_returnValue = false;

	for (i=0; i<(args.length-1); i+=2)
	{
		eval(args[i]+".location='"+args[i+1]+"'");
	}
}

function recordAiportLink()
{
	s_objectID = "LandingAirportLink";
}

// Tracker #2415
function updateBestRateButton(checkbox)
{
	var orangebuttonwhitetext = document.getElementById("orangebuttonwhitetext");

	if (orangebuttonwhitetext == null)
	{
		return;
	}

	// Change the button based on the checkbox
	if (checkbox.checked == true)
	{
		// when on
		orangebuttonwhitetext.value = "continue";
	}
	else
	{
		// when off
		orangebuttonwhitetext.value = "continue";
	}
}

function locationEditFocus(editName)
{
	var editBox = document.getElementById(editName);
	
	if (editBox && editBox.value == 'city, state, country, zip or airport')	
	{
		editBox.value = '';
	}
	else
	{
		editBox.select();
	}
}

function handleLocationEdit(editName)
{
	var editBox = document.getElementById(editName);
		
	if (editBox)
	{
		if (editBox.value == 'city, state, country, zip or airport' || editBox.value == '')	
		{
			// return to location
			document.forms[0].chkReturn.checked = false;	
		}
		else
		{
			// one way rental
			document.forms[0].chkReturn.checked = true;	
		}
	}
}

function toggleDisplay(divID, creative)
{
	expand(divID);
	
	if (imageX=='plus') 
	{
		if (creative == '02')
		{
			document.getElementById('arw').src='/budgetWeb/images/common/optimost/home/02/wht_arw_dwn.gif';
		}
		else
		{
			document.getElementById('arw').src='/budgetWeb/images/common/optimost/home/03/blu_arw_dwn.gif';	
		}
			
		imageX='minus';
	}
	else 
	{
		if (creative == '02')
		{
			document.getElementById('arw').src='/budgetWeb/images/common/optimost/home/02/wht_arw_up.gif';
		}
		else
		{
			document.getElementById('arw').src='/budgetWeb/images/common/optimost/home/03/blu_arw_up.gif';	
		}
				
		imageX='plus';
	}
}

function handleDropOffDate()
{	
	if (loadflag)
	{			
		setDropoffDate();
	}
}

function handlDropOffValidation()
{
	if (loadflag)
	{
		validDropoffDate();
		dateset=true;
	}
}

function MM_openBrWindow(theURL,winName,features) 
{ //v2.0
  window.open(theURL,winName,features);
}

function validateEmail(email)
{
	var result = false;
	
	validEmail = new RegExp("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$");

	if(validEmail.test(email.value))
	{
		result = true;
		email.style.background = "#FFFFFF";
	}
	else
	{
		email.style.background = "yellow";
		alert("Email address format is invalid.");
	}
	
	return result;
}

function signUpForHotDeals()
{
	var result = false;

	var emailEdit = document.getElementById('hotDealsEmailAddress');

	if (emailEdit && validateEmail(emailEdit))
	{
		result = true;
			
		window.location.href= GetFormPostURL("/budgetWeb/home/hotdeals.ex?action=subscribe&emailAddress=" + emailEdit.value);					
	}
	
	return result;
}

/**
	this function handles the url protocol (https doesnt work in OLM)
*/
function GetFormPostURL(str)
{	
	var url = '';

	if (location.hostname == '161.178.175.202' || location.hostname == 'e2dsavfev3ws01.cendant.com')
	{
		url = "http://" + location.hostname + (location.port ? (":"+location.port) : "") + str;
	}
	else
	{
		url = "https://" + location.hostname + (location.port ? (":"+location.port) : "") + str;
	}		
	
	return url;
}

var formUsed='';
var imageX='plus';      // used for changing the image for the 'more options' link/hidden div


function killCoverDiv()
{	
	var loadingCoverDiv = document.getElementById('loading');
	if (loadingCoverDiv)
	{
		loadingCoverDiv.style.display='none';
	}
}

// fall thru... if the code below doenst work, the time out will
// kill the cover div
var timeout = setTimeout(function() { killCoverDiv(); }, 30000);	// kill cover div after 30 seconds.

// all necessary js code is now loaded, so remove loading cover div on home page
jQuery(document).ready(function() 
{       
	killCoverDiv();
	
	$('#moreOptionsLink').attr('onclick', '');
	
	$('#moreOptionsLink').click(function()
	{
		$('div#hiddenStuff').slideToggle(1500); 

		if ($(this).html() == 'more options') 
		{ 
			$(this).html('fewer options');
		} 
		else 
		{
			$(this).html('more options');
		}
	});
	
	var onewayRental = getURLParam(location.href, 'oneway');
	if (onewayRental && onewayRental == 'true')
	{
		$('#moreOptionsLink').click();
	}
	
	if ($('#passcode').val() != null && $('#passcode').val() != '' || $('#discountNumber').val() != '' || $('#couponNumber').val() != '' || ($('#returnLocation').val() != 'city, state, country, zip or airport' && $('#returnLocation').val() != ''))
	{
		if ($('div#hiddenStuff').css('display') == 'none')
		{						
			$('#moreOptionsLink').click();
		}
	}
});

