function URLDecode( str )
{
   // Replace + with ' '
   // Replace %xx with equivalent character
   // Put [ERROR] in output if %xx is invalid.
   var HEXCHARS = "0123456789ABCDEFabcdef"; 
   var encoded = str;
   var plaintext = "";
   var i = 0;
   while (i < encoded.length) {
       var ch = encoded.charAt(i);
	   if (ch == "+") {
	       plaintext += " ";
		   i++;
	   } else if (ch == "%") {
			if (i < (encoded.length-2) 
					&& HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 
					&& HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
				plaintext += unescape( encoded.substr(i,3) );
				i += 3;
			} else {
				alert( 'Bad escape combination near ...' + encoded.substr(i) );
				plaintext += "%[ERROR]";
				i++;
			}
		} else {
		   plaintext += ch;
		   i++;
		}
	} // while

   return plaintext;
};

function getRequestBody(formRef)
{
	var aParams = new Array();
	
	for(var i=0; i< formRef.elements.length; i++)
	{
		if(formRef.elements[i].type=="radio")
		{
			if(formRef.elements[i].checked)
			{
				var sParam = encodeURIComponent(formRef.elements[i].name);
				sParam += "=";
				sParam += encodeURIComponent(formRef.elements[i].value);
				aParams.push(sParam);
			}
		}
		else
		{
			var sParam = encodeURIComponent(formRef.elements[i].name);
			sParam += "=";
			sParam += encodeURIComponent(formRef.elements[i].value);
			aParams.push(sParam);
		}
	}
	return aParams.join("&");
}

function getBlogDataProcess()
{
	document.getElementById('blogLoader').style.display = "inline";
	var oXmlHttp = zXmlHttp.createRequest();
	oXmlHttp.open("post", "/ajaxBlogProcess.php",true);
	oXmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	
	oXmlHttp.onreadystatechange = function()
	{
		if(oXmlHttp.readyState==4)
		{
			if(oXmlHttp.status==200)
			{
				processData(oXmlHttp.responseText);

				if(valid=="y")
				{
					document.getElementById('blogLoader').style.display = "none";
					/////////////////////
					var tmpHtml = "";
					for(var i=0;i<dataCount;i++)
					{
						tmpHtml += '<p>';
						if(window["dataDate"+i].length>0)
						{
							tmpHtml += URLDecode(window["dataDate"+i])+'<br />';
						}
						tmpHtml += '<b>'+URLDecode(window["dataName"+i])+':</b> ';
						tmpHtml += URLDecode(window["dataData"+i]);
						tmpHtml += '</p>';
						//tmpHtml += '<h4></h4>';
					}
					
					document.getElementById('blogDiv').innerHTML = tmpHtml+'<img src="images/spacer.gif" width="1" height="3" />';	
					//alert(document.getElementById('blogDiv').innerHTML);
					/////////////////////
				}
				else
				{
					document.getElementById('blogLoader').style.display = "none";
					//alert(URLDecode(errmsg));	
					document.getElementById('blogDiv').innerHTML = URLDecode(errmsg)+'<img src="images/spacer.gif" width="1" height="3" />';
				}
			}
			else
			{
				document.getElementById('blogLoader').style.display = "none";
				//alert("error: "+oXmlHttp.statusText);
				document.getElementById('blogDiv').innerHTML = "error: "+oXmlHttp.statusText+'<img src="images/spacer.gif" width="1" height="3" />';
			}
		}
	}
	
	//var toSend = getRequestBody(ref);
	oXmlHttp.send("");
	
	
	return false;
}

function processData(data)
{
	
	var txtexp = data.split("&");
	for(var i=0;i<txtexp.length;i++)
	{
		if(txtexp[i].length>0)
		{
		var txtexp2 = txtexp[i].split("=");
		var txtstr = txtexp2[0]+"='"+txtexp2[1]+"';";
		eval(txtstr);
		}
	}
}

function signup()
{
	document.getElementById('signupLoader').style.display = "inline";
	var oXmlHttp = zXmlHttp.createRequest();
	oXmlHttp.open("post", "/script-signup.php",true);
	oXmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	var sParam = encodeURIComponent('ml_email')+"="+encodeURIComponent(document.getElementById('ml_email').value);
	oXmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	
	oXmlHttp.onreadystatechange = function()
	{
		if(oXmlHttp.readyState==4)
		{
		document.getElementById('signupDiv').style.display = "block";
			if(oXmlHttp.status==200)
			{
			document.getElementById('signupLoader').style.display = "none";
			document.getElementById('signupDiv').innerHTML = oXmlHttp.responseText;
			}
			else
			{
				document.getElementById('signupLoader').style.display = "none";
				document.getElementById('signupDiv').innerHTML = "error: "+oXmlHttp.statusText+'<img src="images/spacer.gif" width="1" height="3" />';
			}
		}
	}
	
	//var toSend = getRequestBody(ref);
	oXmlHttp.send(sParam);
	
	
	return false;
}

//****INSTANT CALLBACK RELATED***********************
function showInstantCallback()
{
	document.getElementById('instantCallbackPopup').style.visibility = "visible";
	pageTracker._trackEvent('Instant Callback', 'Request Initiation');
}
function hideInstantCallback()
{
	document.getElementById('instantCallbackPopup').style.visibility = "hidden";
	document.getElementById('instantCallbackError').innerHTML = "";
	document.getElementById('instantCallBackFormName').value = "";
	document.getElementById('instantCallBackFormTel').value = "";
	document.getElementById('instantCallBackFormInterest').selectedIndex = 0;
	document.getElementById('instantCallBackFormEmail').value = "";
	if(document.getElementById('instantCallBackFormTime').selectedIndex)
	{
		document.getElementById('instantCallBackFormTime').selectedIndex = 0;
	}
	document.getElementById('instantCallBackLoader').style.display = "none";
}
function sendInstantCallback()
{
	if(document.getElementById('instantCallBackFormTel').value=="")
	{
		document.getElementById('instantCallbackError').innerHTML = "You did not enter a Telephone number for us to contact you.<br />";
	}
	else
	{
		document.getElementById('instantCallBackLoader').style.display = "inline";
		var oXmlHttp = zXmlHttp.createRequest();
		oXmlHttp.open("post", "/script-instant-callback.php",true);
		oXmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		var sParam = encodeURIComponent('callback_name')+"="+encodeURIComponent(document.getElementById('instantCallBackFormName').value);
		sParam += "&"+encodeURIComponent('callback_tel')+"="+encodeURIComponent(document.getElementById('instantCallBackFormTel').value);
		sParam += "&"+encodeURIComponent('callback_interest')+"="+encodeURIComponent(document.getElementById('instantCallBackFormInterest').value);
		sParam += "&"+encodeURIComponent('callback_email')+"="+encodeURIComponent(document.getElementById('instantCallBackFormEmail').value);
		sParam += "&"+encodeURIComponent('callback_time')+"="+encodeURIComponent(document.getElementById('instantCallBackFormTime').value);
		oXmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		
		oXmlHttp.onreadystatechange = function()
		{
			if(oXmlHttp.readyState==4)
			{
				if(oXmlHttp.status==200)
				{
					document.getElementById('instantCallbackError').innerHTML = oXmlHttp.responseText;
					if(oXmlHttp.responseText.substr(0,5)!="Error")
					{
						//track analytics
						if(document.getElementById('instantCallBackFormInterest').value>0)
						{
							pageTracker._trackEvent('Instant Callback', 'Successful Request Submission', document.getElementById('instantCallBackFormInterest').value);
						}
						else
						{
							pageTracker._trackEvent('Instant Callback', 'Successful Request Submission');
						}
					}
				}
				else
				{
					document.getElementById('instantCallbackError').innerHTML = "Error: "+oXmlHttp.statusText;
				}
				document.getElementById('instantCallBackLoader').style.display = "none";
			}
		}
		
		oXmlHttp.send(sParam);
	}
}
//****EOF INSTANT CALLBACK RELATED*******************