// JavaScript Document
function simple_tooltip(target_items, name){
 $(target_items).each(function(i){
		$("body").append("<div class='"+name+"' id='"+name+i+"'><h3>"+$(this).text()+"<img src='images/close-red-square.png' id='s"+name+i+"' alt='Fermer' title='Fermer' width='24' height='24' /></h3><p>"+$(this).attr('title')+"</p></div>");
		var my_tooltip = $("#"+name+i);
		var shut_tooltip = $("#s"+name+i);
		$(this).removeAttr("title").click(function(kmouse){
				$(".tooltip").fadeOut(50);
				my_tooltip.css({opacity:0.9, left:kmouse.pageX-181, top:kmouse.pageY+1, display:"none"}).fadeIn(400).draggable();
				return false;
		});
		shut_tooltip.click(function(){
				my_tooltip.fadeOut(400);
		});
	});
}

function hebsubchk() {
	var heberror = '';
	var reg = /^[a-z0-9]+[a-z0-9\-\.]*\.[a-z]{2,4}$/i;
    var hebplan = $('input[name=hebplan]:checked').val();
   if ($("input#hebdomain").val()) { var hebdomain = $("input#hebdomain").val(); } else { var hebdomain = "wrong"; }
    var hebmatch = reg.test(hebdomain);
	if( !hebplan && (hebdomain == '' || hebdomain == 'votredomaine.fr')){
		$("#hebvalidatediv").html("<p>Merci de choisir un plan et un domaine pour continuer.</p>"); 
	 } else if (hebplan && (hebdomain == '' || hebdomain == 'votredomaine.fr')) {
		 $("#hebvalidatediv").html("<p>Merci de choisir un domaine pour contnuer.</p>");
	 } else if (hebplan && !hebmatch) {
		 $("#hebvalidatediv").html("<p>Le format de votre domaine n'est pas valide. Il ne doit contenir que les lettres (a-z), des chiffres (0-9), des trait d'union (-) et des points(.), vérifiez également que vous avez spécifié une extension (.fr, .com, .net ...).</p>");
	 } else if (!hebplan && hebmatch) {
		$("#hebvalidatediv").html("<p>Merci de séléctionnner un plan pour continuer.</p>");
	 } else if (!hebplan && !hebmatch) {
		$("#hebvalidatediv").html("<p>Merci de séléctionner un plan et de vérifier le format de votre domaine. Il ne doit contenir que les lettres (a-z), des chiffres (0-9), des trait d'union (-) et des points(.), vérifiez également que vous avez spécifié une extension (.fr, .com, .net ...)</p>");	
	}
	if (hebplan && hebdomain != '' && hebdomain != "votredomaine.fr" && hebmatch && hebdomain.length >= 5 )	{
		$('#hebvalidate').css({opacity: "1"});
		$("#hebvalidatediv").fadeOut(400);
		return true;
	} else {
		$('#hebvalidate').css({opacity: "0.4"});
		return false;
	}
}
function simple_chk(target_items){
		$("body").append("<div id='hebvalidatediv'></div>");
		hebsubchk();
		$(target_items).each(function(){
		$(this).bind("click mouseup keyup mouseover", function(){
			setTimeout(function () {hebsubchk()}, 200);
		});
});


$("input#hebvalidate").mouseover(function(kmouse){
	 if (!hebsubchk()) {
		$("#hebvalidatediv").css({left:kmouse.pageX+15, top:kmouse.pageY+15, display:"none"}).fadeIn(400);
     }
}).mousemove(function(kmouse){
				$("#hebvalidatediv").css({left:kmouse.pageX+15, top:kmouse.pageY+15});
		}).mouseout(function(){
				$("#hebvalidatediv").fadeOut(400);
});
}
function simple_submitform(formid){
	$('#hebform').submit(function() { 	
		return hebsubchk();
	});
}

function heb_getref(element) {
	if (element == 'hebtype') {
		if($('#hebtype').attr('checked') == 1) {
			return $("#hebtype").val();
		} else {
			return 'HN';	
		}
	} else if (element == 'hebplan') {
		if($("#hebplan").val()) {
			return $("#hebplan").val();	
		} else {
			return '1SA';
		}
	} else if (element == 'opt1') {
		if($('input[name=opt1]:checked').val()) {
			return $('input[name=opt1]:checked').val();	
		} else {
			return 'cPanel';	
		}
	} else if (element == 'opt2') {
		if($('input[name=opt2]:checked').val()) {
			return $('input[name=opt2]:checked').val();	
		} else {
			return 'Français';	
		}
	} else if (element == 'hebdescr') {
		if($('#hebtype').attr('checked') == 1) {
			return '<p><strong>Pack hébergement et nom de domaine</strong> :<br />';	
		} else {
			return '<p><strong>Hébergement seul</strong>, sans nom de domaine :<br />';	
		}
	} else {
		return '';	
	}
	
}

function update_hebdata(){				 	
			$.ajax({
				url : "panier/ajax.php",
				type : "POST",
				data : "action=hebupdate&hebtype=" + heb_getref('hebtype') + "&hebplan=" + heb_getref('hebplan'),
				dataType : "json",
				send : function(){ $("#hebnpricedet").html("<th colspan=\"2\">Mise à jour en cours ...</th>"); },
				stop : function(){ $("#hebnpricedet").html("<th colspan=\"2\">La mise à jour a échoué, nous nous en excusions, merci de cliquer sur le bouton mettre à jour à droite du menu détails.</th>"); },
				error : function (xhr, desc, exception) { /*alert("XHR error :" + xhr.responseText);*/ },
				success : function (data) { 
				/*if (data.error) { alert("Data error :" + data.error); return; }*/
				var output = "<td id=\"hebnbasket\">"
				output += "<a href=\"panier/action.php?action=ajouter&amp;ajouter_article=" + heb_getref('hebtype') + heb_getref('hebplan') +"001&amp;ajouter_domaine=" + $('#hebdomain').val() + "&amp;opt1=" + heb_getref('opt1') + "\"><img src=\"images/ajouter-au-panier.png\" alt=\"ajouter au panier\" /></a>"
				output += "</td>"
				output += "<td id=\"heboptdetails\">"
				output += "<h3>" + data.description + "</h3>";
				output += heb_getref('hebdescr');
				output += "Prix pour 12 mois : " + data.prixHT.replace( '.' , ',' ) +" € H.T. | " + data.prixTTC.replace( '.' , ',' ) + " € T.T.C.<br />";
				output += "Panneau de contrôle : cPanel<br />";
				output += "Langue par défaut : " + heb_getref('opt1') + "</p>";
				output += "</td>";
				$("#hebnpricedet").html(output);
				}
			});	
}

function simple_update(target_items){
		$(target_items).each(function(){
		$(this).click(function(){
			update_hebdata();
		});
	});
}

function simple_update_select(target_items){
		$(target_items).each(function(){
		$(this).change(function(){
			update_hebdata();
		});
	});
}

$(document).ready(function(){
	simple_tooltip("a.hebn-info","tooltip");
	simple_chk("input");
	simple_submitform("#form");
	simple_update("input");
	simple_update_select("select");
});