/* 
 * xmlhttp.js
 */
var pos; // variable for posting information
function loadXMLPosDoc(url,posData) {
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        pos = new XMLHttpRequest();
        pos.open("POST", url, false);
		pos.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        pos.send(posData);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        pos = new ActiveXObject("Microsoft.XMLHTTP");
        if (pos) {
            pos.open("POST", url, false);
			pos.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            pos.send(posData);
        }
    }
}

function grabPosXML (tagName) {
	return pos.responseXML.documentElement.getElementsByTagName(tagName)[0].childNodes[0].nodeValue;
}

/**
 * contact.js
 */
function checkMail(x)
{
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(x))
		return true;
	else
		return false;
}
function validateFields() {
var frmEl = document.getElementById('cForm');
var posYName = document.getElementById('posYName');
var posYEmail = document.getElementById('posYEmail');
var posFName = document.getElementById('posFName');
var posFEmail = document.getElementById('posFEmail');
var posText = document.getElementById('posText');
var posSpam = document.getElementById('posSpam');

var whiteSpace = /^[\s]+$/;
	if ( posText.value == '' || whiteSpace.test(posText.value) ) {
		alert("You're trying to send an Empty Email. Please type something and then get on your way.");
	}
	else if ( posYName.value == '' ) {
		alert("Enter Your name.");
		posYName.focus();
	}
	else if ( posYEmail.value == '' || !checkMail(posYEmail.value)) {
		alert("Your email id is not valid.");
		posYEmail.focus();
	}
	else if ( posFName.value == '' ) {
		alert("Enter Your friends name.");
		posFName.focus();
	}
	else if ( posFEmail.value == '' || !checkMail(posFEmail.value)) {
		alert("Your friends email id is not valid.");
		posFEmail.focus();
	} else if (posSpam.value == '' || posSpam.value != '9') {
		alert ("Your answer to the security question is wrong. Please try again.");
		posSpam.focus();
	} else {
		sendPosEmail();
	}
}
function sendPosEmail () {
	var success = document.getElementById('emailSuccess');
	var posYName = document.getElementById('posYName');
	var posYEmail = document.getElementById('posYEmail');
	var posFName = document.getElementById('posFName');
	var posFEmail = document.getElementById('posFEmail');
	var posText = document.getElementById('posText');
	var posSpam = document.getElementById('posSpam').value;

	var page = "xmlHttpRequest.php?contact=true&xml=true";

	showContactTimer(); // quickly begin the load bar
	success.style.display = 'none'; // hide the success bar (incase this is a multi-email

	// convert (&, +, =) to string equivs. Needed so URL encoded POST won't choke.
	var str1 = posYName.value;
	str1 = str1.replace(/&/g,"**am**");
	str1 = str1.replace(/=/g,"**eq**");
	str1 = str1.replace(/\+/g,"**pl**");
	var str2 = posYEmail.value;
	str2 = str2.replace(/&/g,"**am**");
	str2 = str2.replace(/=/g,"**eq**");
	str2 = str2.replace(/\+/g,"**pl**");
	var str3 = posFName.value;
	str3 = str3.replace(/&/g,"**am**");
	str3 = str3.replace(/=/g,"**eq**");
	str3 = str3.replace(/\+/g,"**pl**");
	var str4 = posFEmail.value;
	str4 = str4.replace(/&/g,"**am**");
	str4 = str4.replace(/=/g,"**eq**");
	str4 = str4.replace(/\+/g,"**pl**");

	var str5 = posText.value;
	str5 = str5.replace(/&/g,"**am**");
	str5 = str5.replace(/=/g,"**eq**");
	str5 = str5.replace(/\+/g,"**pl**");

	var stuff = "posYName="+str1+"&posSpam="+posSpam+"&posYEmail="+str2+"&posFName="+str3+"&posFEmail="+str4+"&posText="+str5;
	loadXMLPosDoc(page,stuff)
}
function showContactTimer () {
	var loader = document.getElementById('loadBar');
	loader.style.display = 'block';
	sentTimer = setTimeout("hideContactTimer()",6000);
}

function hideContactTimer () {
	var loader = document.getElementById('loadBar');
	var success = document.getElementById('emailSuccess');
	// Hide the load bar alas! Done Loading
	loader.style.display = "none";
	success.style.display = "block";
	if (grabPosXML("status") == "NOTOK") {
		success.innerHTML = '<strong style="color:red;">'+grabPosXML("confirmation")+'</strong>';
	} else {
		success.innerHTML = '<strong style="color:green;">'+grabPosXML("confirmation")+'</strong>';
		document.getElementById('posFName').value = '';
		document.getElementById('posFEmail').value = '';
	}
}

function ajaxContact() {
	var frmEl = document.getElementById('cForm');
	addEvent(frmEl, 'submit', validateFields, false);
	frmEl.onsubmit = function() { return false; }

//	var fyname = document.getElementById('posYName');
	addEvent(document.getElementById('posYName'), 'blur', insertName, false);
}
addEvent(window, 'load',ajaxContact, false);

function insertName() {
	var vname = document.getElementById('posYName').value;
	posText = document.getElementById('posText');
	posText.value = posText.value.replace("Your name", vname);
	posText.value = posText.value.replace("Your+Name", vname.replace(" ", "+"));
}

/**
 * functionAddEvent.js
 */
function addEvent(elm, evType, fn, useCapture) {
	if (elm.addEventListener) {
	elm.addEventListener(evType, fn, useCapture);
	return true;
	}
	else if (elm.attachEvent) {
	var r = elm.attachEvent('on' + evType, fn);
	EventCache.add(elm, evType, fn);
	return r;
	}
	else {
	elm['on' + evType] = fn;
	}
}
function getEventSrc(e) {
	if (!e) e = window.event;

	if (e.originalTarget)
	return e.originalTarget;
	else if (e.srcElement)
	return e.srcElement;
}
function addLoadEvent(func) {
var oldonload = window.onload;
	if (typeof window.onload != 'function') {
	window.onload = func;
	} else {
	window.onload =
		function() {
		oldonload();
		func();
		}
	}
}
var EventCache = function(){
	var listEvents = [];
	return {
		listEvents : listEvents,

		add : function(node, sEventName, fHandler, bCapture){
			listEvents.push(arguments);
		},

		flush : function(){
			var i, item;
			for(i = listEvents.length - 1; i >= 0; i = i - 1){
				item = listEvents[i];

				if(item[0].removeEventListener){
					item[0].removeEventListener(item[1], item[2], item[3]);
				};

				/* From this point on we need the event names to be prefixed with 'on" */
				if(item[1].substring(0, 2) != "on"){
					item[1] = "on" + item[1];
				};

				if(item[0].detachEvent){
					item[0].detachEvent(item[1], item[2]);
				};

				item[0][item[1]] = null;
			};
		}
	};
}();


addEvent(window,'unload',EventCache.flush, false);