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 + "+" + modelName + "+" + id + ".jpg";
		var title = brandName + " " + modelName;
		$('#modelPicture').attr({
		  src: src,
		  alt: title,
		  title: title
		}).fadeTo("slow", 1);
	}
}

$(document).ready(function() {
	/* Currencies */
	$("#switch_curr a").each(function () {
		var curr = $(this).attr('id');
		$(this).bind ('click', function() {
			$.get('/ajax/do.php?setCurrency=' + curr, function(data){
				if($('#unknownProvider').val()=='true') {
					getUnlockSolution();
				}
				else if ($('select#providerId').val()>0) {
					$('select#providerId').trigger('change');
				}
				else {
					$.get('/ajax/do.php?getText=ShowInfo_step1', function(data2){
						$("#start:visible").html(data2).hide().animate({opacity: 'show'}, 800, "linear");
					});
				}
			});
			$("#switch_curr a.selected").removeClass('selected');
			$(this).addClass('selected');
			return false;
		});
	});
	
	$("#orderForm :input").each(function () {
			var isfocused = false;
			$(this).focus(function() {hideInfoAll(); isfocused=true; showInfo($(this), true);});
			$(this).blur(function() {isfocused=false; hideInfo($(this));});
			$(this).hover(
				function () {hideInfoAll(); showInfo($(this), false);},
				function () {if (isfocused==false) {hideInfo($(this));} }
			);
	});
	$("#noProvider, #warn-imei").hover(
		function () {hideInfoAll();  showInfo($(this), false);},
		function () {hideInfo($(this));}
	);
	$("#quote_solution").hover(function(){hideInfoAll()});
	$("select#countryID").change(function(){
		ajaxSetCountry($(this).val(), false);
	});
	$('#imei').blur( function(){
		imei = $(this).val();
		if(typeof(imei)!="undefined" && imei.length > 1) {
			checkSumIMEI(imei);
		}
		else {
			$("#imeiok, #warn-imei").hide();
			$("#imei").removeClass('formwarn formok');
		}
	});
	$("select#modelID").change(function(){
		var text_option = $(this).find(":selected").text();
		$('input#model').val(text_option);
		loadMobilePhoto($(this).val(), text_option.replace(' ','-'), $("#brand").val(), "small");
		/*if($("#imei").val().length>14 && $("select#providerId").val()>0 && checkSumIMEI($("#imei").val())) {
			getUnlockSolution();
		}*/
	});
	$("select#providerId").change(function(){
		if ($(this).val()>0) {
			$('#providerName').val($(this).find(":selected").text());
			getUnlockSolution ();
		}
	});
	$("#noProvider").toggle(function(){
		setNoProvider();
	},function(){
		UnSetNoProvider();
	});
	
	function ajaxSetCountry(CountryID, ProviderID)
	{
		$("#divNetwork").hide();
		$("#providerId").css({'background-color' : '', 'border' : ''});
		
		if (CountryID != "" && CountryID>0)
		{
			// Show Loading
			$("#divLoading").show();
			var providers;
			$.get('/ajax/providers.xml.php?id=' + CountryID, function(data) {
				var thisCountry = $("select#countryID option:selected").text();
				providers = data;
				$('#providerId').empty().append("<option value='0'> "+ thisCountry +" >> select :</option>");
				$('network', providers).each(function() {
					var optionvalue = $(this).attr('id');
					var optiontitle = $(this).text();
					if (Number(ProviderID)>0 && optionvalue==ProviderID) {
						$('#providerId').append("<option value='"+ optionvalue +"' selected>"+ optiontitle +"</option>");
					}
					else {
						$('#providerId').append("<option value='"+ optionvalue +"'>"+ optiontitle +"</option>");
					}
				});
			}, 'xml');
			
			$("#divNetwork").show();
			$("#providerId").css({'background-color' : '#e0ffff', 'border' : '1px solid #4682b4'});
			
			// Hide Loading
			setTimeout(function(){
				$("#divLoading").hide(500);
				if (Number(ProviderID)>0) {
					$("#providerId").val(ProviderID);
				}
				//else {
//					$('#providerId').trigger('focus');
//				}
			}, 100);
		}
	}

	function ajaxAutoSelectProvider(provider_ID) {      
		if (provider_ID)   {
			$("#providerId").val(provider_ID);
		}
	}

	//////////////////////////////////////////
	//       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(350);
		if (timer) {
			setTimeout(function(){
						hideInfo(mybox);
					}, 7500);
		}
	}
	function hideInfoAll() {
		$("div#formContainer span.hint:visible").hide();
	}

	function getUnlockSolution () {
		var providers;
		var qString = $('#orderForm').serialize();
		$("#getUnlockMsg").hide();
		$.getJSON("/ajax/getSolution.php?" + qString,  function(data){
			if (data.has_solution==true) {
				switch_unlock_solution(data.toolid, data.price, data.old_price, data.delay, data.need_provider, data.need_detail, data.currency, data.detail_name);
			}
			else if(typeof(data.msg)!="undefined" && data.msg.length > 1) {
				hideInfoAll();
				reset_form();
				$("#getUnlockMsg").html(data.msg).show().fadeOut(100).fadeIn(400); // show err msg, not implemented
			}
			else {
				reset_form();
			}
		});
	}

	function reset_form() {
	//	$("#div_PinId").hide();
		$("#div_MEP").hide();
		$("#bb_solution").hide();
		$("#bb_start").animate({opacity: 'show'}, 800, "linear");
	}

	function switch_unlock_solution(toolid, price, old_price, delay, need_provider, need_detail, currency, detail_name) {
		hideInfoAll();
		// fill informations
		$("#bb_solution:visible, #bb_start:visible").hide();
		$("#ToolID").val(toolid);
		$("#bb_delivery").text(delay);
		$("#bb_old_price").html(old_price);
		$("#bb_price").html(price);
		// show up unlock solution
		$("#bb_solution").show();
		if (need_detail==true) {
			$("#div_MEP").show();
			if (need_provider==true) {
				$("div#formContainer, div#quote_solution").css({'height' : '220'});
			}
		}
		else {
			$("#div_MEP:visible").hide();
			$("div#formContainer, div#quote_solution").css({'height' : '200'});
	//		$("#formContainer").css({'height' : 200});
		}
	}

	function setNoProvider() {
		$("#noProvider").addClass("ui-widget-sel");
		$("#divCountry, #divNetwork").hide();
		$('input#providerName').val('factory_code');
		$('select#providerId').val('0');
		$('#unknownProvider').val(true);
		getUnlockSolution();
	}
	function UnSetNoProvider() {
		$('#unknownProvider').val(false);
		//$("#div_PinId").hide();
		if($("select#countryID").val()>0) {
			ajaxSetCountry($("select#countryID").val(), $("select#providerId").val());
			getUnlockSolution();
		}
		else {
			reset_form();
		}
		$("#divCountry").show();
		$("#noProvider").removeClass("ui-widget-sel");
	}

	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
				/*if ($("select#providerId").val()>0) {
					getUnlockSolution ();
				}*/
			}
			else {
				$("#imeiok").hide();
				$("#warn-imei").show();
				$("#imei").removeClass('formok').addClass('formwarn');
			}
		});
	}

});
