// JavaScript Document
var browserCodeName=navigator.appCodeName;
var browser=navigator.appName;
var b_version=navigator.appVersion;
var version=parseFloat(b_version);

//get the available height and width so that we can scale the site appropriately
var aHeight = screen.availHeight;
var aWidth = screen.availWidth;
if(aWidth < 1680){
	aHeight = Math.round(((aWidth*.95)/1620)*730)
}

if(aHeight > 730){
	aHeight = 730;
}

var scalePercent = aHeight/730;
if(scalePercent > 1){
        scalePercent = 1;
}


var origWidth = 1620 * scalePercent;
var origHeight = 730 * scalePercent;

var mediaWidth = 1350 * scalePercent;
var mediaHeight = 185 * scalePercent;

var menuHeight = 730 * scalePercent;
var menuWidth = 270 * scalePercent;

var mapHeight = 30 * scalePercent;

var contentHeight = menuHeight - mediaHeight;
var contentWidth = 900 * scalePercent;

var videoHeight = Math.floor(parseInt(contentHeight*.7));
var videoWidth = Math.floor(parseInt(contentWidth*.7));

var http = createRequestObject();

var searchLoaded = false;
var table = 'lic_table';
var mySearchTerm = null;
var myFunct = null;

//change all links to ajax links
function changeLinks(){
	var linkNodes = document.getElementsByTagName("a");
	for ( i = 0; i < linkNodes.length; i++ ) {
	  linkNode = linkNodes[i];
	  relAttr = linkNode.getAttribute('href');
	  pattern = /.+page\/(\d+)\/.+\/?$/;
	  myMatch = pattern.test(relAttr);
	  var newLink;
	  if(myMatch == true){
	  	newLink = "Javascript: getContent(" + RegExp.$1 + ", 0);";
	  }else{
	  	newLink = relAttr;
	  }
	  linkNode.setAttribute('href', newLink);
	}
}

//scale style sheet for screen maximization
function scaleStyleSheet(){
	var linkNodes = document.getElementsByTagName("link");
	for ( i = 0; i < linkNodes.length; i++ ) {
	  linkNode = linkNodes[i];
	  relAttr = "/CSS/mainStyle.php?height="+aHeight;
	  linkNode.setAttribute('href', relAttr);
	}
}

function createRequestObject(){
	var request_o; //declare the variable to hold the object.
	var browser = navigator.appName; //find the browser name
	if(browser == "Microsoft Internet Explorer"){
		/* Create the object using MSIE's method */
		request_o = new ActiveXObject("Microsoft.XMLHTTP");
	}else{
		/* Create the object using other browser's method */
		request_o = new XMLHttpRequest();
	}
	return request_o; //return the object
}

function getContent(pageID, templateID){
	if(templateID != 3){
		var url = 'PHP/getContent.php?pageID=' + pageID + '&height='+ aHeight;
		http.open('get', url);
		http.onreadystatechange = loadContent; 
		http.send(null);
	}else{
		getLogin();
	}
	
	//updateMediaSelector(pageID);
}

function getLogin(){
	window.location = "PHP/login.php";
};

function getLoginContent(url, sendVars){
		if(sendVars == null){
			http.open('GET', url);
		}else{
			http.open('POST', url);
			http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		}
		http.onreadystatechange = loadContent; 
		http.send(sendVars);
}


//login functions
function submitLogin(){
	
	var elements = document.forms['loginForm'].elements;
	var url = "PHP/login.php";
	var sendVars = "";
	for(var x = 0; x < elements.length; x++){
		sendVars += elements[x].id+"="+elements[x].value+"&";
	}
	sendVars += "sent=1";
	
	getLoginContent(url, sendVars);
}

function loadContent(){
	if(http.readyState == 4){ //Finished loading the response
		var response = http.responseText;
		document.getElementById('contentColumn').innerHTML = response;
	}
}

//update media selector
function updateMediaSelector(requestID){
	
	var url = "Flash/FLA/mediaSelector/sa_mediaSelector_v1.php?requestID="+requestID+"&mediaHeight="+mediaHeight+"&mediaWidth="+mediaWidth+"&funct="+myFunct+"&searchTerm="+mySearchTerm;
	
	document.getElementById('mediaColumn').innerHTML = "<iframe marginwidth='0' marginheight='0' frameborder='0' src='"+url+"' width='"+mediaWidth+"' height='"+mediaHeight+"' allowTransparency='true'/>";
	
	//document.getElementById('mediaColumn').innerHTML = "<xmp>"+url+"</xmp>";
}


//function to submit forms
function submitForm(formName){
  document.getElementById(formName).submit();
  if(formName == 'contactForm'){
  	var html = "<table cellpadding='3'><tr><td width='"+Math.round(scalePercent*150)+"' /></td><td><br /><br /><br /><span class='sentMessage'>Thank you for submitting your message.  We will contact you shortly.</span></td><td width='"+Math.round(scalePercent*150)+"' /></td></tr></table>";
  }
  document.getElementById('contentColumn').innerHTML = html;
}

//AJAX Search Form
var http2 = createRequestObject();

function createRequestObject2(){
	var request_o2; //declare the variable to hold the object.
	var browser = navigator.appName; //find the browser name
	if(browser == "Microsoft Internet Explorer"){
		/* Create the object using MSIE's method */
		request_o2 = new ActiveXObject("Microsoft.XMLHTTP");
	}else{
		/* Create the object using other browser's method */
		request_o2 = new XMLHttpRequest();
	}
	return request_o2; //return the object
}


function drawSearch(){
		document.getElementById('searchColumn').innerHTML = "Loading Search Options";
		http2.open('get', 'PHP/buildSearchForm.php');
		http2.onreadystatechange = loadSearch; 
		http2.send(null);
}

function loadSearch(){
	if(http2.readyState == 4){ //Finished loading the response
		var responseSearch = http2.responseText;
		document.getElementById('searchColumn').innerHTML = responseSearch;
	}
}

function loadBookInfo(id){
	var url = "PHP/loadBookInfo.php?id="+id;
	getSearchContent(url, null);
}

function buildContent(){
	//use percentages to scale and layout tables
	var bodyText = "<table name='alignTable' id='alignTable' align='center'><tr><td align='center' valign='middle' height='"+aHeight+"' width='"+origWidth+"'>";
	bodyText += "<table id='backgroundTable' name='backgroundTable'>";
	bodyText += "<tr><td class='backgroundTable'>";
	bodyText += "<table id='mainTable' name='mainTable' width='"+origWidth+"' height='"+origHeight+"'>";
	bodyText += "<tr><td id='menuColumn' align='left' valign='top' width='"+menuWidth+"'>";
	bodyText += "</td><td align='center'>";
	bodyText += "<table id='contentTable' name='contentTable' width='"+mediaWidth+"' height='"+menuHeight+"'>";
	bodyText += "<tr><td id='mediaColumn' colspan='2' align='center' height='"+mediaHeight+"'>";
	bodyText += "</td></tr>";
	bodyText += "<tr><td id='contentColumn' name='contentColumn' align='left' valign='top' height='"+contentHeight+"' >";
	bodyText += "</td>";
	bodyText += "<td id='searchColumn' class='searchColumn'>";
	bodyText += "</td>";
	bodyText += "</tr>";
	bodyText += "</table>";
	bodyText += "</td></tr>";
	bodyText += "</table></td></tr><tr><td id='siteMap' align='center' valign='top' height='"+mapHeight+"'><a href='http://www.quiredtesting2.com'>AZCLC.com</a></td></tr></table></td></tr>";
	bodyText += "</table>";
	//write HTML
	document.body.innerHTML = bodyText;
}

//search functions
function searchContent(){
	
	var elements = document.forms['searchForm'].elements;
	var url = "PHP/search.php";
	var sendVars = "";
	myFunct = 0;
	var searchTerm;
	for(var x = 0; x < elements.length; x++){
		sendVars += elements[x].id+"="+elements[x].value+"&";
		if(elements[x].value != undefined && elements[x].value != ''){
			searchTerm += " "+elements[x].value;
		}
	}
	sendVars += "sent=1&table="+table;

	url = url + "?"+sendVars;
	mySearchTerm = searchTerm;
	getSearchContent(url, null);
}

function getResults(start, end, index, table){
	var url = "PHP/getResultPage.php?start="+start+"&end="+end+"&table="+table;
	getSearchContent(url, null);
}

function getMenuResults(funct, searchTerm, table){
	var url = "PHP/getMenuResult.php?funct="+funct+"&searchTerm="+searchTerm+"&table="+table;
	myFunct = funct;
	mySearchTerm = searchTerm;
	getSearchContent(url, null);
}

function getSearchContent(url, sendVars){
		if(sendVars == null){
			http.open('GET', url);
		}else{
			http.open('POST', url);
			http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		}
		http.onreadystatechange = loadContent; 
		http.send(sendVars);
		
		var request;
		if(myFunct != null){
			request = myFunct;
		}else{
			request = page;
		}
		//updateMediaSelector(request);
}


/////////////////////////////////////////////
//Functions for attaching event listeners///
///////////////////////////////////////////

function attachEventListener(target, eventType, functionRef, capture){
	if(typeof target.addEventListener != "undefined")
	{
		target.addEventListener(eventType, functionRef, capture);
	}
	else if(typeof target.attachEvent != "undefined")
	{
		target.attachEvent("on"+eventType, functionRef);
	}
	else
	{
		eventType = "on" + eventType;
		if(typeof target[eventType] == "function")
		{
			var oldListener = target[eventType];
			
			target[eventType] = function()
			{
				oldListener();
				return functionRef();
			};
		}
		else
		{
			target[eventType] = functionRef;
		}
	}
}

function detachEventListener(target, eventType, functionRef, capture){
	if(typeof target.removeEventListener != "undefined"){
		target.removeEventListener(eventType, functionRef, capture);
	}else if(typeof target.detachEvent != "undefined"){
		target.detachEvent("on"+eventType, functionRef);
	}else{
		target["on" + eventType] = null;
	}
}

function submitLicenseForm(licenseNum){
	document.forms['licenseNumber'].licensenumber.value = licenseNum;
	document.forms['licenseNumber'].submit();
}
