/**************************************************************************************************************/
// Create xmlhttp object based on browsertype
function getHTTPObject() {
  var xmlhttp;
  /*@cc_on
  @if (@_jscript_version >= 5)
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
        xmlhttp = false;
      }
    }
  @else
  xmlhttp = false;
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
	  xmlhttp.overrideMimeType("text/xml"); 
    } catch (e) {
      xmlhttp = false;
    }
  }
  return xmlhttp;
}
/**************************************************************************************************************/
// Define global variables to be used throughout
var TellafriendBusy = false;
var TellafriendHttp = getHTTPObject();
var step1Over = false;
var row = "";
/**************************************************************************************************************/

function sendMailToFriends(mode,sendurl,sendtitle,fromname,fromemail,friendemails,personalmess)
{
	document.getElementById('emailsenddiv').innerHTML="Processing...";
	if (!TellafriendBusy)
	{
		var url = "processTellafriend.php";
		var params = "mode="+mode+"&sendurl="+escape(sendurl)+"&sendtitle="+escape(sendtitle)+"&fromname="+fromname+"&fromemail="+fromemail+"&friendemails="+friendemails+"&personalmess="+encodeURIComponent(personalmess);
		TellafriendHttp.open("POST", url, true);

		//Send the proper header infomation along with the request
		TellafriendHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		TellafriendHttp.setRequestHeader("Content-length", params.length);
		TellafriendHttp.setRequestHeader("Connection", "close");

		TellafriendHttp.onreadystatechange = sendMailToFriendsResponse;
		TellafriendBusy = true;
		TellafriendHttp.send(params);
	}
}
function sendMailToFriendsResponse()
{
	if (TellafriendHttp.readyState == 4)
	{
		if (TellafriendHttp.responseText.indexOf('invalid') == -1)
		{
			try
			{
				var htmlRecord = TellafriendHttp.responseText;
				// Draw Record status and nav buttons
				document.getElementById('emailsenddiv').innerHTML=htmlRecord;
				TellafriendBusy = false;
			} // end try 
			catch(e)
			{
				alert("Error: " + e) ;
			} // end try catch
		} // end if response index -1
	} // end if readystate 4
} // end tableresponse function

function CheckSendEmailField(form,mode)
{
	var ctr=eval("document."+form);
	if(ctr.yname.value=="")
	{
		alert("Name can not be left blank.");
		ctr.yname.focus();
		return false;
	}
	if(ctr.yemail.value=="")
	{
		alert("E-mail can not be left blank.");
		ctr.yemail.focus();
		return false;
	}
	if (EmailCheckjs(ctr.yemail.value) == false)
	{
		alert("Invalid E-mail Address");
		ctr.yemail.focus();
		return false;
	}
	if(ctr.friendemails.value=="")
	{
		alert("Friends emails can not be left blank.");
		ctr.friendemails.focus();
		return false;
	}
	freinds_emails=ctr.friendemails.value;
	var col_array=freinds_emails.split(",");
	var part_num=0;
	while (part_num < col_array.length)
	 {
		tempemail=Trim(col_array[part_num]);
		if (EmailCheckjs(tempemail)==false)
		{
			alert(tempemail+" Invalid E-mail Address");
			ctr.friendemails.focus();
			return false;
		} 
		part_num+=1;
	 }
	 personalmessage=encodeURIComponent(ctr.mess.value);
	 sendurl=ctr.sendurl.value;
	 sendtitle=ctr.sendtitle.value;
	sendMailToFriends(mode,sendurl,sendtitle,ctr.yname.value,ctr.yemail.value,freinds_emails,personalmessage);
}

function Trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}


function ShowImport(importoption)
{
	document.getElementById('showimportsemail').innerHTML="";
	if (!TellafriendBusy)
	{
		var url="processTellafriend.php?mode=showimport&importoption="+importoption;
		TellafriendHttp.open("GET", url, true);
		TellafriendHttp.onreadystatechange = ShowImportResponse;
		TellafriendBusy = true;
		TellafriendHttp.send(null);
	}
}
function ShowImportResponse()
{
	if (TellafriendHttp.readyState == 4)
	{
		if (TellafriendHttp.responseText.indexOf('invalid') == -1)
		{
			try
			{
				var htmlRecord = TellafriendHttp.responseText;
				// Draw Record status and nav buttons
				var outputArr = new Array();
				outputArr = htmlRecord.split(":--");
				document.getElementById('importimages').innerHTML=outputArr[0];
				document.getElementById('showimportsemail').innerHTML=outputArr[1];
				TellafriendBusy = false;
			} // end try 
			catch(e)
			{
				alert("Error: " + e) ;
			} // end try catch
		} // end if response index -1
	} // end if readystate 4
} // end tableresponse function

function ImportFriendsEmail()
{
	var xx=document.sendlink;
	if(xx.username.value=="")
	{
		alert("Username is required.");
		xx.username.focus();
		return false;
	}
	if(xx.password.value=="")
	{
		alert("Password is required.");
		xx.password.focus();
		return false;
	}
	var spimport=xx.spimport.value;
	var username=xx.username.value;
	var password=xx.password.value;
	ImportEmails(spimport,username,password);
}
function ImportEmails(spimport,username,password)
{
	document.getElementById('processdiv').style.color="#ff0000";
	document.getElementById('processdiv').innerHTML="Fetching Contacts...";
	if (!TellafriendBusy)
	{
		var url="processTellafriend.php?mode=importEmails&spimport="+spimport+"&username="+username+"&password="+password;
		TellafriendHttp.open("GET", url, true);
		TellafriendHttp.onreadystatechange = ImportEmailsResponse;
		TellafriendBusy = true;
		TellafriendHttp.send(null);
	}
}
function ImportEmailsResponse()
{
	if (TellafriendHttp.readyState == 4)
	{
		if (TellafriendHttp.responseText.indexOf('invalid') == -1)
		{
			try
			{
				var htmlRecord = TellafriendHttp.responseText;
				// Draw Record status and nav buttons
				document.getElementById('processdiv').innerHTML="";
				document.getElementById('showimportsemail').innerHTML=htmlRecord;
				TellafriendBusy = false;
			} // end try 
			catch(e)
			{
				alert("Error: " + e) ;
			} // end try catch
		} // end if response index -1
	} // end if readystate 4
} // end tableresponse function

function ImportImage()
{
	document.getElementById('selectedimportimg').src="images/email"+document.sendlink.spimport.value+".gif";
}

function EmailCheckjs(argvalue) { if (argvalue.indexOf(" ") != -1) return false; else if (argvalue.indexOf("@") == -1) return false; else if (argvalue.indexOf("@") == 0) return false; else if (argvalue.indexOf("@") == (argvalue.length-1)) return false; var arrayString = argvalue.split("@"); if(arrayString.length > 2) return false; if (arrayString[1].indexOf(".") == -1) return false; else if (arrayString[1].indexOf(".") == 0) return false; else if (arrayString[1].charAt(arrayString[1].length-1) == ".") { return false; } return true; }
