// JavaScript Document
var inputFieldDefaultValues = {};
var browser = navigator.appName;
var browserVer=parseInt(navigator.appVersion);
var formCallCompleted;
var formTimer;

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			if (oldonload) {
				oldonload();
			}
			func();
		}
	}
}

function displayFormProcessor(element) {
	
	element.setAttribute("style", "display:none;");
	element.style.cssText = 'display:none;';
	
	var sendBtnWrapper = element.parentNode;
		
	if (typeof document.body.style.maxHeight != "undefined") {
		var image = document.createElement('img');
		image.setAttribute("src", "assets/images/global/loader.gif");
		image.setAttribute("id", "formProcessor");
		sendBtnWrapper.appendChild(image);
	} else {
		var processorWrapper = document.createElement("p");
		processorWrapper.setAttribute("id", "formProcessor");
		processorWrapper.setAttribute("className", "loader");
		var processorText = document.createTextNode("Processing...");
		processorWrapper.appendChild(processorText);
		sendBtnWrapper.appendChild(processorWrapper);
	}

}

function getHTTPObject() {
	
	var xhr = false;
	
	if(window.XMLHttpRequest) {	
		xhr = new XMLHttpRequest();
		if(xhr.overrideMimeType) {
			xhr.overrideMimeType('text/html');
		}
	} else if (window.ActiveXObject) {
		try {
			xhr = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {
				xhr = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e) {
				xhr = false;
			}
		}
	}
	return xhr;
}

function prepareServicesTransitions() {
	
	if(browser == 'Microsoft Internet Explorer') {
		setTimeout('',2000);
	}
	
	new Effect.Appear('section-title', {duration: 0.75, from: 0.0, to: 1.0, queue: 'front'});
	new Effect.SlideRight('secondNavContainer', {duration: 1.5, queue: 'end'});
	new Effect.Appear('detail', {duration: 0.75, from: 0.0, to: 1.0, queue: 'end'});
	new Effect.Appear('footer', {duration: 0.75, from: 0.0, to: 1.0, queue: 'end'});

}

function prepareWorkTransitions() {
	
	if(browser == 'Microsoft Internet Explorer') {
		setTimeout('',2000);
	}
	
	new Effect.Appear('section-title', {duration: .75, from: 0.0, to: 1.0, queue: 'front'}); 
	new Effect.SlideRight('secondNavContainer', {duration: 1.5, queue: 'end'});
	var firstProjectDetail = document.getElementById("firstProject");
	firstProjectDetail.onclick();
	new Effect.Appear('detail', {duration: 0.75, from: 0.0, to: 1.0, queue: 'end'});
	new Effect.Appear('footer', {duration: 0.75, from: 0.0, to: 1.0, queue: 'end'});
	
}

function prepareAboutTransitions() {
	
	if(browser == 'Microsoft Internet Explorer') {
		setTimeout('',2000);
	}
	
	new Effect.Appear('section-title', {duration: 0.75, from: 0.0, to: 1.0, queue: 'front'});
	new Effect.Appear('detail', {duration: 0.75, from: 0.0, to: 1.0, queue: 'end'});
	new Effect.Appear('footer', {duration: 0.75, from: 0.0, to: 1.0, queue: 'end'});

}

function prepareContactTransitions() {
	
	if(browser == 'Microsoft Internet Explorer') {
		setTimeout('',2000);
	}
	
	new Effect.Appear('section-title', {duration: 0.75, from: 0.0, to: 1.0, queue: 'front'}); 
	new Effect.SlideRight('secondNavContainer', {duration: 1.5, queue: 'end'});
	new Effect.Appear('detail', {duration: 0.75, from: 0.0, to: 1.0, queue: 'end'});
	new Effect.Appear('footer', {duration: 0.75, from: 0.0, to: 1.0, queue: 'end'});
}

function prepareSecondaryNav() {
	
	if (!document.getElementById || !document.getElementsByTagName) {
		return;
	}
	
	if (!document.getElementById("second-nav")) {
		return;
	}
	
	var secondaryNavContainer = document.getElementById("second-nav");
	var secondaryNavLinks = secondaryNavContainer.getElementsByTagName("a");
	
	for (var i=0; i < secondaryNavLinks.length; i++) {
	
		secondaryNavLinks[i].onclick = function() {
			
			clientContentFile = this.getAttribute("href");
			
			for( var n = 0; n < secondaryNavLinks.length; n++) {
			
				secondaryNavLinks[n].setAttribute("class","");
				secondaryNavLinks[n].setAttribute("className","");
				
			}
						
			this.setAttribute("class", "selected");
			this.setAttribute("className", "selected");
			
			return !grabFile(clientContentFile);
		}
	}
}

function sendFormData(data) {
	
	request = getHTTPObject();
	
	if (request) {
		displayFormProcessor(document.getElementById("send"));
		request.onreadystatechange = function() {
			parseFormProcessingResponse(request);
		};
		request.open("POST","/contact/ContactUs.aspx",true);
		request.setRequestHeader("Content-type","application/x-www-form-urlencoded");
		request.setRequestHeader("Content-length",data.length);
		request.setRequestHeader("Connection","close");
		request.send(data);
		formCallCompleted = false;
		formTimer = setTimeout("formTimerExpired()", 60000);
		
	}	
	
}

function formTimerExpired() {
	
	if(!formCallCompleted) {
		
		request.onreadystatechange = function() {};
		request.abort();
		// Ajax call took too long, indicate to the user the service is unavailable
		document.getElementById("detail").innerHTML = "We apologize, but your request timed out. Our service may be unavailable, please try again.";
		document.getElementById("detail").style.display ="";
	}
}


function parseFormProcessingResponse(request) {
	if(request.readyState == 4) {
		clearTimeout(formTimer);
		formCallCompleted = true;
		
                if(request.status == 200 || request.status == 304) {
                        var items = request.responseText.split("||");
			if (items.length > 1) { 
			
				if (items[0] == "OK") { 
				
					var successMsg = items[1];
					// Go ahead and display the success message
						
					document.getElementById("detail").innerHTML = successMsg;
      				document.getElementById("detail").style.display ="";
					
				} else if (items[0] == "ERROR") { 
				
					for (x=1; x<items.length; x++) { 
					
						var fieldInfo = items[x].split("|"); 
						
						if (fieldInfo.length == 2) { 
							//Highlight the field in red and display the error message
							var fieldName = fieldInfo[0]; 
							var fieldMessage = fieldInfo[1];  
							
							var formField = document.getElementById(fieldName);
							
							formField.style.cssText = 'border: solid 1px #FF0000';
							formField.setAttribute("style","border: solid 1px #FF0000");
							
							var errorContainer = document.getElementById(fieldName+'Error');
																					
							while (errorContainer.hasChildNodes()) {
								errorContainer.removeChild(errorContainer.lastChild);
							}

							var errorText = document.createTextNode(fieldMessage);
							
							errorContainer.appendChild(errorText);
							
							var sendBtn = document.getElementById("send");
							
							var sendBtnWrapper = sendBtn.parentNode;
							
							var formProcessor = sendBtnWrapper.lastChild;
							
							sendBtnWrapper.removeChild(formProcessor);
							
							sendBtn.style.cssText = "display:'';";
							sendBtn.setAttribute("style", "display:'';");
														
						} else { 
							// Got a bad result back, don't know how to process it, indicate to the user the service is unavailable
							document.getElementById("detail").innerHTML = "We apologize, but our contact service is currently unavailable";
      						document.getElementById("detail").style.display ="";
						} 
					} 
					
				} else { 
				
					var fatalMsg = items[1] 
					// Go ahead and display the fatal message 
					document.getElementById("detail").innerHTML = fatalMsg;
      				document.getElementById("detail").style.display ="";
				} 
			} else { 
				// Got a improperly formatted result back, don't know how to process it, indicate to the user the service is unavailable
				document.getElementById("detail").innerHTML = "We apologize, but our contact service is currently unavailable";
				document.getElementById("detail").style.display ="";
			} 
		} else {
			// Got a status other than 200/304, indicate to the user the service is unavailable
			document.getElementById("detail").innerHTML = "We apologize, but our contact service is currently unavailable";
			document.getElementById("detail").style.display ="";
		}
	}
}

function prepareContactForm() {
	
	var name = document.getElementById("name");
	inputFieldDefaultValues[name.name] = name.value;
	var title = document.getElementById("title");
	inputFieldDefaultValues[title.name] = title.value;
	var company = document.getElementById("company");
	inputFieldDefaultValues[company.name] = company.value;
	var email = document.getElementById("email");
	inputFieldDefaultValues[email.name] = email.value;
	var phone = document.getElementById("phone");
	inputFieldDefaultValues[phone.name] = phone.value;
	var comments = document.getElementById("comments");
	inputFieldDefaultValues[comments.name] = comments.value;
		
	if (!document.getElementById) {
		return;
	}
	
	var contactFormContainer = document.getElementById("contact-logicseries");
	
	if (!contactFormContainer) {
		return;
	}
	
	var sendBtn = document.getElementById("send");
	
	sendBtn.onclick = function() {
		
		var okToSendForm = true;
		var nameField = document.getElementById("name");
		var emailField = document.getElementById("email");
		var nameFieldAsteriks = nameField.nextSibling;
		var emailFieldAsteriks = emailField.nextSibling;
		var requiredFieldsDisclaimer = document.getElementById("requiredContentLabel");
		
		
		if(nameField.value == inputFieldDefaultValues[nameField.name]) {
			nameField.style.cssText = 'border: solid 1px #FF0000';
			nameFieldAsteriks.style.cssText = requiredFieldsDisclaimer.style.cssText = 'color: #FF0000';
			nameField.setAttribute("style","border: solid 1px #FF0000");
			nameFieldAsteriks.setAttribute("style", "color: #FF0000");
			requiredFieldsDisclaimer.setAttribute("style", "color: #FF0000");
			
			okToSendForm = false;
		}
		
		if(emailField.value == inputFieldDefaultValues[emailField.name]) {
			emailField.style.cssText = 'border: solid 1px #FF0000';
			emailFieldAsteriks.style.cssText = requiredFieldsDisclaimer.style.cssText = 'color: #FF0000';
			emailField.setAttribute("style","border: solid 1px #FF0000");
			emailFieldAsteriks.style.cssText = 'color: #FF0000';
			requiredFieldsDisclaimer.setAttribute("style", "color: #FF0000");

			okToSendForm = false;
		}	
		
		if(okToSendForm) {
			compileFormFields();
		}
		
	};
		
	var elements = contactFormContainer.elements;
		
	for (var i=0; i < elements.length; i++) {
				
		if ( elements[i].type == "text" ||  elements[i].type == "textarea") {
			
			elements[i].onfocus = function() {
				
				this.style.cssText = 'border: solid 1px #29ABE2; color: #333';
				this.setAttribute("style", "border: solid 1px #29ABE2; color: #333");	
				
				if (this.value.length == 0 || this.value == inputFieldDefaultValues[this.name]) {
					this.value = "";
				}
					
			};
			
			elements[i].onblur = function() {
								
				if(this.value.length == 0) {
					this.value = inputFieldDefaultValues[this.name];
				}
				
				if(this.value == inputFieldDefaultValues[this.name]) {
					this.style.cssText = 'border: solid 1px #999; color: #999';
					this.setAttribute("style", "border: solid 1px #999; color: #999");
				} else {
					this.style.cssText = 'border: solid 1px #999; color: #333';
					this.setAttribute("style", "border: solid 1px #999; color: #333");
				}
			};
		}
		
	}
}

function compileFormFields() {
	var contactFormContainer = document.getElementById("contact-logicseries");
	var elements = contactFormContainer.elements;

	var data = "";
	for (var i=0; i < elements.length; i++) {
		
		if ( elements[i].type == "text" ||  elements[i].type == "textarea") {
			
			data += elements[i].name;
			data += "=";

			if (elements[i].value != inputFieldDefaultValues[elements[i].name]) {			
				data += encodeURI(elements[i].value);
			}
			
			data += "&";
		}
	}
	
	if(data.charAt(data.length - 1)) {
		
		var dataToSend = data.substr(0,data.length-1);
	}
		
	sendFormData(dataToSend);
}	