// Find out what cookies are supported. Returns: 
// null - no cookies 
// false - only session cookies are allowed 
// true - session cookies and persistent cookies are allowed 
// (though the persistent cookies might not actually be persistent, if the user has set 
// them to expire on browser exit) 
// 
function getCookieSupport() { 
    var persist= true; 
    do { 
        var c= 'gCStest='+Math.floor(Math.random()*100000000); 
        document.cookie= persist? c+';expires=Tue, 01-Jan-2030 00:00:00 GMT' : c; 
        if (document.cookie.indexOf(c)!==-1) { 
            document.cookie= c+';expires=Sat, 01-Jan-2000 00:00:00 GMT'; 
            return persist; 
        } 
    } while (!(persist= !persist)); 
    return null; 
} 


function bookmarksite(title,url){
    if (window.sidebar) // firefox
	   window.sidebar.addPanel(title, url, "");
    else if(window.opera && window.print){ // opera
	   var elem = document.createElement('a');
       elem.setAttribute('href',url);
	   elem.setAttribute('title',title);
	   elem.setAttribute('rel','sidebar');
	   elem.click();
    }
    else if(document.all)// ie
	   window.external.AddFavorite(url, title);
}

function popupFullImage(filename)
{
    var width  = 660;
    var height = 530;
    var left   = (screen.width  - width)/2;
    var top    = (screen.height - height)/2;
    var params = 'width='+width+', height='+height;
    params += ', top='+top+', left='+left;
    params += ', directories=no';
    params += ', location=no';
    params += ', menubar=no';
    params += ', resizable=no';
    params += ', scrollbars=no';
    params += ', status=no';
    params += ', toolbar=no';
    newwin=window.open('http://www.carsupermarketsw.co.uk/carimg/' + filename,'imagepopup', params);
    if (window.focus) {newwin.focus()}
    return false;
}

document.getElementsByClassName = function(clsName)
{
    var retVal = new Array();
    var elements = document.getElementsByTagName("*");
    for(var i = 0;i < elements.length;i++)
    {
        if(elements[i].className.indexOf(" ") >= 0)
        {
            var classes = elements[i].className.split(" ");
            for(var j = 0;j < classes.length;j++)
            {
                if(classes[j] == clsName) retVal.push(elements[i]);
            }
        }
        else if(elements[i].className == clsName) retVal.push(elements[i]);
    }
    return retVal;
}

function toggleAdvanced()
{
    var display = document.getElementById('advancedsearch').style.display;    
    document.getElementById('advancedsearch').style.display = (display=='none' || display=='') ? 'block' : 'none';
}

function filterRows(sObjId, filterText)
{
    var oObj = document.getElementById(sObjId);    
    if(oObj)
    {
        var oRows = oObj.rows;
        var display='table-row';
        var start = 1;
    }
    else
    {
        oRows = document.getElementsByClassName('vehlistbox');
        var display='block';
        var start = 0;
    }
    
    for(i=start; i<oRows.length; i++)
    {
        if(display=='table-row')
        {
            var oCells = oRows[i].cells;
        }
        else
        {
            var oCells = oRows[i].childNodes;
        }
        var sValue = "";
        
        for(j=0; j<oCells.length; j++)
        {
            if(oCells[j].className.indexOf('toolbox')<1) sValue += oCells[j].innerHTML;
            //alert(oCells[j].className);
        }
        
        filterText = filterText.toLowerCase();
        sValue = sValue.toLowerCase();
        
        if(sValue.indexOf(filterText)>0 || filterText=='')
        {
            oRows[i].style.display=display;
        }
        else
        {
            oRows[i].style.display='none';
        }
    }
}

function findPos(oObj) // Find absolute position of any DOM object.
{
    var curleft = curtop = 0;
    if (oObj.offsetParent) 
    {
        do 
        {
            curleft += oObj.offsetLeft;
            curtop += oObj.offsetTop;
        } while (oObj = oObj.offsetParent);
        return [curleft,curtop];
    }
    else return false;
}

var featured=0;
function slideShow(tout)
{    
    if(!tout) var tout=10000;
    var eMaxWidth = 480;
    
    resize('featureslide'+featured, eMaxWidth, 0, 750);
    featured++; if(featured>featuredcount) featured=0;
    resize('featureslide'+featured, 0, eMaxWidth, 750);
    
    if(slideint) clearTimeout(slideint);
    slideint = setTimeout('slideShow()', tout);
}

function resize(id, wStart, wEnd, millisec) 
{
    //speed for each frame
    var speed = Math.round(millisec / 150);
    var timer = 0;

    //determine the direction for the blending, if start and end are the same nothing happens
    if(wStart > wEnd) 
    {
        for(i = wStart; i>= wEnd; i--) 
        {
            setTimeout("setWidth(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    } 
    else if(wStart < wEnd) 
    {
        for(i = wStart; i <= wEnd; i++)
        {
            setTimeout("setWidth(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    }
}

function setWidth(w, objId)
{
    document.getElementById(objId).style.width = w+"px";
}

if(typeof addLoadEvent !== 'function')
{
	function addLoadEvent(func) 
	{
		var oldonload = window.onload;
		if (typeof window.onload != 'function') 
		{
			window.onload = func;
		} 
		else 
		{
			window.onload = function() 
			{
				oldonload();
				func();
			}
		}
	}
}

function newCustHTML(return_url, url)
{
	return '<div id="custdiv" style="position:fixed; left:50%; top:50%; margin-left:-200px; margin-top:-210px; width:400px;" class="light_box round_corners large_drop_shadow">'+
'<div class="embossed b padding large_text relative_margin_bottom dark_box small_round_corners l"><div class="fr b pointer" title="Close" id="custclose">X</div>Need Help</div>'+
'<div class="l medium_text relative_margin_bottom">Please describe the issue you have encountered in the box below, then click send.</div>'
+'<form action="contact_do.php" method="post" id="custform" class="contactform validate"><input type="hidden" value="custserv" name="type" /><input type="hidden" value="'+return_url+'" name="return" /><input type="hidden" value="'+url+'" name="url" />'+
'<div class="msg_custctact"></div>'+
'<textarea id="custcomment" class="relative_margin_top inputta reqnotempty" name="comment"></textarea>'+
'<div class="msg_custcomment"></div>'+
'<div class="r relative_margin_top"><input id="custsubmit" class="cssbutton button1" type="submit" value="Send" /></div>'+
'</form></div>';
}

function newCtactHTML(return_url)
{
	return '<div id="ctactdiv" style="position:fixed; left:50%; top:50%; margin-left:-200px; margin-top:-210px; width:400px;" class="light_box round_corners large_drop_shadow">'+
'<div class="embossed b padding large_text relative_margin_bottom dark_box small_round_corners"><div class="fr b pointer" title="Close" id="ctactclose">X</div>Contact Us</div>'+
'<div class="medium_text relative_margin_bottom">Please enter your name, &#101;&#109;&#97;&#105;&#108; address and comment or question, then click send.</div>'
+'<form action="contact_do.php" method="post" id="contactform" class="contactform validate"><input type="hidden" value="contact" name="type" /><input type="hidden" value="'+return_url+'" name="return" />'+
'<div class="medium_text relative_margin_top">Name:</div><input id="ctactname" class="inputta reqalpha reqnotempty" type="text" name="name" />'+
'<div class="msg_ctactname"></div>'+
'<div class="medium_text relative_margin_top">&#69;&#109;&#97;&#105;&#108;:</div><input id="ctactctact" class="inputta reqctact reqnotempty" type="text" name="ctact" />'+
'<div class="msg_ctactctact"></div>'+
'<div class="medium_text relative_margin_top">Question or Comment:</div><textarea id="ctactcomment" class="inputta reqnotempty" name="comment"></textarea>'+
'<div class="msg_ctactcomment"></div>'+
'<div class="r relative_margin_top"><input id="ctactsubmit" class="cssbutton button1" type="submit" value="Send" /></div>'+
'</form></div>';
}

function marketingHTML(return_url)
{
	return '<div id="mrktingdiv" style="position:fixed; left:50%; top:50%; margin-left:-200px; margin-top:-210px; width:400px;" class="light_box round_corners large_drop_shadow">'+
'<div class="embossed b padding large_text relative_margin_bottom dark_box small_round_corners"><div class="fr b pointer" title="Close" id="mrktingclose">X</div>Latest Offers &amp; Discounts</div>'+
'<div class="medium_text relative_margin_bottom">To be kept up to date with all the latest offers and discounts from '+SITE_NAME+', please enter your name and &#101;&#109;&#97;&#105;&#108; address or mobile phone number, then click send.</div>'
+'<form action="contact_do.php" method="post" id="contactform" class="contactform validate"><input type="hidden" value="mrkting" name="type" /><input type="hidden" value="'+return_url+'" name="return" />'+
'<div class="medium_text relative_margin_top">Name:</div><input id="mrktingname" class="inputta reqalpha reqnotempty" type="text" name="name" />'+
'<div class="msg_mrktingname"></div>'+
'<div class="medium_text relative_margin_top">&#69;&#109;&#97;&#105;&#108;:</div><input id="mrktingctact" class="inputta reqctact reqnotempty" type="text" name="ctact" />'+
'<div class="msg_mrktingctact"></div>'+
'<div class="medium_text relative_margin_top">Mobile Phone No.:</div><input id="mrktingphone" class="inputta reqphone" type="text" name="phone" />'+
'<div class="msg_mrktingphone"></div>'+
'<div class="r relative_margin_top"><input id="mrktingsubmit" class="cssbutton button1" type="submit" value="Submit" /></div>'+
'</form></div>';
}
var cartTimer=false;
var cartTimer2=false;
function updateCart(id, qty)
{
	$.post('/update_cart.php', { id: id, qty: qty }, function(data){ 
	if(cartTimer!=false)
	{
		clearTimeout(cartTimer);
		cartTimer=false;
	}
	cartTimer=setTimeout("reloadCart()",500);
	});
}

function startUpdateCart(id, qty)
{
	if(cartTimer2!=false)
	{
		clearTimeout(cartTimer2);
		cartTimer2=false;
	}
	cartTimer2=setTimeout("updateCart("+id+","+qty+")",500);
}

function reloadCart()
{
	var start = new Date().getTime();
	$.post('/update_cart.php', { time: start }, function(data){ 
		var tmp=data.split('[##]');
		if(parseInt(tmp[0])>parseInt($('#updatetime').attr('rel'))) $('#cart_summary').html(tmp[1])});
}

$(document).ready(function() {
	$('.product_box .menuinput').live("propertychange keyup input paste", function() {
		startUpdateCart($(this).attr('rel'),$(this).val());
	});
	
	$('.product_box .plusminus').live('click', function(){
		switch($(this).val())
		{
			case '+':
				$('#prod_'+$(this).attr('rel')).val(function(index, value) {
  				if(parseInt(value)<99) return parseInt(value)+1;
  				else return value;
				});
				startUpdateCart($(this).attr('rel'),$('#prod_'+$(this).attr('rel')).val());			
			break;
			case '-':
				$('#prod_'+$(this).attr('rel')).val(function(index, value) {
  				if(parseInt(value)>0) return parseInt(value)-1;
  				else return value;
  			});	
				startUpdateCart($(this).attr('rel'),$('#prod_'+$(this).attr('rel')).val());		
			break;
		}
	});	
});
