AddHandler(window, 'resize', 'CenterContactForm');

function CreateMask(zIndex) {
    var mask = document.createElement('DIV');
    
    mask.className = 'Mask';
    mask.id = '_Mask';
    mask.style.zIndex = zIndex;
    document.body.insertBefore(mask, document.body.firstChild);
    
    return mask;
}
function CreateContactForm(mask) {
    var str = '';
    var form = document.createElement('DIV');
    
    form.style.zIndex = (mask.style.zIndex + 1);
    form.id = '_ContactForm';
    form.className = 'ContactForm';
    str += '<input type="text" id="_ContactForm_Name" value="Your Name" class="name" onfocus="if(this.value==\'Your Name\') this.value = \'\';" onblur="if(this.value==\'\') this.value = \'Your Name\';" />';
    str += '<input type="text" id="_ContactForm_Email" value="Your Email" class="email" onfocus="if(this.value==\'Your Email\') this.value = \'\';" onblur="if(this.value==\'\') this.value = \'Your Email\';" />';
    str += '<input type="text" id="_ContactForm_Company" value="Your Company Name" class="company" onfocus="if(this.value==\'Your Company Name\') this.value = \'\';" onblur="if(this.value==\'\') this.value = \'Your Company Name\';" />';
    str += '<input type="text" id="_ContactForm_Address1" value="Street Address Line 1" class="address1" onfocus="if(this.value==\'Street Address Line 1\') this.value = \'\';" onblur="if(this.value==\'\') this.value = \'Street Address Line 1\';" />';
    str += '<input type="text" id="_ContactForm_Address2" value="Street Address Line 2" class="address2" onfocus="if(this.value==\'Street Address Line 2\') this.value = \'\';" onblur="if(this.value==\'\') this.value = \'Street Address Line 2\';" />';
    str += '<input type="text" id="_ContactForm_City" value="City" class="city" onfocus="if(this.value==\'City\') this.value = \'\';" onblur="if(this.value==\'\') this.value = \'City\';" />';
    str += '<input type="text" id="_ContactForm_State" value="State" class="state" onfocus="if(this.value==\'State\') this.value = \'\';" onblur="if(this.value==\'\') this.value = \'State\';" />';
    str += '<input type="text" id="_ContactForm_Zip" value="Zip" class="zip" onfocus="if(this.value==\'Zip\') this.value = \'\';" onblur="if(this.value==\'\') this.value = \'Zip\';" />';
    str += '<textarea id="_ContactForm_Comments" class="comments" onfocus="if(this.value==\'Comment\') this.value = \'\';" onblur="if(this.value==\'\') this.value = \'Comment\';">Comment</textarea>';
    str += '<div class="submit" onclick="SubmitContactForm();"></div>';
    str += '<div class="close" onclick="CloseContactForm();"></div>';
    form.innerHTML = str;
    document.body.scrollIntoView(true);
    document.body.insertBefore(form, document.body.firstChild);
	
    var left = (document.body.offsetWidth / 2) - (form.offsetWidth / 2);
    var top = 50;
	
    form.style.left = left + 'px';
    form.style.top = top + 'px';

	
    var mask = document.getElementById('_Mask');
    if (mask) {
    	mask.style.height = document.body.offsetHeight + 'px';
    }
}
function SubmitContactForm() {
	var name = document.getElementById('_ContactForm_Name');
	var email = document.getElementById('_ContactForm_Email');
	var company = document.getElementById('_ContactForm_Company');
	var address1 = document.getElementById('_ContactForm_Address1');
	var address2 = document.getElementById('_ContactForm_Address2');
	var city = document.getElementById('_ContactForm_City');
	var state = document.getElementById('_ContactForm_State');
	var zip = document.getElementById('_ContactForm_Zip');
	var comment = document.getElementById('_ContactForm_Comments');
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;

	if (Trim(name.value) == '' || Trim(name.value) == 'Your Name') {
		name.focus();
		alert('Please enter your name and try again.');
		return;
	}
	if (Trim(email.value) == '' || Trim(email.value) == 'Your Email') {
		email.focus();
		alert('Please enter your email address and try again.');
		return;
	} else if (!reg.test(email.value)) {
		email.focus();
		alert('Please enter a valid email address and try again.');
		return;
	}
	if (Trim(address1.value) == '' || Trim(address1.value) == 'Street Address Line 1') {
		address1.focus();
		alert('Please enter your street address and try again.');
		return;
	}
	if (Trim(city.value) == '' || Trim(city.value) == 'City') {
		city.focus();
		alert('Please enter your city and try again.');
		return;
	}
	if (Trim(state.value) == '' || Trim(state.value) == 'State') {
		state.focus();
		alert('Please enter your state and try again.');
		return;
	}
	if (Trim(zip.value) == '' || Trim(zip.value) == 'Zip') {
		zip.focus();
		alert('Please enter your zip and try again.');
		return;
	}

	var _name = Trim(name.value);
	var _email = Trim(email.value);
	var _company = Trim(company.value);
	var _address1 = Trim(address1.value);
	var _address2 = Trim(address2.value);
	var _city = Trim(city.value);
	var _state = Trim(state.value);
	var _zip = Trim(zip.value);
	var _comment = Trim(comment.value);

	var url = '/demorequest.jsp?';
	url += 'name=' + (_name != 'Your Name' ? escape(_name) : '') + '&';
	url += 'email=' + (_email != 'Your Email' ? escape(_email) : '') + '&';
	url += 'company=' + (_company != 'Your Company Name' ? escape(_company) : '') + '&';
	url += 'address1=' + (_address1 != 'Street Address Line 1' ? escape(_address1) : '') + '&';
	url += 'address2=' + (_address2 != 'Street Address Line 2' ? escape(_address2) : '') + '&';
	url += 'city=' + (_city != 'City' ? escape(_city) : '') + '&';
	url += 'state=' + (_state != 'State' ? escape(_state) : '') + '&';
	url += 'zip=' + (_zip != 'Zip' ? escape(_zip) : '') + '&';
	url += 'comment=' + (_comment != 'Comment' ? escape(_comment) : '') + '';

	getServerData(url, false);

	alert('Thank you for your interest. Your request was submitted successfully.');
	
	CloseContactForm();
}
function Trim(str) {
   return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}
function ShowContactForm() {
    var mask = CreateMask(1000);
    CreateContactForm(mask);
}
function CloseContactForm() {
    document.body.removeChild(document.getElementById('_Mask'));
    document.body.removeChild(document.getElementById('_ContactForm'));
}
function AddHandler(target, eventName, handlerName) {
    if (target.addEventListener) {
        target.addEventListener(eventName, function(e) { target[handlerName](e); }, false);
    }
    else if (target.attachEvent) {
        target.attachEvent('on' + eventName, function(e) { target[handlerName](e); });
    }
    else {
        var handler = target['on' + eventName];
        if (handler) {
            target['on' + eventName] = function(e) { handler(e); target[handlerName](e); };
        }
        else {
            target['on' + eventName] = target[handlerName];
        }
    }
}
function CenterContactForm() {
    var form = document.getElementById('_ContactForm');
    if (!form) return;

    var left = (document.body.offsetWidth / 2) - (form.offsetWidth / 2);
    var top = 50;

    form.style.left = left + 'px';
    form.style.top = top + 'px';

    var mask = document.getElementById('_Mask');
    if (mask) {
    	mask.style.height = document.body.offsetHeight + 'px';
    }
}
function getServerData(page, bCacheConnection) {
	var _this = this;
	var _path = String(window.location);
	this.GenerateRandomString = function(len) {
		var _str = '';
		var arrKeyStr = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
		for (var i = 1; i <= len; i++) {
			var iRndNum = Math.ceil((Math.random() * 100) / 2.8) - 1; //Generates a random number from 0-35
			_str += arrKeyStr[iRndNum];
		}
		return _str;
	}
	this.GetTimeMod = function() {
		var _str = date.getMinutes();
		if (_str < 15) {
			_str = 15;
		} else if (_str < 30) {
			_str = 30;
		} else if (_str < 45) {
			_str = 45;
		} else {
			_str = 60;
		}
		return _str;
	}
	try {
		var _path = String(window.location);
		if (_path.substring(_path.length - 1) != '/') {
			_path = _path.substring(0, _path.lastIndexOf('/') + 1);
		}
		_path += page;

		if (_path.indexOf('?') != -1) {
			_path += '&';
		} else {
			_path += '?';
		}
		var date = new Date()
		if (typeof (bCacheConnection) == 'undefined' || bCacheConnection == true) {
			_path += 'z=' + date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate() + '-' + date.getHours() + '-' + _this.GetTimeMod();
		} else {
			_path += 'z=' + _this.GenerateRandomString(6);
		}
		var oRequest = new XMLHttpRequest();
		oRequest.open("get", _path, false);
		oRequest.send(null);
		return oRequest.responseText;
	} catch (e) {
		//alert('There was an error in the request:' + e.message)
	} finally {
	}
}
