// JavaScript Document

//JQUERY FUNCTIONS:
$(function() {
	
	$(".hint").hint();
	
	$(".qtip").each(function(){
		
		$(this).qtip({
					
		content : "<img src='"+$(this).attr("rel")+"'/>",
		style: { width:350, height:233, name: 'light', tip: 'leftTop', padding: '0', margin: '0' },
		position: {
			corner: {
				target: 'rightMiddle',
				tooltip: 'leftTop'
				}
		}
	
	});
		
	});
	
	//submit button orderForm
	$("#orderForm").submit(function(e){						
		if(!check_form()) { e.preventDefault(); }
	});
	
	//submit button contactForm
	$("#contactForm").submit(function(e){
		if(!check_form_contact()) { e.preventDefault(); }
	});
	
	//submit button eventsForm
	$("#eventsForm").submit(function(e){
		if(!check_form_events()) { e.preventDefault(); }
	});
	
	$("#orderForm input[name=tel]").phone();
	$("#orderForm input[name=fax]").phone();
	
	$("#orderForm input[name=date]").datepicker(Config.datepickerOpts);
	
	$("#eventsForm input[name=date]").datepicker(Config.datepickerOpts);
	
	if ($("#map_canvas").length) { 
		$(window).unload(function(e) { GUnload(); }) 
		$(window).load(function(e) { initialize_gmap(); }) 
	}
	
	$("textarea").keyup(function() { 
		if ($(this).next("div.textarea").length) { $(this).next("div.textarea").html($(this).val().replace(/\n/g, "<br />")); }
	});
	
	$("#print_btn").click(function(e){
		e.preventDefault();
		window.print();
	});
	
});

//Hint Function
jQuery.fn.extend({	
	//////////////////////////////////////////////////
	//////Text Hints in input
	//////////////////////////////////////////////////
	hint: function() {
		if(!$(this).length) { return false; }
		else {
			$(this).each(function() {
				var input = $(this);
				input.focus(function() {
					if($(this).val() == $(this).attr("title")) {
						$(this).val("").removeClass("hasHint");
					}
				}).blur(function() {
					if(jQuery.trim($(this).val()) == "") {
						$(this).val($(this).attr("title")).addClass("hasHint")
					}
				});
				
				input.parents("form").submit(function() {
					if(input.val() == input.attr("title")) { input.val(""); }
				});
				if(!$.trim($(this).val()).length) { $(this).val($(this).attr("title")).addClass("hasHint"); }
			});
		}
		return this;
	}
});

//GOOGLE MAP API
   function initialize_gmap() {
      if (GBrowserIsCompatible()) {
        var map = new GMap2(document.getElementById("map_canvas"));
        map.setCenter(new GLatLng(45.468227,-73.566213), 16);
        map.setUIToDefault();
		
		var point = new GLatLng(45.468227,-73.566213);
		map.addOverlay(new GMarker(point));
		
		map.openInfoWindowHtml(map.getCenter(), "<b>SIMPLÉCHIC</b><br>3610 Rue Wellington,<br>Verdun, QC, Canada, H4G 1T6<br><br><a href='http://maps.google.com/maps?q=3610+Rue+Wellington,+Verdun,+QC,+Canada&sll=37.0625,-95.677068&sspn=23.875,57.630033&ie=UTF8&hq=&hnear=3610+Rue+Wellington,+Verdun,+Communaut%C3%A9-Urbaine-de-Montr%C3%A9al,+Qu%C3%A9bec,+Canada&z=16&iwloc=A' target='_blank'>Google Map Link</a>");

      }
    }


//OTHER FUNCTIONS:
//check email
function echeck(str) {

		var at="@";
		var dot=".";
		var lat=str.indexOf(at);
		var lstr=str.length;
		var ldot=str.indexOf(dot);
		if (str.indexOf(at)==-1){
		   $("#contacterror").text("Adresse courriel invalide").fadeIn(1000);
		   $("#orderForm input[name=courriel]").addClass("redbg"); $("#orderForm input[name=courriel]").focus();
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   $("#contacterror").text("Adresse courriel invalide").fadeIn(1000);
		   $("#orderForm input[name=courriel]").addClass("redbg"); $("#orderForm input[name=courriel]").focus();
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    $("#contacterror").text("Adresse courriel invalide").fadeIn(1000);
			$("#orderForm input[name=courriel]").addClass("redbg"); $("#orderForm input[name=courriel]").focus();
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    $("#contacterror").text("Adresse courriel invalide").fadeIn(1000);
			$("#orderForm input[name=courriel]").addClass("redbg"); $("#orderForm input[name=courriel]").focus();
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    $("#contacterror").text("Adresse courriel invalide").fadeIn(1000);
			$("#orderForm input[name=courriel]").addClass("redbg"); $("#orderForm input[name=courriel]").focus();
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    $("#contacterror").text("Adresse courriel invalide").fadeIn(1000);
			$("#orderForm input[name=courriel]").addClass("redbg"); $("#orderForm input[name=courriel]").focus();
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
			$("#contacterror").text("Adresse courriel invalide").fadeIn(1000);
			$("#orderForm input[name=courriel]").addClass("redbg"); $("#orderForm input[name=courriel]").focus();
		    return false
		 }

 		 return true					
}

//check order form
function check_form() {
	
	//hide notice
	$("#contacterror").hide(); $("#contactnotice").hide(); $("#contactsuccess").hide();
	
	//toggle back all classes
	$("#orderForm input").removeClass("redbg");
	$("#orderForm textarea").removeClass("redbg");
	
	//check fields
	if ($("#orderForm input[name=even]").val().length  == 0) {
   	$("#contacterror").text("SVP entrez le nom de l'évènement").fadeIn(1000);
	$("#orderForm input[name=even]").addClass("redbg"); $("#orderForm input[name=even]").focus();
	return false;
	}
	
	if ($("#orderForm input[name=date]").val().length  == 0) {
   	$("#contacterror").text("SVP entrez la date de l'évènement").fadeIn(1000);
	$("#orderForm input[name=date]").addClass("redbg"); $("#orderForm input[name=date]").focus();
	return false;
	}
	
	//2009-11-18
	var currentTime = new Date();
	var month = currentTime.getMonth() + 1; month = ((month+100)+"").substr(1);
	var day = currentTime.getDate(); day = ((day+100)+"").substr(1);
	var year = currentTime.getFullYear();
	var myDate = year+"-"+month+"-"+day;
	if ($("#orderForm input[name=date]").val() == myDate) {
   	$("#contacterror").text("La date de l'évènement doit être supérieur à la date d'aujourd'hui").fadeIn(1000);
	$("#orderForm input[name=date]").addClass("redbg"); $("#orderForm input[name=date]").focus();
	return false;
	}
	
	if ($("#orderForm input[name=contact_site]").val().length  == 0) {
   	$("#contacterror").text("SVP entrez le contact principale sur le site").fadeIn(1000);
	$("#orderForm input[name=contact_site]").addClass("redbg"); $("#orderForm input[name=contact_site]").focus();
	return false;
	}
	
	if ($("#orderForm input[name=tel_site]").val().length  == 0) {
   	$("#contacterror").text("SVP entrez le nombre de convives présente à l'évènement").fadeIn(1000);
	$("#orderForm input[name=tel_site]").addClass("redbg"); $("#orderForm input[name=tel_site]").focus();
	return false;
	}
	
	if ($("#orderForm input[name=addr_liv]").val().length  == 0) {
   	$("#contacterror").text("SVP entrez l'adresse de livraison").fadeIn(1000);
	$("#orderForm input[name=addr_liv]").addClass("redbg"); $("#orderForm input[name=addr_liv]").focus();
	return false;
	}
	
	if ($("#orderForm input[name=heure_liv]").val().length  == 0) {
   	$("#contacterror").text("SVP entrez l'heure de la livraison").fadeIn(1000);
	$("#orderForm input[name=heure_liv]").addClass("redbg"); $("#orderForm input[name=heure_liv]").focus();
	return false;
	}
	
	if ($("#orderForm input[name=conv]").val().length  == 0) {
   	$("#contacterror").text("SVP entrez le nombre de convives présente à l'évènement").fadeIn(1000);
	$("#orderForm input[name=conv]").addClass("redbg"); $("#orderForm input[name=conv]").focus();
	return false;
	}
	
	if ($("#orderForm textarea[name=commande]").val().length  == 0) {
   	$("#contacterror").text("SVP entrez votre commande").fadeIn(1000);
	$("#orderForm textarea[name=commande]").addClass("redbg"); $("#orderForm textarea[name=commande]").focus();
	return false;
	}
	
	if ($("#orderForm input[name=contact_facture]").val().length  == 0) {
   	$("#contacterror").text("SVP entrez le contact pour la facturation").fadeIn(1000);
	$("#orderForm input[name=contact_facture]").addClass("redbg"); $("#orderForm input[name=contact_facture]").focus();
	return false;
	}
	
	if ($("#orderForm input[name=tel_facture]").val().length  == 0) {
   	$("#contacterror").text("SVP entrez le contact pour la facturation").fadeIn(1000);
	$("#orderForm input[name=tel_facture]").addClass("redbg"); $("#orderForm input[name=tel_facture]").focus();
	return false;
	}
	
	if ($("#orderForm input[name=courriel]").val().length  == 0) {
   	$("#contacterror").text("SVP entrez le contact pour la facturation").fadeIn(1000);
	$("#orderForm input[name=courriel]").addClass("redbg"); $("#orderForm input[name=courriel]").focus();
	return false;
	}

	
	//do check email function
	var emailID=$("#orderForm input[name=courriel]");
	
	if (echeck(emailID.val())==false){
		emailID.focus();
		return false
	}
	
	return true
}

//check contact form
function check_form_contact() {
	
	//hide notice
	$("#contacterror").hide(); $("#contactnotice").hide(); $("#contactsuccess").hide();
	
	//toggle back all classes
	$("#contactForm input").removeClass("redbg");
	$("#contactForm textarea").removeClass("redbg");
	
	//check fields
	if ($("#contactForm textarea[name=msg]").val().length  == 0) {
   	$("#contacterror").text("SVP entrez votre message").fadeIn(1000);
	$("#contactForm textarea[name=msg]").addClass("redbg"); $("#contactForm textarea[name=msg]").focus();
	return false;
	}
	
	
	//do check email function
	var emailID=$("#contactForm input[name=courriel]");
	
	if ((emailID.val()==null)||(emailID.val()=="")){
		$("#contacterror").text("SVP entrez votre adresse courriel").fadeIn(1000);
		$("#contactForm input[name=courriel]").addClass("redbg"); emailID.focus();
		return false
	}
	
	if (echeck(emailID.val())==false){
		emailID.focus();
		return false
	}
	
	return true
}

//check contact form
function check_form_events() {
	
	//hide notice
	$("#contacterror").hide(); $("#contactnotice").hide(); $("#contactsuccess").hide();
	
	//toggle back all classes
	$("#eventsForm input").removeClass("redbg");
	$("#eventsForm textarea").removeClass("redbg");
	
	//check fields
	if ($("#eventsForm input[name=name]").val().length  == 0) {
   	$("#contacterror").text("SVP entrez votre nom").fadeIn(1000);
	$("#eventsForm input[name=name]").addClass("redbg").focus();
	return false;
	}
	
	if ($("#eventsForm input[name=tel]").val().length  == 0) {
   	$("#contacterror").text("SVP entrez votre telephone").fadeIn(1000);
	$("#eventsForm input[name=tel]").addClass("redbg").focus();
	return false;
	}
	
	if ($("#eventsForm input[name=courriel]").val().length  == 0) {
   	$("#contacterror").text("SVP entrez le contact pour la facturation").fadeIn(1000);
	$("#eventsForm input[name=courriel]").addClass("redbg").focus();
	return false;
	}
	
	if ($("#eventsForm input[name=date]").val().length  == 0) {
   	$("#contacterror").text("SVP entrez la date de votre événement").fadeIn(1000);
	$("#eventsForm input[name=date]").addClass("redbg").focus();
	return false;
	}
	
	if ($("#eventsForm input[name=nbpers]").val().length  == 0) {
   	$("#contacterror").text("SVP entrez le nombre de personnes").fadeIn(1000);
	$("#eventsForm input[name=nbpers]").addClass("redbg").focus();
	return false;
	}
		
	if ($("#eventsForm input[name=lieu]").val().length  == 0) {
   	$("#contacterror").text("SVP entrez le lieu de l'événement").fadeIn(1000);
	$("#eventsForm input[name=lieu]").addClass("redbg").focus();
	return false;
	}


	//do check email function
	var emailID=$("#eventsForm input[name=courriel]");
	
	if (echeck(emailID.val())==false){ emailID.addClass("redbg").focus(); return false; }
	
	return true
}