/**************************************************************
	File: pageScripts.js
	Author: Dan Piccolo
	Last Modified: 02-Jul-2007
	
	Description: This file contains all the javascript
	functions required to make the site work.
**************************************************************/


var userName = "info";
var hostName = "markercanada";
var extension = "com";


// most spam spiders don't read javascript files, so this is a crafty way to 
// spam proof the page.
function spamProofEmail(user, host, ext) {
	var genericEmail = "";
	
	if(user.length > 0) userName = user;
	if(host.length > 0) hostName = host;
	if(ext.length > 0) extension = ext;
	
	genericEmail = userName + "&#64;" + hostName + "." + extension;
	document.write ("<a href=\"mailto:");
	
	for (var x=0;x<genericEmail.length;x++) {
		document.write(genericEmail.charAt(x));
	}
	document.write ("\">");
	for (var y=0;y<genericEmail.length;y++) {
		document.write(genericEmail.charAt(y));
	}
	document.write ("</a>");
}


// updateParent accepts a single value.  It's purpose is to update
// data on a parent Window. What to update is decided based on the
// URL of the parent.  The value is the argument.
function updateParent(valueToUpdate) {
	
	
	var theSpan = parent.document.getElementById("rowcount");
	if(theSpan) theSpan.innerHTML = valueToUpdate;
		
	var theParentDiv = parent.document.getElementById("theResults");
	var theData = document.getElementById("theResults");
		
	if(theParentDiv && theData) {
		theParentDiv.innerHTML = theData.innerHTML;
	}
}

function updateData(theForm) {
	if(theForm) {
		theForm.submit();
	}
}


function openNewWindow(href) {
	if(href != null && href != "null") {
		window.open(href, "popup");	
	}
	return false;
}

// validateForm validates the form date.  The arguments are
// an object reference to the form, and a boolean to say if it's 
// a login, or an account creation.
function  validateForm(formReference, isLogin) {
	
	formReference.action = "processAccount.php?lang="+formReference.lang_pref.value;
	formReference.method = "post";
	
	if(isLogin) {
		if(trim(formReference.username1.value).length > 0 && trim(formReference.password1.value).length > 0) {
			// enough info to attempt a login.
			formReference.mode.value = "login";
			formReference.submit();
		}
	}
	else {
		var validForm = true;
		var extraString = "";
		
		if(trim(formReference.lang_pref.value).length == 0) {
			validForm = false;
			formReference.lang_pref.style.backgroundColor = "#FFFF00";
		}
		else {
			formReference.lang_pref.style.backgroundColor = "";
		}
		
		if(trim(formReference.first_name.value).length == 0) {
			validForm = false;
			formReference.first_name.style.backgroundColor = "#FFFF00";
		}
		else {
			formReference.first_name.style.backgroundColor = "";
		}
		
		if(trim(formReference.last_name.value).length == 0) {
			validForm = false;
			formReference.last_name.style.backgroundColor = "#FFFF00";
		}
		else {
			formReference.last_name.style.backgroundColor = "";
		}
		
		if(trim(formReference.email.value).length == 0) {
			validForm = false;
			formReference.email.style.backgroundColor = "#FFFF00";
		}
		else {
			if(!emailValidation(trim(formReference.email.value))) {
				validForm = false;
				formReference.email.style.backgroundColor = "#FFFF00";
			}
			else {
				formReference.email.style.backgroundColor = "";
			}
		}
		
		if(trim(formReference.employer.value).length == 0) {
			validForm = false;
			formReference.employer.style.backgroundColor = "#FFFF00";
		}
		else {
			formReference.employer.style.backgroundColor = "";
		}
		
		if(trim(formReference.work_telephone.value).length == 0) {
			validForm = false;
			formReference.work_telephone.style.backgroundColor = "#FFFF00";
		}
		else {
			formReference.work_telephone.style.backgroundColor = "";
		}
		
		if(trim(formReference.manager.value).length == 0) {
			validForm = false;
			formReference.manager.style.backgroundColor = "#FFFF00";
		}
		else {
			formReference.manager.style.backgroundColor = "";
		}
		
		if(trim(formReference.work_address.value).length == 0) {
			validForm = false;
			formReference.work_address.style.backgroundColor = "#FFFF00";
		}
		else {
			formReference.work_address.style.backgroundColor = "";
		}
		
		if(trim(formReference.work_city.value).length == 0) {
			validForm = false;
			formReference.work_city.style.backgroundColor = "#FFFF00";
		}
		else {
			formReference.work_city.style.backgroundColor = "";
		}
		
		if(trim(formReference.work_province.value).length == 0) {
			validForm = false;
			formReference.work_province.style.backgroundColor = "#FFFF00";
		}
		else {
			formReference.work_province.style.backgroundColor = "";
		}
		
		if(trim(formReference.work_postal.value).length == 0) {
			validForm = false;
			formReference.work_postal.style.backgroundColor = "#FFFF00";
		}
		else {
			formReference.work_postal.style.backgroundColor = "";
		}
		
		if(trim(formReference.username.value).length == 0) {
			validForm = false;
			formReference.username.style.backgroundColor = "#FFFF00";
		}
		else {
			formReference.username.style.backgroundColor = "";
		}
		
		if(trim(formReference.password.value).length == 0) {
			validForm = false;
			formReference.password.style.backgroundColor = "#FFFF00";
		}
		else {
			var password = document.getElementById("password2");
			var confPassword = document.getElementById("password3");
			
			if(trim(password.value) != trim(confPassword.value)) {
				validForm = false;
				password.style.backgroundColor = "#FFFF00";
				confPassword.style.backgroundColor = "#FFFF00";
				alert("Passwords do not match");
			}
			else {
				password.style.backgroundColor = "";
				confPassword.style.backgroundColor = "";
			}
		}
		
		if(validForm) {
			//checkUserName(formReference.username.value);
			document.getElementById("queryWindow").src = "checkUsername.php?username="+formReference.username.value
			//formReference.mode.value = "create";
			//formReference.submit();
		}
		else alert("Missing mandatory fields.");
	}
		
	
}



// use regular expressions to make sure that the email entered as it least plausible,
// with the basic mask of at least 1 valid characters before the @ and at least 2 letter hostname and 
// one 2-3 letter extension.
function emailValidation(email)
{
	var validEmailPattern = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/;
	if (validEmailPattern.test(email)) {
		return true;
	}
	else { 
	   	return false;
	}
}

// removes leading and trailing whitespace from argument string
// author: Dan Piccolo
function trim(stringToTrim) {
		return stringToTrim.replace(/^\s+/g, '').replace(/\s+$/g, '');
}

// confirmDelete confirms the user wishes to delete a record before doing so.
// author: Dan Piccolo
function confirmDelete(theHref) {
	var item = theHref.parentNode.parentNode.parentNode.parentNode;
	
	var confirmString = "Are you sure you wish to delete this " + item.id;
	
	if(item.id == "test") {
		confirmString += " question? \n\nPlease note, all answer choices for this question will also be deleted.";
	}
	else if(item.id == "user") {
		confirmString += "? \n\nPlease note, this user's test results and certificate (if any) will also be deleted. Place the "+item.id+ " as Active=N if you wish to keep it";
	}
	else if(item.id == "catalog" || item.id == "manual" || item.id == "picture" || item.id == "wallpaper" || item.id == "splash" || item.id == "banner") {
		confirmString += "? \n\nAny associated file will also be deleted.  Place the "+item.id+ " as Active=N if you wish to keep it";
	}
	else if(item.id == "staff" || item.id == "rep" || item.id=="admin" || item.id == "dealer") {
		confirmString += "? \n\nPlace the "+item.id+ " as Active=N if you wish to keep it";
	}
	else if(item.id == "logo") {
		confirmString += "? \n\nPlace the "+item.id+ " of type thumbnail as Active=N if you wish hide all these logos";
	}
	else confirmString += "?";
	
	if(confirm(confirmString)) {
		return true;
	}
	else return false;
}

// windowOpener is simply a wrapper for the window.open function, that makes it
// easier.  It accepts a URL as a string, the pop-up window name as a string, and
// the width and height as either a string or number. All windows opened with this 
// have no status bar, toolbar, address field, or menu, and it's resizable as needed.
// author: Dan Piccolo
function windowOpener(url, name, width, height) {
	
	var windowChrome =  "status=1,toolbar=0,location=0,menubar=0,resizable=0,scrollbars=1,height=" + height + ",width=" + width;

	var theWindow = null
	//alert(url);
	theWindow = window.open(url, name, windowChrome);
	
	if(width == screen.width && height == screen.height) {
		theWindow.moveTo(0,0);
	}
	return theWindow;	
}

// editRecord takes the URL from the record you are view, and changes the mode to EDIT.
function editRecord() {
	var oldHref = window.location.href;
	var newHref = oldHref.replace(/VIEW/,"EDIT");
	//alert(newHref);
	window.location = newHref;
}


// validatePasswordForm validates the form The arguments are
// an object reference to the form, and a boolean to say if it's 
// a password change, or just a password reset.
function validatePasswordForm(formReference, ischange) {
	
	var validForm = true;
	var extraString = "";

	if(ischange) {
		var password = formReference.password1;
		var confPassword = formReference.password2;
			
		if (trim(password.value).length == 0) {
			validForm = false;
			password.style.backgroundColor = "#FFFF00";
			extraString = "You must enter a new password.";
		}
		else {
			password.style.backgroundColor = "";
			
		}
		
		if (trim(confPassword.value).length == 0) {
			validForm = false;
			confPassword.style.backgroundColor = "#FFFF00";
			extraString = "You must confirm the new password.";
		}
		else {
			confPassword.style.backgroundColor = "";
		}
		
		if(trim(password.value) != trim(confPassword.value)) {
			validForm = false;
			password.style.backgroundColor = "#FFFF00";
			confPassword.style.backgroundColor = "#FFFF00";
			extraString = "Passwords do not match";
		}
		else {
			password.style.backgroundColor = "";
			confPassword.style.backgroundColor = "";
		}
	}
	else {

		if(trim(formReference.email.value).length == 0) {
			validForm = false;			
			formReference.email.style.backgroundColor = "#FFFF00";
			extraString += "Valid email address is required.\n";
		}
		else {
			if(!emailValidation(trim(formReference.email.value))) {
				validForm = false;
				formReference.email.style.backgroundColor = "#FFFF00";
				extraString += "Valid email address is required.\n";
			}
			else {
				formReference.email.style.backgroundColor = "";
			}
		}
			
		
		if(trim(formReference.username.value).length == 0) {
			validForm = false;
			formReference.username.style.backgroundColor = "#FFFF00";
			extraString += "Username is required.\n";
		}
		else {
			formReference.username.style.backgroundColor = "";
		}
	}
		
	if(validForm) {
		formReference.submit();
	}
	else alert(extraString);
}

function exportData(theForm, fakeForm) {
	if(theForm) {
		var oldAction = theForm.action;
		theForm.action = fakeForm.action;
		theForm.submit();
		theForm.action = oldAction;
	}
}

function processUserName(isValid) {
	
	if(isValid) {
		document.forms[0].mode.value = "create";
		document.forms[0].submit();
	}
	else {
		document.forms[0].username.value = "";
		document.forms[0].username.style.backgroundColor = "#FFFF00";
		alert("Username already exists");
			
	}
}
