function noteshow(_8c){
	for(var i=0;i<5;i++){
		$("star"+(i+1)).src="/img/shared/star_off.png";
	}
	for(var i=0;i<_8c;i++){
		$("star"+(i+1)).src="/img/shared/star_on.png";
	}
}
function notesave(key,_8f){
	_8f=parseInt(_8f);
	if(_8f<1||_8f>5){
		alert("Erreur sur la note");
	}
	$("note").value=_8f;
	$("has_noted").value=1;
}
function noteclear(){
	setTimeout("notereset()",600);
}
function notereset(){
	noteshow($("note").value);
}

function popup(f,nom, w, h) {
	var top=0;
	var left=0;
	if (self.screen)
		left=(screen.width-(w+((screen.width*1)/100)));
	myWin = window.open(f, nom, 'resizable,screenX=0,screenY=0,scrollbars=yes,menubar=no,top='+top+',left='+left +',width=' + w + ',height=' + h );
	myWin.focus();
}

function DisplayMsg(myMsg) {
	if (document.getElementById)
	{
		document.getElementById("orderMessage").innerHTML = myMsg;
		document.getElementById("orderMessage").style.display = '';
	}
	else if (document.all)
	{
		document.all["orderMessage"].innerHTML = myMsg;
		document.getElementById("orderMessage").style.display = '';
	}
}

function clearMsgBox() {
	$('#orderMessage').style.display = 'none';
}

function DisplayWarning(myMsg) {
	if (document.getElementById)
	{
		document.getElementById("warning").innerHTML = myMsg;
		document.getElementById("warning").style.display = '';
	}
	else if (document.all)
	{
		document.all["warning"].innerHTML = myMsg;
		document.getElementById("warning").style.display = '';
	}
}

function DisplayMsg2(myMsg, boxid)
{
	if (document.getElementById)
	{
		document.getElementById(boxid).innerHTML = myMsg;
		document.getElementById(boxid).style.display = '';
	}
	else if (document.all)
	{
		document.all[boxid].innerHTML = myMsg;
		document.getElementById(boxid).style.display = '';
	}
}
function clearMsg2(boxid) {
	$(boxid).style.display = 'none';
}

function loadMobilePhoto(id, modelName, brandName, size)
{
	$('#modelPicture').fadeTo("fast", 0.33);
	// Set image source
	if (id==0 && modelName=='' && brandName=='') {
		var src = "/img/shared/phone_nopict.jpg";
		$('#modelPicture').attr("src", src);
	}
	else {
		var src = "/picture+" + size + "+" + brandName.replace(' ','-') + "+" + modelName.replace(' ','-') + ".jpg";
		var title = brandName + " " + modelName;
		$('#modelPicture').attr({
		  src: src,
		  alt: title,
		  title: title
		}).fadeTo("slow", 1);
	}
}

/*$(function() {
	function setMobile(modelID, modelName, brandName, brandID) {
		$('input#modelId').val(modelID);
		$('input#brandId').val(brandID);
		$('input#modelName').val(modelName);
		$('input#brandName').val(brandName);
		loadMobilePhoto(modelID, modelName, brandName, "small");
		if ($("select#carrier").val()> 0) {
			getUnlockSolution ();
		}
	}
	$("#mobile").autocomplete({
		source: "/ajax/getMobile.php",
		minLength: 2,
		select: function(event, ui) {
//			log(ui.item ? ("Selected: " + ui.item.value + " aka " + ui.item.id) : "Nothing selected, input was " + this.value);
			setMobile(ui.item.id,ui.item.model,ui.item.brand,ui.item.brandid);
		}
	});
});*/

//////////////////////////////////////////
//       ORDER FORM START HERE         ///

function hideInfo(mybox) {
	mybox.next("span.hint").fadeOut(700);
}
function showInfo(mybox, timer) {
	//get the position of the placeholder input
	var pos = mybox.offset();
	var width = mybox.width();
	mybox.next("span.hint").css( { "left": (pos.left + width + 2) + "px", "top":(pos.top - 9) + "px"} ).fadeIn(450);
	if (timer==true) {
		setTimeout(function(){
					hideInfo(mybox);
				}, 8500);
	}
}
function hideInfoAll() {
	$("span.hint:visible").hide();
}

function checkSumIMEI (imei) {
	$.getJSON("/ajax/checkimei.php?imei=" + imei,  function(data){
		if (data.chekimei==true) {
			$("#imei").removeClass('formwarn').addClass('formok');
			$("#warn-imei").hide();
			$("#imeiok").show();
			// reload unlock solution according to IMEI (get available tool)
			getUnlockSolution ();
		}
		else {
			$("#imeiok").hide();
			$("#warn-imei").show();
			$("#imei").removeClass('formok').addClass('formwarn');
		}
	});
}

function getUnlockSolution () {
	$("div#getUnlockMsg:visible, div#getUnlockMsgTitle:visible, #solution:visible, #start:visible, div#divLoading:visible").hide();
	$('div#divLoading').show();
	var providers;
	var qString = $('#orderForm').serialize();
//   $("#getUnlockMsg").hide();
	$.getJSON("/ajax/getSolution.php?" + qString,  function(data){
		if (data.has_solution==true) {
			$("div#getUnlockMsg:visible, div#getUnlockMsgTitle:visible, #start:visible").hide();
			switch_unlock_solution(data.toolid, data.price, data.old_price, data.delay, data.need_provider, data.need_detail, data.currency, data.info);
		}
		else if(typeof(data.msg)!="undefined" && data.msg.length > 1) {
			hideInfoAll();
			$('div#divLoading').hide();
			$("#start:visible, #solution:visible").hide();
			$("#getUnlockMsgTitle").html(data.msgTitle).slideDown(); // show err msg title
			$("#getUnlockMsg").html(data.msg).show().fadeOut(100).fadeIn(400); // show err msg
		}
		else {
			reset_form();
		}
	});
}

function reset_form() {
	$("div#getUnlockMsg:visible, div#getUnlockMsgTitle:visible, #solution:visible, div#divLoading:visible").hide();
	$("#start").animate({opacity: 'show'}, 800, "linear");
}

function switch_unlock_solution(toolid, price, old_price, delay, need_provider, need_detail, currency, info) {
	hideInfoAll();
	$('div#divLoading:visible').hide();
	// fill informations
	$("#ToolID").val(toolid);
	$("#delivery").text(delay);
	$("#old_price").html(old_price);
	$("#price").html(price);
	$("#solution_info").html(info);
	// show up unlock solution
	$("#start").hide();
	$("#solution").hide().animate({opacity: 'show'}, 700, "linear");
}