// JavaScript Document
// dper4mer@yahoo.com
// alfafusion.com

var gAppDomain = "www.alfafustion.com";

function gAjax() {

	this.createAjaxObj = function() {
		var httprequest=false;
		if (window.XMLHttpRequest){ 
			httprequest=new XMLHttpRequest();
		if (httprequest.overrideMimeType)
			httprequest.overrideMimeType('text/xml');
		}
		else if (window.ActiveXObject){ 
		try {
			httprequest=new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e){
			try{
				httprequest=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e){}
			}
		}
		return httprequest;
	}

	this.ajaxpack=new Object();
	this.ajaxpack.basedomain="http://"+window.location.hostname;
	this.ajaxpack.ajaxobj=this.createAjaxObj();
	this.ajaxpack.filetype="txt";
	this.ajaxpack.addrandomnumber=0;

	this.getAjaxRequest = function(url, parameters, callbackfunc, filetype){
		this.ajaxpack.ajaxobj=this.createAjaxObj(); 
		var parameters=parameters+"&ajaxcachebust="+new Date().getTime();
		if (this.ajaxpack.ajaxobj){
			this.ajaxpack.filetype=filetype;
			this.ajaxpack.ajaxobj.onreadystatechange=callbackfunc;
			this.ajaxpack.ajaxobj.open('GET', url+"?"+parameters, true);
			this.ajaxpack.ajaxobj.send(null);
		}
	}

	this.postAjaxRequest=function(url, parameters, callbackfunc, filetype){
		this.ajaxpack.ajaxobj=this.createAjaxObj();
		if (this.ajaxpack.ajaxobj){
			this.ajaxpack.filetype=filetype;
			this.ajaxpack.ajaxobj.onreadystatechange = callbackfunc;
			this.ajaxpack.ajaxobj.open('POST', url, true);
			this.ajaxpack.ajaxobj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			this.ajaxpack.ajaxobj.setRequestHeader("Content-length", parameters.length);
			this.ajaxpack.ajaxobj.setRequestHeader("Connection", "close");
			this.ajaxpack.ajaxobj.send(parameters);
		}
	}	
}

function gSend() {
	var ObjAjax =  new gAjax();
	var url = "gs/ginquire.php";
	
	var iname = document.getElementById("fld_name");
    var iemail = document.getElementById("fld_email");
	var iphone = document.getElementById("fld_phone");
	var imessage = document.getElementById("fld_message");
	var icmd = document.getElementById("cmd_send");
	
	if (iemail.length < 1)
	{
	    iemail.value = "........ No email.";
	} else {
	    iemail.value = "......... email:  " + iemail.value; 
	}
	
	if(iname.value=="" || iphone.value=="" || imessage.value=="" || iname.value=="Name" || iemail.value=="Email" || iphone.value=="Phone" || imessage.value=="Message") {
		alert("Please fill-in input fields. Thank you.");
		return;
	}
	
	var parameters = "n="+iname.value+"&p="+iphone.value+iemail.value+"&m="+imessage.value;
	
	icmd.value = "Sending...";
	
	this.processGetPost = function(){
		var myajax=ObjAjax.ajaxpack.ajaxobj;
		var myfiletype=ObjAjax.ajaxpack.filetype;
		if (myajax.readyState == 4){ 
			if (myajax.status==200 || window.location.href.indexOf("http")==-1){ 
				var lret=myajax.responseText.split("<g>")[1].split("</g>")[0];
				if(lret>0) {
					alert("Thank you!");
					iname.value="Name";
					iphone.value="Phone";
					imessage.value="Message";
					iemail.value="Email";
				} else {
					alert("Unable to send you request! Pls. try again later. Thank you.");
				}				
				icmd.value = "SUBMIT";
			}
		}
	}
	
	ObjAjax.getAjaxRequest(url, parameters, this.processGetPost, "txt");
}


function gSearch() {
	var isearch = document.getElementById("fld_search");	
	if(isearch.value!="")
		window.open("http://www.google.com/custom?domains="+gAppDomain+"&q="+isearch.value,"_blank");
	else
		alert("Search string required!");
}


function gInitFields() {
	var iname = document.getElementById("fld_name");
	var iphone = document.getElementById("fld_phone");
	var imessage = document.getElementById("fld_message");	
	//var isearch = document.getElementById("fld_search");
	var icmd = document.getElementById("cmd_send");
	//var scmd = document.getElementById("cmd_search");
	
	icmd.onclick = function() {
		gSend();	
	}
	//scmd.onclick = function() {
	//	gSearch();	
	//}	
	iname.onfocus = function() {
		if(this.value == "Name")
			this.value = "";
	}
	iname.onblur = function() {
		if(this.value == "")
			this.value = "Name";
	}
	iphone.onfocus = function() {
		if(this.value == "Phone")
			this.value = "";
	}
	iphone.onblur = function() {
		if(this.value == "")
			this.value = "Phone";
	}
	imessage.onfocus = function() {
		if(this.value == "Message")
			this.value = "";
	}
	imessage.onblur = function() {
		if(this.value == "")
			this.value = "Message";
	}	
	//icmd.focus();
}

function gGoto(e) {
	var i = e.selectedIndex;
	window.open(e.options[i].value,"_self");
}

