﻿/**
*	Validation for \modules\BELMCOM\COM\frmRegister.cfm
*
*	Copyright 2007 Belmont, Inc. 
*	Version: 1.0
*			
*	Author:	Alex Tessmer
**/
document.write('<script type="text/javascript" src="/JS_FILES/formValidation.js"></script>'); 
var submitButtonId = "SubmitEmail";
var strFieldValid = "&nbsp;<img src='/IMAGES/WEBSITETEMPLATES/BELMCC/Icons/Checkmark.png' border='0'/>";
var strFieldInvalid = "&nbsp;<img src='/IMAGES/WEBSITETEMPLATES/BELMCC/Icons/Uncheckmark.png' border='0'/>";
var displayError = Array();

/**
*	Determins if an object (form field) is empty. If empty, a red * will be displayed
*	in the passed strElement tag.
*
*	@param obj = An HTML object (formfield) to check if empty.
*	@param strElement = A strElement tag to display the red * in.
**/
function validateEmpty(strObj, strElement, fieldIndex){
	var status = document.getElementById(strElement + "Status");
	var error = document.getElementById(strElement + "Error");
	if(isEmpty(strObj)){
		if(strElement != null && displayError[fieldIndex]){
			error.style.display = "block";
			status.innerHTML = strFieldInvalid;
		}
		return false;
	}else{
		if(strElement != null){
			error.style.display = "none";
			status.innerHTML = strFieldValid;
			displayError[fieldIndex] = true;
		}
		return true;
	}
}

/**
*	Determins if an object (form field) is empty. If so, it will display a red * in the passed
*	strElement tag.
*
*	@param obj1 = An HTML object (formfield) to check compare.
*	@param strObj2 = Name of the ID of the object to compare.
*	@param strElement = The strElement to display the error message in.
*	@param field = Name/Discription of the field being compared. This is used in the error message.
**/
function validateMatch(strObj1, strObj2, strElement, fieldIndex){
	var status1 = document.getElementById(strElement + "Status1");
	var status2 = document.getElementById(strElement + "Status2");
	var error = document.getElementById(strElement + "Error");
	if(!isMatch(strObj1, strObj2)){
		if(strElement != null){
			if(displayError[fieldIndex]){
				error.style.display = "block";
				status1.innerHTML = strFieldInvalid;
				status2.innerHTML = strFieldInvalid;
			}else if(!isEmpty(strObj2)){
				displayError[fieldIndex] = true;
			}
		}
		return false;
	}else{
		if(strElement != null){
			error.style.display = "none";
			status1.innerHTML = strFieldValid;
			status2.innerHTML = strFieldValid;
		}
		return true;
	}
}

/**
*	Determins if a string represents a valid email address. If invalid, a red * will be displayed
*	in the passed strElement tag.
*	
*	@param obj = An HTML object (formfield) to check.
*	@param strElement = A strElement tag to display the red * in.
**/
function validateEmail(strObj, strElement, fieldIndex){
	var obj = document.getElementById(strObj);
	var status = document.getElementById(strElement + "Status");
	var error = document.getElementById(strElement + "Error");
	if(!isEmail(obj.value)){
		if(strElement != null){
			if(displayError[fieldIndex]){
				error.style.display = "block";
				status.innerHTML = strFieldInvalid;
			}else if(!isEmpty(strObj)){
				displayError[fieldIndex] = true;
			}
		}
		return false;
	}else{
		if(strElement != null){
			error.style.display = "none";
			status.innerHTML = strFieldValid;
		}
		return true;
	}
}

/**
*	An AJAX function wich determins if an object's value is a unique UserLogin.
*	
*	@param obj = An HTML object (formfield) to check for unique UserLogin.
*	@param strElement = A strElement tag to display the error message in.	
**/
/*function validateUserLogin(strObj, strElement){
	var element = document.getElementById(strElement);
	if(!bitAjax(strObj, strAjaxUrl)){
		element.style.display = "block";
		return false;
	}else{
		element.style.display = "none";
		return true;
	}
}*/
/*

function validateUserLogin2(obj, strElement){
	var element = document.getElementById(strElement);
	
	if(obj.value == null || obj.value == ""){
		if(element != null){
			element.innerHTML = "&nbsp;*";
			element.style.color = "red";
		}
		setAjaxReturn(false);
		return;
	}else if(obj.value.indexOf(" ") != -1 || obj.value.indexOf(".") != -1){
		if(element != null){
			element.innerHTML = "<br>Invalid user name.";
			element.style.color = "red";
		}
		setAjaxReturn(false);
		return;
	}
	
	var xmlHttp;
	try{    // Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}catch (e){    // Internet Explorer
    	try{
	  		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}catch (e){
	    	try{
		  		xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}catch (e){
		  		document.getElementById(submitButtonId).style.display = "block";
				return null;
			}
		}
	}
    xmlHttp.onreadystatechange=function(){
    	if(xmlHttp.readyState==4){
			if(xmlHttp.responseText.toLowerCase().search("true") != -1){
				if(element != null){
					element.innerHTML = "&nbsp;";
					element.style.color = "";
				}
				setAjaxReturn(true);
			}else{
				if(element != null){
					element.innerHTML = "&nbsp;*<br>User name already in use.";
					element.style.color = "red";
					element.style.weight = "bold";
				}
				setAjaxReturn(false);
				hideSubmit(true);
			}
		}
    }
	// Include the current time to ensure the page is not loaded from cache.
	var url=strAjaxUrl + obj.value + "&time=" + new Date().getTime();
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);
	return ajaxReturn;
}*/

/**
*	Validates a form object.
*	The form's submit button is hidden unless the whole form is valid.
*	
*	@param obj = The form.
*	@param strSubmitButton = The name of the submit button to hide.
**/
function validateForm(obj){
	var bitIsValid = true;
	bitIsValid = validateEmpty('strContactFirst', 'ContactFirst', 0) && bitIsValid;
	bitIsValid = validateEmpty('strContactLast', 'ContactLast', 1) && bitIsValid;
	bitIsValid = validateEmail('strContactEmail', 'ContactEmail', 2) && bitIsValid;
	
	if(bitIsValid){
		formIsValid(true);
	}else{
		formIsValid(false);
	}
}

function formIsValid(bitIsValid){
	var submitButton = document.getElementById(submitButtonId);
	if(bitIsValid){
		submitButton.className = "SubmitButtonActive";
		submitButton.disabled = false;
	}else{
		submitButton.className = "SubmitButtonInactive";
		submitButton.disabled = true;
	}
}

function SetValue( strFieldID, strValue ){
	document.getElementById(strFieldID).value = strValue;
}