var xmlHttpCompare
			
function showDealToCompare(str)
{
xmlHttpCompare=GetXmlHttpObject2();
if (xmlHttpCompare==null)
  {
  alert ("Your browser does not support AJAX!");
  return;
  }
document.getElementById('ajax-loader-compare').style.display="block";
var url="/white_labels/mobiles/compare-deals.asp";
url=url+"?sort="+str;
url=url+"&sid="+Math.random();
xmlHttpCompare.onreadystatechange=stateChanged2;
xmlHttpCompare.open("GET",url,true);
xmlHttpCompare.send(null);
}

function stateChanged2() 
{ 
if (xmlHttpCompare.readyState==4)
{ 
document.getElementById('ajax-loader-compare').style.display="none";
document.getElementById("txtHintCompare").innerHTML=xmlHttpCompare.responseText;
}
}

function GetXmlHttpObject2()
{

var xmlHttpCompare=null;

try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttpCompare=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttpCompare=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttpCompare=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttpCompare;
}
function clearDID()
{
	Cookie.remove('did');//we remove the DID cookie completly
	dealAmount('blinkno');//here we call deal amount so that the deal number in the top right corner is updated in realtime
	var turnDropinOn = document.getElementById('compareDealsDIV');
		turnDropinOn.style.display = 'none';
	//alert("Deals removed");
	blink3();
}

function didSet(a,b,c,blinky)//Here we have the values of the deal ID
{
	var update = 0
	var dealAddedAlready = 'no';
	var currentDealCompareId = Cookie.get('did','');//We get the current deal - if any!
	var newDid = a+'-'+b+'-'+c;//We now put all three elements of the DID together
	if (currentDealCompareId == '')//Here we check to see if there is already a DID set
	{
		//alert(newDid);
		Cookie.set('did',newDid);//As there is no DID set, we set a single DID as a cookie called DID!
	}
	else//If there is already a deal set, we then use this eles to store the DID as an array
	{
		var update = 1
		var checkDealCompareIdArray=currentDealCompareId.split("+");//We split the DID cookie into Arrays
		for (var d = 0; d < checkDealCompareIdArray.length; d++)//we now loop for the amount of Arrays that are in DID
			{
				//alert(checkDealCompareIdArray[d]);
				//alert(newDid);
				if(newDid == checkDealCompareIdArray[d])//we now check to see if the deal has already been selected
				{
					dealAddedAlready = 'yes'
					//alert("Deal already selected!");
					blink2();
					var update = 0;//we make the update variable 0 so that the cookie does not get updated
				}
			}
	}
	if (update == 1)//we check to make sure it is ok to update the cookie
	{
		var newDidMulti = currentDealCompareId+'+'+newDid;//we add on the new new to the exsiting cookie
		Cookie.set('did',newDidMulti);//then update the cookie
	}
	//window.scrollTo(0,0);
	if(blinky == 'blinkyes')
	{
		if(dealAddedAlready == 'no')
		{
			dealAmount('blinkyes');//here we call deal amount so that the deal number in the top right corner is updated in realtime
		}
		else
		{
			dealAmount('blinkno');//here we call deal amount so that the deal number in the top right corner is updated in realtime
		}	
	
	}
	else
	{
		dealAmount('blinkno');//here we call deal amount so that the deal number in the top right corner is updated in realtime
	}
}

function dealAmount(blinky)//Function to update the deal number on the top right of the page
{
	//set some variables
	var amountUpdate = 0;
	var amountUpdateText = '';
	var aspArgument = '';
	var currentDealCompareId = Cookie.get('did','');//we get the full deal cookie
	if (currentDealCompareId == '')//this is executed if the cookie is empty
	{
			var compareDealsDIV = document.getElementById('compareDealsDIV');//we find the HTML that has the deal text data
			//compareDealsDIV.innerHTML = '';//the HTML container that stores the deal names and remove buttons is made empty
	}
	else//if there is a cookie set for DID, then we do the following
	{
		var browser = navigator.appName;
		if (browser == 'Microsoft Internet Explorer')
		{
			var turnDropinOn = document.getElementById('compareDealsDIV');
			turnDropinOn.style.display = 'block';
		}
		else
		{
			$("#compareDealsDIV").fadeIn("slow");
			
		}
		var checkDealCompareIdArray=currentDealCompareId.split("+");//We split the DID cookie into Arrays
		for (var d = 0; d < checkDealCompareIdArray.length; d++)//we now loop for the amount of Arrays that are in DID
		{
			var did = 'did';
			amountUpdate = amountUpdate + 1;//here we are counting how many deals are in DID (to display and the 'Deal Number')
			var amountUpdateTextSplit = checkDealCompareIdArray[d].replace(/-/g,',');//This is TEST text to display the DID cookie in the HTML deal number data box
			//amountUpdateText = amountUpdateText + checkDealCompareIdArray[d] + ' <a href="javascript:;" onclick="javascript:didRemove('+amountUpdateTextSplit+');">Remove</a><br />';//we add in a remove button
			aspArgument = aspArgument + 'did=' + checkDealCompareIdArray[d] + '&';//Here we build the link for the ASP page
		}
			var dealAmount = document.getElementById('dealAmount');//we now look for the HTML that displays the deal number on the top right of the page
			var compareDealsDIV = document.getElementById('compareDealsDIV');//we find the HTML that has the deal text data
			var dealAmount = '<table id="compare-top-left-table" width="137"><tr><td><span>Deals Added('+amountUpdate+')</span></td><td><a href="javascript:clearDID();"><img src="/white_labels/mobiles/images/deal-compare-cancel.png" /></a></td></tr></table>';//We chaneg the deal number to the new number from the loop created above
			var aspArgumentClean = aspArgument.substring(0, aspArgument.length-1);//we strip the trailing '&'
			document.getElementById('compareDealsDIVFirst').innerHTML = dealAmount;//We now apend the 'View Offers' link to the HTMl page
			fbox_engine();
			
	}
	
	if(blinky == 'blinkyes')
	{
		blink();
	}
}

function didRemove(a,b,c)//function to remove DIDs from the DID cookie
{
	var count = 0;//We use this so that if at the end of this function, count is still 0, then the DID cookie is cleared completely
	
	//Below we have to use 2 remove sytx
	var dealToRemove = ('+'+a+'-'+b+'-'+c);//the first one looks for DID numbers with a '+' at the begining
	var dealToRemove2 = (a+'-'+b+'-'+c);// the second one looks for DID numbers that have no '+' (So if it is at the begining of the Array)
	
	var currentDealCompareId = Cookie.get('did','');//We get the current DID cookie
	var checkDealCompareIdArray=currentDealCompareId.split("+");//We split the DID cookie into Arrays
		for (var d = 0; d < checkDealCompareIdArray.length; d++)//we now loop for the amount of Arrays that are in DID
		{
			count = count + 1;//Here we set the count - count will count how many current deals have been logged in the cookie
		}
	var newDealCompareId = currentDealCompareId.replace(dealToRemove,'');//we now remove any string in the DID cookie that matches dealToRemove, which are DID numbers with a '+' at the begining
	var newDealCompareId = currentDealCompareId.replace(dealToRemove2,'');//we now do a 2nd remove, which removes DID numbers that don't have a '+' at the begining
	
	var newDealCompareIdCleanFirst = newDealCompareId.charAt(0);//here we store the first char of the new cookie
	var newDealCompareIdCleanLast = newDealCompareId.charAt(newDealCompareId.length-1);//here we store the last char of the new cookie
	//******************* We are looking for any '+' that have been left either at the begining of the cookie, or at the end ********************
	
	if(newDealCompareIdCleanFirst == '+')//so we check the first char
	{
		var str = newDealCompareId.substring(1, newDealCompareId.length);// if the first char matches '+', then we remove it
		Cookie.set('did',str);//we now set the new cookie
	}
	else
	{
		if(newDealCompareIdCleanLast == '+')//We now check the last char
		{
			var str = newDealCompareId.substring(0, newDealCompareId.length-1);// if the last char matches '+', then we remove it
			Cookie.set('did',str);//we now set the new cookie
		}
		else
		{
			//if none of the first or the last Chars have the '+', then it must be in the middle of the new cookie array, so any double '++' need removing
			var newDealCompareId = newDealCompareId.replace('++','+');//we replace any '++' with '+'
			Cookie.set('did',newDealCompareId);//we now set the new cookie
		}
	}
	if(count == 1)//if the count value is still set to 0, then there are no deals saved
	{
		Cookie.remove('did');//we remove the DID cookie completly
		clearDID();
	}
	dealAmount('blinkno');//here we call deal amount so that the deal number in the top right corner is updated in realtime
	showDealToCompare('refresh');
}

function blink()
{
		var browser = navigator.appName;
		if (browser == 'Microsoft Internet Explorer')
		{
			document.getElementById("compareDealsDIV2").style.display = "block";
		}
		else
		{
		 	$(document).ready(function(){
				$("#compareDealsDIV2").show("slow");
 		 	});
		}
	setTimeout('blinkStop()', 2000);
}

function blinkStop()
{
		var browser = navigator.appName;
		if (browser == 'Microsoft Internet Explorer')
		{
			document.getElementById("compareDealsDIV2").style.display = "none";
		}
		else
		{
			$(document).ready(function(){
				$("#compareDealsDIV2").hide("slow");
 		 	});
		}
}

function blink2()
{
		var browser = navigator.appName;
		if (browser == 'Microsoft Internet Explorer')
		{
			document.getElementById("compareDealsDIV22").style.display = "block";
		}
		else
		{
		 	$(document).ready(function(){
				$("#compareDealsDIV22").show("slow");
 		 	});
		}
	setTimeout('blinkStop2()', 2000);
}

function blinkStop2()
{
		var browser = navigator.appName;
		if (browser == 'Microsoft Internet Explorer')
		{
			document.getElementById("compareDealsDIV22").style.display = "none";
		}
		else
		{
			$(document).ready(function(){
				$("#compareDealsDIV22").hide("slow");
 		 	});
		}
}

function blink3()
{
		var browser = navigator.appName;
		if (browser == 'Microsoft Internet Explorer')
		{
			document.getElementById("compareDealsDIV222").style.display = "block";
		}
		else
		{
		 	$(document).ready(function(){
				$("#compareDealsDIV222").show("slow");
 		 	});
		}
	setTimeout('blinkStop3()', 2000);
}

function blinkStop3()
{
		var browser = navigator.appName;
		if (browser == 'Microsoft Internet Explorer')
		{
			document.getElementById("compareDealsDIV222").style.display = "none";
		}
		else
		{
			$(document).ready(function(){
				$("#compareDealsDIV222").hide("slow");
 		 	});
		}
}

		var browser = navigator.appName;
		if (browser != 'Microsoft Internet Explorer')
		 {
		 $(document).ready(function(){
    		$(".compare-text").click(function(){
			$("#compareDealsDIV").fadeIn("slow");
			});
 		 });
		 }
		 
function fireEvent(obj,evt){
	var fireOnThis = obj;
	if( document.createEvent ) {
	  var evObj = document.createEvent('MouseEvents');
	  evObj.initEvent( evt, true, false );
	  fireOnThis.dispatchEvent(evObj);
	} else if( document.createEventObject ) {
	  		fireOnThis.fireEvent('on'+evt);
	}
}

function CloseFbox()
{
        fireEvent(document.getElementById("fbox_close_a"),'click');
}

