function LimitEditChars(element, maxChars) {

	if ( document.all(element).type == "textarea" ) {
		if (document.all(element).innerText.length > maxChars ) {
			document.all(element).innerText = document.all(element).innerText.substring(0,maxChars);
		}
	} else if ( document.all(element).type == "text") {
		if (document.all(element).value.length > maxChars ) {
			document.all(element).value = document.all(element).value.substring(0,maxChars);
		}
	}
}

function validateLoginForm() {
	var username = document.login_form.username.value;
	var password = document.login_form.password.value;
	
	if (username == "") {
		alert("You must enter a username.");
		document.login_form.username.focus();
		return false;
	}
	if (password == "") {
		alert("You must enter a password.");
		document.login_form.password.focus();
		return false;
	}
	
	encodePassword(password);
}

function encodePassword(password) {
    var hash = hex_md5(password);
	document.login_form.password.value = hash;
	return true;
}

function replaceChars(entry) {
	out = "\\"; // replace this
	add = "/";  // with this
	temp = "" + entry; // temporary holder

	while (temp.indexOf(out)>-1) {
		pos= temp.indexOf(out);
		temp = "" + (temp.substring(0, pos) + add + 
		temp.substring((pos + out.length), temp.length));
	}
	return temp;
}

function isNumeric(strString) {
	var strValidChars = "0123456789+ ";
	var strChar;
	var blnResult = true;

	if (strString.length == 0) return false;
	//  test strString consists of valid characters listed above
	for (i = 0; i < strString.length && blnResult == true; i++) {
		strChar = strString.charAt(i);
		if (strValidChars.indexOf(strChar) == -1) {
			blnResult = false;
		}
	}
	return blnResult;
}

function computePayment() {
	loan = document.loan_form.loan.value;
	term = document.loan_form.term.value;
	rate = document.loan_form.rate.value;
	
	if (isNaN(loan) || loan < 1) {
		document.loan_form.payment.value = '';
		return false;
	}
	if (isNaN(term) || term < 1) {
		document.loan_form.payment.value = '';
		return false;
	}
	if (isNaN(rate) || rate < .1) {
		document.loan_form.payment.value = '';
		return false;
	}
	var payment = 0;
	var payment12 = 0;
	rate = 1 + (document.loan_form.rate.value/100);
	rate = Math.pow(rate,(1/12));
	term = document.loan_form.term.value*12;
	loan = document.loan_form.loan.value;
	var x = 0;
	x = Math.pow(rate,term);
	rate12 = Math.pow(1.12,(1/12));
	x12 = Math.pow(rate12,term);
	if (x==1) {
		payment = loan/term;
	} else {
		payment = ((loan*x)*(1-rate))/(1-x);
	}
	document.loan_form.payment.value = Math.round(payment*100)/100;
}

function hideBlock(divId) {
	document.getElementById(divId).style.display = "none";
}

function showBlock(divId) {
	document.getElementById(divId).style.display = "block";
}

function showTerms() {
    newWindow = window.open(baseUrl+'/home/terms', 'newWindow', 'toolbar=no, location=no, scrollbars=yes, resizable=yes, width=750, height=620, left=150, top=150');
}

function validateCategoryForm() {
	var categoryName = document.category_form.cat_name.value;
	
	if (categoryName == "") {
		alert("You must supply a Category name.");
		document.category_form.cat_name.focus();
		return false;
	}
}

function cancelContent(section) {
	document.forms[0].action = baseUrl+"/content/"+section;
	document.forms[0].submit();	
}

function cancelEditContent(section) {
    var subCategoryId = document.forms[0].sub_category_id.value;
    
	document.forms[0].action = baseUrl+"/content/"+section+"/"+subCategoryId;
	document.forms[0].submit();	
}

function selectSubCategoryType() {
	document.forms[0].action = baseUrl+"/content/selectSubCategoryType";
	document.forms[0].submit();	
}

function addSubCategory() {
    var length = document.sub_category_form.sub_category_type.length;

    for (i = 0; i < length; i++) {
        if (document.sub_category_form.sub_category_type[i].checked) {
            var subCategoryType = document.sub_category_form.sub_category_type[i].value;
        }
    }
    
    if (subCategoryType == 0) {
        document.sub_category_form.action = baseUrl+"/content/addTabularSubCategory";
    } else {
        document.sub_category_form.action = baseUrl+"/content/addFreeTextSubCategory";
    }
	document.sub_category_form.submit();	
}

function showFeatureImage() {
    var imageId = document.getElementById('feature_image_id').value;
    var imageBlock = document.getElementById('image_block');
    if (imageId == -1) {
        imageBlock.style.display = "none";
    } else {
        imageName = featureImages[imageId];
        imageBlock.style.display = "block";
        photoDisplay = "document.sub_category_form.feature_image.src = '"+wwwUrl+"/images/"+imageName+"'";
        eval(photoDisplay);
    }
}

function cancelEditText() {
    var subCategoryId = document.forms[0].sub_category_id.value;
    
	document.forms[0].action = baseUrl+"/content/viewTabularSubCategory/"+subCategoryId;
	document.forms[0].submit();	
}

function validateSubCategoryForm() {
	var pageTitle = document.sub_category_form.sub_cat_name.value;
	
	if (pageTitle == "") {
		alert("You must enter a Page Title.");
		document.sub_category_form.sub_cat_name.focus();
		return false;
	}
}

function validateDealForm() {
	var destination = document.sub_category_form.destination.value;
	
	if (destination == "") {
		alert("You must enter a Destination.");
		document.sub_category_form.destination.focus();
		return false;
	}
}

function validateNewsForm() {
	var description = document.news_form.description.value;
	
	if (description == "") {
		alert("You must enter a Description.");
		document.news_form.description.focus();
		return false;
	}
}

function cancelUser(section) {
	document.forms[0].action = baseUrl+"/user/"+section;
	document.forms[0].submit();	
}

function validateUserForm() {
    var username = document.user_form.username.value;
    var password = document.user_form.password.value;
    var verifyPassword = document.user_form.verify_password.value;
    
    if (username == "") {
		alert("You must specify a Username.");
		document.user_form.username.focus();
		return false;
	}
	if (password == "") {
		alert("You must specify a Password.");
		document.user_form.password.focus();
		return false;
	}
	if (verifyPassword == "") {
		alert("You must verify the Password.");
		document.user_form.verify_password.focus();
		return false;
	}
	if (verifyPassword != password) {
		alert("Password and Verification must match");
		document.user_form.password.focus();
		return false;
	}
	encodeUserPassword(password);
}

function validateEditUserForm() {
    var username = document.user_form.username.value;
    var password = document.user_form.password.value;
    var verifyPassword = document.user_form.verify_password.value;
    
    if (username == "") {
		alert("You must specify a Username.");
		document.user_form.username.focus();
		return false;
	}
	if (verifyPassword != password) {
		alert("Password and Verification must match");
		document.user_form.password.focus();
		return false;
	}
	if (password != "") {
	    encodeUserPassword(password);
	}
}

function encodeUserPassword(password) {
    var hash = hex_md5(password);
	document.user_form.password.value = hash;
	return true;
}

function validateQueryForm() {
	name = document.query_form.name.value;

	if (name == "") {
		alert("You must enter a name!");
		document.query_form.name.focus();
		return false;
	 }
}

function validateContactForm() {
	name = document.contact_form.full_name.value;
	email = document.contact_form.email.value;
	phone = document.contact_form.phone.value;
	
	if (name == "") {
		alert("You must enter a name!");
		document.contact_form.full_name.focus();
		return false;
	 }
	 
	 if (email == "" && phone == "") {
		alert("You must enter an email addesss or contact telephone number!");
		document.contact_form.email.focus();
		return false;
	 }
	 
	if (!isEmail(emailAddress)) { 
        alert('Invalid email address!');
        document.subscribe_form.email.focus(); 
        return false;
	}
}

function isEmail (sEmail) {
    var regEmail = /^([\w-]+\.?)*\w+@([\da-zA-z-]+\.)+[a-zA-z]{2,3}$/;
    if (sEmail==null || sEmail == "") { 
        return false;
    } else {
        return regEmail.test(sEmail);
    }
}

function validateEmail() {
	emailAddress = document.subscribe_form.email.value;
	if (!isEmail(emailAddress)) { 
        alert('Invalid email address!');
        document.subscribe_form.email.focus(); 
        return false;
	}
}

function removeEmail() {
	document.subscribe_form.action = baseUrl+'/home/removeEmail';
	document.subscribe_form.submit();
}

function displayInfo(tabularDataId) {
	newwindow=window.open(baseUrl+'/home/displayInfo/' + tabularDataId,'name','scrollbars=1,height=300,width=400');
	if (window.focus) {newwindow.focus()}
}