var ie4 = (document.all != null);
var cookieTimeout = 300; //In Seconds
var gHLBUTTONID; 
var gCallBackMethod; //function pointer that references the call back method we want to call when returnign from process mobile.


function setContent(name, value) {
	var d;  
	if (ie4) { 
			d = document.all[name];
	} else {
			d = document.getElementById(name);
	}
	d.innerHTML = value;
}       

function getContent(name) {
	var d;
	if (ie4) {
			d = document.all[name];
	} else {
			d = document.getElementById(name);
	}
	return d.innerHTML;
}

function setColor(name, value) {
	var d;  
	if (ie4) { 
			d = document.all[name];
	} else {
			d = document.getElementById(name);
	}
	d.style.color = value;  
}

function getColor(name) {
	var d;
	if (ie4) {
			d = document.all[name];
	} else {
			d = document.getElementById(name);
	}
	return d.style.color;
}

function animate(name, col) {

	if(document.getElementById(name) != null)
	{
		var value = getContent(name);
		if( !value ) return;
		if (value.toLowerCase().indexOf('<span') >= 0 ) { return; }
		var length = 0;
		var str = '';
		var ch;
		var token = '';
		var htmltag = false;
		for (i = 0; i < value.length; i++) {
			ch = value.substring(i, i+1);
			if (i < value.length - 1) { nextch = value.substring(i+1, i+2); } else { nextch = ' '; }
			token += ch;
			if (ch == '<' && '/aAbBpPhHiIoOuUlLtT'.indexOf(nextch) >= 0) { htmltag = true; }
			if (ch == '>' && htmltag) { htmltag = false; }
			if (!htmltag && ch.charCodeAt(0) > 30 && ch != ' ' && ch != '\n') {		
							str += '<span id="' + name + '_' + length + '">' + token + '</span>';
				token = '';
				length++;
			}
		}
		setContent(name, str);
		command = 'animateloop(\'' + name + '\', ' + length + ', 0, 1, \'' + col + '\', 10)';
		setTimeout(command , 100);
	}
}

function animateloop(name, length, ind, delta, col, count) {
	var next = ind + delta;
	if (next >= length) { delta = delta * -1; next = ind + delta; }
	if (next < 0) { delta = delta * -1; next = ind + delta; }
    setColor(name + '_' + ind, getColor(name + '_' + next));
    setColor(name + '_' + next, col);
    command = 'animateloop(\'' + name + '\', ' + length + ', ' + next + ', ' + delta + ', \'' + col + '\', ' + count-- + ')';
    
    if(count > 0)
		setTimeout(command , 100);
}

/*******************************************************************
 * Common Code
 *******************************************************************/

var LockFurtherTransactions = false; //This is the locks until we are through the transactions
var CookieJustSet = false; //This means that a cookie was just set this round don't clear it with the clear logic.

function btnAction_StateChange(hlButtonID, sOrig, sNew)
{
	if(sNew != sOrig)
	{
		var hlOrigSpan = document.getElementById( hlButtonID.replace("hl" +  sOrig,"span" + sOrig) );
		var hlNewSpan = document.getElementById( hlButtonID.replace("hl" +  sOrig, "span" + sNew) );
		animate(hlButtonID.replace("hl"+sOrig,"an"+sNew), "#ff0000");
		if (hlOrigSpan != null) hlOrigSpan.style.display = "none";
		if (hlNewSpan != null) hlNewSpan.style.display = "inline";
	}
}

/*******************************************************************
 * Mobile Prompts
 *******************************************************************/
function ProcessMobilePhone()
{
    var MobilePhone = document.getElementById("txtMobilePhone");
    
    window.status = "Processing Mobile # Request";
	hideMobilePhoneDiv();

	var sDataPacket = MobilePhone.value.replace("#","") + "#" + gHLBUTTONID + "#" + document.location.href;
	RS.Execute("RemoteProcessing.aspx", "ProcessMobilePhone", sDataPacket, gCallBackMethod);
}

function ProcessMobilePassword()
{
    var MobilePhone = document.getElementById("txtMobilePhone");
    var MobilePassword = document.getElementById("txtMobilePassword");
    
    window.status = "Processing Mobile Password Request";
	hideMobilePasswordDiv();

	var sDataPacket = MobilePhone.value.replace("#","") + "#" + gHLBUTTONID + "#" + document.location.href + "#" + MobilePassword.value.replace("#","");
	RS.Execute("RemoteProcessing.aspx", "ProcessMobilePassword", sDataPacket, gCallBackMethod);
}

function ProcessTextMobilePassword()
{
    var MobilePhone = document.getElementById("txtMobilePhone");
    
    window.status = "Processing Mobile Password Text Request";
	hideMobilePasswordDiv();

	var sDataPacket = MobilePhone.value.replace("#","") + "#" + gHLBUTTONID + "#" + document.location.href;
	RS.Execute("RemoteProcessing.aspx", "ProcessTextMobilePassword", sDataPacket, gCallBackMethod);
}

//function cb_ProcessMobilePhone()
//{
//	var sResultArray = new Array();
//	sResultArray = results.split("#!#");
//	
//	window.status = "Processing Phone Response";
//	
//	hideMobilePhoneDiv();
//	
//	if(sResultArray[0] == "CellPhoneError")
//		ShowCellPhoneErrorDiv(sResultArray[1], "Processing", "Download", sResultArray[2]);
//	else if(sResultArray[0] == "Empty")
//	{
//		document.location.href = sResultArray[2];
//		ClearLastDownload();
//		//do NOT deliver CRCdownload yet, when the page loads it up should deliver it.
//		return;
//	}
//}
/*******************************************************************
 * Begin CRC Downloads
 *******************************************************************/

function InitializeDownload( hlButtonID, DigitalProductID )
{
	if(LockFurtherTransactions)
		return;
	
	if(document.getElementById(hlButtonID) == null)
	{
		RS.Execute("RemoteProcessing.aspx", "ClearCartSession", "");
		return;
	}
	
	LockFurtherTransactions = true;
	btnAction_StateChange(hlButtonID, "Download", "Processing");
	hlButtonID = hlButtonID.replace("Download", "Processing");
	ProcessDownload(DigitalProductID, hlButtonID);
}

function ProcessDownload(DigitalProductID, hlButtonID)
{
	window.status = "Processing Download Request";
	
	var sDataPacket = DigitalProductID + "#" + hlButtonID + "#" + document.location.href;
	RS.Execute("RemoteProcessing.aspx", "ProcessCRCDownloadRequest", sDataPacket, cb_ProcessDownload);
}

function cb_ProcessDownload(results)
{
    gCallBackMethod = cb_ProcessDownload;
	var sResultArray = new Array();
	sResultArray = results.split("#!#");
	
	window.status = "Download Request Successful";
	
	//Decrement only if the download was a actual redeemed
	if(sResultArray[0] == "Redeemed" || sResultArray[0] == "PreviousDownload")
	{
	    if(typeof(DecCounter) != "undefined" && DecCounter != null && typeof(sResultArray[4]) != "undefined" && sResultArray[4] != null)
		    DecCounter(sResultArray[4]);
		setTimeout("DeliverCRCDownload(\"" + sResultArray[1] + "\",\"" + sResultArray[2] + "\", \"" + sResultArray[3] + "\", 'downloadStatusDiv', 'downloadInfoDiv');", 1600);
	}
	else if(sResultArray[0] == "MobileRedeemed")
	{
        if(typeof(DecCounter) != "undefined" && DecCounter != null)
		    DecCounter(sResultArray[4]);
		setTimeout("DeliverCRCDownload(\"" + sResultArray[1] + "\",\"" + sResultArray[2] + "\", \"" + sResultArray[3] + "\", 'mobileStatusDiv', 'mobileInfoDiv');", 1600);
	}
	else if(sResultArray[0] == "EnterCellPhone")
		showMobilePhoneDiv(sResultArray[1], sResultArray[2]);
	else if(sResultArray[0] == "EnterCellPhonePassword")
		showMobilePasswordDiv(sResultArray[1], sResultArray[2]);
	else if(sResultArray[0] == "CellPhoneError")
		ShowCellPhoneErrorDiv(sResultArray[1], "Processing", "Download", sResultArray[2]);
	else if(sResultArray[0] == "Empty")
	{
		document.location.href = sResultArray[2];
		ClearLastDownload();
		return;
	}
	
	//Redirect to the download
	window.status = "";
	
	if(sResultArray.length == 8)
		deliverPreLicense( sResultArray[5], sResultArray[6], sResultArray[7] );
}
//shows additional link in popup div for Downloading latest version of JAVA for albums only
function ShowJavaLinkForAlbums(DownloadURL)
{
    if(DownloadURL.indexOf("BulkGet") > -1)
	{
	    adTime = 90;
	}
	else
	{	    
	    document.getElementById("trDownloadJavaLink").style.display = 'none';
	}

}

function DeliverCRCDownload(hlButtonID, DownloadURL, DownloadContent, statusDiv, infoDiv)
{
	btnAction_StateChange(hlButtonID, "Processing", "Downloading");
	hlButtonID = hlButtonID.replace("Processing", "Downloading");
	ShowJavaLinkForAlbums(DownloadURL);
	initAd(DownloadContent, statusDiv, infoDiv);
	setTimeout("btnAction_StateChange('" + hlButtonID + "','Downloading','Redeemed');LockFurtherTransactions = false;", 8000);
	
	setCookie("NMP_LastDownloadFunction", "DeliverCRCDownload", cookieTimeout);
	setCookie("NMP_LastButtonID", hlButtonID, cookieTimeout);
	setCookie("NMP_LastDownloadURL", DownloadURL, cookieTimeout);
	setCookie("NMP_LastDownloadContent", DownloadContent, cookieTimeout);
	setCookie("NMP_DeliveryPage", document.location.href, cookieTimeout);
	setCookie("NMP_AttemptCount", "1", cookieTimeout);
	CookieJustSet = true;
	
	if(DownloadURL != "")
		document.location.href = DownloadURL;
}

/*******************************************************************
 * End CRC Downloads
 *******************************************************************/

/*******************************************************************
 * Begin Free Downloads
 *******************************************************************/

function InitializeFreeDownload( hlButtonID, DigitalProductID )
{
		
	if(LockFurtherTransactions)
		return;
	
	if(document.getElementById(hlButtonID) == null)
	{
		RS.Execute("RemoteProcessing.aspx", "ClearCartSession", "");
		return;
	}

	
	LockFurtherTransactions = true;
	btnAction_StateChange(hlButtonID, "FreeDownload", "FreeProcessing");
	hlButtonID = hlButtonID.replace("Download", "Processing");
	ProcessFreeDownload(DigitalProductID, hlButtonID);
}

function ProcessFreeDownload(DigitalProductID, hlButtonID)
{
	window.status = "Processing Download Request";
	
	var sDataPacket = DigitalProductID + "#" + hlButtonID + "#" + document.location.href;
	RS.Execute("RemoteProcessing.aspx", "ProcessFreeDownloadRequest", sDataPacket, cb_ProcessFreeDownload);
}

function cb_ProcessFreeDownload(results)
{
    gCallBackMethod = cb_ProcessFreeDownload;
    
	var sResultArray = new Array();
	sResultArray = results.split("#!#");
	
	window.status = "Download Request Successful";
	
	//redirect (for instance: redirect to login if not already logged in on payment)
	if(sResultArray[0] == "Empty")
	{
		document.location.href = sResultArray[2];
		ClearLastDownload();
		return;
		//do NOT deliver free download yet, when the page loads it up should deliver it.
	}
	else if(sResultArray[0] == "MobileRedeemed")
		setTimeout("DeliverFreeDownload(\"" + sResultArray[1] + "\",\"" + sResultArray[2] + "\", \"" + sResultArray[3] + "\", 'mobileStatusDiv', 'mobileInfoDiv');", 1600);
	else if(sResultArray[0] == "Redeemed")
		setTimeout("DeliverFreeDownload(\"" + sResultArray[1] + "\",\"" + sResultArray[2] + "\", \"" + sResultArray[3] + "\", \"downloadStatusDiv\", \"downloadInfoDiv\");", 1600);
	else if(sResultArray[0] == "EnterCellPhone")
		showMobilePhoneDiv(sResultArray[1], sResultArray[2]);
	else if(sResultArray[0] == "EnterCellPhonePassword")
		showMobilePasswordDiv(sResultArray[1], sResultArray[2]);
	else if(sResultArray[0] == "CellPhoneError")
		ShowCellPhoneErrorDiv(sResultArray[1], "FreeProcessing", "FreeDownload", sResultArray[2]);
	
	//Redirect to the download
	window.status = "";
	
	if(sResultArray.length == 7)
		deliverPreLicense( sResultArray[4], sResultArray[5], sResultArray[6] );
}

function DeliverFreeDownload(hlButtonID, DownloadURL, DownloadContent, statusDiv, infoDiv)
{
	btnAction_StateChange(hlButtonID, "FreeProcessing", "FreeDownloading");
	hlButtonID = hlButtonID.replace("Processing","Downloading");
	ShowJavaLinkForAlbums(DownloadURL);
	initAd(DownloadContent, statusDiv, infoDiv);
	setTimeout("btnAction_StateChange('" + hlButtonID + "','FreeDownloading','FreeDownloaded');LockFurtherTransactions = false;", 8000);
	
	setCookie("NMP_LastDownloadFunction", "DeliverFreeDownload", cookieTimeout);
	setCookie("NMP_LastButtonID", hlButtonID, cookieTimeout);
	setCookie("NMP_LastDownloadURL", DownloadURL, cookieTimeout);
	setCookie("NMP_LastDownloadContent", DownloadContent, cookieTimeout);
	setCookie("NMP_DeliveryPage", document.location.href, cookieTimeout);
	setCookie("NMP_AttemptCount", "1", cookieTimeout);
	CookieJustSet = true;
	
	if(DownloadURL != "")
	    document.location.href = DownloadURL;
}

/*******************************************************************
 * End Free Downloads
 *******************************************************************/

/*******************************************************************
 * Begin E-Commerce Downloads
 *******************************************************************/

function FinalizePurchase(hlButtonID, DigitalProductID)
{
	if(document.getElementById(hlButtonID) == null)
	{
		RS.Execute("RemoteProcessing.aspx", "ClearCartSession", "");
		return;
	}
	
	LockFurtherTransactions = true;
	window.status = "Finalizing Purchase";
	
	btnAction_StateChange(hlButtonID, 'Buy', 'PaymentProcessing');
	hlButtonID = hlButtonID.replace("Buy","PaymentProcessing");
		
	RS.Execute("RemoteProcessing.aspx", "FinalizePurchaseRequest", DigitalProductID + "#" + hlButtonID + "#" + document.location.href, cb_FinalizePurchase);
}

function cb_FinalizePurchase(results)
{
    gCallBackMethod = cb_FinalizePurchase;

	var sResultArray = new Array();
	sResultArray = results.split("#!#");
	
	if(sResultArray[0] == "FINALIZED")
	{
		window.status = "";
		
		if(sResultArray.length == 7)
			deliverPreLicense( sResultArray[4], sResultArray[5], sResultArray[6] );
		
		setTimeout("DeliverECDownload(\"" + sResultArray[1] + "\",\"" + sResultArray[2] + "\",\"" + sResultArray[3] + "\", 'downloadStatusDiv', 'downloadInfoDiv');", 1600);
	}
	else if(sResultArray[0] == "MOBILEPURCHASED")
		setTimeout("DeliverECDownload(\"" + sResultArray[1] + "\",\"" + sResultArray[2] + "\",\"" + sResultArray[3] + "\", 'mobileStatusDiv', 'mobileInfoDiv');", 100);
	else if(sResultArray[0] == "EnterCellPhone")
		showMobilePhoneDiv( sResultArray[1], sResultArray[2]);
	else if(sResultArray[0] == "EnterCellPhonePassword")
		showMobilePasswordDiv( sResultArray[1], sResultArray[2]);
	else if(sResultArray[0] == "CellPhoneError")
		ShowCellPhoneErrorDiv(sResultArray[1], "PaymentProcessing", "Buy", sResultArray[2]);
	else if(sResultArray[0] == "Empty")
		document.location.href = sResultArray[2];
}

function InitiatePurchase(hlButtonID) 
{
	if(LockFurtherTransactions)
		return;
	
	btnAction_StateChange(hlButtonID, 'Buy', 'Confirm');
}

function ConfirmPurchase(hlButtonID, DigitalProductID) 
{
	if(LockFurtherTransactions)
		return;
		
	LockFurtherTransactions = true;
	btnAction_StateChange(hlButtonID, 'Confirm', 'PaymentProcessing');
	hlButtonID = hlButtonID.replace("Confirm", "PaymentProcessing");
	
	window.status = "Confirming Purchase";
	
	var DataPacket = DigitalProductID + "#" + hlButtonID + "#" + document.location.href;
	RS.Execute("RemoteProcessing.aspx", "ProcessECDownloadRequest", DataPacket, cb_ConfirmPurchase);
}

function cb_ConfirmPurchase(results)
{
    gCallBackMethod = cb_ConfirmPurchase;

	var sResultArray = new Array();
	sResultArray = results.split("#!#");
	
	window.status = "Processing Next State";
	
	if(sResultArray[0] == "PURCHASED")
	{
		//Prelicense the file Here
		
		if(sResultArray.length == 7)
			deliverPreLicense( sResultArray[4], sResultArray[5], sResultArray[6] );
		
		setTimeout("DeliverECDownload(\"" + sResultArray[1] + "\",\"" + sResultArray[2] + "\",\"" + sResultArray[3] + "\", 'downloadStatusDiv', 'downloadInfoDiv');", 1600);
	}
	else if(sResultArray[0] == "MOBILEPURCHASED")
		setTimeout("DeliverECDownload(\"" + sResultArray[1] + "\",\"" + sResultArray[2] + "\",\"" + sResultArray[3] + "\", 'mobileStatusDiv', 'mobileInfoDiv');", 100);
	else if(sResultArray[0] == "EnterCellPhone")
		showMobilePhoneDiv( sResultArray[1], sResultArray[2]);
	else if(sResultArray[0] == "EnterCellPhonePassword")
		showMobilePasswordDiv( sResultArray[1], sResultArray[2]);
	else if(sResultArray[0] == "CellPhoneError")
		ShowCellPhoneErrorDiv(sResultArray[1], "PaymentProcessing", "Buy", sResultArray[2]);
	else
		document.location.href = sResultArray[2];
}

function DeliverECDownload(hlButtonID, DownloadURL, DownloadContent, statusDiv, infoDiv)
{
	btnAction_StateChange(hlButtonID, "PaymentProcessing", "PaymentDownloading");
	hlButtonID = hlButtonID.replace("PaymentProcessing","PaymentDownloading");
	ShowJavaLinkForAlbums(DownloadURL);
	initAd(DownloadContent, statusDiv, infoDiv);
	setTimeout("btnAction_StateChange('" + hlButtonID + "','PaymentDownloading','Purchased');LockFurtherTransactions = false", 8000);
	window.status = "";
	
	setCookie("NMP_LastDownloadFunction", "DeliverECDownload", cookieTimeout);
	setCookie("NMP_LastButtonID", hlButtonID, cookieTimeout);
	setCookie("NMP_LastDownloadURL", DownloadURL, cookieTimeout);
	setCookie("NMP_LastDownloadContent", DownloadContent, cookieTimeout);
	setCookie("NMP_DeliveryPage", document.location.href, cookieTimeout);
	setCookie("NMP_AttemptCount", "1", cookieTimeout);
	CookieJustSet = true;			
	
	if(DownloadURL != "")
		document.location.href = DownloadURL;
}

/*******************************************************************
 * End E-Commerce Downloads
 *******************************************************************/

/*******************************************************************
 * Begin Exit Page Download Functions
 *******************************************************************/
 
function DeliverExitDownload( DownloadURL, DownloadContent, PreLicenseData, divStatus, divInfo )
{
	if(!divStatus)
	    divStatus = "downloadStatusDiv";
	    
	if(!divInfo)
	    divInfo = "downloadInfoDiv";
	
	if(PreLicenseData != null)
	{
		var sPreLicenseArray = PreLicenseData.split("#!#");
		deliverPreLicense( sPreLicenseArray[0], sPreLicenseArray[1], sPreLicenseArray[2] );
	}
    ShowJavaLinkForAlbums(DownloadURL);
	initAd(DownloadContent, divStatus, divInfo);
	setCookie("NMP_LastDownloadFunction", "DeliverExitDownload", cookieTimeout);		
	setCookie("NMP_LastDownloadURL", DownloadURL, cookieTimeout);
	setCookie("NMP_LastDownloadContent", DownloadContent, cookieTimeout);
	setCookie("NMP_DeliveryPage", document.location.href, cookieTimeout);
	setCookie("NMP_AttemptCount", "1", cookieTimeout);
	CookieJustSet = true;
	
	setTimeout("LockFurtherTransactions = false;", 8000);
	if(DownloadURL != "")
	    setTimeout("document.location.href = \"" + DownloadURL + "\"", 800);
}
 
 /*******************************************************************
 * End Exit Page Download Functions
 *******************************************************************/

/*******************************************************************
 * Begin Last Download Functions
 *******************************************************************/

function CheckLastDownload()
{
	if( getCookie("NMP_DeliveryPage") != document.location.href )
	{
		//If we are not on the page we delivered the download from we can't deliver it anymore ...		
		ClearLastDownload();		
	}

	if(getCookie("NMP_LastDownloadFunction") != null && !CookieJustSet)
	{
		var attempts = parseInt(getCookie("NMP_AttemptCount"));		
				
		if(attempts < 2)
		{
			var sDownloadURL = getCookie("NMP_LastDownloadURL");
			var DataPacket = "";
			
			if((sDownloadURL != null) && (sDownloadURL != "") && (sDownloadURL.indexOf("OID=") > -1))
				DataPacket = sDownloadURL.substring(sDownloadURL.indexOf("OID=")+4, sDownloadURL.length);			

			//RS Call to Check downloads that have been issued for this OID
			RS.Execute("RemoteProcessing.aspx", "CheckLastDownload", DataPacket, cb_CheckLastDownload);
		}
		else
			ClearLastDownload();
	}
}

function cb_CheckLastDownload(results)
{	
	if(results == "true")	
	{
		LockFurtherTransactions = true;
		
		switch(getCookie("NMP_LastDownloadFunction"))
		{
			case "DeliverECDownload":
				var sLastButton = getCookie("NMP_LastButtonID");
				sLastButton = sLastButton.replace("PaymentDownloading","Buy");
				btnAction_StateChange(sLastButton, "Buy", "PaymentProcessing");
				sLastButton = sLastButton.replace("Buy","PaymentProcessing");
				DeliverECDownload(sLastButton, getCookie("NMP_LastDownloadURL"), getCookie("NMP_LastDownloadContent"), 'downloadStatusDiv', 'downloadInfoDiv');
				break;
			case "DeliverCRCDownload":
				var sLastButton = getCookie("NMP_LastButtonID");
				sLastButton = sLastButton.replace("Downloading","Download");
				btnAction_StateChange(sLastButton, "Download", "Processing");
				sLastButton = sLastButton.replace("Download","Processing");
				DeliverCRCDownload(sLastButton, getCookie("NMP_LastDownloadURL"), getCookie("NMP_LastDownloadContent"), 'downloadStatusDiv', 'downloadInfoDiv');
				break;
			case "DeliverExitDownload":
				DeliverExitDownload( getCookie("NMP_LastDownloadURL"), getCookie("NMP_LastDownloadContent"), null );
				break;
		}
		
		var AttemptCnt = parseInt(getCookie("NMP_AttemptCount"));
		setCookie("NMP_AttemptCount", AttemptCnt++, cookieTimeout);
	}
	else
		ClearLastDownload();
}

function ClearLastDownload()
{	
	delCookie("NMP_LastDownloadFunction");
	delCookie("NMP_LastButtonID");
	delCookie("NMP_LastDownloadURL");
	delCookie("NMP_LastDownloadContent");	
	delCookie("NMP_DeliveryPage");
	delCookie("NMP_AttemptCount");	
}

//Call This everytime!
setTimeout("CheckLastDownload();", 800);
 
 /*******************************************************************
 * End Last Download Functions
 *******************************************************************/
 
/*******************************************************************
 * Begin Cell Phone Entry Functions
 *******************************************************************/

function ShowCellPhoneErrorDiv(hlButtonID, sOrig, sNew, error)
{
    LockFurtherTransactions = false;
	RS.Execute("RemoteProcessing.aspx", "ClearCartSession", "", cb_ShowCellPhoneErrorDiv);
	btnAction_StateChange(hlButtonID, sOrig, sNew);
	showMobileErrorDiv(error, hlButtonID);
}

function cb_ShowCellPhoneErrorDiv(results)
{
}

 /*******************************************************************
 * End Cell Phone Entry Functions
 *******************************************************************/
 
/*******************************************************************
 * Begin Cookie Functions
 *******************************************************************/
 
function getCookie(NameOfCookie)
{ 
	if (document.cookie.length > 0) 
	{ 
		begin = document.cookie.indexOf(NameOfCookie+"="); 
		if (begin != -1) 
		{ 
			begin += NameOfCookie.length+1; 
			end = document.cookie.indexOf(";", begin);
			if (end == -1) 
				end = document.cookie.length;
			return unescape(document.cookie.substring(begin, end)); 
		} 
	}
	
	return null; 
}

function setCookie(NameOfCookie, value, expireseconds) 
{ 
	var ExpireDate = new Date ();	
	ExpireDate.setTime(ExpireDate.getTime() + ( expireseconds * 1000));
	document.cookie = NameOfCookie + "=" + escape(value) + 
	((expireseconds == null) ? "" : "; expires=" + ExpireDate.toGMTString());
}



function delCookie (NameOfCookie) 
{ 
	if (getCookie(NameOfCookie)) {
		document.cookie = NameOfCookie + "=" + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}

/*******************************************************************
 * End Cookie Functions
 *******************************************************************/
 
 
function getTopOffset(child, startPosition)
{
    var parent = child.offsetParent;
    if(parent)
    {
        var offset = startPosition + child.offsetTop;
        return getTopOffset(parent, offset);
    }
    else
    {
        var x = startPosition;
        return x;
    }
}

function positionDivBelowButton(div, btn)
{
    var btnTopOffset = getTopOffset(btn, 0);
    
    div.style.top = (btnTopOffset + 25) + 'px';
}

function getElement(elementID)
{
    var element = null;
    if (document.getElementById) // DOM3 = IE5, NS6
	{
		element = document.getElementById(elementID);
	}
	else if (document.layers) // Netscape 4
	{
		element = document.divMobileError;
	}
	else // IE 4
	{
		element = document.all.divMobileError;
	}
	return element;
}

function getButton(hlButtonID)
{
    //    var btnID = hlButtonID.replace("PaymentProcessing", "Confirm").replace("Processing", "Download")
    var btnID = hlButtonID.replace("hl", "span");
    return getElement(btnID);
}

function showMobilePhoneDiv(hlButtonID, errMsg)
{
    gHLBUTTONID = hlButtonID;
    var divMobilePhone = getElement('divMobilePhone');
    var button = hlButtonID ? getButton(hlButtonID) : null;
    var divErrorMsg = getElement('divErrMsg');
    
    if(button)
        positionDivBelowButton(divMobilePhone, button);
	
	divMobilePhone.style.visibility = 'visible';
	divErrorMsg.innerHTML = errMsg;
}

function hideMobilePhoneDiv()
{
	if (document.getElementById) // DOM3 = IE5, NS6
		document.getElementById('divMobilePhone').style.visibility = 'hidden';
	else if (document.layers) // Netscape 4
		document.divMobilePhone.visibility = 'hidden';
	else // IE 4
		document.all.divMobilePhone.style.visibility = 'hidden';
}

function showMobileErrorDiv(error, hlButtonID)
{
    var divMobileError = getElement('divMobileError');
    var lblMobileError = getElement('lblMobileError');
    var button = hlButtonID ? getButton(hlButtonID) : null;
    
    if(button)
        positionDivBelowButton(divMobileError, button);
    
    divMobileError.style.visibility = 'visible';
	
	lblMobileError.innerHTML = error;
}

function hideMobileErrorDiv()
{
	if (document.getElementById) // DOM3 = IE5, NS6
		document.getElementById('divMobileError').style.visibility = 'hidden';
	else if (document.layers) // Netscape 4
		document.divMobileError.visibility = 'hidden';
	else // IE 4
		document.all.divMobileError.style.visibility = 'hidden';
}

function showMobilePasswordDiv(hlButtonID, errMsg)
{
    gHLBUTTONID = hlButtonID;
    
    var divMobilePassword = getElement('divMobilePassword');
    var button = hlButtonID ? getButton(hlButtonID) : null;
    
    if(button)
        positionDivBelowButton( divMobilePassword, button );
    
    divMobilePassword.style.visibility = 'visible';

    if( document.getElementById && errMsg && document.getElementById('divErrMsgMobilePassword'))
        document.getElementById('divErrMsgMobilePassword').innerHTML = errMsg;
}

function hideMobilePasswordDiv()
{
	if (document.getElementById) // DOM3 = IE5, NS6
		document.getElementById('divMobilePassword').style.visibility = 'hidden';
	else if (document.layers) // Netscape 4
		document.divMobilePassword.visibility = 'hidden';
	else // IE 4
		document.all.divMobilePassword.style.visibility = 'hidden';
}

function hideCompleteProfileDiv()
{
	if (document.getElementById) // DOM3 = IE5, NS6
		document.getElementById('divCompleteProfile').style.visibility = 'hidden';
	else if (document.layers) // Netscape 4
		document.divCompleteProfile.visibility = 'hidden';
	else // IE 4
		document.all.divCompleteProfile.style.visibility = 'hidden';
}

function showCompleteProfileDiv()
{
	if (document.getElementById && document.getElementById('divPhoneCarriers')) // DOM3 = IE5, NS6
		document.getElementById('divCompleteProfile').style.visibility = 'visible';
	else if (document.layers) // Netscape 4
		document.divCompleteProfile.visibility = 'visible';
	else // IE 4
		document.all.divCompleteProfile.style.visibility = 'visible';
}
function hidePhoneCarrierDiv()
{
	if (document.getElementById && document.getElementById('divPhoneCarriers')) // DOM3 = IE5, NS6
		document.getElementById('divPhoneCarriers').style.visibility = 'hidden';
	else if (document.layers && document.divPhoneCarriers) // Netscape 4
		document.divPhoneCarriers.visibility = 'hidden';
	else if(document.all.divPhoneCarriers) // IE 4
		document.all.divPhoneCarriers.style.visibility = 'hidden';
}

function showPhoneCarrierDiv()
{
	if (document.getElementById) // DOM3 = IE5, NS6
		document.getElementById('divPhoneCarriers').style.visibility = 'visible';
	else if (document.layers && document.divPhoneCarriers) // Netscape 4
		document.divPhoneCarriers.visibility = 'visible';
	else if(document.all.divPhoneCarriers) // IE 4
		document.all.divPhoneCarriers.style.visibility = 'visible';
}
