//Determines what browser and os the user is using
function browserCSSDetection() {
	 // see: http://rafael.adm.br/css_browser_selector/
	 var ua = navigator.userAgent.toLowerCase();
	 var is = function(t){ return ua.indexOf(t) != -1; };
	 var b = (!(/opera|webtv/i.test(ua))&&/msie (\d)/.test(ua)) ?
	             ('ie ie'+RegExp.$1) :
	               is('gecko/') ? 'gecko' :
	               is('opera/9') ? 'opera opera9' :
	               /opera (\d)/.test(ua) ? 'opera opera'+RegExp.$1 :
	               is('konqueror')?'konqueror' :
	               is('applewebkit/') ? 'webkit safari':
	               is('mozilla/')?'gecko':'';
	 // see: http://www.mozilla.org/docs/web-developer/sniffer/browser_type.html
	 var os = (is('x11')||is('linux'))?'linux':is('mac')?'mac':is('win')?'win':'';
	 var css = {browser:b,os:os};
	 return css;
}

function openMenuItem(menuID) {
	Effect.BlindDown(menuID, {duration: 0.5, afterFinish: function() {$(menuID).style.display = "block";}});
}

function closeMenuItem(menuID) {
	Effect.BlindUp(menuID, {duration: 0.3});
}

function showReturnForm() {
	Effect.BlindDown("return_form", {duration: 0.8});
}

function showRequestForm() {
	Effect.BlindDown("request_form", {duration: 0.5});
}

function countryChanged() {
	if($('country').value == "canada") {
		
		$('state_province_label').style.display = "none";
		$('state_label').style.display = "none";
		$('province_label').style.display = "inline";
		
		$('state_province').style.display = "none";
		$('state').style.display = "none";
		$('province').style.display = "inline";
		
		
		$('zip_postal_code_label').style.display = "none";
		$('zip_code_label').style.display = "none";
		$('postal_code_label').style.display = "inline";
		
		$('zip_postal_code').style.display = "none";
		$('zip_code').style.display = "none";
		$('postal_code_front').style.display = "inline";
		$('postal_code_back').style.display = "inline";
				
	} else if($('country').value == "united_states") {
		
		$('state_province_label').style.display = "none";
		$('state_label').style.display = "inline";
		$('province_label').style.display = "none";
		
		$('state_province').style.display = "none";
		$('state').style.display = "inline";
		$('province').style.display = "none";

		
		$('zip_postal_code_label').style.display = "none";
		$('zip_code_label').style.display = "inline";
		$('postal_code_label').style.display = "none";
		
		$('zip_postal_code').style.display = "none";
		$('zip_code').style.display = "inline";
		$('postal_code_front').style.display = "none";
		$('postal_code_back').style.display = "none";	
	}
}

/*
 * Validates the Return Merchandise Authorization form to make sure all the required fields are filled in.
 */
function processRMA() {
	var errorString = "";
	var missingFields = new Array();
	
	if($('contact_name').value == "") missingFields.push('contact_name');
	else $('contact_name_label').style.color = "#666";
	
	if($('organization').value == "") missingFields.push('organization');
	else $('organization_label').style.color = "#666";
	
	if($('country').value == "empty") missingFields.push('country');
	else {
		$('country_label').style.color = "#666";
		if($('country').value == "canada") {
			if($('postal_code_front').value == "" || $('postal_code_back').value == "") {
				missingFields.push('postal_code');
			} else $('postal_code_label').style.color = "#666";
		} else if($('country').value == "united_states") {
			if($('zip_code').value == "") {
				missingFields.push('zip_code');
			} else $('zip_code_label').style.color = "#666";
		}
	}

	if($('address').value == "") missingFields.push('address');
	else $('address_label').style.color = "#666";
	
	if($('city').value == "") missingFields.push('city');
	else $('city_label').style.color = "#666";

	if($('work_phone').value == "") missingFields.push('work_phone');
	else $('work_phone_label').style.color = "#666";

	if($('email').value == "") missingFields.push('email');
	else $('email_label').style.color = "#666";
	
	if($('serial_number').value == "") missingFields.push('serial_number');
	else $('serial_number_label').style.color = "#666";
	
	if($('parts').value == "") missingFields.push('parts');
	else $('parts_label').style.color = "#666";
	
	if($('product').value == "empty") missingFields.push('product');
	else $('product_label').style.color = "#666";
	
	if($('description').value == "") missingFields.push('description');
	else $('description_label').style.color = "#666";
	
	if(!$('agree').checked) missingFields.push('agree');
	else $('agree_label').style.color = "#666";
	
	for(var i = 0; i < missingFields.length; i++) {
		fieldID = missingFields[i];
		$(fieldID + "_label").style.color = "red";
		if(errorString != "") errorString += ", ";
		errorString += fieldID.replace("_", " ");
	}
	
	if(missingFields.length == 0) {
		$('error_div').innerHTML = "&nbsp;";
		return 'validated';
	} else {
		$('error_div').innerHTML = "The following fields are mandatory: " + errorString;
		return 'invalid';
	}
}

/*
 * Submits the Return Merchandise Authorization form to custer-service.php via ajax
 */
function submitRMA() {
	if(processRMA() == 'validated') {
		
		Effect.BlindUp("return_form", {
			duration: 0.5,
			afterFinish: function() {
				showPopup();
			}
		});
		
		var stateProvince;
		var zipPostalCode;
		var country;
		
		if($('country').value == "united_states") {
			country = "USA";
			stateProvince = $('state').value;
			zipPostalCode = $('zip_code').value;
		} else if($('country').value == "canada") {
			country = "Canada";
			stateProvince = $('province').value;
			zipPostalCode = $('postal_code_front').value + "-" + $('postal_code_back').value;
		} 
		
		new Ajax.Request('customer-service.php', {
			method: 'post',
			parameters: {
				mode: 'submit_rma',
				contact_name: $('contact_name').value,
				organization: $('organization').value,
				country: country,
				address: $('address').value,
				city: $('city').value,
				state_province: stateProvince,
				zip_postal_code: zipPostalCode,
				work_phone: $('work_phone').value,
				fax_number: $('fax_number').value,
				email: $('email').value,
				serial_number: $('serial_number').value,
				parts: $('parts').value,
				unit_number: $('unit_number').value,
				product: $('product').value,
				description: $('description').value
			},
			onComplete: function(transport) {
				// alert(transport.responseText);
			}
		});
	}
}

/*
 * Resets the Return Merchandise Authorization form
 */
function resetRMA() {
	var myTextboxes = new Array('contact_name', 'organization', 'address', 'city', 'state', 'province', 'zip_code', 'postal_code_front', 'postal_code_back', 'work_phone', 'fax_number', 'email', 'serial_number', 'parts', 'unit_number', 'description');
	for(var i = 0; i < myTextboxes.length; i++) {
		$(myTextboxes[i]).value = '';
	}
	
	$('country').selectedIndex = 0;
	$('product').selectedIndex = 0;
	
	$('state_province_label').style.display = "inline";
	$('state_label').style.display = "none";
	$('province_label').style.display = "none";
	
	$('state_province').style.display = "inline";
	$('state').style.display = "none";
	$('province').style.display = "none";
	
	
	$('zip_postal_code_label').style.display = "inline";
	$('zip_code_label').style.display = "none";
	$('postal_code_label').style.display = "none";
	
	$('zip_postal_code').style.display = "inline";
	$('zip_code').style.display = "none";
	$('postal_code_front').style.display = "none";
	$('postal_code_back').style.display = "none";
}

/*
 * Shows a generic popup. The content to be displayed in this popup must be set using javascript
 * before this function is called
 */
function showPopup() {
	var windowWidthCenter = 434; 
	var windowHeightCenter = Math.round(eval(getCurrentWindowHeight() / 2));
	
	var messageWidthOffset = windowWidthCenter - Math.round(eval($('popup_div').getWidth() / 2));
	var messageHeightOffset = windowHeightCenter - Math.round(eval($('popup_div').getHeight() / 2));

	messageWidthOffset += $("page-wrapper").offsetLeft;

	$('popup_div').style.left = messageWidthOffset + "px";
	$('popup_div').style.top = messageHeightOffset + "px";
	
	Effect.Appear('popup_div', {
		afterFinish: function() {
			setTimeout("Effect.Fade('popup_div', {duration: 3.0})", 4000);
		}
	});
}

/*
 * Returns the current viewable height of the browser window
 */
function getCurrentWindowHeight()
{
	var myHeight = document.documentElement.clientHeight + document.documentElement.scrollTop;
	return myHeight;
}

/*
 * Validates the Request For Proposal form to make sure that all required fields are filled in.
 */
function processRFP() {
	var errorString = "";
	var missingFields = new Array();
	
	if($('contact_name').value == "") missingFields.push('contact_name');
	else $('contact_name_label').style.color = "#666";
	
	if($('organization').value == "") missingFields.push('organization');
	else $('organization_label').style.color = "#666";
	
	if($('work_phone').value == "") missingFields.push('work_phone');
	else $('work_phone_label').style.color = "#666";
	
	if($('email').value == "") missingFields.push('email');
	else $('email_label').style.color = "#666";
	
	if($('buses').value == "") missingFields.push('buses');
	else $('buses_label').style.color = "#666";
	
	if($('description').value == "") missingFields.push('description');
	else $('description_label').style.color = "#666";
	
	for(var i = 0; i < missingFields.length; i++) {
		fieldID = missingFields[i];
		$(fieldID + "_label").style.color = "red";
		if(errorString != "") errorString += ", ";
		errorString += fieldID.replace("_", " ");
	}	
	
	if(missingFields.length == 0) {
		$('error_div').innerHTML = "&nbsp;";
		return 'validated';
	} else {
		$('error_div').innerHTML = "The following fields are mandatory: " + errorString;
		return 'invalid';
	}
}

/*
 * Submits the Request For Proposal form.
 */
function submitRFP() {

	$('popup_content').innerHTML = "Your request for an RFP form has been made. <br /><br />";
	$('popup_content').innerHTML += "One of our representatives will contact you in the next 48 hours." 

	if(processRFP() == 'validated') {
		Effect.BlindUp("request_form", {
			duration: 0.5,
			afterFinish: function() {
				showPopup();
			}
		});
		
		new Ajax.Request('contact-us.php', {
			method: 'post',
			parameters: {
				mode: 'submit_rfp',
				contact_name: $('contact_name').value,
				organization: $('organization').value,
				work_phone: $('work_phone').value,
				email: $('email').value,
				expected_due_date: $('expected_due_date').value,
				buses: $('buses').value,
				description: $('description').value
			}
		});
	}
}

/*
 * Resets the Request For Proposal form
 */
function resetRFP() {
	var myTextboxes = new Array('contact_name', 'organization', 'work_phone', 'email', 'expected_due_date', 'buses', 'description');
	for(var i = 0; i < myTextboxes.length; i++) {
		$(myTextboxes[i]).value = '';
	}
}

/*
 * Opens in the information box that appears when you hover over one of the items
 * on the Smart Solutions or Fare Media Distribution page
 * @param hoverItem: The image that triggers the slide down when hovered over
 * @param slideDownID: The id of the hidden item that contains the info
 */
function fareMediaInfoSlideDown(hoverItem, slideDownID) {
	var location = findPos(hoverItem);
	var x = location[0];
	
	if(slideDownID == "barcodes_and_receipts_info") x = x + 8;
	if(slideDownID == "fftvm_info" || slideDownID == "ctvm_info" || slideDownID == "affiliates_info") {
		x = x - ((174 - hoverItem.offsetWidth) / 2 ); 
	}
	if(slideDownID == "on_the_bus_info") x = x - (174 - hoverItem.offsetWidth);
	
	var y = location[1] + hoverItem.offsetHeight + 5;
	
	$(slideDownID).style.left = x + "px";
	$(slideDownID).style.top = y + "px";

	Effect.BlindDown(slideDownID, {duration: 0.5});
}

// finds the position of an element relative to the page
// and not the parent element
function findPos(obj) 
{
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	return [curleft,curtop];
}

function showInfoBubble(objOwnerID) {
	var ownerPosition = findPos($(objOwnerID));
	var x = ownerPosition[0] - 70;
	var y = ownerPosition[1] - 180;
	
	switch(objOwnerID) {
		case "transit_office_hover":
			$('info_bubble_contents').innerHTML = "The Attended Reload Station POS device can be used at any transit office location with a connection to the Transit Agency's network.  The POS device can be used to sell SmartCards, SmartTickets, Magnetic Cards and Magnetic Tickets.  In addition to selling new cards, existing SmartCards and Smart Tickets can be reloaded.";
			break;
		case "tvm_hover":
			$('info_bubble_contents').innerHTML = "The Ticket Vending Machine can be used to sell barcoded tickets, tokens, and pre-programmed SmartCards and Smart Tickets as well as reload existing SmartCards and Smart Tickets.  The Ticket Vending Machine can be placed either outdoors or indoors.";
			break;
		case "affiliates_hover":
			$('info_bubble_contents').innerHTML = "Affiliates such as local stores and businesses can be used to sell tokens, pre-programmed SmartCards, Smart Tickets, Magnetic Cards and Magnetic Tickets.  The SmartCards and Smart Tickets can be personalized using the Attended Reload Station POS devices at any Transit Office.  In addition, educational institutions already using SmartCards or Magnetic Cards as student ID's can be easily integrated into the Voyager Farebox and the Stand-Alone SmartCard solution.";
			break;
		case "bus_hover":
			$('info_bubble_contents').innerHTML = "The Voyager Farebox dispenses barcoded tickets for use as transfers and day passes.  In addition, the Voyager Farebox can optionally be used as an Attended Reload Station to allow passengers to reload SmartCards and Smart Tickets.";
			break;
		case "smartcards_hover":
			$('info_bubble_contents').innerHTML = "SmartCards offer a reloadable solution that is cost effective over the long term.  SmartCards use embedded Radio Frequency Identification (RFID) technology that allows the data on the card to be written, erased, and re-written multiple times.  SmartCards can be read and written via the Attended Reload Station POS, the Ticket Vending Machine and the Voyager Farebox.  This provides flexible options for both the transit provider and the passenger.";
			break;
		case "smart_tickets_hover":
			$('info_bubble_contents').innerHTML = "SmartTickets offer a reloadable solution that is cost effective over the short term.  SmartTickets use embedded Radio Frequency Identification (RFID) technology that allows the data on the ticket to be written, erased, and re-written multiple times.  SmartTickets can be read and written via the Attended Reload Station POS, the Ticket Vending Machine and the Voyager Farebox.  This provides flexible options for both the transit provider and the passenger.";
			break; 
		case "magnetic_tickets_hover":
			$('info_bubble_contents').innerHTML = "Magnetic Tickets offer a reloadable solution that is cost effective over the long term.  Magnetic Tickets use a programmable magnetic stripe which allows the data on the ticket to be written, erased and re-written multiple times via the Attended Reload Station POS.";
			break;
		case "magnetic_cards_hover":
			$('info_bubble_contents').innerHTML = "Magnetic Cards offer a reloadable solution that is cost effective over the long term.  Magnetic Cards use a programmable magnetic stripe which allows the data on the card to be written, erased and re-written multiple times via the Attended Reload Station POS.";
			break;
		case "credit_cards_hover":
			$('info_bubble_contents').innerHTML = "The Voyager Farebox and Ticket Vending Machine can accept credit cards as a payment option.  The Ticket Vending Machine will process credit cards live, while the Voyager Farebox will process credit cards in off-line mode at the end of each day.  Both the Ticket Vending Machine and Voyager Farebox utilize blacklists to reduce the risk to the Transit Agency.";
			break;
		case "tokens_hover":
			$('info_bubble_contents').innerHTML = "Tokens are a cost-effective, reusable payment method that is ideal for single-rides and can be easily distributed by businesses and educational institutions.  The Voyager Farebox can be programmed to recognize a wide assortment of tokens.";
			break;
		case "barcoded_tickets_hover":
			$('info_bubble_contents').innerHTML = "The Voyager Farebox and Ticket Vending Machine can print time-sensitive barcoded tickets for use as transfers, single-ride tickets or day passes.  The barcoded tickets are generated from the Ticket Vending Machine and Voyager Farebox using standard card stock and are printed with the barcode at the time they are generated.  Transfers, single-ride tickets and day passes are all dynamically generated when required and are therefore properly encoded with the expiration date and time when printed.";
			break;
	}
	$('info_bubble').style.left = x + "px";
	$('info_bubble').style.top = y + "px";
	
	$('info_bubble').style.display = "block";
}

function hideInfoBubble() {
	$('info_bubble').style.display = "none";
}