//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''	
// Global Request Function -Mark 1/14/2010
//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''	


//'''''''''''''''''''VARIABLES'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''	
var GlobalMethod, GlobalxmlHttp, GlobalAppend=false;
//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''	
		

//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''		
// Basic Ajax call..  just call and return a response.  Well some stuff is special, we might append a element, or we might overwrite
//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''		
function AjaxBasic(strURL, strElement)
{  

	try
	{	
		var xmlHttp, blnAppend=false;
		xmlHttp = GetXmlHttpObject();
			
		xmlHttp.onreadystatechange=function()
		{

			if(xmlHttp.readyState == 4) 
			{
				if(strElement == "return")
				{
					return xmlHttp.responseText;
				}
				else
					SendResponse(strURL, xmlHttp.responseText, strElement, blnAppend);
			}		
		}
		xmlHttp.open("GET", strURL, true);
		xmlHttp.send(null);
	}
	catch(e)
	{
		alertError(e);
	}		
}


//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''	
// MAIN POST FUNCTION Try not to change
//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
function AjaxPOST(url, frmParams, divElement, blnAsync, AppendResponse)
{
	//this function will create "Threads" of responses.  Because many requests can happen at once, the browser doesn't handle for this natively.
	if (msieversion() < 7)			
	{
		//Some reason IE 6 doesn't like the Ajax Post, so lets get them out of here and send them to AjaxGET
		AjaxGET(url, frmParams, divElement, blnAsync, AppendResponse);
	}
	else
	{
		frmParams = CleanParams(frmParams);
		
		try
		{
			var xmlHttp=GetXmlHttpObject();	
			if(null != xmlHttp)
			{
				url = url + "?SID=" + NewID();
				xmlHttp.open("POST", url, blnAsync);
				xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
				xmlHttp.setRequestHeader("Content-length", frmParams.length);
				xmlHttp.setRequestHeader("Connection", "close");
				
				if (blnAsync == true)
				{
					xmlHttp.onreadystatechange = function()
					{
						if(xmlHttp.readyState == 4)  
						{
							SendResponse(url, xmlHttp.responseText, divElement, AppendResponse);
						}	
					};
					xmlHttp.send(frmParams);
				}
				else
				{
					xmlHttp.send(frmParams);
					SendResponse(url, xmlHttp.responseText, divElement, AppendResponse);
				}
			}
			else
			{
			
				alertError("NULL Ajax Request Element");
			}				
		}
		catch(e)
		{
			alertError(e);
		}
	}
}

//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''	
// MAIN GET FUNCTION Try not to change 
//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''	
function AjaxGET(url, qsParams, divElement, blnAsync, AppendResponse)
{
	//this function will create "Threads" of responses.  Because many requests can happen at once, the browser doesn't handle for this natively.
	try
	{		
		frmParams = CleanParams(qsParams);
		
		qsOrAmp = "?";
		if(url.match("\\?"))
			qsOrAmp = "&";
		
		url = url + qsOrAmp + "SID=" + NewID() + "&" + frmParams;
		
		var xmlHttp=GetXmlHttpObject();
		
		if(null != xmlHttp)
		{						
			if (blnAsync == true)
			{
				xmlHttp.onreadystatechange=function()
				{
					if(xmlHttp.readyState == 4) 
						SendResponse(url, xmlHttp.responseText, divElement, AppendResponse);
				};
			}
			xmlHttp.open("GET",url, blnAsync);
			xmlHttp.send(null);
			if (blnAsync == false)
				SendResponse(url, xmlHttp.responseText, divElement, AppendResponse);
			
		}
		else
			alertError("NULL Ajax Request Element");
	}
	catch(e)
	{
		alertError(e);
	}
}



//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''	
// Returns the "strResponseText" the "Element" and will append or replace depending on "Append"
//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''	
function SendResponse(strURL, strResponseText, Element, Append)
{	
	try
	{
		if (Element != "")
		{
			if(null != document.getElementById(Element) && typeof(document.getElementById(Element)) != "undefined")
				document.getElementById(Element).innerHTML = strResponseText;
				
			else if(Element == "javascript" && strResponseText != "")
				eval(strResponseText);
		}
	}
	catch(e)
	{
		alertError(e);
	}
}

//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''	
// Cross-Ajax-Enabled-Browser Ajax XML request function !NEVER CHANGE!
//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''	
function GetXmlHttpObject()
	{
		var xmlHttp=null;
		try
		  	{
		  		// Firefox, Opera 8.0+, Safari
				xmlHttp=new XMLHttpRequest();
		  	}
		catch (e)	
		  	{
				// Internet Explorer
			  	try
					{
						xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
					}
			  	catch (e)
					{
						try
							{
								//Older Internet Exploder versions
								xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
							}
						catch(e)
							{
								//alertError(e);
								return;
							}
					}
		  	}
		return xmlHttp;
	}
	
	
//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''	
// Cleans up strings passed to the Request functions.
//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''	
function CleanParams(frmString)	
{
		try
		{
			// We can't have any "?" or "&" in the begining of the parameters for either requests
			if((Left(frmString,1) == "?") || (Left(frmString,1) == "&"))
				frmString = Right(frmString,String(frmString).length-1);	
			return frmString;
		}
		catch(e)
		{
			alertError(e);
		}
}

//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''	
// Quickly trim lefts without errors
//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''	
function Left(str, x)
{
	try
	{
		if(x <= 0)
			return "";	
		else if(x > String(str).length)
			return str;
		else
			return String(str).substring(0,x);
	}
	catch(e)
	{
		alertError(e);
	}				
}	
	
//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''	
// Quickly trim rights without errors
//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''	
function Right(str, x)
{
	try
	{
		if(x <= 0)
			return "";	
		else if(x > String(str).length)
			return str;
		else
			return String(str).substring(String(str).length, String(str).length - x);
	}
	catch(e)
	{
		alertError(e);
	}	
}

//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''	
// Create a new request id each time a request is made.	
//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''	
function NewID()
{
	try
	{
		return Left(Math.random(),8);
	}
	catch(e)
	{
		alertError(e);
	}		
}

//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''	
// If an element is hidden we are going to make it visible	
//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''	
function UnHide(Element)
{
	try
	{	

		if(undefined != document.getElementById(Element) && null != document.getElementById(Element))
		{
			if(document.getElementById(Element).style.visibility=="hidden" || document.getElementById(Element).style.display=="none")
			{
				document.getElementById(Element).style.visibility="visible";
				document.getElementById(Element).style.display="inline";
			}
		}
	}
	catch(e)
	{
		alertError(e);	
	}
}
	
//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''	
// Error function
//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''	
function alertError(err)
{
	// We don't want to alert anyone of an error so internal only, also maybe we can set up a Email about this so external we can get the info we need?
	if (document.URL.indexOf('executivepulse.com') <= 0)
	{
		try
		{
			//informative alert (some browser may not support the err class)
			if(null != err.lineNumber)
				Line = err.lineNumber;
			else
				Line = err.number;
				
			alert ("Your browser does not support a feature used on this page.\n\nPlease contact Technical Support by email at support@e-pulse.net or phone: toll free 1-866-397-8573 (1-866-TEC-LINK).\n\nSorry for the inconvenience.\n\nDetails:\n\nError name: " + err.name+"\nLine number: "+Line+"\nError description: " + err.message);
		}
		catch(e)
		{
			alert ("Your browser does not support a feature used on this page.\n\nPlease contact Technical Support by email at support@e-pulse.net or phone: toll free 1-866-397-8573 (1-866-TEC-LINK).\n\nSorry for the inconvenience.\n\nDetails:\n\n" + err);
		}
	}
}


function ChangeTitle(strTitle)
{
	document.title = "ExecutivePulse - " + strTitle;
}


function subscribeCheckErrors(strType, strEmailText)
{
	var daFrom = document.getElementById("myForm");
	var msg = "";
	
	if(null != daFrom)
	{
		msg += checkText("FirstName", "First name");
		msg += checkText("LastName", "Last name");
		msg += checkText("Email", strEmailText);
		msg += checkText("ConfirmEmail", "Confirm " + strEmailText);
		
		if(daFrom.ConfirmEmail.value != daFrom.Email.value)
			msg += " - Confirm " + strEmailText + " and " + strEmailText + " do not match."		
		
		if(strType == "Email" || strType == "email")
			msg += checkText("HTMLEmail", "Preferred email format");
	}
	else
		msg = "An unexpected error occured, please email socialnetworking@e-pulse.net to notify us of the issue.";	
	
	if(msg != "")
	{
		alert("Errors were detected on the form please correct and resubmit.\n\r\n\r" + msg);
		return false;
	}
	else
		return true;	
	
}
	
function checkText(strElementID, strName)
{
	var el = document.getElementById(strElementID);
	if(null != el)
	{
		if(undefined != el.value)
		{
			switch(el.type)
			{
				case "text":
					if("" != el.value)
						return "";

					else
						return " - " + strName + " is a required field.\n\r";
				break;	
				
				case "select-one":
						
					if("" != el.selectedIndex > 0)
						return "";
					
					else
						return " - " + strName + " is a required field.\n\r";
				break;
				
				case "radio":
					if(!el)
						return "";
					var elLength = el.length;
					if(elLength == undefined)
					{
						if(!el.checked)
							return " - " + strName + " is a required field.\n\r";
						else
							return "";
					}
					else
						return "";

				break;
				
			}
		}
		else
			return " - " + strName + " is a required field.\n\r";
	}
	else
		return " - " + strName + " was not present on the form. Please contact us by email at socialnetworking@e-pulse.net.\n\r	";
}


function saveForm()
{
	FirstName =  document.getElementById("FirstName").value;
	LastName =  document.getElementById("LastName").value;
	Email =  document.getElementById("Email").value;
	HTMLEmail =  document.getElementById("HTMLEmail").value;
	strType =  document.getElementById("Type").value;
	Action = document.getElementById("Action").value;
	
	AjaxPOST("Scripts/SubscribeBox.asp", "&Action="+Action+"&Type="+strType+"&FirstName="+FirstName+"&LastName="+LastName+"&Email="+Email+"&HTMLEmail="+HTMLEmail, "followUsSub", false, false)		
}
