var xmlHttp;

//---- Setup AJAX -----------------------//
function GetXmlHttpObject() {
	var xmlHttp=null;
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e) {
		// Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}


//----- Validate Email Addresses -----------//
// checks for valid looking email addy
function checkemail(str){
	var filter=/^.+@.+\..{2,3}$/;	
	return (filter.test(str));
}



//----- Trim White Space -----------//
//trim whitespace for validation
function trim(s){
	if((s==null)||(typeof(s)!='string')||!s.length)return'';return s.replace(/^\s+/,'').replace(/\s+$/,'')
}



//----- Open New Window -----------//
//Open a new window with specific width/height/location 
function openWindow(location, width, height) {		
	newWidth = width + 20;
	newHeight = height + 20;
	widthHeight = "width=" + newWidth + ",height=" + newHeight;
	window.open(location,"",widthHeight);		
}

//----- Open New Window -----------//
//Open a new window with specific width/height/location & scroll bars
function openWindowScroll(location, width, height) {		
	newWidth = width + 20;
	newHeight = height + 20;
	windowParameters = "width=" + newWidth + ",height=" + newHeight + ",scrollbars=yes";
	window.open(location,"",windowParameters);		
}


//----- Google Map Code -----------//
function mapLoad() {
	if (GBrowserIsCompatible()) {
		var map = new GMap2(document.getElementById("map"));
		map.setCenter(new GLatLng(53.4881393, -113.4815369), 14);		  
		// Add ASI marker to the map
		var point = new GLatLng(53.4881393, -113.4815369);
		map.addOverlay(new GMarker(point));
		// Add controls
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
	}
}



//-----------------------------------------------//
//-------  Form Code        --------------//
//-----------------------------------------------//
//Load Forms via js: i know this might sound dumb but...
//---Note: doing this because modal box setup right now requires
//         that the form be on each page; but I don't want this in 
//         my SEO page copy, dilutes my keyword density. So loading form via js.
//         There's prolly a better way to set this up in future.
//		   ~RDM: March 10, 2009

var baseURL = "http://www.hybridforge.com/";
var url;

function checkEnterClient(e) {
	var code;
	if (!e) var e = window.event;
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;
	var character = String.fromCharCode(code);
	
	if (character == "13") { //if generated character code is equal to ascii 13 (if enter key)
		return false;
	} else {
		return true;
	}
}



//---- Load Forms --------------//
function loadContactForm() {
	var req = false;
	// For Safari, Firefox, and other non-MS browsers
	if (window.XMLHttpRequest) {
		try {
			req = new XMLHttpRequest();
		} catch (e) {
		req = false;
		}
	} else if (window.ActiveXObject) {
    // For Internet Explorer on Windows
		try {
			req = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				req = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				req = false;
			}
		}
	}
	
	//load contact form
	var element = document.getElementById("contactFormHolder");
	if (!element) {
		alert("Bad id 'contactFormHolder' passed to loadContactForm.");
		return;
	}
	if (req) {
		// Synchronous request, wait till we have it all
		req.open('GET', baseURL + "includes/contact-form.php", false);
		req.send(null);
		element.innerHTML = req.responseText;
	} else {
		element.innerHTML = "Sorry, your browser does not support XMLHTTPRequest objects. This page requires Internet Explorer 5 or better for Windows, or Firefox for any system, or Safari.";
	}	
	
	//load client login form	
	var element2 = document.getElementById("loginFormHolder");
	if (!element2) {
		alert("Bad id 'loginFormHolder' passed to loginFormHolder.");
		return;
	}
	if (req) {
		// Synchronous request, wait till we have it all
		req.open('GET', baseURL + "includes/client-login-form.php", false);
		req.send(null);
		element2.innerHTML = req.responseText;
	} else {
		element2.innerHTML = "Sorry, your browser does not support XMLHTTPRequest objects. This page requires Internet Explorer 5 or better for Windows, or Firefox for any system, or Safari.";
	}
}


//------ Client Login Form Check ------//
function checkLoginForm(theForm) {		
	var username = document.getElementById("uname");
	username.value = trim(username.value);
	var password = document.getElementById("pword");
	password.value = trim(password.value);			
	if (username.value.length == 0) {
		alert("Please enter your user name.");
		username.focus();
		return false;
	} else if (password.value.length == 0) {
		alert("Please enter your password.");
		password.focus();
		return false;
	} else {
		//alert("Submission Good");		
		//Ajax form submission
		var ajaxURL = baseURL + "clients/ajax.login.php";
		var objParams = {
			method:'post',
			parameters: 'Username=' + username.value + '&Password=' + password.value
		}
		var objRequest = new Ajax.Request(ajaxURL, objParams);
		return false;
	}		
}


//----- Contact Form Validation -----------//
function checkContactForm(theForm) {
	var ic = document.getElementById("firstName");			
	var firstName = document.getElementById("middleName");
	firstName.value = trim(firstName.value);
	var lastName = document.getElementById("lastName");
	lastName.value = trim(lastName.value);
	var organization = document.getElementById("organization");
	organization.value = trim(organization.value);
	var phoneNum = document.getElementById("phone");
	phoneNum.value = trim(phoneNum.value);
	var emailObject = document.getElementById("email");
	emailObject.value = trim(emailObject.value);
	var city = document.getElementById("city");
	city.value = trim(city.value);
	var province = document.getElementById("province");
	var country = document.getElementById("country");
	var interest = document.getElementById("interest");
	var comments = document.getElementById("comments");
	var newsletter = document.getElementById("newsletter");
	 
	if (firstName.value.length == 0) {
		alert("Please enter your first name.");
		firstName.focus();
		return false;
	} else if (lastName.value.length == 0) {
		alert("Please enter your last name.");
		lastName.focus();
		return false;
	} else if (city.value.length == 0) {
		alert("Please enter your city.");
		city.focus();
		return false;
	} else if (emailObject.value.length == 0) {
		alert("Please enter your email address.");
		emailObject.focus();
		return false;
	} else if (!checkemail(emailObject.value)) {
		alert("Please check your email address for errors.");
		emailObject.focus();
		return false;
	} else if (phoneNum.value.length > 25) {
		alert("Please check your phone number - it should not contain more than 25 digits.");
		phoneNum.focus();
		return false;
	} else if (organization.value.length > 40) {
		alert("Please abbreviate your company name to make it less than 40 characters long.");
		organization.focus();
		return false;
	} else {
		//alert("Submission CONTACT Good");
		//theForm.submit();	
		
		//update the modalbox panel
		document.getElementById("contactFormTable").style.display = "none";
		document.getElementById("contactFormHeader").style.display = "block";
		document.getElementById("contactFormHeader").innerHTML = "<p align='center'>Sending Your Request<br /><img src='" + baseURL + "images/spinner.gif' width='40' height='40' /></p>";
		Modalbox.resizeToContent();
		
		//Ajax form submission
		url= baseURL + "scripts/mailer.php?leadtype=contact";
		url=url+"&firstname="+ic.value;
		url=url+"&middlename="+firstName.value;
		url=url+"&lastname="+lastName.value;
		url=url+"&organization="+organization.value;
		url=url+"&city="+city.value;
		url=url+"&province="+province.value;
		url=url+"&country="+country.value;
		url=url+"&email="+emailObject.value;
		url=url+"&phone="+phoneNum.value;
		//url=url+"&interest="+interest.value;
		url=url+"&comments="+comments.value;
		//url=url+"&newsletter="+newsletter.value;
		setTimeout("submitContact()",700);
		
		//Call Analytics Virtual Page View
		pageTracker._trackPageview('contact-goal1');
		
	}
}

//----- Wrapper function to allow global variable (url) ------//
function submitContact() {
	submitContactRequest(url);
}


//----- AJAX Contact Form Submission -----------//
function submitContactRequest(theURL) { 
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null) {
		document.getElementById("contactFormHeader").innerHTML="<p>Our apologies, An error was encountered.<br />Please email us directly with your inquiry.<br />(Error: 1)</p>";
		Modalbox.resizeToContent();
		//alert ("Our apologies, An error was encountered./n Please email us directly with your inquiry. (Error: 1)");
		return;
	}
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("GET",theURL,true);
	xmlHttp.send(null);
}		

//when xmlhttp shows "loaded"
//write the response to the object in the page
function stateChanged() { 
	if (xmlHttp.readyState==4) { 
		if (xmlHttp.status==200) {	
			//document.getElementById("contactFormHeader").innerHTML=xmlHttp.responseText;
			document.getElementById("contactFormHeader").innerHTML="<p>Thank you for your interest in Hybrid Forge. Your request has been sent to a Hybrid Forge representative who will be responsible for your inquiry.<br /><input type='button' value='Close' onclick='Modalbox.hide();' /></p>";
		}
		else {
			document.getElementById("contactFormHeader").innerHTML="<p>Our apologies, An error was encountered.<br />Please email us directly with your inquiry.<br />(Error: 2 / " + xmlHttp.status + ")</p>";
			//alert ("Our apologies, An error was encountered./n Please email us directly with your inquiry. (Error: 2)");
			return;
		}
		Modalbox.resizeToContent();
	}
}

