var emailId;
var maxDownloads = -1;
var numDownloads = -1;

function ShowDefaultMenu()
{
	try
	{
		var objResp = new WebResponse(0, "");
			
		objResp = xhttp.doSyncGet("methodname=ShowDefaultMenu");
		if (objResp.eId > 0)
			throw objResp.eMesg;

		var xml = GetXml(objResp);

		if (xml != null)
		{
			if (GetTagValue(xml, "eId") == "0")
			{
				var divMain = document.getElementById("divMain");
				
				divMain.innerHTML = GetTagValue(xml, "divBack");
				divMain.innerHTML += GetTagValue(xml, "divStatus");
				divMain.innerHTML += GetTagValue(xml, "divErrorStatus");
				divMain.innerHTML += GetTagValue(xml, "divEmail");
				//divMain.innerHTML += GetTagValue(xml, "divSites");
			}
			else
			{
				alert(GetTagValue(xml, "eMesg"));
			}
			document.getElementById("txtEmail").focus();				
		}

	}
	catch (ex)
	{
		alert(ex);
	}
}


function ShowFilterPanel()
{
	try
	{
		var objResp = new WebResponse(0, "");
			
		objResp = xhttp.doSyncGet("methodname=ShowFilterPanel");
		if (objResp.eId > 0)
			throw objResp.eMesg;

		var xml = GetXml(objResp);

		if (xml != null)
		{
			if (GetTagValue(xml, "eId") == "0")
			{
				var divMain = document.getElementById("divMain");
				
				divMain.innerHTML = GetTagValue(xml, "divBack");
				divMain.innerHTML += GetTagValue(xml, "divStatus");
				divMain.innerHTML += GetTagValue(xml, "divErrorStatus");
				//divMain.innerHTML += GetTagValue(xml, "divEmail");
				divMain.innerHTML += GetTagValue(xml, "divSites");
			}
			else
			{
				alert(GetTagValue(xml, "eMesg"));
			}				
		}

	}
	catch (ex)
	{
		alert(ex);
	}
}

function ShowRequestPanel()
{
	try
	{
		var objResp = new WebResponse(0, "");
			
		objResp = xhttp.doSyncGet("methodname=ShowRequestPanel");
		if (objResp.eId > 0)
			throw objResp.eMesg;

		var xml = GetXml(objResp);

		if (xml != null)
		{
			if (GetTagValue(xml, "eId") == "0")
			{
				var divMain = document.getElementById("divMain");
				
				divMain.innerHTML = GetTagValue(xml, "divBack");
				divMain.innerHTML += GetTagValue(xml, "divStatus");
				divMain.innerHTML += GetTagValue(xml, "divErrorStatus");
				//divMain.innerHTML += GetTagValue(xml, "divEmail");
				divMain.innerHTML += GetTagValue(xml, "divSites");
			}
			else
			{
				alert(GetTagValue(xml, "eMesg"));
			}				
		}

	}
	catch (ex)
	{
		alert(ex);
	}
}

function VerifyEmail()
{
	if (!isFirefoxFamily())
	{
		return;
	}

	if (document.getElementById("txtEmail") != null)
		emailId = document.getElementById("txtEmail").value;
	if (trimWhitespace(emailId) == "")
	{
		alert("Please enter a valid email address");
		return;
	}
//	MakeHashHistory("FormFunction=VerifyEmail");
		
	try
	{
		emailId = escape(emailId);
		var objResp = new WebResponse(0, "");
		objResp = xhttp.doSyncPost("EmailId=" + emailId, "methodname=VerifyEmail");
		if (objResp.eId > 0)
			throw objResp.eMesg;

		var xml = GetXml(objResp);

		if (xml != null)
		{
			if (GetTagValue(xml, "eId") == "0")
			{
				var isError = GetTagValue(xml, "isError");
				var divErrorStatus = document.getElementById("divErrorStatus");
				if (isError == "yes")
				{
					divErrorStatus.innerHTML = GetTagValue(xml, "divErrorStatus");
				}
				else
				{
					var divStatus = document.getElementById("divStatus");
					divStatus.innerHTML = GetTagValue(xml, "divStatus");
					divEmail.innerHTML = GetTagValue(xml, "divEmail");
					divErrorStatus.innerHTML = GetTagValue(xml, "divErrorStatus");
				}
				
			}
			else
			{
				alert(GetTagValue(xml, "eMesg"));
			}				
		}

	}
	catch (ex)
	{
		alert(ex);
	}
}

function RequestSites()
{
	if (document.getElementById("divRequest").style.display == "none")
	{
		document.getElementById("divRequest").style.display = "block";
		document.getElementById("txtRequest").focus();
		document.getElementById("btnRequestSite").value = "Hide Request site";
	}
	else
	{
		document.getElementById("divRequest").style.display = "none";
		document.getElementById("btnRequestSite").value = "Request a site";
	}
}

function DoRequest()
{
	//document.getElementById("divRequest").style.display = "none";
	var reqUrl = document.getElementById("txtRequest").value;
	if (trimWhitespace(reqUrl) == "")
	{
		alert("Please enter a url!");
		return;
	}
	else
	{
		try
		{
			reqUrl = escape(reqUrl);
			reqEmailId = document.getElementById("txtRequestEmail").value;
			var objResp = new WebResponse(0, "");
			objResp = xhttp.doSyncPost("ReqEmailId=" + reqEmailId + "&ReqUrl=" + reqUrl, "methodname=MakeRequest");
			if (objResp.eId > 0)
				throw objResp.eMesg;
	
			var xml = GetXml(objResp);
	
			if (xml != null)
			{
				if (GetTagValue(xml, "eId") == "0")
				{
					alert("Request Sent!");
					//RequestSites();
				}
				else
				{
					alert(GetTagValue(xml, "eMesg"));
				}				
			}
	
		}
		catch (ex)
		{
			alert(ex);
			return;
		}
	}
}


function DownloadSites()
{
	var arrSites = new Array();
	var i = 1;
	while (i < 100)
	{
		var chkbox = document.getElementById("chkSites_" + i);
		
		if (chkbox != null)
		{
			if (chkbox.checked)
				arrSites.push(chkbox.value);
		}
		else
			break;
		i++;	
	}
	
	if (arrSites.length > 0)
	{
		siteIdString = arrSites.join(";");
		var url = "/ext/dictionarytip/xml/serveXml.php?act=download&siteidstring=" + siteIdString;
		setTimeout( "window.location.href = '" + url + "'", 1000 );
	}
	else
		alert("You have to select atleast one Dictionary/site!");
}


function ShowXml(evt, counter, xmlUrl)
{
	var xmlDiv = document.getElementById("xmlDiv_" + counter);
	
	try
	{
		if (xmlDiv.style.display == "none")
		{
			xmlDiv.setAttribute("src", xmlUrl);
			xmlDiv.style.display = "block";
			xmlDiv.style.height = "300px";
			document.getElementById("btnShowXml_" + counter).value = "Hide Xml";
			
			var txtDiv = document.getElementById("txt_" + counter);
			txtDiv.style.display = "none";
			document.getElementById("btnShowCode_" + counter).value = "View Code";
		}			
		else
		{
			xmlDiv.style.display = "none";
			document.getElementById("btnShowXml_" + counter).value = "View Xml";
		}


	}
	catch (ex)
	{
		alert(ex);
	}	
}




function ShowCode(evt, counter, xmlUrl)
{
	var txtDiv = document.getElementById("txt_" + counter);
		
	try
	{
		var objResp = new WebResponse(0, "");
		objResp = xhttp.doSyncGet("", xmlUrl);
		if (objResp.eId > 0)
			throw objResp.eMesg;
			
		var xml = GetXml(objResp);
						
		var jsString = getJSStringFromNode(xml.firstChild); //make the JS Code for the current node
		
		if (txtDiv.style.display == "none")
		{
			//jsString = "<pre>" + jsString + "</pre>";
			txtDiv.value = jsString;
			txtDiv.style.display = "block";
			txtDiv.style.height = "300px";
			document.getElementById("btnShowCode_" + counter).value = "Hide Code";
			
			var xmlDiv = document.getElementById("xmlDiv_" + counter);
			xmlDiv.style.display = "none";
			document.getElementById("btnShowXml_" + counter).value = "View Xml";
		}			
		else
		{
			txtDiv.style.display = "none";
			document.getElementById("btnShowCode_" + counter).value = "View Code";
		}

	}
	catch (ex)
	{
			alert(ex);
	}	
}


//make JS from the given node
function getJSStringFromNode(dictNode)
{
	var isExpertMode = dictNode.getAttribute("expert-mode");
	var developedBy = (dictNode.getAttribute("contributor") == null) ? "" : "//developed by " + dictNode.getAttribute("contributor");
	var jsString = developedBy + "\n";
	jsString += "tempPreviewDict = \n{\n";
	jsString += "\tdictName : \"" + dictNode.getAttribute("dictionary-name") + "\",\n";
	jsString += "\turl : \"\",\n";
	jsString += "\tsubCatName : \"" + dictNode.getElementsByTagName("sub-cat-name")[0].firstChild.nodeValue + "\",\n";
	jsString += "\tsubCatValue : \"" + dictNode.getElementsByTagName ("sub-cat-value")[0].firstChild.nodeValue + "\",\n";
	jsString += "\tselectedText : \"\",\n";
	
	//assignUrl function
	jsString += "\tassignUrl : function(selectedText) \n\t{ \n";
	jsString += "\t\tvar url = \"\";\n";
	//jsString += "\t\tselectedText = escape(selectedText);\n";
	//if (dictNode.getAttribute("convert-from-unicode") == "true")
	//	jsString += "\t\tselectedText = convertFromUnicode( 'UTF-8', selectedText );\n";
	
	var strUrl = "";
	if (isExpertMode == "yes")
		strUrl = dictNode.getElementsByTagName ("dictionary-url")[0].firstChild.nodeValue;    
	else
		strUrl = "url = \"" + dictNode.getElementsByTagName("dictionary-url")[0].firstChild.nodeValue + "\";";
	
	strUrl = strUrl.replace(/\$\$/gi, "\" + escape(selectedText) + \"");
	jsString += "\t\ttry\n\t\t{\n";
	jsString += "\t\t\t" + strUrl + "\n";
	jsString += "\t\t}\n\t\tcatch (ex)\n\t\t{\n";
	jsString += "\t\t\talert(ex);\n\t\t}\n";
	
	jsString += "\t\tthis.url = url;\n";
	jsString += "\t\tthis.selectedText = selectedText;\n";
	jsString += "\t},\n";

	//filterWebpage function
	jsString += "\tfilterPage : function (ftDoc) \n\t{ \n";
	var functionDefinition = "";
	if (isExpertMode == "yes")
	{
		functionDefinition = dictNode.getElementsByTagName("function-definition")[0].firstChild.nodeValue;
		functionDefinition = functionDefinition.replace(/\n/g, "\n\t\t\t");
	}
	
	jsString += "\t\ttry\n\t\t{\n";
	jsString += "\t\t\t" + functionDefinition + "\n";
	jsString += "\t\t}\n\t\tcatch (ex)\n\t\t{\n";
	jsString += "\t\t\talert(ex);\n\t\t}\n";
	jsString += "\t}\n";

	jsString += "}\n\n";
	
	return jsString;
}



function GetXmlDiv()
{
	var xmlDiv = document.getElementById("xmlDiv");
	if (xmlDiv == null)	//create one
	{
		xmlDiv = document.createElement("TEXTAREA");
		xmlDiv.setAttribute("ID", "xmlDiv");
		xmlDiv.setAttribute("type", "text");
		xmlDiv.setAttribute("class", "xmldiv");
		//document.body.appendChild(xmlDiv);
		
		return xmlDiv;
	}
	else
		return xmlDiv;
}

function DownloadExtension(url, pnumDownloads, pmaxDownloads)
{
	maxDownloads = (maxDownloads < 0) ? pmaxDownloads : maxDownloads;
	numDownloads = (numDownloads < 0) ? pnumDownloads : numDownloads;
	
	if (numDownloads >= maxDownloads)
	{	
		alert("Sorry, you exceeded the limit on number of downloads! You can download it only " + maxDownloads + " times. You may have to buy again (or) contact the author if you any problem...");
		//var btnDownload = document.getElementById("btnDownload");
		//btnDownload.disabled = true;
		return;	
	}
	else
	{
		numDownloads++;
		setTimeout( "window.location.href = '" + url + "'", 1000 );
	}
}

function isFirefoxFamily()
{
	//alert(navigator.userAgent + ", " + navigator.appName + ", " + navigator.appCodeName + "," + navigator.appVersion);
	//return false;
	if (navigator.userAgent.indexOf("Firefox/") != -1)
		return true;
	if (navigator.userAgent.indexOf("Gecko/") != -1)
		return true;
		
//	alert("You need to have firefox or mozilla to purchase or login and download. Other browsers are not supported!");
	return true;
}




