// class manipulation
var popUpWin=0;

function popUpWindow(URLStr)

{

  if(popUpWin)

  {

    if(!popUpWin.closed) popUpWin.close();

  }

  popUpWin = open(URLStr, 'popUpWin', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=yes,width=450,height=150,left=100, top=100,screenX=100,screenY=100');
  popUpWin.document.close;
  popUpWin.focus();
}


function setClassname(id, classname){ document.getElementById(id).className = classname; }
function getClassname(id){ return document.getElementById(id).className; }
function replaceClassname(id, find, replace){ setClassname(id, getClassname(id).replace(new RegExp(find), replace)); }

/* show hide drop down menus */

var old_menu = null, hide = null;
function hide_menu(id){
	if(id == null) return;
	obj = document.getElementById('dropnav-' + id);
	navobj = 'nav-' + id;
	if(obj) obj.style.visibility = 'hidden';
    if(navobj) replaceClassname(navobj, 'nav_mouseon', 'nav_mouseoff');
	clear_timeout();
}

function show_menu(id){
	if(old_menu) hide_menu(old_menu);
	obj = document.getElementById('dropnav-' + id);
	navobj = 'nav-' + id;
	if(obj){
		obj.style.visibility = 'visible';
	    obj.className = 'menu_navigation';
	}
	if(navobj){
	    replaceClassname(navobj, 'nav_mouseoff', 'nav_mouseon');
	}
	old_menu = id;
	clear_timeout();
}

function set_timeout(){
	hide = setTimeout("hide_menu('" + old_menu + "')",700);
}

function clear_timeout(){
	clearTimeout(hide);
}

function show(id, url,label) {
	var a = document.getElementById(id);
	a.setAttribute('href',url);
	a.setAttribute('title',label);
	Lightbox.prototype.start(a);
}

function validate_form(frm) {
	var msg = "";
	
	if (frm == "frm_login") {
		if (document.forms[frm].username.value == "") {
			msg += "Please enter your username.\n";
		}
		
		if (document.forms[frm].password.value == "") {
			msg += "Please enter your password.\n";
		}
	}
	
	
	if (frm == "frm_request") {
		if (document.forms[frm].email.value == "") {
			msg += "Please enter your email address.\n";
		}
	}
	
	if (frm == "frm_register") {
		if (document.forms[frm].clientno.value == "") {
			msg += "Please enter your clientno.\n";
		}
		
		if (document.forms[frm].password.value == "") {
			msg += "Please enter your password.\n";
		} else {
			if (document.forms[frm].password2.value == "") {
				msg += "Please reenter your password.\n";
			} else {
				if ((document.forms[frm].password.value.length < 6 || document.forms[frm].password.value.length > 12) ||
					(document.forms[frm].password2.value.length < 6 || document.forms[frm].password2.value.length > 12)) {
					msg += "Passwords must be 6 to 12 chars.\n";				
				} else {
					if (document.forms[frm].password.value != document.forms[frm].password2.value) {
						msg += "Passwords do not match.\n";
					}
				}
			}
		}
	}
	
	if (frm == "frm_register" || frm == "frm_contact") {
		if (document.forms[frm].firstname.value == "") {
			msg += "Please enter your first name.\n";
		}
		
		if (document.forms[frm].lastname.value == "") {
			msg += "Please enter your last name.\n";
		}
	}
	
	if (frm == "frm_register") {
		if (document.forms[frm].address.value == "") {
			msg += "Please enter your address.\n";
		}
	}
	
	if (frm == "frm_contact") {
		if (document.forms[frm].address1.value == "") {
			msg += "Please enter your address.\n";
		}
	}
	
	if (frm == "frm_register" || frm == "frm_contact") {
		if (document.forms[frm].city.value == "") {
			msg += "Please enter your city.\n";
		} 
		
		if (document.forms[frm].state.selectedIndex == 0) {
			msg += "Please select your state.\n";
		}
		
		if (document.forms[frm].zipcode.value == "") {
			msg += "Please enter your zip code.\n";			
		} else {
			if (document.forms[frm].zipcode.value.length < 5) {
				msg += "Zip code must be 5 digits.\n";
			} else if (isNaN(document.forms[frm].zipcode.value)) {
				msg += "Zip code must be digits only.\n";				
			}
		}
	}
	
	if (frm == "frm_register") {	
		if (document.forms[frm].email.value == "") {
			msg += "Please enter your email address.\n";
		} else {
			if (document.forms[frm].email.value.indexOf('@') == -1 || document.forms[frm].email.value.indexOf('.') == -1) {
				msg += "Email is not in correct format.\n";
			} else {
				var valid_chars = "abcdefghijklmnopqrstuvwxyz0123456789@._";
			
				for (x=0;x<document.forms[frm].email.length;x++) {
					if(valid_chars.indexOf(document.forms[frm].email.value.charAt(x)) == -1) {
						msg += "Email has invalid characters.\n";	
						break;
					}
				}
			}
		}
		
		if (document.forms[frm].phone.value == "") {
			msg += "Please enter your phone number.\n";
		} else {
			if (isNaN(document.forms[frm].phone.value)) {			
				msg += "Phone number must be digits only.\n";
			} else if(document.forms[frm].phone.length < 10) {
				msg += "Phone number must be 10 digits.\n";
			}
		}
	}
	
    if (frm == "frm_contact") {
		if (document.forms[frm].email.value != "") {
			if (document.forms[frm].email.value.indexOf('@') == -1 || document.forms[frm].email.value.indexOf('.') == -1) {
				msg += "Email is not in correct format.\n";
			} else {
				var valid_chars = "abcdefghijklmnopqrstuvwxyz0123456789@._-";
			
				for (x=0;x<document.forms[frm].email.length;x++) {
					if(valid_chars.indexOf(document.forms[frm].email.value.charAt(x)) == -1) {
						msg += "Email has invalid characters.\n";	
						break;
					}
				}
			}
		}
		
		if (document.forms[frm].phone.value == "") {
			msg += "Please enter your phone number.\n";
		} else {
			if (isNaN(document.forms[frm].phone.value)) {			
				msg += "Phone number must be digits only.\n";
			} else if(document.forms[frm].phone.value.length < 10) {
				msg += "Phone number must be 10 digits.\n";
			}
		}

		if (document.forms[frm].cphone.value != "") {
			if (isNaN(document.forms[frm].phone.value)) {			
				msg += "Cell phone number must be digits only.\n";
			} else if(document.forms[frm].phone.value.length < 10) {
				msg += "Cell phone number must be 10 digits.\n";
			}
		}
		
		if (document.forms[frm].time.selectedIndex == 0) {
			msg += "Please select the best time for contact.\n";
		}
		
		if (document.forms[frm].firstmort.value != "") {
			var valid_chars = "0123456789,.";
			for (x=0;x<document.forms[frm].firstmort.value.length;x++) {
				if(valid_chars.indexOf(document.forms[frm].firstmort.value.charAt(x)) == -1) {
					msg += "First mortgage has invalid characters.\n";	
					break;
				}
			}
		}
		
		if (document.forms[frm].secondmort.value == "" && document.forms[frm].second.value == "Yes") {
			msg += "Please enter the amount of your second mortgage.\n";				
		} else 	if (document.forms[frm].secondmort.value != "") {
			var valid_chars = "0123456789,.";
			for (x=0;x<document.forms[frm].secondmort.value.length;x++) {
				if(valid_chars.indexOf(document.forms[frm].secondmort.value.charAt(x)) == -1) {
					msg += "Second mortgage has invalid characters.\n";	
					break;
				}
			}
		}
		
		if (document.forms[frm].estval.value != "") {
			var valid_chars = "0123456789,.";
			for (x=0;x<document.forms[frm].estval.value.length;x++) {
				if(valid_chars.indexOf(document.forms[frm].estval.value.charAt(x)) == -1) {
					msg += "Estimated value has invalid characters.\n";	
					break;
				}
			}
		}		
	}
	
	if (msg != "") {
		alert(msg);
		return false;
	}
	
	if (frm == "frm_contact") {
		popUpWindow('');
		document.forms[frm].target = "popUpWin";
		document.forms[frm].action="sendinfo.php"
		document.forms[frm].submit();
		document.forms[frm].reset();		
	} else {
		return true;
	}
}

function show_area() {
	if (document.getElementById('register').style.visibility == "hidden") {
		document.getElementById('register').style.visibility = "visible";
		document.getElementById('register').style.display = "block";
		return;
	}
	
	if (document.getElementById('register').style.visibility == "visible") {
		document.getElementById('register').style.visibility = "hidden";
		document.getElementById('register').style.display = "none";
		return;
	}
}

function trim(str)
{
   return str.replace(/^\s+|\s+$/g,'');
}