
function updatePrices(){
	
	//BasePrice
	pagesPrice = $j(".pages .pages-result strong").text().split("€",2);
	//dateCoeff = $j(".calendar li.active").attr("data-coeff");
	dateCoeff = $j(".calendar .datepickerDays td.active").attr("data-coeff");
	
	
	if(typeof dateCoeff == "undefined"){
		dateCoeff = 1;
	}
	basePrice = parseFloat(pagesPrice[0]) * parseFloat(dateCoeff);
	basePrice = Math.floor(basePrice);
	$j(".order-box li.base strong").html(basePrice+" <span>€</span>");
	$j("input#c-baseamount").attr("value",basePrice);
	
	//OptionsPrice
	layoutPrice = 0;
	$j("#order-options .layout ul li.active").each(function(){
		if($j(this).find("strong").text() != "Gratuit"){
			layoutPrice = layoutPrice + parseFloat($j(this).find("strong").text().split("€",2));
		}
	});
	$j(".order-box li.layout strong").html(layoutPrice+" <span>€</span>");
	$j("input#c-optionamount1").attr("value",layoutPrice);
	
	integrationPrice = 0;
	$j("#order-options .integration ul li.active").each(function(){
		if($j(this).find("strong").text() != "Gratuit"){
			integrationPrice = integrationPrice + parseFloat($j(this).find("strong").text().split("€",2));
		}
	});
	$j(".order-box li.integration strong").html(integrationPrice+" <span>€</span>");
	$j("input#c-optionamount2").attr("value",integrationPrice);
	
	advancedPrice = 0;
	$j("#order-options .advanced ul li.active").each(function(){
		if($j(this).find("strong").text() != "Gratuit"){
			advancedPrice = advancedPrice + parseFloat($j(this).find("strong").text().split("€",2));
		}
		
	});
	$j(".order-box li.advanced strong").html(advancedPrice+" <span>€</span>");
	$j("input#c-optionamount3").attr("value",advancedPrice);
	
	//TotalPrice
	totalSum = basePrice + layoutPrice + integrationPrice + advancedPrice;
	$j(".order-box .total strong").html(totalSum+" <span>€</span>");
	$j(".payment .methods .all .price").html(totalSum+" <span>€</span>");
	$j(".payment .methods .accompte .price").html(Math.floor((totalSum *30)/100)+" <span>€</span>");
	
}



function updateCalendar(){
	
	//Graphic
	$j(".cal2").css("opacity","0.3");
	$j(".cal-loader").addClass("active");
	
	//Dernier jour du mois d'avant
	if ($j("td.off-last.c-31")){
		lastDay = 31;
	} else if($j("td.off-last.c-30")){
		lastDay = 30;
	}
	
	//Grisage jours d'avant today
	if($j("td.today").length > 0){
		oDay = $j("td.today").attr("class").split("c-",2);
		oDay = parseInt(oDay[1]);
		
		for(i=1; i<oDay; i++){
			$j(".cal2 .c-"+i).addClass("off");
		};
	}
	
	//Cases rouges urgentes selon today
	if($j("td.today").length > 0){
		dataCoeffArray = new Array();
		dataCoeffArray = ['2','1.7','1.5','1.3','1.2','1.1'];
		iArray = 0;
		for(i=oDay+1; i<oDay+7; i++){
			$j(".cal2 .c-"+i).addClass("urgent").attr("data-coeff",dataCoeffArray[iArray]);
			iArray++;
		}
	}
	
	
	//Activation au clic d'une date
	$j(".calendar .cal2 .datepickerDays td").click(function(){
		if($j(this).hasClass("off") || $j(this).hasClass("today")){}else{
			$j(".calendar .cal2 .datepickerDays td").removeClass("active").find("div").css("opacity","1");
			$j(this).addClass("active").find("div").css("opacity","0.2");
			$j(this).find("div").animate({opacity:1});
			var curMonth = $j(".cal2 .datepickerMonth span").text().split(",",2);
			var curDay = $j(this).find("span").text();
			$j(".order-box .ship strong").html(curDay+" "+curMonth[0]+" "+curMonth[1]);
			if($j(this).hasClass("urgent")){
				$j(".order-box .ship strong").html(curDay+" "+curMonth[0]+" "+curMonth[1]+" <span>[URGENT]</span>");
			}
		}
		updatePrices();
		inputDate = $j(this).attr("data-date");
		$j("input[name='c-date']").attr("value",inputDate);
		
	});
	
	//Gestion graphique hover date
	$j(".calendar .cal2 .datepickerDays td").hover(
		function(){
			if($j(this).hasClass("active") || $j(this).hasClass("off") || $j(this).hasClass("today")){}else{
				$j(".calendar .cal .datepickerDays td div").css("opacity","1");
				$j(this).find("div").css("opacity","0.7");
			}
		},
		function(){
			$j(".calendar .cal2 .datepickerDays td div").css("opacity","1");
		}
	);

	//Gestion des days off	
	$j.ajax({
		type: "POST",
		url: "php/retrievedaysoff.php",
		success : function(dataReturn){
			
			
			var daysOffAjax = dataReturn.split("--");
			var i = 0;
			for(i;i < daysOffAjax.length; i = i+1){
				var curDayOff = daysOffAjax[i];
				$j(".datepicker .datepickerViewDays tr td[data-date='"+curDayOff+"']").addClass("off");
				
			}
			
			//Graphic
			$j(".cal2").css("opacity","1");
			$j(".cal-loader").removeClass("active");

			
		}
	});
	
		
}



function isMail(emailAddress) {
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
}


function rand(m,M){return M?m+rand(M-m):Math.random()*++m<<.5}

$j(function(){

	updatePrices();

	//Uploadify
	$j("#c-file").uploadify({
		'uploader'  : '/js/uploadify/uploadify.swf',
		'script'    : '/js/uploadify/uploadify.php',
		'cancelImg' : '/js/uploadify/cancel.png',
		'auto'      : true,
		'folder'    : '/uploads'
	});

	//PricesBox
	rightOffset = $j("#col-right").offset();
	$j(window).scroll(function(){
		if($j(window).scrollTop() > rightOffset.top){
			$j("#col-right .box").addClass("floating");
		} else {
			$j("#col-right .box").removeClass("floating");
		}
	});
	
	//FormID
	var cFormid = Math.abs(rand(1,9999999999));
	$j("#c-formid").attr("value",cFormid);
	
	//Navigation
	$j("#content.order .timeline ul li").click(function(){
		$j("#content.order .timeline ul li").removeClass("active");
		$j(this).addClass("active");
		$j("#col-left fieldset").hide();
		$j(".ibox").hide();
		if($j(this).hasClass("details")){
			$j("#col-left fieldset#order-details").fadeIn();
			$j(".intro-step1").show();
		} else if($j(this).hasClass("options")){
			$j("#col-left fieldset#order-options").fadeIn();
			$j(".intro-step2").show();
		} else {
			$j("#col-left fieldset#order-payment").fadeIn();
			$j(".intro-step3").show();
		}
	});
	
	$j("#content.order #order-details .next-step").click(function(e){
		e.preventDefault();
		$j("#content.order .timeline ul li").removeClass("active");
		$j("#content.order .timeline ul li.options").addClass("active");
		$j("#col-left fieldset").hide();
		$j(".ibox").hide();
		$j("#col-left fieldset#order-options").fadeIn();
		$j(".intro-step2").show();
	});
	
	$j("#content.order #order-options .next-step").click(function(e){
		e.preventDefault();
		$j("#content.order .timeline ul li").removeClass("active");
		$j("#content.order .timeline ul li.payment").addClass("active");
		$j("#col-left fieldset").hide();
		$j(".ibox").hide();
		$j("#col-left fieldset#order-payment").fadeIn();
		$j(".intro-step3").show();
	});
	
	$j("#content.order #order-options .previous-step").click(function(e){
		e.preventDefault();
		$j("#content.order .timeline ul li").removeClass("active");
		$j("#content.order .timeline ul li.details").addClass("active");
		$j("#col-left fieldset").hide();
		$j(".ibox").hide();
		$j("#col-left fieldset#order-details").fadeIn();
		$j(".intro-step1").show();
	});

	$j("#content.order #order-payment .previous-step").click(function(e){
		e.preventDefault();
		$j("#content.order .timeline ul li").removeClass("active");
		$j("#content.order .timeline ul li.options").addClass("active");
		$j("#col-left fieldset").hide();
		$j(".ibox").hide();
		$j("#col-left fieldset#order-options").fadeIn();
		$j(".intro-step2").show();
	});
	
	//STEP1
	
	//PageSlider
	$j("#col-left .pages-slider").slider({
		value:1,
		min: 1,
		max: 15,
		step: 1,
		slide: function(event, ui) {
			switch(ui.value){
				case(1):
					$j(".pages-result span, .order-box .pages strong").html("1 page");
					$j(".pages-result strong, .order-box li.base strong").html("199 €");
				break;
				case(2):
					$j(".pages-result span, .order-box .pages strong").html("2 pages");
					$j(".pages-result strong, .order-box li.base strong").html("299 €");
				break;
				case(3):
					$j(".pages-result span, .order-box .pages strong").html("3 pages");
					$j(".pages-result strong, .order-box li.base strong").html("399 €");
				break;
				case(4):
					$j(".pages-result span, .order-box .pages strong").html("4 pages");
					$j(".pages-result strong, .order-box li.base strong").html("499 €");
				break;
				case(5):
					$j(".pages-result span, .order-box .pages strong").html("5 pages");
					$j(".pages-result strong, .order-box li.base strong").html("599 €");
				break;
				case(6):
					$j(".pages-result span, .order-box .pages strong").html("6 pages");
					$j(".pages-result strong, .order-box li.base strong").html("699 €");
				break;
				case(7):
					$j(".pages-result span, .order-box .pages strong").html("7 pages");
					$j(".pages-result strong, .order-box li.base strong").html("799 €");
				break;
				case(8):
					$j(".pages-result span, .order-box .pages strong").html("8 pages");
					$j(".pages-result strong, .order-box li.base strong").html("899 €");
				break;
				case(9):
					$j(".pages-result span, .order-box .pages strong").html("9 pages");
					$j(".pages-result strong, .order-box li.base strong").html("999 €");
				break;
				case(10):
					$j(".pages-result span, .order-box .pages strong").html("10 pages");
					$j(".pages-result strong, .order-box li.base strong").html("1099 €");
				break;
				case(11):
					$j(".pages-result span, .order-box .pages strong").html("11 pages");
					$j(".pages-result strong, .order-box li.base strong").html("1199 €");
				break;
				case(12):
					$j(".pages-result span, .order-box .pages strong").html("12 pages");
					$j(".pages-result strong, .order-box li.base strong").html("1299 €");
				break;
				case(13):
					$j(".pages-result span, .order-box .pages strong").html("13 pages");
					$j(".pages-result strong, .order-box li.base strong").html("1399 €");
				break;
				case(14):
					$j(".pages-result span, .order-box .pages strong").html("14 pages");
					$j(".pages-result strong, .order-box li.base strong").html("1499 €");
				break;
				case(15):
					$j(".pages-result span, .order-box .pages strong").html("15 pages");
					$j(".pages-result strong, .order-box li.base strong").html("1599 €");
				break;
			}
			updatePrices();
			$j("input[name='c-pagenumber']").attr("value",ui.value);
		}
	});
	
	
	//Calendar
	$j(".cal2").DatePicker({
		flat: true,
		calendars: 1,
		date: currentDate,
		current: currentDate,
		format: 'd-m-Y',
		onRender: function(date) {
			return {
				disabled: (date.valueOf() < nowDate.valueOf()),
				className: date.valueOf() < nowDate.valueOf() ? 'off' : false
			};
		}
	});
	
	//Update Calendar
	updateCalendar();
	$j(".cal2 .datepickerViewDays th").click(function(e){
		setTimeout("updateCalendar();",10);
	});
	
	
	
	//STEP2
	
	//FoldUnfold
	$j(".orange-box span.open").click(function(){
		$j(this).toggleClass("active").parent().find("dl").slideToggle();
	});
	
	//LiCheck
	$j(".orange-box dl ul li").click(function(){
		$j(this).parent().find("li").removeClass("active");
		$j(this).addClass("active").css("opacity","0.3").animate({opacity:1});
		updatePrices();
		var thisValue = $j(this).attr("id").substr($j(this).attr("id").length -1, $j(this).attr("id").length);
		$j(this).parent().parent().find("input").attr("value",thisValue);
	});
	
	
	//STEP3
	
	//***ContactForm
	//Reset
	$j("#content.order .contact input, #content.order .contact textarea").attr("value","");
	
	//Gfx
	$j("#content.order .contact input, #content.order .contact textarea").focus(function(){
		$j("#content.order .contact input, #content.order .contact textarea").removeClass("input-active");
		$j(this).addClass("input-active");
	});
	
	//InputFile
	$j("#c-file").change(function(){
		var cFileValue = $j(this).attr("value");
		$j("#c-file-fake").attr("value",cFileValue);
	});
	
	//Contact
	$j("#content.order .contact input, #content.order .contact textarea").blur(function(){
		$j("#content.order form p input").removeClass("input-active");
		if($j(this).attr("value") == ""){
			$j(this).addClass("input-error").parent().find("span:last").removeClass("required").addClass("error");
		} else {
			$j(this).removeClass("input-error");
			//EmailValidation
			if($j(this).attr("id") == "c-mail"){
				if(isMail($j(this).attr("value"))){
					$j(this).removeClass("input-error").parent().find("span:last").removeClass().addClass("valid");
				} else {
					$j(this).addClass("input-error").parent().find("span:last").removeClass().addClass("error");
				}
			} else {
				$j(this).removeClass("input-error").parent().find("span:last").removeClass().addClass("valid");
			}
		}
		
	});
	
	
	
	
	//*********************FinalValidation
		$j(".payment .button").click(function(e){
			e.preventDefault();
			$j("#content.order .contact input").each(function(){
				if($j(this).attr("id") == "c-file"){}
				else if($j(this).attr("id") == "c-mail"){
					//EmailValidation
					if(isMail($j(this).attr("value"))){
						$j(this).removeClass("input-error").parent().find("span:last").removeClass().addClass("valid");
					} else {
						$j(this).addClass("input-error").parent().find("span:last").removeClass().addClass("error");
					};
				}
				else{
					if($j(this).attr("value") == ""){
						$j(this).addClass("input-error");
						$j(this).parent().find("span:last").addClass("error");
					} else {
						$j(this).removeClass("input-error");
						$j(this).parent().find("span:last").removeClass("error");
					}
				}
			});
			
			$j("#content.order .contact textarea").each(function(){
				if($j(this).attr("value") == ""){
					$j(this).addClass("input-error");
					$j(this).parent().find("span:last").addClass("error");
				} else {
					$j(this).removeClass("input-error");
					$j(this).parent().find("span:last").removeClass("error");
				}
			});

			
			e.preventDefault();
			
			globalError = 0;
			errorCount = 0;
			errorName = new Array();
			
			//Verification step1
			if($j("td.active").length == 0){
				globalError = 1;
				errorCount++;
				errorName.push("Vous n'avez pas choisi de date de livraison.");
			}
			
			//Verification step3
			if($j("#content.order .contact .error").length > 0){
				globalError = 1;
				$j("#content.order .contact .input-error").each(function(){
					errorCount++;
					switch($j(this).attr("id")){
						case("c-name"):
							errorName.push("Vous n'avez pas renseigné votre nom.");
						break;
						case("c-company"):
							errorName.push("Vous n'avez pas indiqué votre entreprise.");
						break;
						case("c-mail"):
							errorName.push("Vous n'avez pas renseigné votre adresse e-mail ou celle-ci n'est pas valide.");
						break;
						case("c-phone"):
							errorName.push("Vous n'avez pas renseigné votre numéro de téléphone.");
						break;
						case("c-file"):
							errorName.push("Vous n'avez pas envoyé vos fichiers.");
						break;
						case("c-message"):
							errorName.push("Vous n'avez pas écrit de message.");
						break;
						case("c-address"):
							errorName.push("Vous n'avez pas renseigné votre adresse.");
						break;
					}
					
				});
			}
			
			//Affichage Erreurs
			if(globalError == 1){
				$j(".error-dialog .content ul li").remove();
				$j("body > .ui-dialog").remove();
				$j(".error-dialog").dialog({
					width: 500,
					modal: true,
					draggable : true
				});
				for(i=0;i < errorCount;i++){
					$j(".error-dialog .content ul").append("<li>"+errorName[i]+"</li>");
				}
			}
			
			
			if(globalError == 0){
				//WaitingScreen
				$j("#content.order .sending-dialog").dialog({
					width: 500,
					modal: true
				});
				
				if($j(this).parent("div").hasClass("all")){
					$j("#c-totalamount").attr("value",Math.floor(totalSum));
					$j("#c-totalsansaccompte").attr("value",Math.floor(totalSum));
					$j("#c-paymentmethod").attr("value",1);
					
				} else if($j(this).parent("div").hasClass("accompte")){
					$j("#c-totalamount").attr("value",Math.floor((totalSum *30)/100));
					$j("#c-totalsansaccompte").attr("value",Math.floor(totalSum));
					$j("#c-paymentmethod").attr("value",2);
				} else {
					$j("#c-paymentmethod").attr("value",3);
					$j("#content.order form").attr("action","check-confirm.php");
				}
				
				//AjaxSend
				var dataString = $j("#content.order form").serialize();
				
				$j.ajax({
					type: "POST",
					url: "php/baseclientorder.php",
					data : dataString,
					success : function(dataReturn){
						$j(".ui-dialog-titlebar-close").click();
						$j("#content.order form").submit();
					}
				});

			}
			
			
		
		});
		
	
	
	
});