var DHTML = (document.getElementById || document.all || document.layers);


function sendToFriend()
 {
 	FriendName = document.sendtofriend.FriendName.value;
	YourName   = document.sendtofriend.YourName.value;
	YourEmail  = document.sendtofriend.YourEmail.value;
	Message = document.sendtofriend.YourMessage.value;
	errors = false;
	
	if ((YourName==""))
	{
 		changeCol('Red', 'yname');
		errors =true;
	}
	else
	{
		changeCol('#663300', 'yname');
	}
	
	if ((FriendName==""))
	{
 		changeCol('Red', 'femail');
		errors =true;
	}
	else
	{
		if (checkEmail2(FriendName))
		{
			changeCol('#663300', 'femail');
		}
		else
		{
 		changeCol('Red', 'femail');
		errors =true;
		}
	}
	
	
	if ((YourEmail==""))
	{
 		changeCol('Red', 'yemail');
		errors =true;
	}
	else
	{
		if (checkEmail2(YourEmail))
		{
			changeCol('#663300', 'yemail');
		}
		else
		{
 		changeCol('Red', 'yemail');
		errors =true;
		}
	} //your,email
	
	if (errors)
	{
	    setIDMessage2('erromessage1', 'Missing or invalid information entered.');
		changeCol('Red', 'erromessage1');
	}
	else
	{
		setIDMessage2('erromessage1', 'Sending message. Please wait......');
		changeCol('#663300', 'erromessage1');
		var poststr = "FriendName=" + encodeURI(FriendName) + "&YourName=" + encodeURI(YourName)+ "&YourEmail=" + encodeURI(YourEmail)+ "&Message=" + encodeURI(Message);
		ajaxpack.postAjaxRequest(ajaxpack.basedomain+"/php/sendToFriend.php",poststr, processGetPost, "txt");
	}
 }
 
 function updateStatus(message)
 {
 	setIDMessage2('erromessage1', message);
	changeCol('#663300', 'erromessage1');
	document.sendtofriend.FriendName.value="";
	document.sendtofriend.YourName.value="";
	document.sendtofriend.YourEmail.value="";
	document.sendtofriend.YourMessage.value=""
 }
 
 function processGetPost(){
var myajax=ajaxpack.ajaxobj
var myfiletype=ajaxpack.filetype
if (myajax.readyState == 4){ //if request of file completed
if (myajax.status==200 || window.location.href.indexOf("http")==-1){  
if (myfiletype=="txt")
		{
		//alert(myajax.responseText);
		updateStatus("Message sent.");
		}
	else
	{
		alert(myajax.responseXML);
	}
}
}
}


function getObj(name)
{
  if (document.getElementById)
  {
  	this.obj = document.getElementById(name);
	this.style = document.getElementById(name).style;
  }
  else if (document.all)
  {
	this.obj = document.all[name];
	this.style = document.all[name].style;
  }
  else if (document.layers)
  {
   	this.obj = document.layers[name];
   	this.style = document.layers[name];
  }
}

function invi(flag, objName)
{
	if (!DHTML) return;
	var x = new getObj(objName);
	x.style.visibility = (flag) ? 'hidden' : 'visible'
}


function setIDMessage2(objName, message)
{
	var div=document.getElementById(objName);
	div.innerHTML= message ;
}

function setIDMessage(objName, message)
{
	if (!DHTML) return;
	var x = new getObj(objName);
	x.innerHTML = message;
}

function changeCol(col, objName)
{
	if (!DHTML) return;
	var x = new getObj(objName);
	x.style.color = col;
}

 function checkEmail2(myemail) {
if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(myemail)){
	return (true)
}
return (false)
}







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

var ajaxpack=new Object()
ajaxpack.basedomain="http://"+window.location.hostname
ajaxpack.ajaxobj=createAjaxObj()
ajaxpack.filetype="txt"
ajaxpack.addrandomnumber=1 //Set to 1 or 0. See documentation.

ajaxpack.getAjaxRequest=function(url, parameters, callbackfunc, filetype){
ajaxpack.ajaxobj=createAjaxObj() //recreate ajax object to defeat cache problem in IE
if (ajaxpack.addrandomnumber==1) //Further defeat caching problem in IE?
var parameters=parameters+"&ajaxcachebust="+new Date().getTime()
if (this.ajaxobj){
this.filetype=filetype
this.ajaxobj.onreadystatechange=callbackfunc
this.ajaxobj.open('GET', url+"?"+parameters, true)
this.ajaxobj.send(null)
}
}

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