/***************************************
this module is to consolidate all javascript functions
created 8/24/2004 by Miguel
****************************************/
function findType(array, value){
	this.array = array;
	this.len = array.length;
	this.value = value;
	this.id = 0;
	for(i=0;i < this.len;i++){
		if(this.array[i].value == this.value){
			this.id = i;
			break;
		}
	}
	return this.id;
}
//this counts the number of characters and tabs to the next field when the max is reached
function autoTab(fieldObject){
	len = fieldObject.value.length;
	//alert(getIndex(fieldObject));
	//document.forms[0].elements[9].focus();
	if(len == fieldObject.size){
		fieldObject.form[(getIndex(fieldObject)+1) % fieldObject.form.length].focus();
	}
	return true;
}
function getIndex(input) {
	var index = -1, i = 0, found = false;
	while (i < input.form.length && index == -1){
		if (input.form[i] == input)index = i;
		else i++;
		return index;
	}
	return true;
}
function matchData(arrayName,ID,type){
	var thisArray = new Array;
	for(x=0;x < arrayName.length;x++){
		if(type=='contact') 
			{if(arrayName[x][0] == ID){thisArray = arrayName[x];break;}}
		else{ if(arrayName[x][1] == ID){thisArray = arrayName[x];break;}}
	}
	return thisArray;
}
function updateSelect(selectName, array2Search,id,formName){
	thisSelect = formName.elements[selectName];
	if(thisSelect == null) return;
	thisSelect.options.length = 0;
	n=0;
	thisSelect.options[n++] = new Option('[select an option]','');
	for(i=0;i < array2Search.length;i++){
		if(array2Search[i][1] == id){
			thisSelect.options[n++] = new Option(array2Search[i][2],array2Search[i][0]);
		}
	}
}
function showOnSelect(value, thisLayer){
	if(value != ''){
		showValue = 'block';
	}else{
		showValue='none';
	}
	document.getElementById(thisLayer).style.display = showValue;
}
function showLayers(thisLayer){
	currentDisplay = document.getElementById(thisLayer).style.display;
	if(currentDisplay == 'none' || currentDisplay == ''){
		document.getElementById(thisLayer).style.display = 'block';
	}else{
		document.getElementById(thisLayer).style.display = 'none';
	}
}
function openWin( windowURL, windowName, windowFeatures ) { 
	if (windowFeatures==""){
		windowFeatures="width=660,height=520,toolbar=0,location=0,directories=0,status=1,menuBar=0,scrollBars=1,resizable=1";
	}
	if(windowName == ''){
		windowName = 'acWindow';
	}
	//attempt to open up coldfusion generated ajax window
	try{
		try{
			var winOb = ColdFusion.Window.getWindowObject(windowName);
			ColdFusion.navigate(windowURL,windowName,mycallBack,myerrorhandler);
		}
		catch(err){
			ColdFusion.Window.create(windowName,'test window',windowURL,{height:100,width:300,modal:true,closable:true,draggable:true,resizable:true,center:true});
			var winOb = ColdFusion.Window.getWindowObject(windowName);
		}
		//mlsWindow_title.innerHTML='<font color="black">Importing MLS Data</font>';
		ColdFusion.Window.show(windowName);
	}
	catch(err){//coldfusion javascript not included so open up pop up window
		var oWin = window.open(windowURL, windowName, windowFeatures ) ;		
	}
/*if (oWin==null || typeof(oWin)=="undefined") 
		{
		alert("window failed to open");
		}
	else
		{
		oWin.moveTo(0, 0);
		oWin.focus();
		//return window.open( windowURL, windowName, windowFeatures ) ; 
	}
	*/
} 
function placeFocus(){
	if (document.forms.length > 0){
		var field = document.forms[0];
		for (i = 0; i < field.length; i++){
			if ((field.elements[i].type == "password") || (field.elements[i].type == "text") || (field.elements[i].type == "textarea")){
				try{document.forms[0].elements[i].focus();}
				catch(e){break;}
				break;
			}
		}
	}
}
function alertsWindow(n){
	switch(n) {
		case(n="password"): 
		  alertString = "Incorrect Password.";
		  break;
		case(n="badEmail"): 
		  alertString = "That is not a valid email.";
		  break;
		case(n="usedLogin"): 
		  alertString = "That login has already been taken.";
		  break;
		 case(n="invalidLogin"): 
		  alertString = "Invalid Login, please re-login.";
		  break;
		 case(n="badUserName"): 
		  alertString = "That is not a valid User Name.";
		  break;
		 case(n="blackList"): 
		  alertString = "You have exceeded the total number of lenders\you may blacklist\n\nYou must have at least 3 lender per market area available.";
		  break;
		default:
		 
		break;
	}
	window.alert(alertString);
}
/*******************************************************/
//              FORM VALIDATION SCRIPT                 //
// fields2check = [fieldname, displayText,(type),(associated field2check)]
/******************************************************
	Initialize variables:
		fieldList = list of variables not filled in
		emptyArray = alerted fields name
		fields2check = required fields
		docName = form name	
		fields2check = [form.field, display name(,fieldType:NULL-radio-checkbox-confirm-hidden,[relatedFieldArray])];
**********************************************************/
fieldList = '';
emptyArray = new Array();
fields2check = new Array();
docName = '';
function addArray(array2add,checkedYN){
	var i = 0;
	if(checkedYN){
		this.len = array2add.length;
		for(i=0;i<this.len;i++){
			fields2check.push(array2add[i]);
		}
	}else{
		this.len = fields2check.length;
		start = 0;
		for(i=0;i<this.len;i++){
			//alert(fields2check[i] + '==' + array2add[start]);
			if(fields2check[i] == array2add[start]){
				fields2check.splice(i,1);
				i=i-1;
				this.len = this.len -1;
				start++;
			}
		}
	}
}
function isblank(s){
	//took off & on 7/05/05, not deploy.
	//took off ' on 8/09/05, not deploy.
	//took off * on 8/15/05, not deploy.
	//deployed 8/15/2005 - MO
	var iChars = "|\":<>[]{}`\;@$^%";
	for (var x = 0; x < s.length; x++) {
	  if (iChars.indexOf(s.charAt(x)) != -1){
		  return true;
	  }
	}
	return false;
}
function isEmail(formfield) {
	var go = false;
	var emailList = formfield.split(',');
	var i = 0;
	if (formfield.indexOf("@") + "" != "-1" && formfield.indexOf(".") + "" != "-1") {
		for(i=0;i < emailList.length;i++){//loop through multiple email addresses
			if (emailList[i].search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1){
				go = true;
			}else{
				go = false;
				break;
			}
		}
	} else {
		go = false;
	}
	return go;
}
function formatPhone(nVar){
	var nChars = '1234567890';
	var thisPhone = '';
	var x = 0;
	for (var x = 0; x < nVar.length; x++) {
	 if (nChars.indexOf(nVar.charAt(x)) >= 0){
		 //delete character
		thisPhone = thisPhone + nVar.charAt(x);
	  }
	}
	thisPhone = thisPhone.substring(0,3) + '-' + thisPhone.substring(3,6) + '-' + thisPhone.substring(6,10);
	return thisPhone;
}

// phone number format is xxx-xxx-xxxx
function isPhone(formfield) {
	formfield = formatPhone(formfield);
	if(formfield.search(/^[1-9]\d{2}\-\s?\d{3}\-\d{4}$/)!=-1){
		return true;
	}else{
		return false;
	}
}

//checks to see if its a date
function isDate(value){
    return !isNaN(new Date(value));
}
//checks to see if the first character is a numeral
function isFirstNumber(value){
	var re = new RegExp(/\d/);
	var m = re.exec(value);
	if(m !== null){
		return m.index;
	}else{
		return 1;
	}
}

//checks for valid zipcode
//http://code.dreamincode.net/snippet70.htm
function isZip(s) {
      // Check for correct zip code
     reZip = new RegExp(/(^\d{5}$)|(^\d{5}-\d{4}$)/);
 
     if (!reZip.test(s)) {
        return false;
     }
 	return true;
}
function checkValue(indexNum,newValue,valueName,type,array2check){
	var x = 'false';
	var skip = false;
	try{
		dn_value=docName[newValue].value;
	}
	catch(error){
			//alert(error);
			//alert(newValue);
			skip = true;
	}
	/*dn_value=dn_value.replace('&','');
	dn_value=dn_value.replace("'","");*/
	if(!type && !skip){
		if(dn_value && !isblank(dn_value)){
			x = indexNum;
		}else{
			fieldList = fieldList+'  - '+valueName+'\n  ';
			emptyArray.push(newValue);
		}
	}else if(!skip){
		if(type == 'text'){
			if(docName[newValue].value && !isblank(docName[newValue].value)){
				x = indexNum;
			}else{
				fieldList = fieldList+'  - '+valueName+'\n  ';
				emptyArray.push(newValue);
			}
		}
		if(type == 'textarea'){
			if(docName[newValue].value){
				x = indexNum;
			}else{
				fieldList = fieldList+'  - '+valueName+'\n  ';
				emptyArray.push(newValue);
			}
		}
		if(type == 'req_text'){ //this field is required
			if(docName[newValue].value && !isblank(docName[newValue].value) && isFirstNumber(docName[newValue].value) != 0){
					x = indexNum;
			}else{// this value is formatted incorrectly no digits at the beginning of the value or is blank
				fieldList = fieldList+'  - '+valueName+'\n  ';
				emptyArray.push(newValue);
			}
		}
		if(type == 'zipcode'){ //this field is not required
			if(docName[newValue].value && isZip(docName[newValue].value)){
				x = indexNum;
			}else if(docName[newValue].value != ''){// this value is formatted incorrectly no digits at the beginning of the value or is blank
				fieldList = fieldList+'  - '+valueName+'\n  ';
				emptyArray.push(newValue);
			}
		}
		if(type == 'notreq'){ //this field is not required
			if(docName[newValue].value && !isblank(docName[newValue].value)){
				x = indexNum;
			}
		}
		if(type == 'notreq_text'){ //this field is not required
			if(docName[newValue].value && !isblank(docName[newValue].value)){
				if(isFirstNumber(docName[newValue].value) != 0){
					x = indexNum;
				}else if(isFirstNumber(docName[newValue].value) == 0){// this value is formatted incorrectly no digits at the beginning of the value
					fieldList = fieldList+'  - '+valueName+'\n  ';
					emptyArray.push(newValue);
				}
			}
		}
		if(type == 'date_req'){
			if(!isDate(docName[newValue].value)){
				fieldList = fieldList+'  - '+valueName+'\n  ';
				emptyArray.push(newValue);
			}
		}
		if(type == 'date_not_req'){
			if(docName[newValue].value!=' ' && docName[newValue].value!=''){
				if(!isDate(docName[newValue].value)){
					fieldList = fieldList+'  - '+valueName+'\n  ';
					emptyArray.push(newValue);
				}
			}
		}
		if(type=='hidden'){
			if(docName[newValue].value && !isblank(docName[newValue].value)){
				x = indexNum;
			}else{
				fieldList = fieldList+'  - '+valueName+'\n  ';
			}
		}
		if(type=='confirm'){
			if(docName[newValue].value != docName[array2check].value){
				fieldList = fieldList+'  - '+valueName+'\n  ';
				emptyArray.push(newValue);
			}
		}
		if(type=='login'){
			s = docName[newValue].value;
			//add this to list of not acceptable characters:
			// &
			newExp = s.split(/\W+/);
			if(newExp.length > 1 || s.search(/^\w+( \w+)?$[A-Za-z0-9]/) != -1){
				fieldList = fieldList+'  - '+valueName+'\n  ';
				emptyArray.push(newValue);
			}
		}
		if(type=='email'){
			if(!isEmail(docName[newValue].value)){
				fieldList = fieldList+'  - '+valueName+'\n  ';
				emptyArray.push(newValue);
			}
		}
		if(type=='email_not_req'){
			if(docName[newValue].value!=' ' && docName[newValue].value!=''){
				if(!isEmail(docName[newValue].value)){
					fieldList = fieldList+'  - '+valueName+'\n  ';
					emptyArray.push(newValue);
				}else{
					x = indexNum;
				}
			}
		}
		if(type=='phone'){
			//alert(!isPhone(docName[newValue].value) && !isblank(docName[newValue].value));
			if(!isPhone(docName[newValue].value) && docName[newValue].value!='' && !isblank(docName[newValue].value)){
			//if(!isPhone(docName[newValue].value) && !isblank(docName[newValue].value)){
					fieldList = fieldList+'  - '+valueName+'\n  ';
					emptyArray.push(newValue);
			}
		}
		
		if(type == 'radio'){
			if (typeof(docName[newValue].length) != 'number'){
				(docName[newValue].checked) ? x=1 : x=x;
			}else{
				for(l=0;l<docName[newValue].length;l++){
					(docName[newValue][l].checked) ? x=l : x=x;
				}
			}
			if(x == 'false'){
				fieldList = fieldList+'  - '+valueName+'\n  ';
				emptyArray.push(newValue);
			}
		}
		if(type == 'checkbox'){
			var y = 'false';
			if(typeof(docName[newValue].length) != 'number'){
				(docName[newValue].checked)? y=1 : y=y;
			}else{
				for(m=0;m<docName[newValue].length;m++){
					if(docName[newValue][m].checked){
						y = m;
						if(array2check){
							if(array2check[y]){
								checkValue(y,array2check[y][0],array2check[y][1],array2check[y][2],array2check[y][3]);
							}
						}
					}
				}
			}
			if(y == 'false'){
				fieldList = fieldList+'  - '+valueName+'\n  ';
				emptyArray.push(newValue);
			}
		}
	}
	if(x!='false' && array2check){
		///alert(x + '-' + array2check);
		if(array2check || array2check[x]){
			checkValue(x,array2check[0],array2check[1],array2check[2],array2check[3]);
		}
	}
}
function alertWindow(message){
	window.alert('The following fields are required/incorrect: \n  '+message);
	/*if(emptyArray.length > 0){
		for(i=0;i<emptyArray.length;i++){
			docName[emptyArray[i]].id = emptyArray[i];
			docName[emptyArray[i]].className = 'isRequired';
		}
		docName[emptyArray[0]].focus();
	}*/
}
function validateForm(formName){
	docName = document.forms[formName];
	fieldList = '';
	for(i=0;i<fields2check.length;i++){
		checkValue(i,fields2check[i][0],fields2check[i][1],fields2check[i][2],fields2check[i][3]);
	}
	if(fieldList == ''){
		return true;
	}else{
		alertWindow(fieldList);
		return false;
	}
}
//*******************************************************/
//              END FORM VALIDATION SCRIPT             //
/*******************************************************/
//image validation script//
img=null;
img2=null;
readyToGo=false;
function loadImg(image2upload, formName) {
	imgext = ",jpg,gif,JPG,GIF,";
	ext=image2upload;
	ext=ext.substring(ext.lastIndexOf(".")+1,ext.length);
	if(imgext.indexOf(","+ext+",")<0) {
		alert("This file type ("+ext+") is not supported.");
	} else{
		document.forms[formName].extension.value=ext;
	}
}


// calendar function
function ShowCalendar(FormName, FieldName){
	window.open("/popup.cfm?FormName=" + FormName + "&FieldName=" + FieldName, "CalendarWindow", "width=200,height=180");
}
//end of calendar function
function checkRadio(elementName,elementValue){
	var i=0;
	for(i=0;i < elementName.length;i++){
		if(elementValue.length == 1){
			if(elementName[i].value == elementValue){
				elementName[i].checked = true;
			}
		}else{
			for(x=0;x<elementValue.length;x=x+2){
				if(elementName[i].value == elementValue.charAt(x)){
					elementName[i].checked = true;
					break;
				}
			}
		}
	}
}
//populates form from elements Array
function populateForm(formName){
	tForm = document.forms[formName];
	if(elementsArray.length!= 1){
		for(e=0;e<elementsArray.length-1;e++){
			if(typeof(tForm.elements[elementsArray[e][0]]) == 'object'){
				element2check = tForm.elements[elementsArray[e][0]]; 
				value2check = elementsArray[e][1];
				switch(element2check.type){
					case "checkbox":
						if(value2check!=0 && typeof(element2check.length) == 'undefined'){
							element2check.checked = true;
						}
						break;
					case "text":
						element2check.value = value2check;
						break;
					case "textarea":
						element2check.value = value2check;
						break;
					case "hidden":
						element2check.value = value2check;
						break;
					case "select-one":
						element2check.value = value2check;
						break;
					case "submit":
						element2check.value = value2check;
						element2check.name = value2check;
					default:
						checkRadio(element2check,value2check);
						break;
				}
			}
		}
	}	
}
// return all the fieldNames in a given form
function getFormFields(formName){
	this.Form = document.forms[formName];
	formFields = this.Form.elements[0].name;
	for(i=0;i<this.Form.elements.length;i++){
		if(this.Form.elements[i].name != 'submit' && this.Form.elements[i].name != 'formFields' && i != 0){
			formFields = formFields + ',' + this.Form.elements[i].name;
		}
	}
	return formFields;
}
//resets a form
function clearForm(formName){
	var form = document.forms[formName];
	var els = form.elements;
	for(i=0;i<els.length;i++){
		//alert(els[i].name + ' = ' +els[i].value);
		if(els[i].name != 'submit' && els[i].name != '' && els[i].name != 'submitBtn' && els[i].name != 'formFields'){
			var v = ColdFusion.getElementValue(els[i].name);
			//alert(v);
			els[i].value = '';
		}
	}
}
function parseForm(array,index){
	var f;
	var returnThis='';
	for(f=0;f < array.length;f++){
		if(index == 0){
			returnThis = returnThis + array[f][index];
		}else{
			if(array[f][1] != ''){
				returnThis = returnThis + array[f][index];
			}else{
				returnThis = returnThis + '^';
			}
		}
		if(f<(array.length-1)) returnThis = returnThis + ',';
	}
	return returnThis;
}
function getName(){
	thisName = '';
	thisName = window.prompt('Enter the New Name: ','');
	if(thisName == '' && thisName != null){
		getName();
	}
	return thisName;
}

function SetAllCheckBoxes(FormName, FieldName, CheckValue){
	if(!document.forms[FormName]){
		return;
	}
	var objCheckBoxes = document.forms[FormName].elements[FieldName];
	if(!objCheckBoxes){
		return;
	}
	var countCheckBoxes = objCheckBoxes.length;
	if(!countCheckBoxes)
		objCheckBoxes.checked = CheckValue;
	else
		// set the check value for all check boxes
		for(var i = 0; i < countCheckBoxes; i++){
			objCheckBoxes[i].checked = CheckValue;
		}
}
// opens and closes iframes
openRows = new Array();
eventRows = new Array();
function openIframe(rowID, frameID, idNum, eventRow,url,begin,end,adminVar){
	for(i=0; i<openRows.length; i++){
		if(eval(openRows[i]) != rowID){
			closeIframe(eval(openRows[i]),eval(eventRows[i]));
		}
	}
	if (rowID.style.display == 'block' && url == null){
		rowID.style.display = 'none';
		if(eventRow != null){
			if(eventRow.className == 'altRow'){
				eventRow.style.background = '#e7e7e7';			
			}else if(eventRow.className == 'ltPurple'){
				eventRow.style.background = '#EBE9EE';	
			}else{
				eventRow.style.background = '#FFFFFF';
			}
		}
	}else{
		rowID.style.display = 'block';
		if(eventRow != null){
			eventRow.style.background = '#DBEDFA';
		}
		rowID.style.background = '#DBEDFA';
	}
	if(url!= null){
		document.all[frameID].src= url;
	}else{
		document.all[frameID].src='/mua/officeDetail.cfm?office_id='+ idNum + '&edit='+ adminVar;
	}

}
function closeIframe(rowID,eventID){
	rowID.style.display = 'none';
	if(eventID.className == 'altRow'){
		eventID.style.background = '#e7e7e7';			
	}else if(eventID.className == 'ltPurple'){
		eventID.style.background = '#EBE9EE';	
	}else{
		eventID.style.background = '#FFFFFF';
	}
}
function get_focus(i)
	{
		document.forms[0].elements[i].focus();
	}
//used to reset the elementsArray used in the populate Form function
function resetElements(){elementsArray = new Array();}

//used to alert users if the user needs to be deleted or not
function threeRuleAlert(field){
	var deleteUser;
	if(!field.checked){
		deleteUser = confirm('The license you are trying to adjust has been added and taken away over 3 times for this user. In order to alter the license again you must delete this user. Click OK if you wish to delete this user?');
		if(deleteUser){
			document.forms['f'].elements[document.forms['f'].elements.length-3].click();
		}else{
			field.checked = true;
		}
	}
}

//searches a list for duplicate values
function dedupeArray(value,list){
	var array2search = list.split(',');
	var value2search = new String(value);
	var thisString = new String();
	value2search = value;
	//loop through the array and look for a value
	for(d=0;d <(array2search.length-1);d++){
		thisString = thisString = new String(array2search[d]);
		if(trim(value2search) == trim(thisString)){
			return false;
		}
	}
	return true;
}
//trims a string
function trim(s) {
 	if(s.charCodeAt(0) == 10) s = s.substr(1);
  	while (s.substring(0,1) == ' ') {
    	s = s.substring(1,s.length);
  	}
  	while (s.substring(s.length-1,s.length) == ' ') {
    	s = s.substring(0,s.length-1);
  	}
  	return s;
}

// hide and show form button functions
// buttons must be within a label tag
showEl = function() {
	this.style.display = "";
	if(this.parentNode.tagName.toLowerCase() == "label")
	  this.parentNode.style.display = "";
};
hideEl = function() {
	this.style.display = "none";
	if(typeof this.checked !== "undefined") this.checked = false;
	else this.value = "";
	if(this.parentNode.tagName.toLowerCase() == "label")
	  this.parentNode.style.display = "none";
	this.hidden = true;
};

showElement = function(){
	this.style.display = 'block';
}

hideElement = function(){
	this.style.display = 'none';
}
// end form field hide functions
closeCFWindow = function(name){
	try{ColdFusion.Window.hide(trim(name));}
	catch(err){}
}