// x browser detection
var xVersion='3.8',xOp7=false,xOp5or6=false,xIE4Up=false,xNN4=false,xUA=navigator.userAgent.toLowerCase();
if(window.opera){
  xOp7=(xUA.indexOf('opera 7')!=-1 || xUA.indexOf('opera/7')!=-1);
  if (!xOp7) xOp5or6=(xUA.indexOf('opera 5')!=-1 || xUA.indexOf('opera/5')!=-1 || xUA.indexOf('opera 6')!=-1 || xUA.indexOf('opera/6')!=-1);
}
else if(document.layers) xNN4=true;
else {xIE4Up=document.all && xUA.indexOf('msie')!=-1 && parseInt(navigator.appVersion)>=4;}

jpIE4 = (document.all) ? true:false;
jpNS6 = (!jpIE4 && document.getElementById) ? true:false;
jpNS4 = (document.layers) ? true:false;

function xDef() {
	for(var i=0; i<arguments.length; ++i){if(typeof(arguments[i])=="" || typeof(arguments[i])=="undefined") return false;}
	return true;
}

function xGetElementById(e) {
	if(typeof(e)!="string") return e;
	if(document.getElementById) e=document.getElementById(e);
	else if(document.all) e=document.all[e];
	else if(document.layers) e=xLayer(e);
	else e=null;
	return e;
}

function xShow(e) {
	if(!(e=xGetElementById(e))) return;
	if(e.style && xDef(e.style.visibility)) e.style.visibility='inherit';
	else if(xDef(e.visibility)) e.visibility='show';
}

function xHide(e) {
	if(!(e=xGetElementById(e))) return;
	if(e.style && xDef(e.style.visibility)) e.style.visibility='hidden';
	else if(xDef(e.visibility)) e.visibility='hide';
}

function displayError(message) {
	alert(message);
}

function validateEmailAddress(theAddress) {
	result = "OK";
    emailFilter = /^.+@.+\.[^\.]{2,}$/;
    illegalChars = /[\(\)\<\>\,\;\:\\\"\[\]]/;
	if (theAddress.length == 0) {
		result = "EMPTY";
	} else if (!emailFilter.test(theAddress)) {
		result = "BADFORMAT";
	} else if (theAddress.match(illegalChars)) {
		result = "BADCHARS";
	}
	return result;
}

function validatePassword(thePassword) {
	result = "OK";
	if (thePassword.length == 0) {
		result = "EMPTY";
	} else if (thePassword.length < 6) {
		result = "TOOSHORT";
	}
	return result;
}

function validateString(theString) {
	result = "OK";
	if (theString.length == 0) {
		result = "EMPTY";
	}
	return result;
}

function validateForm(theForm) {
	if (theForm.name == "login") {
		v = validateEmailAddress(theForm.login_email.value);
		if (v != "OK") {
			if (v == "EMPTY") displayError("You must provide your email address to log on.");
			if (v == "BADFORMAT" || v == "BADCHARS") displayError("The email address you provided is invalid.");
			theForm.login_email.select();
			return;
		}
		v = validatePassword(theForm.login_password.value);
		if (v == "EMPTY") {
			displayError("You must provide a valid password to log in.");
			theForm.login_password.select();
			return;
		}
		theForm.mode.value = "login";
		theForm.useragent.value = navigator.userAgent;
		theForm.submit();
	}
	if (theForm.name == "add_project" || theForm.name == "change_project") {
		v = validateString(theForm.project_name.value);
		if (v != "OK") {
			displayError("You must provide a project name.");
			theForm.project_name.select();
			return;
		}
		v = validateString(theForm.project_status[theForm.project_status.selectedIndex].value);
		if (v != "OK") {
			displayError("You must select a project status.");
			theForm.project_status.focus();
			return;
		}
		fDay = theForm.project_start_day[theForm.project_start_day.selectedIndex].value;
		fMonth = theForm.project_start_month[theForm.project_start_month.selectedIndex].value;
		fYear = theForm.project_start_year[theForm.project_start_year.selectedIndex].value;
		if (fDay == 0 || fMonth == 0 || fYear == 0) {
			displayError("You must select a start date.");
			theForm.project_start_day.focus();
			return;
		}
		selectPickList('project_hisercontacts');
		selectPickList('project_clientcontacts');
		selectPickList('project_related');
		theForm.submit();
	}
	if (theForm.name == "project_user_roles") {
		theForm.submit();
	}
	if (theForm.name == "add_document" || theForm.name == "change_document") {
		v = validateString(theForm.document_title.value);
		if (v != "OK") {
			displayError("You must provide a document title.");
			theForm.document_title.select();
			return;
		}
		v = validateString(theForm.document_category[theForm.document_category.selectedIndex].value);
		if (v != "OK") {
			displayError("You must select a document category.");
			theForm.document_category.focus();
			return;
		}
		v = validateString(theForm.document_status[theForm.document_status.selectedIndex].value);
		if (v != "OK") {
			displayError("You must select a document status.");
			theForm.document_status.focus();
			return;
		}
		if (theForm.name == "add_document") {
			v = validateString(theForm.document_file.value);
			if (v != "OK") {
				displayError("You must select a document to add.");
				theForm.document_file.focus();
				return;
			}
		}
		theForm.submit();
	}
	if (theForm.name == "your_details") {
		v = validateString(theForm.user_firstname.value);
		if (v != "OK") {
			displayError("You must provide your first name.");
			theForm.user_firstname.select();
			return;
		}
		v = validateString(theForm.user_lastname.value);
		if (v != "OK") {
			displayError("You must provide your last name.");
			theForm.user_lastname.select();
			return;
		}
		v = validateEmailAddress(theForm.user_email.value);
		if (v != "OK") {
			if (v == "EMPTY") displayError("You must provide your email address.");
			if (v == "BADFORMAT" || v == "BADCHARS") displayError("The email address you provided is invalid.");
			theForm.user_email.select();
			return;
		}
		v = validatePassword(theForm.user_password.value);
		if (v != "OK") {
			displayError("You must provide a password of at least 6 characters.");
			theForm.user_password.select();
			return;
		}
		theForm.submit();
	}
	if (theForm.name == "request_access") {
		v = validateString(theForm.user_firstname.value);
		if (v != "OK") {
			displayError("You must provide the user's first name.");
			theForm.user_firstname.select();
			return;
		}
		v = validateString(theForm.user_lastname.value);
		if (v != "OK") {
			displayError("You must provide the user's last name.");
			theForm.user_lastname.select();
			return;
		}
		v = validateEmailAddress(theForm.user_email.value);
		if (v != "OK") {
			if (v == "EMPTY") displayError("You must provide the user's email address.");
			if (v == "BADFORMAT" || v == "BADCHARS") displayError("The email address you provided is invalid.");
			theForm.user_email.select();
			return;
		}
		theForm.submit();
	}
	if (theForm.name == "add_user" || theForm.name == "change_user") {
		v = validateString(theForm.user_firstname.value);
		if (v != "OK") {
			displayError("You must provide the user's first name.");
			theForm.user_firstname.select();
			return;
		}
		v = validateString(theForm.user_lastname.value);
		if (v != "OK") {
			displayError("You must provide the user's last name.");
			theForm.user_lastname.select();
			return;
		}
		v = validateEmailAddress(theForm.user_email.value);
		if (v != "OK") {
			if (v == "EMPTY") displayError("You must provide the user's email address.");
			if (v == "BADFORMAT" || v == "BADCHARS") displayError("The email address you provided is invalid.");
			theForm.user_email.select();
			return;
		}
		v = validateString(theForm.user_companyid[theForm.user_companyid.selectedIndex].value);
		if (v != "OK") {
			displayError("You must select the user's company.");
			theForm.user_companyid.focus();
			return;
		}
		v = validateString(theForm.user_status[theForm.user_status.selectedIndex].value);
		if (v != "OK") {
			displayError("You must select the user's status.");
			theForm.user_status.focus();
			return;
		}
		v = validatePassword(theForm.user_password.value);
		if (v != "OK") {
			if (v == "EMPTY") displayError("You must provide the user's password.");
			if (v == "TOOSHORT") displayError("Passwords must be at least 6 characters long.");
			theForm.user_password.select();
			return;
		}
		theForm.submit();
	}
	if (theForm.name == "add_company" || theForm.name == "change_company") {
		v = validateString(theForm.company_name.value);
		if (v != "OK") {
			displayError("You must provide the company's name.");
			theForm.company_name.select();
			return;
		}
		v = validateString(theForm.company_hisercodeprefix.value);
		if (v != "OK") {
			displayError("You must provide the company's Hiser code prefix.");
			theForm.company_hisercodeprefix.select();
			return;
		}
		v = validateString(theForm.company_status[theForm.company_status.selectedIndex].value);
		if (v != "OK") {
			displayError("You must select the company's status.");
			theForm.company_status.focus();
			return;
		}
		theForm.submit();
	}
}

function submitOnEnterKey(theForm) {
	if (xIE4Up && event.keyCode == 13) validateForm(theForm);
}

function passwordReminder(theForm) {
	v = validateEmailAddress(theForm.login_email.value);
	if (v != "OK") {
		if (v == "EMPTY") displayError("You must provide your email address to receive a password reminder.");
		if (v == "BADFORMAT" || v == "BADCHARS") displayError("The email address you provided is invalid.");
		theForm.login_email.select();
		return;
	}
	theForm.mode.value = "remind";
	theForm.submit();
}

function navigateYourProjectsShowSort(theForm) {
	v1 = validateString(theForm.project_status[theForm.project_status.selectedIndex].value);
	v2 = validateString(theForm.project_sort[theForm.project_sort.selectedIndex].value);
	if (v1 == "OK" && v2 == "OK") theForm.submit();
}

function navigateYourProjectsRecent(theForm) {
	projectSelected = theForm.recent_projects[theForm.recent_projects.selectedIndex].value;
	if (projectSelected != "") location.href = "project_documents.cfm?projectid=" + projectSelected;
}

function navigateProjectDocumentsStatus(theForm) {
	v = validateString(theForm.document_status[theForm.document_status.selectedIndex].value);
	if (v == "OK") theForm.submit();
}

function navigateProjectDocumentsRelatedProjects(theForm) {
	projectSelected = theForm.related_projects[theForm.related_projects.selectedIndex].value;
	if (projectSelected != "") location.href = "project_documents.cfm?projectid=" + projectSelected;
}

function processUserCompanyChange(theForm) {
	userCompany = theForm.user_companyid[theForm.user_companyid.selectedIndex].value;
	if (userCompany == "HISER") { 
		// show the box
		xShow("administratorcheckbox");
	} else {
		// hide the box
		xHide("administratorcheckbox");
		theForm.user_isadministrator.checked = 0;
	}
}

function processUserStatusChange(theForm) {
	oldStatus = theForm.user_oldstatus.value;
	newStatus = theForm.user_status[theForm.user_status.selectedIndex].value;
	if ((oldStatus == "" || oldStatus == 0) && newStatus == 1) { 
		// show the box
		xShow("notifycheckbox");
		theForm.user_notify.checked = 1;
	} else {
		// hide the box
		xHide("notifycheckbox");
		theForm.user_notify.checked = 0;
	}
}

function processProjectStatusSelectChange(theForm) {
	oldstatus = theForm.old_project_status.value;
	newstatus = theForm.project_status[theForm.project_status.selectedIndex].value;
	if (newstatus == "5") {
		uconfirm = confirm("Setting the status of this project to \"Deleted\" will remove\nthis project and ALL of its documents from the Client Centre.\n\nAre you sure you want to continue?");
		if (uconfirm == false) {
			for (i = 0; i < theForm.project_status.length; i++) {
				if (theForm.project_status[i].value == oldstatus) theForm.project_status[i].selected = true;
			}
		}
	}
}

function processHiserContactSelectChange(theForm) {

}

function processClientContactSelectChange(theForm) {

}

donePreLoadRollovers = 0;

function swapOn(imageName) {
	if (!document.images || donePreLoadRollovers == 0) return;
	document.images[imageName].src = eval(imageName + "On.src");
}

function swapOff(imageName) {
	if (!document.images || donePreLoadRollovers == 0) return;
	document.images[imageName].src = eval(imageName + "Off.src");
}

function preLoadRollovers() {

	emailreminderOn = new Image();
	emailreminderOn.src = "img/hemailreminder2.gif";
	emailreminderOff = new Image();
	emailreminderOff.src = "img/hemailreminder1.gif";

	loginOn = new Image();
	loginOn.src = "img/hlogin2.gif";
	loginOff = new Image();
	loginOff.src = "img/hlogin1.gif";

	cancelOn = new Image();
	cancelOn.src = "img/hcancel2.gif";
	cancelOff = new Image();
	cancelOff.src = "img/hcancel1.gif";

	goOn = new Image();
	goOn.src = "img/hgo2.gif";
	goOff = new Image();
	goOff.src = "img/hgo1.gif";

	go1On = goOn;
	go2On = goOn;
	go3On = goOn;
	go1Off = goOff;
	go2Off = goOff;
	go3Off = goOff;

	/*
	go1On = new Image();
	go1On.src = "img/hgo2.gif";
	go1Off = new Image();
	go1Off.src = "img/hgo1.gif";

	go2On = new Image();
	go2On.src = "img/hgo2.gif";
	go2Off = new Image();
	go2Off.src = "img/hgo1.gif";

	go3On = new Image();
	go3On.src = "img/hgo2.gif";
	go3Off = new Image();
	go3Off.src = "img/hgo1.gif";
	*/

	savechangesOn = new Image();
	savechangesOn.src = "img/hsavechanges2.gif";
	savechangesOff = new Image();
	savechangesOff.src = "img/hsavechanges1.gif";

	sendrequestOn = new Image();
	sendrequestOn.src = "img/hsendrequest2.gif";
	sendrequestOff = new Image();
	sendrequestOff.src = "img/hsendrequest1.gif";

	addcompanyOn = new Image();
	addcompanyOn.src = "img/haddcompany2.gif";
	addcompanyOff = new Image();
	addcompanyOff.src = "img/haddcompany1.gif";

	addanewcompanyOn = new Image();
	addanewcompanyOn.src = "img/haddanewcompany2.gif";
	addanewcompanyOff = new Image();
	addanewcompanyOff.src = "img/haddanewcompany1.gif";

	adddocumentOn = new Image();
	adddocumentOn.src = "img/hadddocument2.gif";
	adddocumentOff = new Image();
	adddocumentOff.src = "img/hadddocument1.gif";

	addanewdocumentOn = new Image();
	addanewdocumentOn.src = "img/haddanewdocument2.gif";
	addanewdocumentOff = new Image();
	addanewdocumentOff.src = "img/haddanewdocument1.gif";

	addprojectOn = new Image();
	addprojectOn.src = "img/haddproject2.gif";
	addprojectOff = new Image();
	addprojectOff.src = "img/haddproject1.gif";

	addanewprojectOn = new Image();
	addanewprojectOn.src = "img/haddanewproject2.gif";
	addanewprojectOff = new Image();
	addanewprojectOff.src = "img/haddanewproject1.gif";

	adduserOn = new Image();
	adduserOn.src = "img/hadduser2.gif";
	adduserOff = new Image();
	adduserOff.src = "img/hadduser1.gif";

	addanewuserOn = new Image();
	addanewuserOn.src = "img/haddanewuser2.gif";
	addanewuserOff = new Image();
	addanewuserOff.src = "img/haddanewuser1.gif";

	continueOn = new Image();
	continueOn.src = "img/hcontinue2.gif";
	continueOff = new Image();
	continueOff.src = "img/hcontinue1.gif";

	updatecontactsOn = new Image();
	updatecontactsOn.src = "img/hupdatecontacts2.gif";
	updatecontactsOff = new Image();
	updatecontactsOff.src = "img/hupdatecontacts1.gif";

	savecontactsOn = new Image();
	savecontactsOn.src = "img/hsavecontacts2.gif";
	savecontactsOff = new Image();
	savecontactsOff.src = "img/hsavecontacts1.gif";

	donePreLoadRollovers = 1;
}

function movePicklistSelection(from, to, totop, highlight) {
	fromList = xGetElementById(from);
	toList = xGetElementById(to);
	if (fromList == null || toList == null) return;
	fromOptions = fromList.options;
	fromIndex = fromList.selectedIndex;
	toOptions = toList.options;
	toOptionsLength = toOptions.length;
	while (fromIndex > -1) {
		toOptions[toOptionsLength] = new Option(fromList[fromIndex].text);
		toOptions[toOptionsLength].value = fromList[fromIndex].value;
		if (totop == 1) {
			// move all the options down
			workOptions = toList.options;
			workOptionsLength = workOptions.length - 1;
			for (i = workOptionsLength; i > 0; i--) {
				toList[i].text = toList[i-1].text;
				toList[i].value = toList[i-1].value;
			}
			// create the new option in to list
			toOptions[0] = new Option(fromList[fromIndex].text);
			toOptions[0].value = fromList[fromIndex].value;
		}
		// remove the selected option in from list
		fromOptions[fromIndex] = null;
		// next selection
		fromIndex = fromList.selectedIndex;
		toOptionsLength = toOptions.length;
	}
	deselectPickList(to);
	if (totop == 1 && highlight == 1) selectFirstInPickList(to);
	if (totop == 0 && highlight == 1) selectLastInPickList(to);
}
function selectFirstInPickList(list) {
	workList = xGetElementById(list);
	if (workList == null) return;
	workOptions = workList.options;
	workOptionsLength = workOptions.length - 1;
	if (workOptionsLength > -1) {
		workOptions[0].selected = true;
	}
}
function selectLastInPickList(list) {
	workList = xGetElementById(list);
	if (workList == null) return;
	workOptions = workList.options;
	workOptionsLength = workOptions.length - 1;
	if (workOptionsLength > -1) {
		workOptions[workOptionsLength].selected = true;
	}
}
function sortPickList(list) {
	workList = xGetElementById(list);
	if (workList == null) return;
	workOptions = workList.options;
	workOptionsIndex = workOptions.length - 1;
	while (workOptionsIndex > 0 && workOptions[workOptionsIndex] && workOptions[workOptionsIndex - 1] && compareIsLess(workOptions[workOptionsIndex].text, workOptions[workOptionsIndex - 1].text)) {
		tempText = workOptions[workOptionsIndex - 1].text;
		tempValue = workOptions[workOptionsIndex - 1].value;
		workOptions[workOptionsIndex - 1].text = workOptions[workOptionsIndex].text;
		workOptions[workOptionsIndex - 1].value = workOptions[workOptionsIndex].value;
		workOptions[workOptionsIndex].text = tempText;
		workOptions[workOptionsIndex].value = tempValue;
		workOptionsIndex = workOptionsIndex - 1;
	}
}
function compareIsLess(a, b) {
	aWork = a.substr(a.indexOf(' ') + 1, a.length);
	bWork = b.substr(b.indexOf(' ') + 1, b.length);
	return (aWork < bWork);
}
function selectPickList(list) {
	workList = xGetElementById(list);
	if (workList == null) return;
	workOptions = workList.options;
	workOptionsLength = workOptions.length;
	for (i = 0; i < workOptionsLength; i++) {
		workOptions[i].selected = true;
	}
}
function deselectPickList(list) {
	workList = xGetElementById(list);
	if (workList == null) return;
	workOptions = workList.options;
	workOptionsLength = workOptions.length;
	for (i = 0; i < workOptionsLength; i++) {
		workOptions[i].selected = false;
	}
}

