// Name...........: Main Javascript file
// Version........: 1.00
// Copyright......: Core Marketing Solutions, All Rights Reserved

// --------------------------------------------------
// CSS Dropdown Box Support (courtesy alistapart.com)
// --------------------------------------------------

startList = function() {
	if (document.all&&document.getElementById) {
		navRoot = document.getElementById("menubar");
		for (i=0; i<navRoot.childNodes.length; i++) {
			node = navRoot.childNodes[i];
			if (node.nodeName=="LI") {
				node.onmouseover=function() {
					this.className+=" over";
				}
				node.onmouseout=function() {
					this.className=this.className.replace(" over", "");
				}
			}
		}

		navRoot = document.getElementById("nav");
		if (navRoot != null) {
			for (i=0; i<navRoot.childNodes.length; i++) {
				node = navRoot.childNodes[i];
				if (node.nodeName=="LI") {
					node.onmouseover=function() {
						this.className+=" over";
					}
					node.onmouseout=function() {
						this.className=this.className.replace(" over", "");
					}
				}
			}
		}

		navRoot = document.getElementById("nav1");
		if (navRoot != null) {
			for (i=0; i<navRoot.childNodes.length; i++) {
				node = navRoot.childNodes[i];
				if (node.nodeName=="LI") {
					node.onmouseover=function() {
						this.className+=" over";
					}
					node.onmouseout=function() {
						this.className=this.className.replace(" over", "");
					}
				}
			}
		}

		navRoot = document.getElementById("nav2");
		if (navRoot != null) {
			for (i=0; i<navRoot.childNodes.length; i++) {
				node = navRoot.childNodes[i];
				if (node.nodeName=="LI") {
					node.onmouseover=function() {
						this.className+=" over";
					}
					node.onmouseout=function() {
						this.className=this.className.replace(" over", "");
					}
				}
			}
		}

		navRoot = document.getElementById("nav3");
		if (navRoot != null) {
			for (i=0; i<navRoot.childNodes.length; i++) {
				node = navRoot.childNodes[i];
				if (node.nodeName=="LI") {
					node.onmouseover=function() {
						this.className+=" over";
					}
					node.onmouseout=function() {
						this.className=this.className.replace(" over", "");
					}
				}
			}
		}
	}
}
window.onload=startList;

// ---------------------------------
// Form Input Validator - Contact Us
// ---------------------------------

	// Declaring required variables
	var digits = "0123456789";
	var delims = "()- +";
	var mindigits = 10;
	
	function isint(s) {
		var i;
		for (i = 0; i < s.length; i++) {
			var c = s.charAt(i);
			if (((c < "0") || (c > "9"))) return false;
		}
	    return true;
	}
	
	function ctrim(s) {
		var i;
		var r = "";
	    for (i = 0; i < s.length; i++) {
	    	var c = s.charAt(i);
	    	if (c != " ") r += c;
	    }
	    return r;
	}

	function cstrip(s, bag) {
		var i;
		var r = "";
		for (i = 0; i < s.length; i++) {
			var c = s.charAt(i);
			if (bag.indexOf(c) == -1) r += c;
	    }
	    return r;
	}
		
	function check_phonenumber(phonenum) {
		var digits = cstrip(ctrim(phonenum),delims);
		
		// check string only contains digits and number of digits is above threshold
		if (isint(digits) != true || digits.length < mindigits) {
			return "Phone number does not contain enough digits";
		}
		
		// check if brackets are used completely
		if ( ( phonenum.indexOf("(") >  -1 && phonenum.indexOf(")") == -1 ) || 
			 ( phonenum.indexOf("(") == -1 && phonenum.indexOf(")") >  -1 ) ||
			 ( phonenum.indexOf("(") >  -1 && phonenum.indexOf(")") >  -1 && phonenum.indexOf("(") > phonenum.indexOf(")") ) ) {
			return "Parentheses are not closed properly";
		}
		
		return "";
	}
	
	function check_email(e) {
		var em = ctrim(e);
		var vchars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!#$%&'*+-/=?^_`{|}~.@[]";
		
		// Illegal characters
		if ( cstrip(em, vchars) !="" ) {
		alert(cstrip(em, vchars));
			return "Email address contains illegal characters";
		}
		
		// Use of @ sign
		if ( (em.indexOf("@") < 1) || (em.indexOf("@") > em.length - 5) ) {
			return "@ sign is in an unexpected location";
		}

		// Multiple @ signs
		if ( (em.indexOf("@", em.indexOf("@") + 1) > -1) ) {
			return "Email address contains multiple @ signs";
		}

		// Use of period
		var ppos = -1;
		var cpos = em.indexOf(".", ppos + 1);
		while (cpos > -1) {
			if ( (cpos < 1) || (cpos > em.length - 3) || (cpos == em.indexOf("@") - 1) || (cpos == em.indexOf("@") + 1) ) {
				return "Period is in an unexpected location";
			};
			var ppos = cpos;
			var cpos = em.indexOf(".", ppos + 1);
		}

		// Use of brackets
		if ( ( em.indexOf("[") > -1  && em.indexOf("]") == -1 ) || 
			 ( em.indexOf("[") == -1 && em.indexOf("]") > -1  ) || 
			 ( em.indexOf("[") > -1  && em.indexOf("[") < em.indexOf("@") ) ||
			 ( em.indexOf("]") > -1  && em.indexOf("]") < em.indexOf("@") ) || 
			 ( em.indexOf("[") > -1  && em.indexOf("]") > -1  && em.indexOf("]") < em.indexOf("[") ) ) {
			return "Brackets are not closed properly or placed in the right location";
		}
		
		return "";
	}

function checkform_contactus( form ) {

	// CHECK 1 - Name
	if (form.name.value == "") {
		alert( "Please enter your name." );
		form.name.focus();
		return false;	
	}

	// CHECK 2 - Email address or phone number
	if (form.phone.value == "" && form.email.value=="") {
		alert( "Please enter a phone number or email address where we can reach you." );
		form.phone.focus();
		return false;	
	}

	// CHECK 2a - Phone number format
	if (form.phone.value != "" && check_phonenumber(form.phone.value)!="") {
		alert("Please enter a valid phone number - " + check_phonenumber(form.phone.value));
		form.phone.focus();
		return false;	
	}

	// CHECK 2b - Email address format
	if (form.email.value != "" && check_email(form.email.value)!="") {
		alert("Please enter a valid email address - " + check_email(form.email.value));
		form.email.focus();
		return false;	
	}

	// CHECK 3 - How heard from us
	if (form.hear_oth.checked && form.hear_oth_desc.value=="") {
		alert(form.hear_oth.checked);
		alert( "Please enter in the space provided how you heard about us." );
		form.hear_oth_desc.focus();
		return false;	
	}

	// CHECK 4 - Services
	if (form.svc_oth.checked && form.svc_oth_desc.value=="") {
		alert( "Please enter in the space provided what services you are interested in." );
		form.svc_oth_desc.focus();
		return false;	
	}

	return true;
}

