var focusElement;
//Sets the path of an image element.
function changeImage(element, path) {
	element.src = path;
}

function capitalize(element){
	var origString = element.value;
	
	element.value = origString.toUpperCase();
}

function formatText(element){
	var origString = element.value;

	element.value = origString.toUpperCase();
	
	//this next part is done to stay at the end of what is in the text box
	//when the entry is longer than the text box
	var r = element.createTextRange();
	r.moveStart('character', element.value.length);
	r.collapse();
	r.select();
}

function formatEmail(element){
	var origString = element.value;
	var returnString = "";
	for(var i = 0; i < origString.length; i++){

		if(origString.charAt(i) != " "){
			returnString += origString.charAt(i);
		}
	}
	element.value = returnString;
	
	//this next part is done to stay at the end of what is in the text box
	//when the entry is longer than the text box
	var r = element.createTextRange();
	r.moveStart('character', element.value.length);
	r.collapse();
	r.select();
}

function formatPhone(element){
	var origString = "";
	var returnString = "";
	var digitPattern = /\d/;

	origString = element.value;
	for(var i = 0; i < origString.length; i++){
		if(returnString.length == 0){
			returnString = "(";
			if(origString.charAt(i) != "(" && origString.charAt(i).match(digitPattern)){
				returnString += origString.charAt(i);
			}
		}
		else if(returnString.length == 4){
			returnString += ")";
			if(origString.charAt(i) != ")" && origString.charAt(i).match(digitPattern)){
				returnString += origString.charAt(i);
			}
		}
		else if(returnString.length == 8){
			returnString += "-";
			if(origString.charAt(i) != "-" && origString.charAt(i).match(digitPattern)){
				returnString += origString.charAt(i);
			}
		}
		else if(origString.charAt(i).match(digitPattern) && returnString.length < 13){
			returnString += origString.charAt(i);
		}
	}
	element.value = returnString;
	
	//this next part is done to stay at the end of what is in the text box
	//when the entry is longer than the text box
	var r = element.createTextRange();
	r.moveStart('character', element.value.length);
	r.collapse();
	r.select();
}

function PrintPopBehind(xJSP) {
	if (!(navigator.userAgent.indexOf("MSIE 5.5")>=0
			|| navigator.userAgent.indexOf("MSIE 6") >= 0)
			|| window.external.Document == null){
		window.open(xJSP,'','scrollbars=yes,left=100,top=100,width=600,height=400,location=0,menubar=1,resizable=yes');
	}
}

function setFocusName(inputBox) {
	if(focusElement != null && focusElement.className != "HideMe"){
		focusElement.className = "InputUnhighlight";
	}
	focusElement = inputBox;
	
	var r = inputBox.createTextRange();
	r.moveStart('character', inputBox.value.length);
	r.collapse();
	r.select();
}

function Refocus() {
	if (focusElement != null || focusElement != "") {
		try{
			focusElement.focus();
		}
		catch(e){
		}
	}
}

function processBack() {
	try {
		var currentid = document.all.currentid.value;
		var previousid = 0;

		// Get the last Digit in the value (indicating the number of the field).
            currentid = currentid.substring(currentid.length-1, currentid.length);

		if ( currentid == "0" ) {
			javascript:history.back();
		} else {
			previousid = "input" + (parseInt(currentid) - 1);

			// Set focus to previous field.
                  document.all(previousid).focus();
		}
	} catch (e) {
		// Go back a page since the above elements only exist during the input page.
		javascript:history.back();
	}
}

function processContinue() {
	try {
            // Get ID values from document to indicate which field on the page has focus.
		var currentid = document.all.currentid.value;
		var lastid    = document.all.lastid.value;
            var nextid    = 1;

            // Get the last Digit in the value (indicating the number of the field).
            currentid = currentid.substring(currentid.length-1, currentid.length);
            lastid = lastid.substring(lastid.length-1, lastid.length);

		if ( currentid == lastid ) {
			document.forms[0].submit();
		} else {
                  nextid = "input" + (parseInt(currentid) + 1);

			// Set focus to next field.
                  document.all(nextid).focus();
		}
	} catch (e) {
		// Submit form since the above elements only exist during the input page.
		document.forms[0].submit();
	}
}

function setupRedirect(nextPage){
	redirPage = nextPage;
	if(redirPage.indexOf("/templates") == 0){
		redirPage = redirPage.substring(10);
	}
	document.location.href=redirPage;
}

function validateEmail(email){
	var result = false;

	var emailPattern = /^([a-zA-Z0-9._+-]+@[a-zA-Z0-9._-]+\.[a-zA-Z]*){1,50}$/;
	
	if(emailPattern.test(email)){
		result = true;
	}

	return result;
}

function enter(input){
	if(input == "backspace"){
		var numBackSpaces = 1;
		//We have to jump back 2 spaces when we hit a '-' or ')' in the phone fields so that
		//it formats correctly
		if(focusElement.id == "createHomePhoneGuest1"
				|| focusElement.id == "createWorkPhoneGuest1"
				|| focusElement.id == "createHomePhoneGuest2"
				|| focusElement.id == "createWorkPhoneGuest2"
				|| focusElement.id == "editHomePhone"
				|| focusElement.id == "editWorkPhone"){
			if(focusElement.value.charAt(focusElement.value.length - 1) == '-'
					|| focusElement.value.charAt(focusElement.value.length - 1) == ')'){
				numBackSpaces = 2;
			}
		}
		if(focusElement.value.length >= numBackSpaces){
			focusElement.value = focusElement.value.substring(0,focusElement.value.length - numBackSpaces);
		}
	}
	else{
		if(focusElement.value.length < focusElement.maxLength){
			if(focusElement.id == "createHomePhoneGuest1"
					|| focusElement.id == "createWorkPhoneGuest1"
					|| focusElement.id == "createHomePhoneGuest2"
					|| focusElement.id == "createWorkPhoneGuest2"
					|| focusElement.id == "editHomePhone"
					|| focusElement.id == "editWorkPhone"){
				focusElement.value += input;
				formatPhone(focusElement);
			}
			else{
				if((focusElement.id != "createEmailAddressGuest1"
						&& focusElement.id != "createEmailAddressGuest2"
						&& focusElement.id != "editEmailAddress")
						|| input != " "){
					focusElement.value += input;
				}
			}
		}
	}
	
	//this next part is done to stay at the end of what is in the text box
	//when the entry is longer than the text box
	var r = focusElement.createTextRange();
	r.moveStart('character', focusElement.value.length);
	r.collapse();
	r.select();
}
