//-------------------------------------------------------------------------------------------
function getURLParameters() {
// Read a page's GET URL variables and return them as an "associative array."
// source: http://snipplr.com/view.php?codeview&id=19838
//-------------------------------------------------------------------------------------------
	var map = {};
	var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
		map[key] = value;
	});
	return map;
}

//-------------------------------------------------------------------------------------------
function preloadImages() {
// accepts n args
// loop through args and preload them
//
// implementation:
// <body onload="preloadImages('../images/test1.gif','../images/test2.gif','../images/test3.gif')"> 
//-------------------------------------------------------------------------------------------
	var d = document;
	if (d.images) { 
		if (!d.preloadArray) d.preloadArray = new Array();
		var i, j = d.preloadArray.length, a = preloadImages.arguments; 
		for (i=0; i<a.length; i++) {
			if (a[i].indexOf("#")!=0) { 
				d.preloadArray[j] = new Image; 
				d.preloadArray[j++].src=a[i];
			}
		}
	}
}

//-------------------------------------------------------------------------------------------
function newWindow(URL, winName) {
// open a new browser window
//-------------------------------------------------------------------------------------------
	if (winName == "photo") {
		height = "height=480";
		width = "width=640";
	} else if (winName == "doc") {
		height = "height=600";
		width = "width=800";
	} else if (winName == "brochure") {
		height = "height=800";
		width = "width=700";
	} else { //default for client report
		height = "height=650";
		width = "width=598";
	}
	
	args = "menubar=0,status=0,scrollbars=0,toolbar=0,location=0,resizable=1,directories=0,"+height+","+width;
	var win = window.open(URL, winName,args);
	
	try {
		// catch wierd error
		win.focus();
	} catch (e) {
	}
	
}

//-------------------------------------------------------------------------------------------
function GetWidth() {
// cross-browser solution to obtaining the WIDTH of the browser window
//-------------------------------------------------------------------------------------------
	var x = 0;
	if (self.innerHeight) {
		x = self.innerWidth;
	} else if (document.documentElement && document.documentElement.clientHeight) {
		x = document.documentElement.clientWidth;
	} else if (document.body) {
		x = document.body.clientWidth;
	}
	return x;
}

//-------------------------------------------------------------------------------------------
function GetHeight() {
// cross-browser solution to obtaining the HEIGHT of the browser window
//-------------------------------------------------------------------------------------------
	var y = 0;
	if (self.innerHeight) {
		y = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight){
		y = document.documentElement.clientHeight;
	} else if (document.body) {
		y = document.body.clientHeight;
	}
	return y;
}

function resizeToWindow(element) {
	var winWidth = GetWidth();
	var winHeight = GetHeight();
	var elm = document.getElementById(element);
	var headerOffset = document.getElementById("reportHeader").offsetHeight;
	
	elm.style.width = winWidth;
	try {
		// catch error if headerOffset = 0 
		elm.style.height = winHeight - headerOffset;
	} catch (e) {
	}
}

function resizeToWindow2(element) {
	var winWidth = GetWidth();
	var winHeight = GetHeight();
	var element = document.getElementById(element);

	element.style.width = winWidth;
	element.style.height = winHeight;
	
	try {
		// catch error if headerOffset = 0 
	} catch (e) {
	}
}
	
function hidePhotoLoadingMessage() {
// hide Photo loading message
	document.getElementById("photoLoadingMessage").style.display = "none";
}

function showPhoto(img) {
// this function created to resolve a problem with image scaling in IE7 (and possibly Google Chrome Beta)
// show a photo. Called after it has loaded
// make parent DIV visible and size correctly

	img.parentNode.style.width = "49.5%";
	img.parentNode.style.visibility = "visible";
}

function showCertificate(img) {
// this function created to resolve a problem with image scaling in IE7 (and possibly Google Chrome Beta)
// show a photo. Called after it has loaded
// make parent DIV visible and size correctly

	img.parentNode.style.visibility = "visible";
}

function getElementsByClassName(classname, node)  {
// code retrieved from: http://snipplr.com/view.php?codeview&id=1696

    if(!node) node = document.getElementsByTagName("body")[0];
    var a = [];
    var re = new RegExp('\\b' + classname + '\\b');
    var els = node.getElementsByTagName("*");
    for(var i=0,j=els.length; i<j; i++)
        if(re.test(els[i].className))a.push(els[i]);
    return a;
}

function printPage() {
// resize photos for print, call print, then resize photos back to normal
	var x = getElementsByClassName("photoGroup");
	var y = document.getElementById("reportContainer");
	
	// loop through all photoGroups and resize:
	for (i=0;i<x.length;i++){ 
		x[i].style.width='563px';
	}
	y.style.width="auto";
	y.style.height="auto";

	// print
	window.print();
	
	// loop through all photoGroups and resize:
	for (i=0;i<x.length;i++){ 
		x[i].style.width='auto';
	}
	resizeToWindow("reportContainer");
}

function printPage2() {
// TRYING TO FIX IE7 SO THAT IT PRINTS PROPERLY
	var x = getElementsByClassName("photoGroup");
	var y = document.getElementById("reportContainer");
	


	bodyNode = document.getElementsByTagName("body")[0];
	reportContainerNode = document.getElementById("reportContainer");
	reportContainerNodes = document.getElementById("reportContainer").childNodes;
	
	//remove container
	bodyNode.removeChild(document.getElementById("reportContainer"));
	
	//append container nodes back into body
	for (i=0;i<reportContainerNodes.length;i++){ 
		alert(reportContainerNodes[i]);
		bodyNode.appendChild(reportContainerNodes[i]);
	}
	
	// loop through all photoGroups and resize:
	for (i=0;i<x.length;i++){ 
		x[i].style.width='563px';
	}
	y.style.width="auto";
	y.style.height="auto";
	
	// print
	window.print();
	
	//clear body
	bodyNodes = document.getElementsByTagName("body")[0].childNodes;
	for (i=0;i<bodyNodes.length;i++){ 
		bodyNode.removeChild(bodyNodes[i]);
	}
	
	bodyNode.appendChild(reportContainerNode);
	
	// loop through all photoGroups and resize:
	for (i=0;i<x.length;i++){ 
		x[i].style.width='auto';
	}
	
	resizeToWindow("reportContainer");
}

// revisit functions:
//----------------------------------------------------------------------
function submitRevisit() {
// performed before submit
	return true;
}

function showRevisitBar() {
	document.getElementById("revisitBar").style.display = "";
	
	var hd = document.getElementById("revisitButton").childNodes;
	hd[0].style.visibility = "hidden";
	
	document.getElementById("revisitReasonBox").focus();
}


function showEmailBar() {
	document.getElementById("emailBar").style.display = "";

	var hd = document.getElementById("emailButton").childNodes;
	hd[0].style.visibility = "hidden";
}

function hideRevisitBar() {
	document.getElementById("revisitBar").style.display = "none";
	
	var hd = document.getElementById("revisitButton").childNodes;
	hd[0].style.visibility = "visible";
}

function hideEmailBar() {
	document.getElementById("emailBar").style.display = "none";
	
	var hd = document.getElementById("emailButton").childNodes;
	hd[0].style.visibility = "visible";
}

function emailSelectAll() {
	var checkedFound = false;
	var ucheckedFound = false;
	var job = document.getElementById("emailAll").checked;
	var hd = document.getElementById("ul_email").childNodes;
	
	// loop through all check boxes and determine whether to check or uncheck
	for (x in hd) {
		if (hd[x].nodeType != 3 && hd[x].nodeType != undefined) {
			//alert(hd[x].nodeType);
			var aa = hd[x].childNodes;
			var bb = aa[0].childNodes;
			var cc = bb[0].childNodes;
			
			if (bb[0].checked) {
				checkedFound = true;
			} else {
				ucheckedFound = true;
			}
		}
	}
	
	var valu;
	if (checkedFound && ucheckedFound) {
		valu = true;
	} else if (checkedFound) {
		valu = false;
	} else {
		valu = true;
	}
	
	//alert("value = " + valu + " job = " + job);
	
	// apply checked state
	for (x in hd) {
		if (hd[x].nodeType != 3 && hd[x].nodeType != undefined) {
			//alert(hd[x].nodeType);
			var aa = hd[x].childNodes;
			//alert("aa.length = " + aa.length);
			var bb = aa[0].childNodes;
			var cc = bb[0].childNodes;
			
			bb[0].checked = valu;
		}
	}
	
}

function emailSelectOne() {
	var checkedFound = false;
	var ucheckedFound = false;
	var hd = document.getElementById("ul_email").childNodes;
	
	// loop through all check boxes and determine whether to check or uncheck
	for (x in hd) {
		if (hd[x].nodeType != 3 && hd[x].nodeType != undefined) {
			//alert(hd[x].nodeType);
			var aa = hd[x].childNodes;
			var bb = aa[0].childNodes;
			var cc = bb[0].childNodes;
			
			if (bb[0].checked) {
				checkedFound = true;
			} else {
				ucheckedFound = true;
			}
		}
	}
	
	var valu;
	if (checkedFound && ucheckedFound) {
		valu = false;
	} else if (checkedFound) {
		valu = true;
	} else {
		valu = false;
	}
	
	document.getElementById("emailAll").checked = valu;
}

var tmpEditReason = ""
function editRevisit() {
	// store reason. so if user cancels
	tmpEditReason = document.getElementById("revisitReasonBox").value;
	
	document.getElementById("revisitReason").style.display = "none";
	document.getElementById("revisitQuestion").style.display = "";
	document.getElementById("revisitTextbox").style.display = "";
	document.getElementById("revisitEditButton").style.display = "none";
	//document.getElementById("revisitReasonBox").style.display = "";
	//document.getElementById("revisitReasonBox").focus();
}

function cancelEditRevisit() {
	// restore reason
	document.getElementById("revisitReasonBox").value = tmpEditReason;
	
	document.getElementById("revisitReason").style.display = "";
	document.getElementById("revisitQuestion").style.display = "none";
	document.getElementById("revisitTextbox").style.display = "none";
	document.getElementById("revisitEditButton").style.display = "";
}


// login functions:
//----------------------------------------------------------------------

var loginState = "none";
function chooseState(choice) {
// depending on whether user selected VIC or QLD, show login controls
	
	// hide all
	document.getElementById("VICLoginTitle").style.display = "none";
	document.getElementById("QLDLoginTitle").style.display = "none";
	document.getElementById("VICLoginClientList").style.display = "none";
	document.getElementById("QLDLoginClientList").style.display = "none";
	document.getElementById("userName_vic").selectedIndex = 0;
	document.getElementById("userName_qld").selectedIndex = 0;
	document.getElementById("passwordRow").style.display = "none";
	document.getElementById("loginButton").style.display = "none";
	document.getElementById("peg_vic").style.backgroundImage = "url(../assets/images/mapPeg_unsel.png)";
	document.getElementById("peg_qld").style.backgroundImage = "url(../assets/images/mapPeg_unsel.png)";
	document.getElementById("peg_gold").style.backgroundImage = "url(../assets/images/mapPeg_unsel.png)";
	document.getElementById("otherLogin").style.display = "none";
	document.getElementById("clientLogo_row").style.display = "none";
	
	// hide client logo
	//document.getElementById("clientLogo_row").style.display = "none";
		
	// show
	if (choice == "vic") {
		loginState = "vic";
		document.getElementById("VICLoginTitle").style.display = "";
		document.getElementById("VICLoginClientList").style.display = "";
		document.getElementById("passwordRow").style.display = "";
		document.getElementById("userName_vic").focus();
		document.getElementById("peg_vic").style.backgroundImage = "url(../assets/images/mapPeg_sel.png)";
		document.getElementById("loginButton").style.display = "";
		
	} else if (choice == "qld") {
		loginState = "qld";
		document.getElementById("QLDLoginTitle").style.display = "";
		document.getElementById("QLDLoginClientList").style.display = "";
		document.getElementById("passwordRow").style.display = "";
		document.getElementById("userName_qld").focus();
		document.getElementById("peg_qld").style.backgroundImage = "url(../assets/images/mapPeg_sel.png)";
		document.getElementById("loginButton").style.display = "";
	
	} else if (choice == "gold") {
		loginState = "gold";
		document.getElementById("QLDLoginTitle").style.display = "";
		document.getElementById("QLDLoginClientList").style.display = "";
		document.getElementById("passwordRow").style.display = "";
		document.getElementById("userName_qld").focus();
		document.getElementById("peg_gold").style.backgroundImage = "url(../assets/images/mapPeg_sel.png)";
		document.getElementById("loginButton").style.display = "";
	}
}

function showHideOtherLoginField(selBox) {
// show or hide extra text box depending if user has selected "Other" in user_id dropdown box
	
	var x = selBox;
	
	if (x.options[x.selectedIndex].text == "Other") {
		document.getElementById("otherLogin").style.display = "";
		
		// hide client logo
		document.getElementById("clientLogo_row").style.display = "none";
	
	} else {
		document.getElementById("otherLogin").style.display = "none";
		
		// show client logo if it exists
		showClientLogo();
	}
}

function showClientLogo() {
// test load client logo before displaying it. If image does not exist, hide it.
	var testImage = new Image()
	var selBox = document.getElementById("userName_vic");
	var imageURL = "../assets/images/logos/" + selBox.options[selBox.selectedIndex].value + ".png";
	var clientLogo = document.getElementById("clientLogo");
	var clientLogo_row = document.getElementById("clientLogo_row");
	
	testImage.src = imageURL;
	//alert(testImage.onerror + ", " + imageURL + ", " + testImage.complete + ", " + testImage.width + ", " + testImage.height + ", ");
	
	// set image events
	clientLogo.onerror = function (evt) {
		// no image exists, hide image element
		clientLogo_row.style.display = "none";
	}
	clientLogo.onload = function (evt) {
		// image loaded
		clientLogo_row.style.display = "";
	}
	
	clientLogo.src = imageURL;
	
	clientInputSelected = true;
}

function submitenter(myfield,e) {
// when user presses enter, perform action
	var keycode;

	// detect event
	if (window.event) 
		keycode = window.event.keyCode;
	else if (e) 
		keycode = e.which;
	else 
		return true;
		
	// perform action
	if (keycode == 13) {
		if (myfield.id == "userName_vic") {
			var x = document.getElementById("userName_vic");
			
			if (x.options[x.selectedIndex].text == "Other") {
				document.getElementById("userNameOther").focus();
				return false;
			} else {
				document.getElementById("password").focus();
				return false;
			}
			
		} else if (myfield.id == "userName_qld") {
			var x = document.getElementById("userName_qld");
			
			if (x.options[x.selectedIndex].text == "Other") {
				document.getElementById("userNameOther").focus();
				return false;
			} else {
				document.getElementById("password").focus();
				return false;
			}
			
		} else if (myfield.id == "userNameOther") {
			document.getElementById("password").focus();
			return false;
		
		} else if (myfield.id == "password") {
			myfield.form.submit();
			return false;
		}
	} else {
		return true;
	}
}

// reference/temp code:
//----------------------------------------------------------------------

function fillScreen(element) {
	element.style.width = GetWidth();
	element.style.height = GetHeight();
}


function pausecomp(millis) {
	// not used
	var date = new Date();
	var curDate = null;

	do { curDate = new Date(); } 
	while(curDate-date < millis);
} 


function tmp1() {
// tmp code to create DOM img:

	// clear old
	var logoContainer = document.getElementById("logoContainer");
	var oldImg = document.getElementById("clientLogo_img");
	logoContainer.removeChild(oldImg);

	// insert new
	var newImg = document.createElement('img');
	newImg.setAttribute('id',"clientLogo_img");
	newImg.setAttribute('src',"");
	newImg.innerHTML = "";
	logoContainer.appendChild(newImg);
}

function dialougeBox(dType, dMessage) {
// beta function to show cool dialouge box that fills screen
	
}