/*
----------------------------------------------------------------------
	CAFFEINE build 2.0
	Author: Eric Shiarla, Ryan Merket
	Employed By: Pendergraphics
	
	ABOUT
	This file contains all of caffeine's basic javascript functions.
	These functions do not require other libraries. This library
	will be included in the _header.php file which is included on
	every Caffeine page.

----------------------------------------------------------------------	
*/


//
// Simple function to validate that the 
// username and password were entered.
//

function validateLoginForm ( form )
{

  if (form.username.value == "") {
    alert( "Please enter a login." );
    form.username.focus();
    return false ;
  }
  
  if (form.password.value == "") {
    alert( "Please enter a password." );
    form.password.focus();
    return false ;
  }
  
  
// End script
  return true ;
}




function confirmMessage() {
	
	var confirmation = confirm("Are you sure you want to continue?");
	
	if (confirmation == true)
		smoothTransition();
	else
		return false;
}



//
// Dreamweaver jump menu script
//

function MM_jumpMenu(targ,selObj,restore){ //v3.0
	eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
	if (restore) selObj.selectedIndex=0;
}


//
// Used for all of Caffeine's XML requests.
//

function createXMLHttpRequest() {
	if (window.ActiveXObject) {
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else if (window.XMLHttpRequest) {
		xmlHttp = new XMLHttpRequest ();
	}
}




//
// openSort() & openSearch() give a current state
// to the sort and search tabs connected with
// the view modules.
//

function openSort() {
	
	var openSearch = document.getElementById("openSearch");
	var openSort = document.getElementById("openSort");
	
	openSearch.style.backgroundColor = "transparent";
	openSearch.style.color = "#8F7C5F";
	openSearch.style.borderColor = "#8F7C5F";		
	
	if (openSort.style.backgroundColor == "rgb(83, 67, 51)") {
		openSort.style.backgroundColor = "transparent";
		openSort.style.color = "#8F7C5F";
		openSort.style.borderColor = "#8F7C5F";
	}
	else {
		openSort.style.backgroundColor = "rgb(83, 67, 51)";
		openSort.style.color = "#fff";
		openSort.style.border = "1px solid #3D3024";
		openSort.style.borderBottom = "none";		
	}
	
		
	return false;	
	
}




function openSearch() {
	
	var openSearch = document.getElementById("openSearch");
	var openSort = document.getElementById("openSort");
	
	openSort.style.backgroundColor = "transparent";
	openSort.style.color = "#8F7C5F";
	openSort.style.borderColor = "#8F7C5F";		


	if (openSearch.style.backgroundColor == "rgb(83, 67, 51)") {
		openSearch.style.backgroundColor = "transparent";
		openSearch.style.color = "#8F7C5F";
		openSearch.style.borderColor = "#8F7C5F";
	}
	else {
		openSearch.style.backgroundColor = "rgb(83, 67, 51)";
		openSearch.style.color = "#fff";
		openSearch.style.border = "1px solid #3D3024";
		openSearch.style.borderBottom = "none";		
	}

		
	return false;	
	
}




function openGallery() {
	
	var openGallery = document.getElementById("openGallery");
	
	
	if (openGallery.style.backgroundColor == "rgb(83, 67, 51)") {
		openGallery.style.backgroundColor = "transparent";
		openGallery.style.color = "#8F7C5F";
		openGallery.style.borderColor = "#8F7C5F";
	}
	else {
		openGallery.style.backgroundColor = "rgb(83, 67, 51)";
		openGallery.style.color = "#fff";
		openGallery.style.border = "1px solid #3D3024";
		openGallery.style.borderBottom = "none";		
	}
	
		
	return false;	
	
}





//
// checkPassword() checks the password against
// the confirm password.  Writes an error if they 
// do not match.
//

function checkPassword() {
	var pass = document.getElementById("Password").value;
	var passConfirm = document.getElementById("ConfirmPassword").value;
	var checkPassword = document.getElementById("checkPassword");
	
	if (passConfirm != pass) {
		checkPassword.style.color = "red";
		checkPassword.innerHTML = "Your passwords do not match.  Please try again.";
	}
	else
		checkPassword.innerHTML = "";
	
}




//
// checkUser(), checkWebUser() checkUserLength(), checkDone() work
// together to allow a user to check if a username is
// available.  Also makes sure it is between 5 & 15 chars.
//

function checkUser() {
	var user = document.getElementById("UserName").value;
	var checkResults = document.getElementById("checkResults");
	
	if (user.length > 5 && user.length < 15) {
		checkResults.innerHTML = "<img src=\"images/loading.gif\" alt=\"loading..\" />";
		createXMLHttpRequest();
		xmlHttp.onreadystatechange = checkDone;
		var querystr = base+"caffeine/assets/libraries/lib_xmlUserCheck.php?user=" + user;
		xmlHttp.open("GET", querystr, true);
		xmlHttp.send(null);
		
	}
	else {
		checkResults.style.color = "red";
		checkResults.innerHTML = "Username must be between 5 and 15 characters.";		
	}
}

function checkWebUser() {
	var user = document.getElementById("UserName").value;
	var checkResults = document.getElementById("checkResults");
	
	if (user.length > 5 && user.length < 15) {
		checkResults.innerHTML = "<img src=\"images/loading.gif\" alt=\"loading..\" />";
		createXMLHttpRequest();
		xmlHttp.onreadystatechange = checkDone;
		var querystr = base+"caffeine/assets/libraries/lib_xmlWebUserCheck.php?user=" + user;
		xmlHttp.open("GET", querystr, true);
		xmlHttp.send(null);
		
	}
	else {
		checkResults.style.color = "red";
		checkResults.innerHTML = "Username must be between 5 and 15 characters.";		
	}
}


function checkUserLength() {
	var user = document.getElementById("UserName").value;
	var checkResults = document.getElementById("checkResults");
	
	if (user.length < 5 || user.length > 15) {
		checkResults.style.color = "red";
		checkResults.innerHTML = "Username must be between 5 and 15 characters.";
	}
	else
		checkResults.innerHTML = "";
	
}




function checkDone() {
	if(xmlHttp.readyState == 4) {
		if(xmlHttp.status == 200) {
			var user = xmlHttp.responseXML.getElementsByTagName('user')[0].firstChild.data;
			var result = xmlHttp.responseXML.getElementsByTagName('taken')[0].firstChild.data;
			var checkResults = document.getElementById("checkResults");
			
			if (result == "true") {
				checkResults.style.color = "red";
				checkResults.innerHTML = "Username is taken.";
			}
			else {
				checkResults.style.color = "green";
				checkResults.innerHTML = "Username is available!";
			}

		}
	}
}


function refresh() {
	
	window.location.reload( true );
	
}




//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
// Modifications by - http://huddletogether.com/projects/lightbox2/
//
function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}




//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Modifications by - http://huddletogether.com/projects/lightbox2/
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}




//
// Checks string for illegal characters
//
function isLegal(string) {

	return true;
   
   var error = "The name you chose contained illegal characters.\n\n Please try another name.\n\n Some illegal characters include:\n \t * | , \ \" : < > [ ] { } ` \ ' ; ( ) @ & $ # %\n\n";

   if (!string) return false;
   var iChars = "*|,\":<>[]{}`\\';()@&$#%";

   for (var i = 0; i < string.length; i++) {
      if (iChars.indexOf(string.charAt(i)) != -1) {
	  	 alert(error);
         return false;
	  }
   }
   return true;
} 




//
// confirmFileDelete() && fileDeleteDone()
// work together to delete a file from the server.
//
function confirmFileDelete(table,field,id,file,path) {
	
	var confirmdel = confirm("You are about to delete this file. \nYou will not be able to recover any information once the file is deleted. \n\nAre you sure you want to continue?");
	
	var update = document.getElementById("update");
					
	if (confirmdel == true) {
	
		update.style.display = "block";
		update.innerHTML = "Deleting file.. <img src=\"images/loading.gif\" alt=\"Updating..\" style=\"vertical-align: middle;\"/>";	
	
			createXMLHttpRequest();
		xmlHttp.onreadystatechange = fileDeleteDone;
		var querystr = base+"caffeine/assets/libraries/lib_xmlFileDelete.php?delete="+table+"&match="+field+"&id="+id+"&file="+file+"&path="+path;
		xmlHttp.open("GET", querystr, true);
		xmlHttp.send(null);
	}
	else
		return false;
}




function fileDeleteDone() {
		if(xmlHttp.readyState == 4) {
			if(xmlHttp.status == 200) {
				var res = xmlHttp.responseXML.getElementsByTagName('valid')[0].firstChild.data;
				var id2 = xmlHttp.responseXML.getElementsByTagName('id')[0].firstChild.data;

				if (res == "true") {
					
					var update = document.getElementById("update");
					
					update.style.display = "block";
					update.innerHTML = "File successfully deleted.. <img src=\"images/loading.gif\" alt=\"Updating..\" style=\"vertical-align: middle;\"/>";
					
					setTimeout( "refresh()", 1000 );
				
				}
				else 
					alert("Caffeine was unable to delete the file.  Please contact your web administrator.");
			}
		}
}


//
// validateRequired()
// Cycles through all elements with a class of "reqField", 
// and uses some basic validation.  If the element has an ID of "email"
// or "Email" it will test the value against a regular expression.
//
function validateRequired() {
	
	var fields = document.getElementsByClassName("reqField");
	var agreement = document.getElementById("Yes");
	var error = "The following required fields are incomplete or contain errors.\n\n";
	var returnError = false;
	var emailFilter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	
	if(agreement) {
		if(!agreement.checked) {
			alert("You must agree to the Terms & Conditions before continuing.");
			return false;			
		}
	}
	
	if (fields.length != 0) {
	
		for (var i=0; i<fields.length; i++) {
			
			if (fields[i].value == null || fields[i].value.length == 0) {
				error += " - "+fields[i].id+" is empty\n";
				returnError = true;
			}
			else if (fields[i].id == "email" || fields[i].id == "Email") {
			
				if (!emailFilter.test(fields[i].value)) { 
					error += " - "+fields[i].id+" is invalid\n";
					returnError = true;
				}
			
			}

				
		}		
	
	}
	
	error += "\n\nPlease correct these fields before continuing.";

	if (returnError == true) {
		alert(error);
		return false;
	}
	else {
		return true;
	}
}


//
// clearThis()
// Clears a form element
//
function clearThis(obj) {
	obj.value = "";
}

function setFooter() {
	
	var nav = document.getElementById("navBox");
	var content = document.getElementById("content");
	
	if(nav.offsetHeight > content.offsetHeight)
		content.style.paddingBottom = 15+(nav.offsetHeight-content.offsetHeight)+'px';

}

/*
window.onload = function() {
	if(document.getElementById("navBox"))
		setFooter();
}
*/
