﻿// JScript File
// Scroller Thing 

var moverIval;
var defMsg = "Please choose &gt;&gt;&gt;";

function focusDL() {

	var constpos = 100;
	var topMargin = 100;
	var oTop = document.body.offsetTop+constpos;
	var iTop = parseInt(document.getElementById("indexdiv").style.top);

	try {
	
		var newpos = Math.abs(iTop-oTop);
		var dir = (iTop > oTop ? -1 : 1); 

		newpos = iTop + dir * (newpos/2);
		if(newpos<topMargin) newpos=topMargin;
		document.getElementById("indexdiv").style.top = newpos;
		if(dir==1) if(newpos >= (oTop)-1) clearInterval(moverIval);
		if(dir==-1) if(newpos <= (oTop)+1) clearInterval(moverIval);
		
	} catch(e) { alert(e.message); }
	
}

function setOnScrollEvent() 
{
	window.onscroll = function () {
		try {
			clearInterval(moverIval);
			moverIval=setInterval("focusDL()",50);
		} catch(e) {}
	}
	window.onresize = function() {
		positionMDNL();
	}
}

function positionMDNL()
{
	try {
		document.getElementById("indexdiv").style.visibility="visible";
		document.getElementById("indexdiv").style.left="0px;"; 
	} catch(e) {}
}

// End Scroller Thing


function gatherIndex()
{
	var ulElem = document.getElementById("indexul");
	var anchorElem = document.getElementsByTagName("A");
	var i, j=0, tmp, aLength=anchorElem.length;
	var descrs = new Array();
	var names = new Array();

	for(i=0;i<aLength;i++) {
		if(anchorElem[i].descr!=null) {
		    if(anchorElem[i].descr!="" && anchorElem[i].descr!="undefined" && anchorElem[i].name!="X") {
	            //alert(anchorElem[i].attributes['descr'].value);	    
			    names[j] = anchorElem[i].name;
			    descrs[j++] = anchorElem[i].descr;
            }			    
		}
	}
    
   	//tmp = document.createElement("LI");
	//tmp.innerHTML = "<A href='#absolutehome' style='color: silver;'>Top of page</a>";
    //tmp.className = "IndexItemTOP";	
	//ulElem.appendChild(tmp);

	for(i=0;i<descrs.length;i++) {
		if(names[i]!="separator") {
		    tmp = document.createElement("LI");
		    tmp.className = "IndexItem";
		    tmp.innerHTML = "<A id='navlink' href='#"+names[i]+"'>"+descrs[i]+"</a>";
        }
        else {
            tmp = document.createElement("HR");
		    tmp.noshade=true;
		    tmp.style.color='silver';
		    tmp.size=1; 
        }
		ulElem.appendChild(tmp);
	}
	
	//alert(document.body.innerHTML);
}


function itemNavigator()
{
	var i, j, moreInfColl = document.getElementsByTagName("DIV");
	var bMI, bDL, bBY;
	var dNav, dInf = null, dDnl = null, dBuy = null;
	var iSpan; 
	var dlItemsId, byItemsId, n;

	//debugger;

	for(i=0;i<moreInfColl.length;i++) {
		if(moreInfColl[i].id=="productInfo") {

			dNav = null;
			dInf = null;
			dDnl = null;
			dBuy = null;
			
			dlItemsId = new Array();
			byItemsId = new Array();
			
			if(moreInfColl[i].attributes["DownloadItems"]) {
			    dlItemsId = String(moreInfColl[i].attributes["DownloadItems"].value).split(';');
			}
			
			if(moreInfColl[i].attributes["BuyItems"]) {
			    byItemsId = String(moreInfColl[i].attributes["BuyItems"].value).split(';');
			    //alert("LEn="+byItemsId.length+"/"+byItemsId[0]);
			}

			for(j=0;j<moreInfColl[i].childNodes.length;j++) {

				if(moreInfColl[i].childNodes[j].id=="p_navigator") dNav = moreInfColl[i].childNodes[j];
				if(moreInfColl[i].childNodes[j].id=="p_details") dInf = moreInfColl[i].childNodes[j];
				if(moreInfColl[i].childNodes[j].id=="p_download") dDnl = moreInfColl[i].childNodes[j];
				if(moreInfColl[i].childNodes[j].id=="p_buy") dBuy = moreInfColl[i].childNodes[j];
			}

            iSpan = document.createElement("SPAN");
            iSpan.className = "infoNav";

			bMI = document.createElement("BUTTON");
			bDL = document.createElement("BUTTON");
			bBY = document.createElement("BUTTON");

			bMI.className = "btSupp";
			bDL.className = "btSupp";
			bBY.className = "btSupp";

			bMI.innerHTML = "More details...";
			bDL.innerHTML = "Download info...";
			bBY.innerHTML = "Purchase info...";
			
			bMI.stat = iSpan;
			bDL.stat = iSpan;
			bBY.stat = iSpan;
			
			bMI.hideFocus = true;
			bDL.hideFocus = true;
			bBY.hideFocus = true;
						
			bMI.b1 = bDL;
			bMI.b2 = bBY;
			bMI.opened = false;
			bMI.myDiv = dInf;
			bMI.onclick = function() { 
				if(this.opened) { this.myDiv.style.display="none"; this.opened = false; this.stat.innerHTML = defMsg; this.className = "btSupp";} 
				else { 
					if(this.b1.myDiv!=null) { this.b1.myDiv.style.display="none"; this.b1.className = "btSupp"; } this.b1.opened = false;  
					if(this.b2.myDiv!=null) { this.b2.myDiv.style.display="none"; this.b2.className = "btSupp"; } this.b2.opened = false; 
					this.myDiv.style.display="block"; 
					this.opened = true; 
					this.stat.innerHTML = "";
					this.className = "btSupp_on";
				}
			}

			bDL.b1 = bMI;
			bDL.b2 = bBY;
			bDL.opened = false;
			bDL.myDiv = dDnl;
			bDL.onclick = function() { 
				if(this.opened) { this.myDiv.style.display="none"; this.opened = false; this.stat.innerHTML = defMsg; this.className = "btSupp"; } 
				else { 
					if(this.b1.myDiv!=null) { this.b1.myDiv.style.display="none"; this.b1.className = "btSupp"; } this.b1.opened = false;  
					if(this.b2.myDiv!=null) { this.b2.myDiv.style.display="none"; this.b2.className = "btSupp"; } this.b2.opened = false; 
					this.myDiv.style.display="block"; 
					this.opened = true; 
					this.stat.innerHTML = "Downloading...";
					this.className = "btSupp_on";
				}
			}

			bBY.b1 = bMI;
			bBY.b2 = bDL;
			bBY.opened = false;
			bBY.myDiv = dBuy;
			bBY.onclick = function() { 
				if(this.opened) { this.myDiv.style.display="none"; this.opened = false; this.stat.innerHTML = defMsg; this.className = "btSupp"; } 
				else { 
					if(this.b1.myDiv!=null) { this.b1.myDiv.style.display="none"; this.b1.className = "btSupp"; } this.b1.opened = false;  
					if(this.b2.myDiv!=null) { this.b2.myDiv.style.display="none"; this.b2.className = "btSupp"; } this.b2.opened = false; 
					this.myDiv.style.display="block"; 
					this.opened = true; 
					this.stat.innerHTML = "Purchasing ...";
					this.className = "btSupp_on";
				}
			}

			if(dNav!=null) {
			    iSpan.innerHTML = defMsg;
			    dNav.appendChild(iSpan);
				
				if(dInf!=null) dNav.appendChild(bMI); 
				if(dDnl!=null) dNav.appendChild(bDL); 
				if(dBuy!=null) dNav.appendChild(bBY); 
			}

		    for(n=0;n<dlItemsId.length;n++) FetchDownloadInfo(dlItemsId[n]);
		    for(n=0;n<byItemsId.length;n++) FetchPurchaseInfo(byItemsId[n]);

		}
	}
}	
	
	function FetchDownloadInfo(itemId)
	{
	    //alert("1:"+itemId);
	    Comm.sendCommand("<CMD name='GetProductInfoDL'><PARAM name='ITEMID'>"+itemId+"</PARAM></CMD>", cb_FetchDownloadInfo, "Getting download information for product "+itemId+"...");
	}
	
	function FetchPurchaseInfo(itemId)
	{
	    //alert("2:"+itemId);
	    Comm.sendCommand("<CMD name='GetProductInfoDL'><PARAM name='ITEMID'>"+itemId+"</PARAM></CMD>", cb_FetchPurchaseInfo, "Getting purchase information for product "+itemId+"...");
	}
	
	function cb_FetchDownloadInfo(args)
	{
	    var i, j, coll, contentx;
	    var subarg = args.selectSingleNode("RET");
	    var isForItem, tmp;
	    var hr;
	    
	    var ItemId = subarg.selectSingleNode("ITEMID").text;
	    var FName = subarg.selectSingleNode("FILENAME").text;
	    var FDescr = subarg.selectSingleNode("DESCR").text;
	    var FSize = subarg.selectSingleNode("SIZE").text;
	    var FVer = subarg.selectSingleNode("VER").text;
	    var FDate = subarg.selectSingleNode("DATE").text;
	    var Price = subarg.selectSingleNode("PRICE").text;
	    
	    contentx = document.createElement("TABLE");
	    contentx.style.width="100%";
	    var tr = contentx.insertRow(0);
	    var td = tr.insertCell(0);
	    td.style.verticalAlign="top";
	    td.style.padding="5px";
	    td.style.width="130px";
	    var bt = document.createElement("BUTTON");
	    bt.className = "btDownload";
	    bt.productID = ItemId;
	    bt.onclick = function() { DownloadProduct(this.productID); }
	    td.appendChild(bt);
	    var ch = document.createElement("DIV");
	    ch.style.textAlign="center";
	    ch.innerHTML="Click here to download";
	    td.appendChild(ch);
	    td = tr.insertCell(1);
	    td.style.padding="5px";
	    td.style.borderLeft="1px solid gray";
	    td.innerHTML  = "<b><font color='red' size=2>"+FDescr+"</font></b><br>";
	    td.innerHTML += "Filename: "+FName+"<br>";
	    td.innerHTML += "Version: "+FVer+"<br>";
	    td.innerHTML += "Date: "+FDate+"<br>";
	    td.innerHTML += "Price: <font color='blue'><b>"+Price+"</b></font>";
	    
	    coll = document.getElementsByTagName("DIV");
	    
	    for(i=0;i<coll.length;i++) {
	        isForItem = false;
	        if(coll[i].attributes["DownloadItems"]!=null) {
	            tmp = String(coll[i].attributes["DownloadItems"].value).split(";");
	            for(j=0;j<tmp.length;j++) {
	                if(ItemId==tmp[j]) { isForItem=true; break; }
	            }
	        }
	        if(coll[i].id=="productInfo" && isForItem) {
                for(j=0;j<coll[i].childNodes.length;j++) {
                    if(coll[i].childNodes[j].id=="p_download") {
                        if(coll[i].childNodes[j].innerHTML!="") {
                            hr = document.createElement("HR");
                            hr.size=1;
                            hr.nodhade=true;
                            hr.color="#cccccc";
                            coll[i].childNodes[j].appendChild(hr);
                        }
                        coll[i].childNodes[j].appendChild(contentx);
                    }
                }
	        }
	    }
	}
	
	function cb_FetchPurchaseInfo(args)
	{
	    var i, j, coll, contentx;
	    var subarg = args.selectSingleNode("RET");
	    var isForItem, tmp;
	    var hr;
	    
	    var ItemId = subarg.selectSingleNode("ITEMID").text;
	    var FName = subarg.selectSingleNode("FILENAME").text;
	    var FDescr = subarg.selectSingleNode("DESCR").text;
	    var FSize = subarg.selectSingleNode("SIZE").text;
	    var FVer = subarg.selectSingleNode("VER").text;
	    var FDate = subarg.selectSingleNode("DATE").text;
	    var Price = subarg.selectSingleNode("PRICE").text;
	    
	    contentx = document.createElement("TABLE");
	    contentx.style.width="100%";
	    var tr = contentx.insertRow(0);
	    var td = tr.insertCell(0);
	    td.style.verticalAlign="top";
	    td.style.padding="5px";
	    td.style.width="130px";
	    var bt = document.createElement("BUTTON");
	    bt.className = "btBuy";
	    bt.productID = ItemId;
	    bt.onclick = function() { BuyProduct(this.productID); }
	    td.appendChild(bt);
	    var ch = document.createElement("DIV");
	    ch.style.textAlign="center";
	    ch.innerHTML="Click here to purchase";
	    td.appendChild(ch);
	    td = tr.insertCell(1);
	    td.style.padding="5px";
	    td.style.borderLeft="1px solid gray";
	    td.innerHTML  = "<b><font color='red' size=2>"+FDescr+"</font></b><br>";
	    td.innerHTML += "Filename: "+FName+"<br>";
	    td.innerHTML += "Version: "+FVer+"<br>";
	    td.innerHTML += "Date: "+FDate+"<br>";
	    td.innerHTML += "Price: <font color='blue'><b>"+Price+"</b></font>";
	    
	    coll = document.getElementsByTagName("DIV");
	    
	    for(i=0;i<coll.length;i++) {
	        isForItem = false;
	        if(coll[i].attributes["BuyItems"]!=null) {
	            tmp = String(coll[i].attributes["BuyItems"].value).split(";");
	            for(j=0;j<tmp.length;j++) {
	                //alert(ItemId+"/"+tmp[j]);
	                if(ItemId==tmp[j]) { isForItem=true; break; }
	            }
	        }
	        if(coll[i].id=="productInfo" && isForItem) {
                for(j=0;j<coll[i].childNodes.length;j++) {
                    if(coll[i].childNodes[j].id=="p_buy") {
                        if(coll[i].childNodes[j].innerHTML!="") {
                            hr = document.createElement("HR");
                            hr.size=1;
                            hr.nodhade=true;
                            hr.color="#cccccc";
                            coll[i].childNodes[j].appendChild(hr);
                        }
                        coll[i].childNodes[j].appendChild(contentx);
                    }
                }
	        }
	    }
	}
	
	function DownloadProduct(id)
	{
	    // 1. check if logged in
	    Comm.sendCommand("<CMD name='Download'><PARAM name='PRODID'>"+id+"</PARAM><PARAM name='MODE'>FR</PARAM></CMD>", cb_Download, "Downloading product...");
	}
	
	function DownloadProductB(id)
	{
	    // 1. check if logged in
	    Comm.sendCommand("<CMD name='Download'><PARAM name='PRODID'>"+id+"</PARAM><PARAM name='MODE'>BY</PARAM></CMD>", cb_Download, "Downloading product...");
	}
	
	function cb_Download(args)
	{
	    var stat = args.selectSingleNode("RET/STAT").text;
	    
	    if(stat!="OK") {
            // 1.1 if not - advise to login or register
	        alert(stat); //"Please Login or Register first to proceed with download");
	    }
	    else {
	        // 2. download
	        var ifdld = document.getElementById("IFRX");
	        if(ifdld==null) {
	            //alert("isnull");
	            ifdld = document.createElement("IFRAME");
	            ifdld.id="IFRX";
	        }
	        
	        ifdld.style.display="none";
	        document.body.appendChild(ifdld);
	        ifdld.src = "code/serverside/download.aspx";
	        
	    }
	}
	
	function BuyProduct(id)
	{
	    // 1. check if logged in
	    //Comm.sendCommand("<CMD name='Purchase'><PARAM name='PRODID'>"+id+"</PARAM></CMD>", cb_Purchase, "Purchasing product...");
	    Comm.sendCommand("<CMD name='Purchase'><PARAM name='PRODID'>"+id+"</PARAM></CMD>", cb_Purchase, "Purchasing init...");   
	 }   
	 
	 function cb_Purchase(args)
	 {
	    var ret; 
	    var productid, productcode, phone, state, country, email, address, cardholder, city, zip;
    
        ret = args.selectSingleNode("RET");
        
        if(ret.selectSingleNode("STAT").text=="OK") {
        
            productid = ret.selectSingleNode("PRODID").text;
            productcode = ret.selectSingleNode("PRODCODE").text;
            phone = ret.selectSingleNode("PHONE").text;
            state = ret.selectSingleNode("STATE").text;
            country = ret.selectSingleNode("COUNTRY").text;
            email = ret.selectSingleNode("EMAIL").text;
            address = ret.selectSingleNode("ADDRESS").text;
            cardholder = ret.selectSingleNode("CARDHOLDER").text;
            city = ret.selectSingleNode("CITY").text;
            zip = ret.selectSingleNode("ZIP").text;
        
            alert("You will be redirected now to our partner 2CO (www.2Checkout.com) via secure link to complete your payment. There, you will be asked to provide payment and possibly other additional information, which SpindleScape will not keep track of. When you complete your purchase, you will be directed back to this site with options to download your purchased software.");
            
            document.location="https://www.2checkout.com/2co/buyer/purchase?sid=488543&quantity=1&product_id="+productcode+"&phone="+phone+"&state="+state+"&Country="+country+"&email="+email+"&street_address="+address+"&card_holder_name="+cardholder+"&city="+city+"&zip="+zip; //+"&demo=Y";
            //document.location = "http://localhost/spindlescape4/paysim.aspx";
        }            
            
        else {
            alert(ret.selectSingleNode("STAT").text);
        }
        
	 }
	 
