var _ROOT = "http://www.multiforme.com/";

$(document).ready(function() {
	_CORE.iniTelephone();			   
					
	
	$("#soumissionform").validate({
							   
		rules: {
			name: {
				required: true
			},
			email: {
				required: true,
				email: true
			},
			product_type: {
				required: true
			},
			paper_type: {
				required: true
			},
			colors: {
				required: true
			},
			format: {
				required: true
			},
			qty: {
				required: true
			},
			details: {
				required: true
			}
		}

	});
	
	$("#adduserform").validate({
		
		errorPlacement: function(error, element) {
			//alert(element.name);
			var n = jQuery(element).attr('name');
			if(n=='tel1' || n=='tel2' || n=='tel3'){
				jQuery(error).hide();
			}else{
				error.insertAfter(element);
			}
		},
		
		errorElement: "label",
							   
		rules: {
			Firstname: {
				required: true
			},
			Lastname: {
				required: true
			},
			Courriel: {
				required: true,
				email: true
			},
			Password: {
				required: true
			},
			Permissions: {
				required: true
			},
			Rep_type: {
				required: true
			}
		}

	});
	
	$("#addproductoptionsform").validate({
		
		errorElement: "label",
							   
		rules: {
			Type: {
				required: true
			}
		}

	});
	
	$("#loginform").validate({
							   
		rules: {
			username: {
				required: true,
				email: true
			},
			password: {
				required: true
			}
		}

	});
	
	$("#contactform").validate({
							   
		rules: {
			prenom: {
				required: true
			},
			nom: {
				required: true
			},
			telephone: {
				required: true
			},
			courriel: {
				required: true,
				email: true
			}
		}

	});
	
	$("#form-cart").validate({
		
		errorPlacement: function(error, element) {
			//alert(element.name);
			var n = jQuery(element).hasClass('tel');
			if(n){
				jQuery(error).hide();
			}else{
				error.insertAfter(element);
			}
		},
		
		errorElement: "label",
							   
		rules: {
			first_name: {
				required: true
			},
			last_name: {
				/*required: true*/
			},
			address1: {
				required: true
			},
			city: {
				required: true
			},
			state: {
				required: true
			},
			zip: {
				required: true
			},
			
			night_phone_a: {
				required: true,
				digits: true
			},
			night_phone_b: {
				required: true,
				digits: true
			},
			night_phone_c: {
				required: true,
				digits: true
			},
			telephone: {
				required: true
			},
			email: {
				required: true,
				email: true
			}
		}

	});

});

var _USERS = {
	
	del: function(id){
		var b = confirm("Voulez-vous supprimer cet utilisateur ?");
		if(b){
			window.location.href = _ROOT+"php/php_actions.php?action=deleteuser&id="+id;
		}
	}
	
}

var _PRODUCTS = {
	
	del: function(id){
		var b = confirm("Voulez-vous supprimer ce produit ?");
		if(b){
			window.location.href = _ROOT+"php/php_actions.php?action=deleteproduct&id="+id;
		}
	},
	
	_OPTIONS : {
	
		del: function(id,t){
			var b = confirm("Voulez-vous supprimer cet item ?");
			if(b){
				var r = escape(window.location.href);
				window.location.href = _ROOT+"php/php_actions.php?action=deleteproductoptions&tableext="+t+"&id="+id+"&return="+r;
			}
		},
		
		add: function(a, t){
			jQuery('#action').val(a);
			jQuery('#tableext').val(t);
			jQuery('#pricelistform').submit();
		}
	
	}
	
}

var _CORE = {
	
	ini: function(){
		jQuery('.eskape-cart-step-prev').click(function(){_CORE.prevTab();});
		jQuery('.eskape-cart-step-next').click(function(){_CORE.nextTab();});
	},
	
	date: function(){
		jQuery('input.datepicker').datepicker({prevText:'<',nextText:'>'});
	},
	
	iniTelephone: function(){
		var i =0;
		if(jQuery('.telephone').length>0){
				
				var ii = 0;
				var l = jQuery('.telephone input').not(':hidden').length;
				for(ii=0;ii<l;ii++){
					jQuery('.telephone input').not(':hidden').eq(ii).keyup(function(){
						var n = jQuery(this).attr('class');
						n = n.replace(/ ([a-z0-9]*)/gi,'');
						var c = jQuery(this).parent();
						var m = jQuery(this).attr('maxlength');
						if(jQuery(this).val().length == m){
							jQuery(c).find('input').eq(jQuery(this).index() + 1).focus();
						}
						if(jQuery(c).find('input').eq(0).val().length==3&&jQuery(c).find('input').eq(1).val().length==3&&jQuery(c).find('input').eq(2).val().length==4){
							jQuery(c).find('input:hidden').val("("+jQuery(c).find('input').eq(0).val()+") "+jQuery(c).find('input').eq(1).val()+"-"+jQuery(c).find('input').eq(2).val());
						}				   
					});
				}
		}
	},
	
	send: function(f){
		jQuery(f).submit();
	}
	
}

var _PRICELIST = {
	
	_QTY: 0,
	_PRICE:0,
	_MAX:0,
	_MAX2:0,
	_MAX3:0,
	
	update: function(obj){
		var id = obj.id;
		isCheckbox = id.indexOf('_Max')>-1 ? false : true;

		id = id.replace(/_Max/,'');
		obj = $('#'+id);
		obj = obj[0];
		
		var params = obj.value.split("::");
		_PRICELIST._PRICE = parseFloat(params[0]);
		_PRICELIST._QTY = parseFloat(params[1]);
		_PRICELIST._MAX = $('#'+obj.id+'_Max').val();
		_PRICELIST._MAX2 = $('#'+obj.id+'_Max2').val();
		if(_PRICELIST._MAX2 != _PRICELIST._MAX){
			_PRICELIST._MAX3 = _PRICELIST._MAX-_PRICELIST._MAX2;
		}
		_PRICELIST._MAX3 = _PRICELIST._MAX3==0 ? _PRICELIST._MAX : _PRICELIST._MAX3;
		$('#'+obj.id+'_Max2').val(_PRICELIST._MAX)
		
		if(obj.defaultChecked == obj.checked && isCheckbox){
			// decrease
			$('[id^=Price]').each(function(i){
				
				var ip = parseFloat($(this).text());
										   
				if(ip>0){
					if(_PRICELIST._QTY == 0){
						var price = _PRICELIST._MAX>0 ? _PRICELIST._PRICE*_PRICELIST._MAX :  _PRICELIST._PRICE;
						var newprice = (ip-price);
						$(this).text(newprice.toFixed(2)+" $");
						var inp = "addcart_"+$(this).attr('id');
						//$('#'+inp).val(newprice.toFixed(2));
					}else if(_PRICELIST._QTY > 0){
						var params2 = $(this).attr('id');
						params2 = params2.split("_");
						var qty = $("#Qty_"+params2[2]).text();
						var qty2 = parseFloat(qty.replace(/\s/,''));
						var price = _PRICELIST._PRICE * (qty2/_PRICELIST._QTY);
						var newprice = (ip-price);
						$(this).text(newprice.toFixed(2)+" $");
						var inp = "addcart_"+$(this).attr('id');
						//$('#'+inp).val(newprice.toFixed(2));
					}
				}
			});
		}else if(obj.checked && obj.defaultChecked != obj.checked){
			// increase
			$('[id^=Price]').each(function(i){
				
				var ip = parseFloat($(this).text());
										   
				if(ip>0){
					if(_PRICELIST._QTY == 0){
						var price = _PRICELIST._MAX>0 ? _PRICELIST._PRICE*_PRICELIST._MAX3 :  _PRICELIST._PRICE;
						var newprice = (ip+price);
						$(this).text(newprice.toFixed(2)+" $");
						var inp = "addcart_"+$(this).attr('id');
						//$('#'+inp).val(newprice.toFixed(2));
					}else if(_PRICELIST._QTY > 0){
						var params2 = $(this).attr('id');
						params2 = params2.split("_");
						var qty = $("#Qty_"+params2[2]).text();
						var qty2 = parseFloat(qty.replace(/\s/,''));
						var price = _PRICELIST._PRICE * (qty2/_PRICELIST._QTY);
						var newprice = (ip+price);
						$(this).text(newprice.toFixed(2)+" $");
						var inp = "addcart_"+$(this).attr('id');
						//$('#'+inp).val(newprice.toFixed(2));
					}
				}
			});
			
			
		}
		_PRICELIST._PRICE = 0;
		_PRICELIST._QTY = 0;
		_PRICELIST._MAX = 0;
		_PRICELIST._MAX3 = 0;
	}
	
}

var _ORDER = {
	
	updateStatus: function(){
		var I = setInterval('_ORDER.updateStatusEvent()',10000);
	},
	
	updateStatusEvent: function(){
		jQuery.post("/php/php_cart.php", {action:'getstatus'}, function(data2){
			var data = eval(data2);
			if(data[1]=='error'){
				return false;
			}
			_ORDER.updateStatusEventComplete(data);
		});
	},
	updateStatusEventComplete: function(data){
		var arr = ["En attente"," En impression", "Complété", "Complété - livré"];
		for(i in data){
			jQuery('#etat-'+i).html(arr[parseFloat(data[i])-1]);
		}
	},
	
	updateView: function(){
		var I = setInterval('_ORDER.updateViewEvent()',10000);
	},
	updateViewEvent: function(){
		jQuery.post("/php/php_cart.php", {action:'checkorders'}, function(data){
																		/*data = eval(data);
																		if(data[1]=='error'){
																			return false;
																		}
																		*/
																		_ORDER.updateViewEventComplete(data);
																		});
	},
	updateViewEventComplete: function(data){
		jQuery('table.cart-table > tbody > tr').eq(0).after(data);

	},
	
	setStatus: function(obj){
		jQuery.post("/php/php_cart.php", {action:'setstatus',val:jQuery(obj).val(),id:jQuery(obj).attr('id')}, function(data){
																		/*
																		data = eval(data);
																		if(data[1]=='error'){
																			return false;
																		}
																		*/
																		});
	},
	
	del: function(id){
		var c = confirm('Voulez-vous vraiment supprimer cette commande ?');
		if(!c){
			return false;
		}
		jQuery.post("/php/php_cart.php", {action:'deleteinvoice',id:id}, function(data){
																		data = eval(data);
																		if(data[1]=='error'){
																			return false;
																		}
																		jQuery('#'+data[0]).remove();
																		});
	}

}

var _PASSWORD = {
	
	get: function(f, l){
		jQuery.post("php/php_actions.php", {action:'getpwd',field:f,len:l}, function(data){
																		data = eval(data);
																		if(data[1]=='error'){
																			return false;
																		}
																		_PASSWORD.getComplete(data[0]);
																		});
	},
	
	getComplete: function(data){		
		jQuery('#Password').val(data);
		
	}
	
	
}

var _CART = {
	
	send: function(a){
		jQuery('#cmd').val(a);
		jQuery('#form-cart').submit();
		/*
		jQuery.post("php/php_cart.php", jQuery('#form-cart').serialize(), function(data){
																	data = eval(data);
																	if(data[1]=='error'){
																		return false;
																	};
																	var url = data[0];
																	jQuery('#form-cart').attr('ACTION',url);
																	if(url.indexOf('paypal')>-1){
																		jQuery('#form-cart').attr('target','_blank');
																	}
																	jQuery('#form-cart').submit();
																	//_CART.sendComplete();
																	});
		*/
	},
	
	sendComplete: function(){
		//jQuery('#form-cart').submit();
	}
	
}
