/***********************************************************************************************************
										Software products processing
	. This JS file feeds on service hosted by Digital River for software products
	. The service is hosted at http://shop.hrblock.com/store/taxcut/DisplayDRProductInfoJSPage/
	. 5/27/2009 Earlier this was for all software pages, now this is only for Back edition pages
************************************************************************************************************/

var total_req_prods=0;
var request_success_flag=false;
var soft_productnames=new Array(
"Dlx_2010","Dlx_St_2010","P_2010","PB_2010","S_2010",
"Dlx_2009","Dlx_St_2009","P_2009","PB_2009","S_2009",
"B_2008","P_2008","PSE_2008","HB_2008","S_2008",
"P_2007","PS_2007","HB_2007","state_2007",
"P_2006","PS_2006","HB_2006","S_2006",
"DlxS_2005","PS_2005","BS_2005","Std_2005","S_2005",
"Dlx_2004","P_2004","HB_2004","PMac_2004","S_2004","BS_2004",
"Dlx_2003","P_2003","HB_2003","PMac_2003","Std_2003","S_2003","BS_2003",
"Plat_2002","Platmac_2002","Dlx_2002","S_2002",
"Fed_2001","S_2001",
"Fed_2000","Dlx_2000","S_2000",
"Fed_1999","S_1999",
"Fed_1998","S_1998",
"Fed_1997","S_1997",
"Fed_1996","S_1996",
"Fed_1995","S_1995",
"Fed_1994",
"Fed_1993",
"Fed_1992"
);




//stores the product ids 
var soft_productid = new Array(
200998200,191342400,220813300,200998500,200981900,  /*2010 */
156615000,156615900,134571300,156618300,164066100,	/* 2009 */					   
108091600,104758400,104758800,110515300,	/* 2008 */
83769200,83996600,84141600,86081300,	/* 2007 */
95311300,94116700,76793400,76797700,	/* 2006 */
76182900,76692300,76692600,77695500,	/* 2005 */
76664000,76664300,76664900,76664600,76874500,77718500,		/* 2004 */
78067200,77762400,77762600,77762500,76630200,		/* 2003 */
77706000,77707100,77704500,76598600,	/* 2002 */
76170000,76576600,	/* 2001 */
76560000,76559700,77752900,		/* 2000 */
76170300,77768400,	/* 1999 */
76535700,77764000,	/* 1998 */
76524400,77754800, 	/* 1997 */
76534400,77756700,	/* 1996 */
76170600,77759500,	/* 1995 */
76534800,	/* 1994 */
76535100,	/* 1993 */
76535400	/* 1992 */
);



// store the base price retrived from the response
var basePrice=new Array(
'$44.95','$54.95','$74.95','$89.95','$36.95',    /*2010 */
'$44.95','$54.95','$69.95','$89.95','$39.95',	/* 2009 */
'$59.95','$69.95','$89.95','$39.95',	/* 2008 */
'$59.95','$69.95','$89.95','$39.95',	/* 2007 */
'$59.95','$69.95','$89.95','$39.95', 	/* 2006 */
'$54.95','$69.95','$89.95','$39.95',	/* 2005 */
'$44.95','$59.95','$79.95','$49.95','$39.95','$39.95',	/* 2004 */
'$44.95','$59.95','$79.95','$59.95','$39.95',	/* 2003 */
'$59.95','$59.95','$44.95','$39.95',	/* 2002 */
'$39.95','$39.95',	/* 2001 */
'$39.95','$44.95','$39.95',	/* 2000 */
'$39.95','$39.95',	/* 1999 */
'$39.95','$39.95',	/* 1998 */
'$39.95','$39.95',	/* 1997 */
'$39.95','$39.95',	/* 1996 */
'$39.95','$39.95',	/* 1995 */
'$39.95',	/* 1994 */
'$39.95',	/* 1993 */
'$39.95'	/* 1992 */
);



// store the offered(may be discounted)price retrived from the response
var yourPrice=new Array(
'$44.95','$54.95','$74.95','$89.95','$36.95',    /*2010 */
'$44.95','$54.95','$69.95','$89.95','$39.95',	/* 2009 */
'$59.95','$69.95','$89.95','$39.95',	/* 2008 */
'$59.95','$69.95','$89.95','$39.95',	/* 2007 */
'$59.95','$69.95','$89.95','$39.95', 	/* 2006 */
'$54.95','$69.95','$89.95','$39.95',	/* 2005 */
'$44.95','$59.95','$79.95','$49.95','$39.95','$39.95',	/* 2004 */
'$44.95','$59.95','$79.95','$59.95','$39.95',	/* 2003 */
'$59.95','$59.95','$44.95','$39.95',	/* 2002 */
'$39.95','$39.95',	/* 2001 */
'$39.95','$44.95','$39.95',	/* 2000 */
'$39.95','$39.95',	/* 1999 */
'$39.95','$39.95',	/* 1998 */
'$39.95','$39.95',	/* 1997 */
'$39.95','$39.95',	/* 1996 */
'$39.95','$39.95',	/* 1995 */
'$39.95',	/* 1994 */
'$39.95',	/* 1993 */
'$39.95'  /* 1992 */
);





// store the offer id for the page
var offerid=null;

//store the pgm id for the page
var pgmid=null;


/*******************************************************************************************************
  ***************************************************************************************************/

function getSoftwareProductsInfo()
{
     var prodlist =searchPageForProducts();
	   if (prodlist!=null && prodlist.length>0){
        total_req_prods=prodlist.length;
        sendRequest('/json/JSONProxyServlet',constructReqURL(prodlist));
    }   


}


/*******************************************************************************************************
					Search for products (go through each span id ) 
*******************************************************************************************************/

function searchPageForProducts()
{
	var prodidarr =new Array();
	var spanlist = document.getElementsByTagName("span");
	for (var i=0;i<spanlist.length;i++) {

		var span_id=''+spanlist[i].id;
		if (span_id.indexOf("sp_") >=0 && span_id.indexOf(".price") >0){
			prodidarr[prodidarr.length]= span_id.substring(span_id.indexOf("sp_")+3,span_id.indexOf(".price"));
		}
	}
	g_log.debug("span tag found on page:::prodidarr"+prodidarr);
	return prodidarr;

}


/*******************************************************************************************************
					Construct the request URL based on the products 
*******************************************************************************************************/
function constructReqURL(prodarr) {

	var prod_params=""
	
	if (typeof(prodarr) !='undefined' && prodarr!=null && prodarr.length>0){
		for (var i=0;i< prodarr.length;i++ ){
			if (i<(prodarr.length-1)){
				prod_params+=prodarr[i]+":";
			}
			else{
				prod_params+=prodarr[i];
			}
		}

	}
	
	var parameters ="pid_list="+prod_params
	if (offerid!=null && offerid!='' ){
		parameters+="&off_id="+offerid;
	}
	if (pgmid!=null && pgmid!='' ){
		parameters+="&pgm_id="+pgmid;
	}
	else  // hardcoded for now, this should be removed
	{
		parameters+="&pgm_id="+14166300;
	}
	g_log.debug("soft_product_processing: parameters-"+parameters);
	return  parameters;
}


/*******************************************************************************************************
					Connect to the proxy and send the request parameters
*******************************************************************************************************/
function sendRequest (url, parameters) {
				g_log.debug("soft_product_processing: Inside method sendRequest");

		new ajaxreq(url,parameters,"backEditionCallSuccess","backEditionCallFailed","");

}


/*******************************************************************************************************
					Handle the JSON response
*******************************************************************************************************/

function backEditionCallFailed()
{
			g_log.debug("Inside method backEditionCallFailed");
			request_success_flag=false;
			updateSoftPricesOnPage();

}
function backEditionCallSuccess(responsetxt)
{
			g_log.debug("soft_product_processing: Inside method backEditionCallSuccess");
			if (responsetxt==''||responsetxt=='ERROR'){
				//JSON response is blank, show the default pricing for the products
				updateSoftPricesOnPage();
		
				return;
			}
			request_success_flag=true;
			var jsonstr="";
			//modified the parsing on 4/7/2009 since there were additional comments in the response
			if (total_req_prods==1){
				jsonstr=responsetxt.substring(responsetxt.indexOf("{"),responsetxt.indexOf("}}")+2);
			}
			else{
				jsonstr=responsetxt.substring(responsetxt.indexOf("["),responsetxt.indexOf("}]")+2);
			}
			var jsonSoftObj = eval("(" + jsonstr + ")");
			if (typeof jsonSoftObj =='undefined' || jsonSoftObj.length==0 ){
				g_log.error("soft_product_processing:JSON object is empty");
			}
			else{
				if (total_req_prods==1){

					var checkindex=-1;
					for (var j=0;j<soft_productid.length ;j++ ){

						if (soft_productid[j]==jsonSoftObj.productID){
							checkindex=j;
							break;
						}
					}
					
					if (checkindex!=-1)	{
						basePrice[checkindex]=jsonSoftObj.price.unitPrice;
						yourPrice[checkindex]=jsonSoftObj.price.unitPriceWithDiscount;
					}
				}				
				else{
					for (var i=0;i< jsonSoftObj.length;i++ ){
					   var checkindex=-1;
						for (var j=0;j<soft_productid.length ;j++ ){

							if (soft_productid[j]==jsonSoftObj[i].productID)	{
								checkindex=j;
								break;
							}
						}

						if (checkindex!=-1)
						{
							basePrice[checkindex]=jsonSoftObj[i].price.unitPrice;
							yourPrice[checkindex]=jsonSoftObj[i].price.unitPriceWithDiscount;
						}
					}
				}
			}
			jsonSoftObj=null;
	
		updateSoftPricesOnPage();
}

/*******************************************************************************************************
					get the software product base price
*******************************************************************************************************/
function getSoftProductBasePrice(pid)
{
	for (var i=0;i<soft_productid.length ;i++ ){
		if (soft_productid[i]==(''+pid)){
			return basePrice[i];
		}
	}
}

/*******************************************************************************************************
					get the software product Your(based on offer) price
*******************************************************************************************************/
function getSoftProductYourPrice(pid)
{
for (var i=0;i<soft_productid.length ;i++ ){
		if (soft_productid[i]==(''+pid)){
			return yourPrice[i];
		}
	}

}

/*******************************************************************************************************
					update the price on the html page, search span tag and update the price
*******************************************************************************************************/
function updateSoftPricesOnPage()
{
	var spanlist = document.getElementsByTagName("span");
	for (var i=0;i<spanlist.length;i++) {

		var span_id=''+spanlist[i].id;
		if (span_id.indexOf("sp_") >=0 && span_id.indexOf(".price") >0){
			var prodid= span_id.substring(span_id.indexOf("sp_")+3,span_id.indexOf(".price"))
			g_log.debug("span tag found on page:::prodid"+prodid)

			var bprice=getSoftProductBasePrice(prodid);
			bprice=bprice.replace(/^\s+|\s+$/g, '');
			bprice=bprice.substring(bprice.indexOf("$")+1);
			
			var yprice=getSoftProductYourPrice(prodid);
			yprice=yprice.replace(/^\s+|\s+$/g, '');
			yprice=yprice.substring(yprice.indexOf("$")+1);

			g_log.debug("bprice::"+bprice+"::yprice::"+yprice);

			if (parseInt(yprice) < parseInt(bprice)){
				document.getElementById(span_id).innerHTML="<strike style='font-size:12px;'>$"+bprice+"</strike><br><h4>$"+yprice+"</h4>";
			}
			else{
				if (request_success_flag==false){
				document.getElementById(span_id).innerHTML="<span style='color:#7D007D;'>$"+bprice+"</span>";
				}
				else{
				document.getElementById(span_id).innerHTML='<span>$'+bprice+'</span>';
				}
			}
		}//end for if
	}//End for for loop
}

function  callsoftProdStartNow(dr_pid,year)
{
	var tmpname=document.getElementById(dr_pid+'_variation');
	var variation="";
	if (typeof tmpname!= 'undefined' &&   tmpname!=null &&   tmpname!='' ){
		variation=tmpname.value;
	}
	else{
		variation=dr_pid;
	}
	var pageNameToUse   = ((typeof omni_pagename)!="undefined" && omni_pagename != "") ? omni_pagename : "";
    var newanchor = document.createElement("a");
    newanchor.setAttribute('href', document.location.href);
	try{
		pageNameToUse   = pageNameToUse.replace(/&/g, "^");
        var prop5_eVar10 = "NFD-198-BackEd-"+variation+"-"+year+pageNameToUse;
        webAnalytics(newanchor, "trackvars:prop5="+prop5_eVar10+"&eVar10="+prop5_eVar10+"&", "trackevents:event14" ,"linkname:"+pageNameToUse+"-"+requestedProductId+"-"+uniqueId, "type:o");
    }
    catch (e) {}
	window.top.location="http://shop.hrblock.com/store/taxcut/en_US/AddItemToRequisition/productID."+variation;
}
