// Returns the computed style attribute for the given element.
    function getStyle( elem, prop ) {
         if (elem.ownerDocument) return elem.ownerDocument.defaultView.getComputedStyle( elem, '' ).getPropertyValue( prop );
         else return 0;
    }

function hover(el) {
	document.getElementById(el.id+"a").childNodes[0].src = "images/hover.png";
	el.style.cursor = "pointer";
}
function out(el) {
	document.getElementById(el.id+"a").childNodes[0].src = "images/normal.png";
}
function down(el) {
	document.getElementById(el.id+"a").childNodes[0].src = "images/push.png";
}
function click(el) {
	switch(el.id) {
		case "m1":
			window.location="index.php";break;
		case "m2":
			window.location="culture.php";break;
		case "m3":
			window.location="annex/index.php";break;
		case "m4":
			window.location="community.php";break;
		case "m5":
			window.location="aboutus.php";break;
		case "m6":
			window.location="support.php";break;
		default:
			alert("error");
	}
}
function cHover(el) {
	switch(el.id) {
		case "event1c":
			el.id = "event1cb";
			break;
		case "event2c":
			el.id = "event2cb";
			break;
		default:
			break;	
		
	}
}
function cOut(el) {
	switch(el.id) {
		case "event1cb":
			el.id = "event1c";break;
		case "event2cb":
			el.id = "event2c";break;
		default:
			break;
	}
}	
function eHover(el) {
		switch(el.id) {
		case "event1":
			el.id = "event1b";
			el.style.height = "110px";
			break;
		case "event2":
			el.id = "event2b";
			el.style.height = "100px";
			break;
		case "event3":
			el.id = "event3b";
			el.style.height = "100px";
			break;
		case "event4":
			el.id = "event4b";
			el.style.height = "110px";
			break;	
		default:
		break;
	}
}
function eOut(el) {
	switch(el.id) {
		case "event1b":
			el.id = "event1";break;
		case "event2b":
			el.id = "event2";break;
		case "event3b":
			el.id = "event3";break;
		case "event4b":
			el.id = "event4";break;	
		default:
		break;
	}
}

function pressMenu(cl) {
	document.getElementById("menuDiv").setAttribute("class",cl);
	doBlueBars(); // finds the blue bars div and creates it
	thumbs(); // adds events for thumbnail viewing
	doVideoIcon(); // ads video icon
}


	function db(key) {
	var ajaxRequest;
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			res = ajaxRequest.responseText;
		}
	}	
	ajaxRequest.open("GET", "verifyInstall.php?key="+key, true);
	ajaxRequest.send(null); 
}
function format(amount) {
	var i = parseFloat(amount);
	if(isNaN(i)) { i = 0.00; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + s;
	return s;
}

	function install (aEvent)
	{
	  var params = {
		"Annex": { URL: aEvent.target.href,
				 IconURL: aEvent.target.getAttribute("iconURL"),
				 Hash: aEvent.target.getAttribute("hash"),
				 toString: function () { return this.URL; }
		}
	  };
	  InstallTrigger.install(params);
		mv();
	  return false;
	}

function removeClass(objElement, strClass) {
	// if there is a class
	if ( objElement && objElement.className ) {

		// the classes are just a space separated list, so first get the list
		var arrList = objElement.className.split(' ');

		// get uppercase class for comparison purposes
		var strClassUpper = strClass.toUpperCase();

		// find all instances and remove them
		for ( var i = 0; i < arrList.length; i++ ) {

			// if class found
			if ( arrList[i].toUpperCase() == strClassUpper ) {

				// remove array item
				arrList.splice(i, 1);

				// decrement loop counter as we have adjusted the array's contents
				i--;
			}
		}
	// assign modified class name attribute
	objElement.className = arrList.join(' ');

	}
}

function addClass(objElement, strClass) {

	// if there is a class
	if ( objElement && objElement.className ) {

		// the classes are just a space separated list, so first get the list
		var arrList = objElement.className.split(' ');

		// add the new class to end of list
		arrList[arrList.length] = strClass;

		// add the new class to beginning of list
		//arrList.splice(0, 0, strClass);

		// assign modified class name attribute
		objElement.className = arrList.join(' ');
	
	// if there was no class
	} else {

	// assign modified class name attribute      
	objElement.className = strClass;

	}
}

function hasClass(objElement, strClass) {

	// if there is a class
	if (objElement && objElement.className ) {
	
		// the classes are just a space separated list, so first get the list
		var arrList = objElement.className.split(' ');

		// get uppercase class for comparison purposes
		var strClassUpper = strClass.toUpperCase();

		// find all instances and remove them
		for ( var i = 0; i < arrList.length; i++ ) {

			// if class found
			if ( arrList[i].toUpperCase() == strClassUpper ) {

				// we found it
				return true;
			}
		}
	}

	// if we got here then the class name is not there
	return false;
}

function getWinWidth() {
isNav = (document.all) ? false : true;
isIE = (document.all) ? true : false;
if (isNav && !isIE)
return(window.innerWidth);
else if (isIE && !isNav)
return(document.body.clientWidth);
else
return(100);
}

function validateEmail(val) {
		var ajaxRequest;
		try{
			// Opera 8.0+, Firefox, Safari
			ajaxRequest = new XMLHttpRequest();
		} catch (e){
			// Internet Explorer Browsers
			try{
				ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try{
					ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e){
					// Something went wrong
					alert("Your browser broke!");
					return false;
				}
			}
		}
		ajaxRequest.onreadystatechange = function(){
			if(ajaxRequest.readyState == 4){
				res = ajaxRequest.responseText;
				if (res) {
					document.getElementById("emailError").innerHTML = "Email already exists";
				} else {
					document.getElementById("emailError").innerHTML = "";
				}
			}
		}	
		ajaxRequest.open("GET", "../store/verifyEmail.php?email="+val+"&noType=1", true);
		ajaxRequest.send(null); 
	}
	
function mv() {
	var black = document.getElementById("black");
  	black.style.height = document.body.clientHeight+"px"
  	black.style.width = getWinWidth() - 15+"px";
  	black.style.display = "block";
  	
	var pop = document.getElementById("pop");
  	pop.style.left = getWinWidth() - 330+"px";
  	pop.style.display = "block";
  	
}  
function closeit() {
	var black = document.getElementById("black");
  	black.style.display = "none";
	var pop = document.getElementById("pop");
  	pop.style.display = "none";
}

function doBlueBars() {
	$(document).ready(function() {
		$(".blueBar").each(function() {
			$(this).html("\
				<table cellpadding='0px' cellspacing='0px' id='eventBar'>\
					<tr>\
						<td id='lt'>\
							<br>\
						</td>\
						<td id='blueBar' style='width:"+$(this).attr("width")+"px'>\
							<div id='blueHead'>\
								"+$(this).attr("value")+"\
							</div>\
						</td>\
						<td id='rt'>\
							<br>\
						</td>\
					</tr>\
				</table><div id='smpace'></div>");
		});
	});
}	
/*
	$(document).ready(function() {
		var a = $(".blueBar").get();
		for(var i=0;i<$(".blueBar").length;i++) {
			a[i].innerHTML = "\
			<table cellpadding='0px' cellspacing='0px' id='eventBar'>\
				<tr>\
					<td id='lt'>\
						<br>\
					</td>\
					<td id='blueBar' style='width:"+a[i].getAttribute("width")+"px'>\
						<div id='blueHead'>\
							"+a[i].getAttribute("value")+"\
						</div>\
					</td>\
					<td id='rt'>\
						<br>\
					</td>\
				</tr>\
			</table><div id='smpace'></div>"
		}
	});
}
*/

// just add the attribute video for this to work
function doVideoIcon() {
	$(document).ready(function() {
		// create the image
		var jimg = $("<img></img>");
		// add the icon
		jimg.attr("src","/images/VideoIconSm.png");
		jimg.attr("alt","Play "+$("[video]").attr("video"));
		// style it
		jimg.css({"margin-right":"5px", "cursor":"pointer"});
		// store the current text color that is next to the image
		jimg.attr("color",$("[video]").css("color"));
		// create a reference so we can bind events to it
		jimg.addClass("jimg");
		// add the image to the page
		$("[video]").prepend(jimg);
		// add the click
		$(".jimg").click(function() {
			window.location="/help/#"+$(this).parent().attr("video");
		});
		// add the hover and out states
		$(".jimg").hover(
      		function () {
        		$(this).parent().css("color","green");
      		}, 
      		function () {
       			 $(this).parent().css("color",$(this).attr("color"));
      		}
   		 );
	});
}

function thumbs() {
	$(document).ready(function() {
		$(".thumb").click(function() {
			
			var img = document.createElement("IMG");
			img.src = $(this).attr("src");
			img.style.position = "fixed";
			img.style.top  = "0px";
			img.id = "imgPrev";
			document.body.appendChild(img);
			img.style.left = window.innerWidth/2 - img.clientWidth/2+"px";
			img.style.top = window.innerHeight/2 - img.clientHeight/2+"px";
			
			var text = document.createElement("DIV");
			text.innerHTML = "Click to close";
			text.style.backgroundColor = "#666";
			text.style.color = "white";
			text.style.fontSize = "16px";
			text.style.padding = "7px";
			text.style.fontFamily = "arial";
			text.style.position = "fixed";
			text.id = "textPrev";
			document.body.appendChild(text);
		
			text.style.top  = parseInt(img.style.top) + img.clientHeight + "px";
			text.style.left = parseInt(img.style.left) + img.clientWidth/2 - text.clientWidth/2 + "px";
			
			$(document).mouseup(function() {
				$("#textPrev").remove();
				$("#imgPrev").remove();
			});
		});
	});	
}

function chkCookies() {
			var cookieEnabled=(navigator.cookieEnabled)? true : false
		
			//if not IE4+ nor NS6+
			if (typeof navigator.cookieEnabled=="undefined" && !cookieEnabled){ 
			document.cookie="testcookie"
			cookieEnabled=(document.cookie.indexOf("testcookie")!=-1)? true : false
			}
			
			if (!cookieEnabled) {
				document.getElementById("formDiv").style.display = "none";
				document.getElementById("cookieText").innerHTML = "Sorry for the inconvenience,<br>You need to enable cookies in order to use this form.<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>";
			}
		}
	function killError (id,val) {
		var el = document.getElementById(id);
		var clas = "errorBig";
		if (val == "") {
			el.className = clas;
		} else {
			removeClass(el,clas);
		}
	}
	function verifyPassword() {
		var pass = document.getElementById("password").value;
		var err = document.getElementById("passwordError");
		if (pass.length > 4 && pass.length < 17) {
			if (pass == document.getElementById("password2").value) {
				err.innerHTML = "";
			} else {
				err.innerHTML = "Passwords do not match";	
			}
		} else {
			err.innerHTML = "Password needs to be between 5 and 16 characters";	
		}
	}
	function verifyZip(val) {
		if (val.length != 5) {
			document.getElementById("zipErr").innerHTML = "Zip code must be 5 digits";
		} else {
			document.getElementById("zipErr").innerHTML = "";
		}
	}
	function validateEmail(val) {
		var ajaxRequest;
		try{
			// Opera 8.0+, Firefox, Safari
			ajaxRequest = new XMLHttpRequest();
		} catch (e){
			// Internet Explorer Browsers
			try{
				ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try{
					ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e){
					// Something went wrong
					alert("Your browser broke!");
					return false;
				}
			}
		}
		ajaxRequest.onreadystatechange = function(){
			if(ajaxRequest.readyState == 4){
				res = ajaxRequest.responseText;
				if (res) {
					document.getElementById("emailError").innerHTML = "Email already exists";
				} else {
					document.getElementById("emailError").innerHTML = "";
				}
			}
		}	
		ajaxRequest.open("GET", "verifyEmail.php?email="+val+"&noType=1", true);
		ajaxRequest.send(null); 
	}

function chkBrowser2() {
	jQuery.each(jQuery.browser, function(i, val) {
	  if(i=="mozilla" && jQuery.browser.version.substr(0,3)=="1.9") {
		$("#warn").html("");
	  } else {
	  	$("#warn").html("You are not using Firefox.<br>In order to install Annex at the end of this process you need to be using Firefox<br>You can continue to sign up for the trial but you will need to switch over to Firefox before you can install Annex.<br>Thanks.");
	  }
	});
}

// ajax function that adds a play to the given video, called from flash
function trackPlay(vid) {
	$(document).ready(function() {
		$.ajax({
			type: "GET",
			url: "trackVideo.php",
			data: "video="+vid,
		});
	});
}

// ajax function that finds the related videos and fills the data into the blocks
function findRelated(vid) {
	$(document).ready(function() {	
		$.post("findRelated.php", 
		{ video: vid },
		function(data){
			$("#related1").html(data.name1);
			$("#related2").html(data.name2);
	  	}, "json");
	}); 
}

// called from flash when a movie is done
function vidComplete(vid) {
	$(document).ready(function(){
		$("#related1").fadeIn("slow");
		$("#related2").fadeIn("slow");
		findRelated(vid);
	});	
}


// this is called from top.php which is included on every page
// it will drop a div down with a login box that uses verifyLogin below
// to ajax verify the form.
// you can have multiple topDrop classes which can send different destinations
// to goto after a successfull login. (uses a global variable)
var loginTo = "";

function showLoginBox(loginto) {
	$(document).ready(function() {
		if (loginto) loginTo = loginto;
		$(".topDrop").slideToggle("slow");
	});
}
		
		
// this will verify the login on the page from the email input
// and the pass input.  If its succeeds it will go to the locaiton 
// specifed in the attribute loginto that is on the link that
// dropped down the box
//, if it fails, it will send the error to the fail div
function verifyLogin() {
	$(document).ready(function() {
		$("form[name=doLogin]").submit(function() {
			$.post("/verifyLogin.php",{
			email:$("input[name=email]").val(),
			pass:$("input[name=pass]").val()
			},
			function(data) {
				if (data.sucess) {
					if (loginTo && loginTo != "") {
						window.location = loginTo;
					} else {
						// slide the box up, change the text to the logged in details clear the form
						$(".topDrop").slideUp("slow");
						$(".loginText").html("Hello "+data.name+"<a href='Javascript:void 0;' onclick=\"window.location='/store/account.php'\" style='margin-left:15px;text-decoration:none'>My Account</a><a href='Javascript:void 0;' onclick='logOut()' style='margin-left:15px;text-decoration:none'>Log Out</a>");
						$("[name=email]").val("");
						$("[name=pass]").val("");
						document.doLogin.pass.value = "";
					}
				} else {
					$("#loginError").html(data.error);
					$("#loginError").fadeOut("fast",function() {
						$(this).fadeIn("fast");
					});
					
				}
			},"json");
			return false;
		});
	});
}

// logs out the user and changes the top to login
function logOut() {
	$(document).ready(function() {
		$.post("/logOut.php",
		function() {
			$(".loginText").html("<a href='Javascript:void 0;' onclick=\"showLoginBox()\">Login</a>");
			loginTo = "";
			if (window.location.href.match(/store/)) {
				window.location = "/";
			}
		});
		return false;
	});
}


function findState() {
	var select = document.getElementById("state");
	for (var i=0;i<select.length;i++) {
		if (select.options[i].text == "<?=$_SESSION['state']?>") {
			select.selectedIndex = i;
		}
	}
}

// given that the select box name, id match the session variable
// this will select the matching value in the box
// ex. <select id='state' name='state'> and $_SESSION['state']
function findSelectBox(sb) {
	$(document).ready(function() {
		$.post("/getSession.php",{data:sb},
		function(data) {
			var select = document.getElementById(sb);
			for (var i=0;i<select.length;i++) {
				if (select.options[i].value == data) {
					select.selectedIndex = i;
					break;
				}
			}
		});
	});
}

function sbVal(form,sb) {
	return eval("document."+form+"."+sb+".options[document."+form+"."+sb+".selectedIndex].value");
}


