//*************************************************************************************

// Purpose	: To track the visitor movement in campaigns using WT.mc_id, product launch "victoria"

// Author	: Rattanender Kanwar

// Created on	: 12/23/2009

// Version	: 1.1

// Modified on	: 21/09/2010

// Modified by	: Rattanender Kanwar

//*************************************************************************************

function customUserActionTracking(carryOnString)

    {

    var baseURL=document.location.href;

	var qryString=baseURL.substring(baseURL.lastIndexOf("?"),baseURL.length)

	var qryElement=baseURL.substring(baseURL.lastIndexOf("?")+1,baseURL.length).split('&');

	var outerLoop,innerLoop;

	if (baseURL.lastIndexOf("?") >0)

		{

		    onPagehrefCollection=document.getElementsByTagName("a");

		    for(outerLoop=0;outerLoop<onPagehrefCollection.length;outerLoop++)

		    {

			for(innerLoop=0;innerLoop<qryElement.length;innerLoop++)

			{

			    if(qryElement[innerLoop].substring(0,qryElement[innerLoop].lastIndexOf("="))==carryOnString)

			    {

				var removeHash=qryElement[innerLoop];

				removeHash=removeHash.split("=");

				if (removeHash[1].indexOf('#')>0)

					{removeHash[1]=removeHash[1].substring(0, removeHash[1].indexOf('#'));

					qryElement[innerLoop]=removeHash[0] + "=" + removeHash[1];}

				var requestedURL=onPagehrefCollection[outerLoop].href;

				var requestedinnerHTML=onPagehrefCollection[outerLoop].innerHTML;

				//alert(requestedinnerHTML.substring(0,3));

				if ((requestedinnerHTML.substring(0,3)!="www") && (requestedinnerHTML.substring(0,4)!="http"))

				{

					if (requestedURL.lastIndexOf("?")>0)

					{

						requestedURL = requestedURL.substring(0,requestedURL.lastIndexOf("?"));

					}

					var userCriteriaA=requestedURL.substring(requestedURL.length-1, requestedURL.length);

					if (userCriteriaA!='/')

						var userCriteriaB=requestedURL.substring(requestedURL.lastIndexOf('.')+1,requestedURL.length);

					else

						userCriteriaB=""

					if (userCriteriaA=='/' || userCriteriaB=='html' || userCriteriaB=='htm')

					{

						if (onPagehrefCollection[outerLoop].href.indexOf('#')<=0) 

						{

							if(onPagehrefCollection[outerLoop].href.indexOf('?')>0)

							{

							    onPagehrefCollection[outerLoop].href=onPagehrefCollection[outerLoop].href+"&"+ qryElement[innerLoop];

							}

							else

							{

							    onPagehrefCollection[outerLoop].href=onPagehrefCollection[outerLoop].href+"?"+ qryElement[innerLoop];

							}

						}

					}

				}

				break;

			    }

			}

		    }

		}



    }





function customUserActionTrackingSubmit(carryOnString)

    {

      var baseURL=document.location.href;

	var qryString=baseURL.substring(baseURL.lastIndexOf("?"),baseURL.length)

	//alert(qryString);

	var qryElement=baseURL.substring(baseURL.lastIndexOf("?")+1,baseURL.length).split('&');

	var outerLoop,innerLoop,innerLoop2;

	if (baseURL.lastIndexOf("?") >0)

		{

		    onPagehrefCollection=document.getElementsByTagName("form");

outer:

		for(outerLoop=0;outerLoop<onPagehrefCollection.length;outerLoop++)

		    {

			var mcId=0;

			for(innerLoop=0;innerLoop<qryElement.length;innerLoop++)

				{

					if(qryElement[innerLoop].substring(0,qryElement[innerLoop].lastIndexOf("="))==carryOnString)

					{

						var removeHash=qryElement[innerLoop];

						removeHash=removeHash.split("=");

						if (removeHash[1].indexOf('#')>0)

							{removeHash[1]=removeHash[1].substring(0, removeHash[1].indexOf('#'));

							qryElement[innerLoop]=removeHash[0] + "=" + removeHash[1];}

						var requestedURL=onPagehrefCollection[outerLoop].action;

						var qryStringInner=requestedURL.substring(requestedURL.lastIndexOf("?"),requestedURL.length)

						if (qryStringInner.length >0 && qryStringInner!=requestedURL)

							{

								var qryElementInner=requestedURL.substring(requestedURL.lastIndexOf("?")+1,requestedURL.length).split('&');

								//alert(qryElementInner.length);

								for(innerLoop2=0;innerLoop2<qryElementInner.length;innerLoop2++)

									{

										if(qryElementInner[innerLoop2].substring(0,qryElementInner[innerLoop2].lastIndexOf("="))==carryOnString)

											{

												mcId=1;

												break outer;

											}

									}

								if (mcId==0)

									{

										onPagehrefCollection[outerLoop].action=onPagehrefCollection[outerLoop].action+"&"+ qryElement[innerLoop];

									}

							}

						else

							{onPagehrefCollection[outerLoop].action=onPagehrefCollection[outerLoop].action+"?"+ qryElement[innerLoop];}

					}

				}

			}

	 	}

    }

// This code is to add LP parameter to all pages dynamically and added on 01/05/2010	

function setLP()

	{

		var qry=window.location.search.toLowerCase();

		if (qry!="")

		{

			var myLen=getQueryVariable("WT.mc_id");

			if (myLen.length>0)

			{

				if ((document.referrer.indexOf("http://www.hds.com")==-1)&&(qry.indexOf("wt.mc_id")!=-1))

				{

					if (document.createElement) 

						{

							meta = document.createElement('meta');

							meta.name = "DCSext.lp";

							meta.content = window.location.hostname + window.location.pathname;

							document.getElementsByTagName('head').item(0).appendChild(meta);

						}

				}

				

			}

			

		}

	}

function getQueryVariable(variable) 

	{ 

		var query = window.location.search.substring(1); 

		var vars = query.split("&"); 

		var k=0;

		for (var i=0;i<vars.length;i++) 

		{ 

			var pair = vars[i].split("="); 

			if (pair[0] == variable) 

				{ 

					k=1;

					break;

				} 

		} 

		if (k==1)

			if (pair[1])

				return pair[1]; 

			else

				return "rattan"

		else

			return "rattan"; 

	}

//***************************************************Start:Victoria Product Launch************************************************************

function getMetaContents(mn){
  var m = document.getElementsByTagName('meta');
  var result=false;
for(var i=0;i<m.length;i++){
   if(m[i].name == mn){ ;
     result=true;
   }
  }
	 return result;
}

function productLaunchTracking(carryOnString)

    {

    var productvalue=getMetaContents(carryOnString);
	var outerLoop,innerLoop;

	if (productvalue==true)

		{

		    onPagehrefCollection=document.getElementsByTagName("a");

		    for(outerLoop=0;outerLoop<onPagehrefCollection.length;outerLoop++)

		    {

				var requestedURL=onPagehrefCollection[outerLoop].href;

				var requestedinnerHTML=onPagehrefCollection[outerLoop].innerHTML;

				//if ((requestedinnerHTML.substring(0,3)!="www") && (requestedinnerHTML.substring(0,4)!="http"))

				//{

					if (requestedURL.lastIndexOf("?")>0)

					{

						requestedURL = requestedURL.substring(0,requestedURL.lastIndexOf("?"));

					}

					var userCriteriaA=requestedURL.substring(requestedURL.length-1, requestedURL.length);

					if (userCriteriaA!='/')

						var userCriteriaB=requestedURL.substring(requestedURL.lastIndexOf('.')+1,requestedURL.length);

					else

						userCriteriaB=""

					if (userCriteriaA=='/' || userCriteriaB=='html' || userCriteriaB=='htm')

					{

						if (onPagehrefCollection[outerLoop].href.indexOf('#')<=0) 

						{

							if(onPagehrefCollection[outerLoop].href.indexOf('?')>0)

							{

							    onPagehrefCollection[outerLoop].href=onPagehrefCollection[outerLoop].href+"&_p=v";

							}

							else

							{

							    onPagehrefCollection[outerLoop].href=onPagehrefCollection[outerLoop].href+"?_p=v";

							}

						}

					}//usercriteria

				//}//text not www or http


			    }//onPagehrefCollection

			}//productvalue
}

function productLaunchTrackingSubmit(carryOnString)

    {

    var productvalue=getMetaContents(carryOnString);
	var outerLoop,innerLoop,innerLoop2;

	if (productvalue!="")
		{

		    onPagehrefCollection=document.getElementsByTagName("form");

outer:

		for(outerLoop=0;outerLoop<onPagehrefCollection.length;outerLoop++)

		    {

			var mcId=0;

						var requestedURL=onPagehrefCollection[outerLoop].action;

						var qryStringInner=requestedURL.substring(requestedURL.lastIndexOf("?"),requestedURL.length)

						if (qryStringInner.length >0 && qryStringInner!=requestedURL)

							{

								var qryElementInner=requestedURL.substring(requestedURL.lastIndexOf("?")+1,requestedURL.length).split('&');

								//alert(qryElementInner.length);

								for(innerLoop2=0;innerLoop2<qryElementInner.length;innerLoop2++)

									{

										if(qryElementInner[innerLoop2].substring(0,qryElementInner[innerLoop2].lastIndexOf("="))==carryOnString)

											{

												mcId=1;

												break outer;

											}

									}
								if (mcId==0)

									{
											
										onPagehrefCollection[outerLoop].action=onPagehrefCollection[outerLoop].action+"&_p=v";

									}

							}

						else

							{onPagehrefCollection[outerLoop].action=onPagehrefCollection[outerLoop].action+"?_p=v";}//qryStringInner

					}//onPagehrefCollection

				}//productvalue

			}

function writeMeta(mn,value)

	{

		var qry=window.location.search.toLowerCase();

		if (qry!="")

		{

			var myLen=getQueryVariable(mn);

			if (myLen.length>0)

			{

				if ((qry.indexOf(mn)!=-1))

				{
				
					if (document.createElement) 

						{

							meta = document.createElement('meta');

							meta.name = "DCSext.product-flow";

							meta.content = getGeoName(value);

							document.getElementsByTagName('head').item(0).appendChild(meta);
							
						}

				}

				

			}

			

		}

	}

function getGeoName(value)
	{
		var geonames= new Array('/br/','/uk/','/es/','/cn/','/fr/','/de/','/it/','/ru/','/se/','/ch/','/pl/','/fi/','/dk/','/at/','/nl/','/no/','/ar/','/cl/','/ve/','/co/','/mx/','/kr/','/tw/','/hk/','/in/','/ase','/anz/');
		var currURL=document.location.href;
		var currGeo=currURL.substr(document.location.hostname.length+7,4);
		var found=false;
		for(count=0;count<geonames.length;count++)
			{
				if (currGeo==geonames[count]){found=true;break;}
			}
		
		if (found)
		{
			value=currGeo.substr(1,2)+"-" + value ;
		}
		return value;
	}
	
function formReferrerpage()

    {

	var outerLoop,innerLoop,innerLoop2;

    onPagehrefCollection=document.getElementsByTagName("form");

	for(outerLoop=0;outerLoop<onPagehrefCollection.length;outerLoop++)

		    {

						var mcId=0;

						var requestedURL=onPagehrefCollection[outerLoop].action;

						var qryStringInner=requestedURL.lastIndexOf("WT.pfr");

						if (qryStringInner >0)

							{
										onPagehrefCollection[outerLoop].action=onPagehrefCollection[outerLoop].action+"&WT.rff="+window.document.referrer;
										mcId=1;

							}
			}
		if (mcId==0)
			{
	
				onPagehrefCollection=document.getElementsByTagName("input");

				for(outerLoop=0;outerLoop<onPagehrefCollection.length;outerLoop++)

		    		{

						var mcId=0;

						var requestedURL=onPagehrefCollection[outerLoop].value;

						var qryStringInner=requestedURL.lastIndexOf("WT.pfr");

						if (qryStringInner >0)

							{
										onPagehrefCollection[outerLoop].value=onPagehrefCollection[outerLoop].value+"&WT.rff="+window.document.referrer;
										mcId=1;

							}				
					}
	}
	}

writeMeta('_p','victoria-flow');
productLaunchTracking('DCSext.product-flow');
productLaunchTrackingSubmit('DCSext.product-flow');
//***************************************************End:Victoria Product Launch************************************************************

customUserActionTracking('WT.mc_id');
customUserActionTrackingSubmit('WT.mc_id'); 
setLP();
formReferrerpage();


function detects(){

		var mainDiv = document.getElementById('Contents');
		if (mainDiv!=null){
		var llinks;

		llinks = mainDiv.getElementsByTagName('a')

		//alert(llinks.length)

		for(var mo=0;mo<llinks.length;mo++){

			var anchorss = llinks[mo];

			if (anchorss.className == 'zipDownload'){

				anchorss.onclick = doit;

			}

		}
		}
}

detects();

function doit(){

		setTimeout ("window.location = '"+this.href+"'", 1500);

		return false;

}

