var lastcategory = null;
var lastCountry = null;
var changeP = false;

function showEm(pid){
	window.open(lastCountry+'/notification/'+ pid+'.html','_blank','toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=no,resizable=yes,copyhistory=no,width=570,height=205,screenX=50,screenY=50,top=50,left=50'); 
}	  

function EcrireCookie(valeur,nom){
	var argv= EcrireCookie.arguments;
	var argc= EcrireCookie.arguments.length;
	var expires= (argc > 2) ? argv[2] : null;
	var path= (argc > 3) ? argv[3] : null;
	var domain= (argc > 4) ? argv[4] : null;
	var secure= (argc > 5) ? argv[5] : false;
	document.cookie=nom+"="+escape(valeur)+
	((expires==null) ? "" : ("; expires="+expires.toGMTString()))+
	((path==null) ? "" : ("; path="+path))+
	((domain==null) ? "" : ("; domain="+domain))+
	((secure==true) ? "; secure" : "");
}
function getCookieVal(offset){
	var endstr=document.cookie.indexOf (";", offset);
	if (endstr==-1) endstr=document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}

function doneButton(){
	GetContent(lastcategory,lastCountry);
}
function LireCookie(nom){
	var arg=nom+"=";
	var alen=arg.length;
	var clen=document.cookie.length;
	var i=0;
	while (i<clen){
		var j=i+alen;
		if (document.cookie.substring(i, j)==arg) return getCookieVal(j);
		i=document.cookie.indexOf(" ",i)+1;
		if (i==0) break;
	}
	return null;
}

function loaded(){
	document.getElementById('categories_select').value = category;
	document.getElementById('countryS').value = country;
	if(LireCookie('checkmax') != null){
		if(LireCookie('checkmax') == "on"){
			checkMax('on');
		}
		else{
			checkMax('off');
		}
	}
	else{
		checkMax('off');
	}
	document.getElementById('maxprice').value = Default_maxprice;
	
	lastcategory = category;
	lastCountry = country;
	changeC(lastCountry);
}

function changeC(val){
	country = val;
	if(country=='us' || country == 'ca' || country == 'au' || country == 'nz'){ cur='$';}
	else if(country == 'uk'){ cur='£';}
	else if(country =='ch'){cur='CHF';}
	else{cur='EUR';}
	document.getElementById('deviseNL').innerHTML = cur;
	GetContent(lastcategory,val);
	EcrireCookie(val,'Country');
	lastCountry = val;
}
function array_unique(TabInit){
	NvTab= new Array();
	var q=0;
	var LnChaine= TabInit.length;
	for(x=0;x<LnChaine;x++){
    	for(i=0;i<LnChaine;i++){
        	if(TabInit[x]== TabInit[i] && x!=i) TabInit[i]='faux';
        }
    	if(TabInit[x]!='faux'){
			NvTab[q] = TabInit[x];
			q++;
		}
    }
	return NvTab;
}
function selectProduct(product){
	EcrireCookie(product,'category');
	GetContent(product,lastCountry);
}
function GetContent(category,cn){
	if(!cn){
		country = lastCountry;
	}
	else{
		country = cn;
	}
	document.getElementById('content').innerHTML = " ";
	document.getElementById('content').innerHTML = '<div id="loading"> Loading...</div>';
	lastcategory = category;
	var xhr = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
	maxp = document.getElementById('maxprice').value;
	// Don't use the link bellow without autorisation
	theLink = "/xhr-iphone/?category="+category+"&country="+country+"&maxprice="+maxp;
	xhr.open("GET",theLink,true);
	xhr.onreadystatechange = function(){
		if (xhr.readyState == 4){
			if (xhr.status == 200){
				response = xhr.responseText;
				str = '<div id="products">';
				str += response;
				document.getElementById('content').innerHTML = " ";
				document.getElementById('content').innerHTML = str;
			}
			else{
				if(xhr.status == 0){
					document.getElementById('content').innerHTML ="<div class='noprod'><br>Not connected to internet</div>";
				}
				else if(xhr.status == 404){
					document.getElementById('content').innerHTML ="<div class='noprod'><br>There are a problems with refurb's servers.<br><em>Please contact the developer.</em></div>";
				}
				else{
					document.getElementById('content').innerHTML ="<div class='noprod'><br>Error " + xhr.status+"</div>";
				}
			}
		}
	}
	xhr.send(null);

	
}
function checkMax(args,tt) {
	if(args == "on"){
		document.getElementById('checkMax').checked = true;
		document.getElementById('maxprice').disabled = false;
		EcrireCookie('on','checkmax');
		document.getElementById('labelmax').style.color = "#717171";
		document.getElementById('deviseNL').style.color = "#717171";
	}
	else{
		document.getElementById('checkMax').checked = false;
		document.getElementById('maxprice').disabled = true;
		document.getElementById('maxprice').value = 0;
		EcrireCookie('off','checkmax');
		document.getElementById('labelmax').style.color = "#BABABA";
		document.getElementById('deviseNL').style.color = "#BABABA";
	}
	if(tt){
		changeP = true;
	}
}
function maxpSave(args) {
	EcrireCookie(args,'maxp');
	changeP = true;
}

function showD(mydiv) {
	if (document.getElementById(mydiv).style.display == 'block') {
		document.getElementById(mydiv).style.display = 'none';
	}
	else {
		document.getElementById(mydiv).style.display = 'block';
	}
}
function showDes(id){
	document.getElementById(id).style.display = 'block';
	slide(currentSlide,-1*(parseInt(document.getElementById(id).style.left)));
}
function slide(initX, finalX){
	window.clearTimeout(slideTimer);
	if (initX != finalX){
		if (initX > finalX){
			rate = Math.floor((finalX - initX) / inc);
		}
		else {
			rate = Math.ceil((finalX - initX) / inc);
		}
		newLeft = parseInt(document.getElementById('canvas').style.left) + rate;
		document.getElementById('canvas').style.left = newLeft + 'px';
		slideTimer = window.setTimeout("slide(" + newLeft + ", " + finalX + ");", 2);
		currentSlide= finalX;
	}
}

