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 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 = '';
	}
}

$(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 ($('select#carrier').val()>0 || $("#found_solution").val()>0) {
					$('select#carrier').trigger('change');
				}
				else {
					$.get('/ajax/do.php?getText=ShowInfo_step1&brand=' + $('#brandName').val(), function(data2){
						$("#start:visible").html(data2).hide().animate({opacity: 'show'}, 800, "linear");
					});
				}
			});
			$("#switch_curr a.selected").removeClass('selected');
			$(this).addClass('selected');
			return false;
		});
	});

	function loadMobilePhoto(id, modelName, brandName, size)
	{
		// 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;
			if ($('#modelPicture').attr("src") != src ) {
				$("#modelPicture").stop(true).fadeTo(200, 0.33);
				$('#modelPicture').attr({
				  src: src,
				  alt: title,
				  title: title
				}).stop(false, true).fadeTo(450, 1);
			}
		}
	}

	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(400);
//		mybox.nextAll("span.hint:first").css( { "left": (pos.left + width + 2) + "px", "top":(pos.top - 9) + "px"} ).fadeIn(400);
		if (timer==true) {
			setTimeout(function(){
						hideInfo(mybox);
					}, 7000);
		}
	}

	function hideInfoAll() {
		$("span.hint:visible").hide();
	}

	function setMobile(modelID, modelName, brandName, brandID) {
		$("#mobile").removeClass('formwarn formerr', 500);
		$('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').bind('keyup', function() {
		if ($(this).val()=='') {
			setMobile(0,'','',0);
		}
	});
	$("#mobile").autocomplete({
		source: "/ajax/getMobile.php",
		minLength: 2,
		select: function(event, ui) {
			setMobile(ui.item.id,ui.item.model,ui.item.brand,ui.item.brandid);
		},
		focus: function(event, ui) {
			loadMobilePhoto(ui.item.id,ui.item.model,ui.item.brand,"small");
		}
	});

	$("#orderForm :input, #help, #applycoupon, #coupon").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()});
	$('#imei').bind('blur keyup mouseleave focusout', function() {
		hideInfo($(this));
		imei = $(this).val();
		if(typeof(imei)!="undefined" && imei.length > 1) {
			if ($('#fooimei').val()!=imei) {
				$('#fooimei').val(imei);
				if (imei.length > 10)
					checkSumIMEI(imei);
			}
		}
		else {
			$("#imeiok, #warn-imei").hide(1200);
			$("#imei").removeClass('formwarn formok', 1200);
		}
	});
	$("select#carrier").change(function(){
		hideInfoAll();
		thiscarrier = $(this).find(":selected");
		$('#providerName').val(thiscarrier.text());
		$('#country').val(thiscarrier.parents("optgroup").attr("label"));
		getUnlockSolution ();
	});

	function getUnlockSolution () {
		$("div#getUnlockMsg:visible, div#getUnlockMsgTitle:visible, #solution:visible, #start:visible, div#divLoading:visible").hide();
		$('div#divLoading').show();
		$("#found_solution").val(0);
		var providers;
		var qString = $('#orderForm').serialize();
		$.getJSON("/ajax/getSolution.php?" + qString,  function(data){
			if (data.has_solution==true) {
				$("div#getUnlockMsg:visible, div#getUnlockMsgTitle:visible, #start:visible").hide();
				$("#found_solution").val(1);
				switch_unlock_solution(data.toolid, data.price, data.old_price, data.delay, data.need_provider, data.need_detail, data.currency, data.info, data.need_detail, data.detail_name, data.discount);
			}
			else if(typeof(data.msg)!="undefined" && data.msg.length > 1) {
				hideInfoAll();
				$('div#divLoading').hide();
				$("#start:visible, #solution:visible, #divPinId:visible").hide();
				$("#getUnlockMsgTitle").html(data.msgTitle).slideDown(); // show err msg title
				$("#getUnlockMsg").html(data.msg).show().fadeOut(100).fadeIn(400); // show err msg
				if(typeof(data.form_error)!="undefined" && data.form_error.length > 1) {
					if (data.form_error=='imei') {ImeiError();}
					else if (data.form_error=='mobile') {
						$("#mobile").addClass('formwarn');
					}
				}
			}
			else {
				reset_form();
			}
			if (data.coupon_error==true) {
				$('#coupon').removeClass('formok').addClass('formerr');
				var $dialog = $('#help-dialog').html('<img class="center" src="/img/shared/ajax-loader.gif" />');
				$dialog.load('/ajax/do.php?getText=ERR_COUPON');
				$dialog.dialog({
					title: 'Coupon',
					modal: true,
					autoOpen: false,
					height: 200,
					width: 380,
					draggable: false,
					resizeable: true
				});
				$dialog.dialog('open');
			}
		});
	}

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

	function switch_unlock_solution(toolid, price, old_price, delay, need_provider, need_detail, currency, info, need_detail, detail_name, discount) {
		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");
		if (discount!=false) {
			$("#discount").html('<br><span class="listprice">Coupon: </span> <span class="important2">-' + discount + '</span>').show();
		}
		else {
			$('#discount:visible').hide();
		}
		if (need_detail==true) {
			$("div#formContainer, div#quote_solution").animate({height:'+=5px'}, 800);
			$("#div_phone_detail").show();
			$("#help").attr('title',detail_name);
			$("#detail_libelle").html(detail_name);
		}
		else {
			/*$("div#formContainer, div#quote_solution").animate({height:'190px'}, 600);*/
			$("#div_phone_detail").hide();
		}
	}
	function ImeiError() {
		$("#imeiok:visible").hide();
		$("#warn-imei").show();
		$("#imei").removeClass('formok formerr').addClass('formwarn');
	}
	function ImeiValid() {
		$("#imei").removeClass('formwarn formerr').addClass('formok');
		$("#warn-imei:visible").hide();
		$("#imeiok").show();
	}
	function checkSumIMEI (imei) {
		$.getJSON("/ajax/checkimei.php?imei=" + imei,  function(data){
			if (data.chekimei==true) {
				ImeiValid();
				// reload unlock solution according for BB and HTC
				if ($("#brandId").val()==44 || $("#brandId").val()==106) {
					getUnlockSolution ();
				}
			}
			else {
				ImeiError();
			}
		});
	}
});

