/************************************
* function defaultButton(sourcefield, defaultbuttonid, evt)
* Trigger specififed button if enter key is pressed
*************************************/
function defaultButton(sourcefield, defaultbuttonid, evt){
    evt = (evt) ? evt : event;
    var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode : ((evt.which) ? evt.which : 0));
    // Check for the Enter Key Only
    if(charCode == 13) {
        // Calls the click event on the button of choice
        document.getElementById(defaultbuttonid).click();
        // Cancel the original event so that it doesn't bubble up
        return false;
    }
}

/************************************
* function changeResultView()
* sets styles for search results page
*************************************/
function changeResultView(whichCSS){
	if(whichCSS == "List"){
		setActiveStyleSheet('searchList');
	}
	else if (whichCSS == "Grid2"){
		setActiveStyleSheet('search2');
	}
	else{
		setActiveStyleSheet('search');
	}
}

/************************************
* shows or hides expert bios sections
************************************/	
function showDiv(displayDiv,divElement){
	//sets initial state of all bio sections inner div to display none
	if(displayDiv == "initial"){
		temp = document.getElementById("exploreContent");
		for(i=0;(divSection=temp.getElementsByTagName("div")[i]);i++){
			var theMenu = document.getElementById("exploreContent").getElementsByTagName("div")[i];
		    var listItems = theMenu.className;
		  
			if((theMenu.parentNode.className == "bioSection") && (theMenu.id != '')){
				theMenu.style.display = "none";	
			}
		}
	}
	//displays or hides bio section
	else{
		var showDiv = document.getElementById(displayDiv).style;
		var changeElem = document.getElementById(divElement).style;
		if(showDiv.display == "block"){
			showDiv.display = "none";
			changeElem.backgroundPosition = "bottom left";
		}
		else{
			showDiv.display = "block";
			changeElem.backgroundPosition = "bottom right";
		}
	}
}

/*************************
* function checkKeycode()
* adds key commands for
* main nav buttons
*************************/
var checkID;
function closeMenu(e){
temp=document.getElementById("dropMenuItems");

	if((e=="home") || (e=="airplanes") || (e=="boats") || (e=="cars") || (e=="helis") || (e=="radios") || (e=="more")){
		checkID = e;
		for(i=0;(dropMenuItem=temp.getElementsByTagName("ul")[i]);i++){
		var theMenu = document.getElementById("dropMenuItems").getElementsByTagName("ul")[i];
	    var listItems = theMenu.className;
		if(theMenu.style.display == "block"){
			theMenu.style.display = "none";
		}
		}
		
	}
}
function checkKeycode(e){
	temp=document.getElementById("dropMenuItems");

	if((e=="home") || (e=="airplanes") || (e=="boats") || (e=="cars") || (e=="helis") || (e=="radios") || (e=="more")){
		checkID = e;
		for(i=0;(dropMenuItem=temp.getElementsByTagName("ul")[i]);i++){
		var theMenu = document.getElementById("dropMenuItems").getElementsByTagName("ul")[i];
	    var listItems = theMenu.className;
		if(theMenu.style.display == "block"){
			theMenu.style.display = "none";
		}
		}
		
	}
	// NEED TO FIX IE 6 ISSUE
	else{
		if (typeof document.body.style.maxHeight != "undefined") {}
		else { // IE6, older browsers
			for(i=0;(dropMenuItem=temp.getElementsByTagName("ul")[i]);i++){
				var theMenu = document.getElementById("dropMenuItems").getElementsByTagName("ul")[i];
				theMenu.style.top = "0px";
		   	}
		}
	}
	
	document.onkeydown = checkKeycode;
	var keycode;
	
	if(window.event){ // IE
		
		if (typeof document.body.style.maxHeight != "undefined") {	
			keycode = window.event.keyCode;
		} 
		else { // IE6, older browsers
			keycode = event.keyCode;
		}
	}
	else if(e){

		keycode = e.which;
	}
	
	if((keycode != 13) || (keycode != 9)){
		var tempID;
		
		if(checkID == "airplanes"){
			tempID = "menu1two";
		}
		if(checkID == "boats"){
			tempID = "menu1four";
		}
		if(checkID == "cars"){
			tempID = "menu1three";
		}
		if(checkID == "helis"){
			tempID = "menu1five";
		}
		if(checkID == "radios"){
			tempID = "menu1six";
		}
		if(checkID == "more"){
			tempID = "menu1seven";
		}
		
		document.getElementById(tempID).style.display = "block";
		document.getElementById(tempID).style.position = "absolute";
		if (typeof document.body.style.maxHeight == "undefined") {	
			document.getElementById(tempID).style.marginTop = "15px";
		}
		
		for(i=0;(dropMenuItem=temp.getElementsByTagName("a")[i]);i++){
			var theMenu = document.getElementById(tempID).getElementsByTagName("a")[i];
		    var listItems = theMenu.style;
		    listItems.width = "125px";
		    listItems.marginBottom = "0px";
		    listItems.padding = "0px";
		    listItems.height = "auto";
		    listItems.lineHeight = "15px";
	    }
	
	}
}



/* Cookies */
function createCookie(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 readCookie(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);
}


/* COOKIES */
var Cookies = {
	init: function () {
		var allCookies = document.cookie.split('; ');
		for (var i=0;i<allCookies.length;i++) {
			var cookiePair = allCookies[i].split('=');
			this[cookiePair[0]] = cookiePair[1];
		}
	},
	create: function (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=/";
		this[name] = value;
	},
	erase: function (name) {
		this.create(name,'',-1);
		this[name] = undefined;
	}
};
//alert(document.cookie);
Cookies.init();


function init() {
// SPS 20071030 Rem out all of the init() function - just in case something still tries to call it. 

//	var x = Cookies['displayWhichTab'];
//	
//	var homeTab = document.getElementById("homeTab").style;
//	var rcTab = document.getElementById("RCTab").style;
//	var trainsTab = document.getElementById("trainsTab").style;
//	var playTab = document.getElementById("playTab").style;
//
//	if(x == "RC"){
//		homeTab.backgroundPosition = "top left";
//		rcTab.backgroundPosition = "bottom left";
//		rcTab.marginLeft = "-5px";
//		rcTab.zIndex = "2";
//		trainsTab.marginLeft = "-5px";
//		trainsTab.backgroundPosition = "top left";
//		playTab.backgroundPosition = "top left";		
//		//ajaxpage('/Content/Header/RCNav.html', 'navContent');
//		//ajaxpage('/Content/Header/header.html', 'navContent2');
//	}
//	else if (x == "Trains"){
//		homeTab.backgroundPosition = "top left";
//		rcTab.backgroundPosition = "top left";
//		trainsTab.backgroundPosition = "bottom left";
//		trainsTab.marginLeft = "-5px";
//		trainsTab.zIndex = "2";
//		playTab.marginLeft = "-5px";
//		playTab.backgroundPosition = "top left";
//		//ajaxpage('/Content/Header/trainsNav.html', 'navContent');
//	}
//	else if(x == "Play"){
//		homeTab.backgroundPosition = "top left";
//		rcTab.backgroundPosition = "top left";
//		trainsTab.backgroundPosition = "top left";
//		playTab.backgroundPosition = "bottom left";
//		playTab.marginLeft = "-5px";
//		playTab.zIndex = "2";
//		//ajaxpage('/Content/Header/playNav.html', 'navContent');
//	}
//	else{
//		homeTab.backgroundPosition = "bottom left";
//		rcTab.marginLeft = "-5px";
//		homeTab.zIndex = "2";
//		homeTab.marginLeft = "-3px";
//		rcTab.backgroundPosition = "top left";
//		trainsTab.backgroundPosition = "top left";
//		playTab.backgroundPosition = "top left";
//		//ajaxpage('/Content/Header/mainNav.html', 'navContent');
//	}
	
}

function initExplore(selectTab) {
//	var x = Cookies['displayExplore'];
	var x = selectTab;
	//document.getElementById("cookie").innerHTML = x;
	//alert(x);
	var mainTab = document.getElementById("explMainTab").style;
	var flyTab = document.getElementById("flyTab").style;
	var driveTab = document.getElementById("driveTab").style;
	var boatTab = document.getElementById("boatTab").style;

	if(x == "Fly"){
		mainTab.backgroundPosition = "top left";
		flyTab.backgroundPosition = "bottom left";
		driveTab.backgroundPosition = "top left";
		boatTab.backgroundPosition = "top left";		
		ajaxpage('../Content/ExploreRC/Fly.html', 'explContent');
	}
	else if (x == "Drive"){
		mainTab.backgroundPosition = "top left";
		flyTab.backgroundPosition = "top left";
		driveTab.backgroundPosition = "bottom left";
		boatTab.backgroundPosition = "top left";
		ajaxpage('../Content/ExploreRC/Drive.html', 'explContent');
	}
	else if(x == "Boat"){
		mainTab.backgroundPosition = "top left";
		flyTab.backgroundPosition = "top left";
		driveTab.backgroundPosition = "top left";
		boatTab.backgroundPosition = "bottom left";
		ajaxpage('../Content/ExploreRC/Boat.html', 'explContent');
	}
	else{
		mainTab.backgroundPosition = "bottom left";
		flyTab.backgroundPosition = "top left";
		driveTab.backgroundPosition = "top left";
		boatTab.backgroundPosition = "top left";
		ajaxpage('../Content/ExploreRC/Main.html', 'explContent');
	}
}


var bustcachevar=1; //bust potential caching of external pages after initial request? (1=yes, 0=no)
var loadedobjects="";
var rootdomain="http://"+window.location.hostname;
var bustcacheparameter="";

function ajaxpage(url, containerid){

	var page_request = false;
	if (window.XMLHttpRequest){ // if Mozilla, Safari etc
		page_request = new XMLHttpRequest();
	}
	else if (window.ActiveXObject){ // if IE
		try {
			page_request = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e){
			try{
				page_request = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e){}
		}
	}
	else{
		return false;
	}
	page_request.onreadystatechange=function(){
		loadpage(page_request, containerid);
	}
	if (bustcachevar){ //if bust caching of external page
		bustcacheparameter=(url.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime();
		page_request.open('GET', url+bustcacheparameter, true);
		page_request.send(null);
	}

	function loadpage(page_request, containerid){
		if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1)){
			//alert(containerid);
			//this line is causing IE6 to err
			if (document.getElementById(containerid)) {
				document.getElementById(containerid).innerHTML=page_request.responseText;
			}
		}
	}
}


function loadurl(dest,pageLocation,image) {
 toggleGallerySection (image, pageLocation);
 try {

 // Moz supports XMLHttpRequest. IE uses ActiveX.
 // browser detction is bad. object detection works for any browser
 xmlhttp = window.XMLHttpRequest?new XMLHttpRequest():
new ActiveXObject("Microsoft.XMLHTTP");

 }
 catch (e) {
 // browser doesn't support ajax. handle however you want
 }

 // the xmlhttp object triggers an event everytime the status changes
 // triggered() function handles the events
 //xmlhttp.onreadystatechange = triggered;
 xmlhttp.onreadystatechange = function () { triggered(pageLocation); }

 // open takes in the HTTP method and url.
 xmlhttp.open("GET", dest);

 // send the request. if this is a POST request we would have
 // sent post variables: send("name=aleem&amp;gender=male)
 // Moz is fine with just send(); but
 // IE expects a value here, hence we do send(null);
xmlhttp.send(null);

}

function triggered(pageLocation) {
// if the readyState code is 4 (Completed)
// and http status is 200 (OK) we go ahead and get the responseText
// other readyState codes:
 // 0=Uninitialised 1=Loading 2=Loaded 3=Interactive
 if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) {

 // xmlhttp.responseText object contains the response.
 document.getElementById(pageLocation).innerHTML =
 xmlhttp.responseText;
 }

}


