/*$(document).ready(function() {
            			        
  $('a#vbv').live('click', function() {
		var url=$(this).attr("href");
       	 $.fn.colorbox({
			    href:url,								 		
				opacity:"0.20",
				title:"Verified By Visa",
				width:"690", height:"480",
				overlayClose:false,
				iframe:true
		}); 
			return false;
		}); 
		
	$('a#msc').live('click', function() {
		var url=$(this).attr("href");
       	 $.fn.colorbox({
			    href:url,								 		
				opacity:"0.20",
				title:"MasterCard SecureCode",
				width:"690", height:"480",
				overlayClose:false,
				iframe:true
		}); 
			return false;
		}); 
})*/			


function isCheckedById(id) 
{ 
   var checked = $("input[id="+id+"]:checked").length; 
   if (checked == 0) 
   { 
       return "0"; 
   } 
   else 
   { 
        return "1"; 
   } 
}


function changeSort()
{
	var sortBY=$("#sort").val();			
	var page=window.location.href;
	
	//Remove p and sort
	var p=page.indexOf("&sort=");
	if(p==-1)
	{ var newpage=page; }
	else
	{ var newpage=page.substr(0,p); }	
	
	newpage=newpage+"&sort="+sortBY;					
	window.location.href=newpage;			
}



function switch_account(acc)
{
	switch(acc)
	{
		case "login":
			//Hide step_1_register,step_1_guest
				$("#step_1_register").hide();
				$("#step_1_guest").hide();
			//Display step_1_login
				$("#step_1_login").show();
		break;
		case "register":
			//Hide step_1_login,step_1_guest
				$("#step_1_login").hide();
				$("#step_1_guest").hide();
			//Display step_1_register
				$("#step_1_register").show();
		break;
		case "guest":
			//Hide step_1_login,step_1_register
				$("#step_1_register").hide();
				$("#step_1_login").hide();
			//Display step_1_guest
				$("#step_1_guest").show();
		break;
	}
}

//*******************************************//
//************** CART dispatch **************//
//*******************************************//
//************** add2Cart **************//
function add2Cart(tblID,qty)
{
	$.ajax({
	type: "GET",
	dataType: "json",
	url: "cart/dispatch.php",
	data: ({ action:'add',tblID : tblID,qty:qty}),
	beforeSend:function(){
			
	}//start
	,success: function (j) {
		
		var totalItems=j.totalItems;
		var totalPrice=j.totalPrice+" &#8364;";
				
		if (j.msg) {
			$.jGrowl(j.msg, {
				header: 'Λάθος',
				theme:'jGrowl-error'
			});
		}
		else {
			var miniCart="<p><strong>"+totalItems+"</strong> Τεμάχια στο καλάθι</p> <p>Συνολική αξία : <span>"+totalPrice+"</span></p> <p class=\"fpa\">(Οι τιμές συμπεριλαμβάνουν ΦΠΑ)</p>";
			$('#miniCart').html(miniCart);
			//$('#checkout').show();
			$.jGrowl('Το προιόν προστέθηκε επιτυχώς', {
				header: 'Προσθήκη στο Καλάθι'
			});
		}			
			
	}//success
	,complete:function(){
			
	}//complete
			
});//ajax
}

//************** delCartItem **************//
function delCartItem(tblID)
{
	$.ajax({
	type: "GET",
	dataType: "json",
	url: "cart/dispatch.php",
	data: ({ action:'del',tblID : tblID}),
	beforeSend:function(){
			
	}//start
	,success: function (j) {
		
		$('tr#cartItem_' + tblID + "_1" ).remove();
		$('tr#cartItem_' + tblID + "_2" ).remove();
		$('tr#cartItem_' + tblID + "_3" ).remove();
		
		$('#totalBasket').html(j.totalPrice+" &#8364;");
			
		if(j.totalPrice=="") { $('tr#emptyBasket').show(); $('#cartTotals').hide(); $('#go_checkout').hide();	}
		
		
	}//success
	,complete:function(){
			
	}//complete
			
	});//ajax
}


function incCartItem(tblID)
{
	$.ajax({
	type: "GET",
	dataType: "json",
	url: "/cart/dispatch.php",
	data: ({ action:'inc',tblID : tblID}),
	beforeSend:function(){
			
	}//start
	,success: function (j) {	
	
		if (j.msg) {
			$.jGrowl(j.msg, {
				header: 'Λάθος'
			});
		}
		else {
			
			window.location.href=window.location.href;
		}			
		
			
	}//success
	,complete:function(){
			
	}//complete
			
	});//ajax
}

function decCartItem(tblID)
{
	$.ajax({
	type: "GET",
	dataType: "json",
	url: "/cart/dispatch.php",
	data: ({ action:'dec',tblID : tblID}),
	beforeSend:function(){
			
	}//start
	,success: function (j) {		
		window.location.href=window.location.href;	
	}//success
	,complete:function(){
			
	}//complete
			
	});//ajax
}

function delCartItem(tblID)
{
	$.ajax({
	type: "GET",
	dataType: "json",
	url: "/cart/dispatch.php",
	data: ({ action:'del',tblID : tblID}),
	beforeSend:function(){
			
	}//start
	,success: function (j) {		
		window.location.href=window.location.href;		
	}//success
	,complete:function(){
			
	}//complete
			
	});//ajax
}


function clearBasket()
{
	$.ajax({
	type: "GET",
	dataType: "json",
	url: "/cart/dispatch.php",
	data: ({ action:'clearBasket'}),
	beforeSend:function(){
			
	}//start
	,success: function (j) {		
		window.location.href=window.location.href;	
	}//success
	,complete:function(){
			
	}//complete
			
	});//ajax
}

function checkCoupon()
{
	var code=$("#coupon-code").val(); 
	
	$.ajax({
	type: "GET",
	dataType: "json",
	url: "/cart/dispatch.php",
	data: ({ action:'check-coupon',code:code}),
	beforeSend:function(){
			
	}//start
	,success: function (j) {	
	
		if (j.msg) {
			$.jGrowl(j.msg, {
				header: 'Λάθος'
			});
		}
		else {
			
			window.location.href=window.location.href;
		}			
		
			
	}//success
	,complete:function(){
			
	}//complete
			
	});//ajax
}

function delCoupon()
{	
	$.ajax({
	type: "GET",
	dataType: "json",
	url: "/cart/dispatch.php",
	data: ({ action:'del-coupon'}),
	beforeSend:function(){
			
	}//start
	,success: function (j) {	
	
		if (j.msg) {
			$.jGrowl(j.msg, {
				header: 'Λάθος'
			});
		}
		else {
			
			window.location.href=window.location.href;
		}			
		
			
	}//success
	,complete:function(){
			
	}//complete
			
	});//ajax
}



//*******************************************//
//*********** ACCOUNT dispatch *************//
//*******************************************//

//************** customerLogin **************//
function accountLogin(mode)
{
	//Retrive form data
	var u=$("#user_login").val(); 
	var p=$("#user_password").val(); 
	
	$.ajax({
	type: "POST",
	dataType: "json",
	url: "/account/dispatch.php",
	data: ({ action:'login',u:u ,p:p }),
	beforeSend:function(){
			
	}//start
	,success: function (j) {					
		if (j.msg) {
			$.jGrowl(j.msg, {
				header: 'Λάθος'
			});
		}
		else {
			
			if (mode == "login") {
				window.location.href = '/index.php';
			}
			if (mode == "cart") {
				window.location.href = '/checkout/'
			}
		}			
	}//success
	,complete:function(){
			
	}//complete
			
});//ajax
	
}


function accountLogout()
{
	
	$.ajax({
	type: "POST",
	dataType: "json",
	url: "/account/dispatch.php",
	data: ({ action:'logout'}),
	beforeSend:function(){
			
	}//start
	,success: function (j) {					
		 window.location.href='/index.php';		
	}//success
	,complete:function(){
			
	}//complete
			
});//ajax
	
}

function short_register(mode)
{
	//Retrive form data
	var formData=$("#registerForm").serialize(); 
	formData+="&action=short_register";
	
	$.ajax({
	type: "POST",
	dataType: "json",
	url: "/account/dispatch.php",
	data: (formData),
	beforeSend:function(){
			
	}//start
	,success: function (j) {					
		if (j.msg) {
			$.jGrowl(j.msg, {
				header: 'Λάθος'
			});
		}
		else {
			
			if (mode == "register") {
				window.location.href = '/index.php';
			}
			if (mode == "cart") {
				window.location.href = '/checkout/'
			}
		}			
				
	}//success
	,complete:function(){
			
	}//complete
			
	});//ajax
	
}


function edit_register()
{
	//Retrive form data
	var formData=$("#editRegisterForm").serialize(); 
	formData+="&action=edit_register";
	
	$.ajax({
	type: "POST",
	dataType: "json",
	url: "/account/dispatch.php",
	data: (formData),
	beforeSend:function(){
			
	}//start
	,success: function (j) {					
		if(j.msg) { $.jGrowl(j.msg,{ header: 'Λάθος'}); }
		else { window.location.href='/checkout/index.php?step=2'}			
	}//success
	,complete:function(){
			
	}//complete
			
	});//ajax
	
}

function edit_account()
{
	
	var formData=$("#accountForm").serialize(); 
	formData+="&action=edit_account";
	
	$.ajax({
	type: "GET",
	dataType: "json",
	url: "/account/dispatch.php",
	data: (formData),
	beforeSend:function(){
			
	}//start
	,success: function (j) {
		if (j.msg) {
			$.jGrowl('Δεν μπόρεσε να γίνει η αλλαγή των στοιχείων', {
				header: 'Λάθος'
			});
		}
		else
		{	
			$.jGrowl('Τα Στοιχεία σας άλλαξαν επιτυχώς', {
				header: 'Αλλαγή Στοιχείων'
			});
		}	
	}//success
	,complete:function(){
			
	}//complete
			
	});//ajax
}

//*******************************************//
//*********** CHECKOUT dispatch *************//
//*******************************************//
function checkout_dispatch(action)
{
	switch(action) 
	{
		case "shippingMethod":
			shippingMethod();
		break;
		case "processorTpl":
			processorTpl();
		break;
		case "paymentMethod":
			paymentMethod();
		break;
	}
}

function billingAddress()
{	
	var formData=$("#addressForm").serialize(); 
	formData+="&action=billingAddress";
	
	$.ajax({
	type: "GET",
	dataType: "json",
	url: "/account/dispatch.php",
	data: (formData),
	beforeSend:function(){
			 $("#billingAddressForm").trigger("liveLoad");
	}//start
	,success: function (j) {	
		var r=j.r;	
		$("#address").html("");
		$("#addressesHtml").load("/checkout/dispatch.php?action=htmlAddress&r="+r);
	}//success
	,complete:function(){
			
	}//complete
			
	});//ajax
}

function shippingAddress()
{	
	var formData=$("#addressForm").serialize(); 
	formData+="&action=shippingAddress";
	
	$.ajax({
	type: "GET",
	dataType: "json",
	url: "/account/dispatch.php",
	data: (formData),
	beforeSend:function(){
			 $("#billingAddressForm").trigger("liveLoad");
	}//start
	,success: function (j) {
		var r=j.r;	
		$("#address").html("");
		$("#addressesHtml").load("/checkout/dispatch.php?action=htmlAddress&r="+r);
		
	}//success
	,complete:function(){
			 
	}//complete
			
	});//ajax
}

function setShippingAddress()
{
	var shippingAddressID=$(("#shippingAddress input:radio:checked")).val();
	$.ajax({
	type: "GET",
	dataType: "json",
	url: "/checkout/dispatch.php",
	data: ({action:'setShippingAddress',tblID:shippingAddressID}),
	beforeSend:function(){
			
	}//start
	,success: function (j) {	
		
			
	}//success
	,complete:function(){
			
	}//complete
			
	});//ajax
}

function shippingMethod()
{	
	var formData=$("#shippingMethodForm").serialize(); 	
	//alert(formData);
	formData+="&action=shippingMethod"
	
	$.ajax({
	type: "GET",
	dataType: "json",
	url: "https://www.hac.gr/checkout/dispatch.php",
	data: (formData),
	beforeSend:function(){
			
	}//start
	,success: function (j) {	
		window.location.href='/checkout/index.php?step=4'	
	}//success
	,complete:function(){
			
	}//complete
			
	});//ajax
}

function processorTpl()
{

	//GET selected paymentMethod
	var paymentID=$(("#paymentMethodForm input:radio:checked")).val();
	$.ajax({
	type: "POST",
	dataType: "html",
	url: "/checkout/dispatch.php",
	data: ({action:'processorTpl',paymentID:paymentID}),
	beforeSend:function(){
			
	}//start
	,success: function (html) {	
		//put it in processorTpl
		$(("#processorTpl")).html(html);
	}//success
	,complete:function(){
			
	}//complete
			
	});//ajax
}

function paymentMethod()
{	
	var formData=$("#paymentMethodForm").serialize(); 	
	//alert(formData);
	formData+="&action=paymentMethod"
	
	$.ajax({
	type: "POST",
	dataType: "json",
	url: "/checkout/dispatch.php",
	data: (formData),
	beforeSend:function(){
			
	}//start
	,success: function (j) {	
		window.location.href='/checkout/index.php?step=5'	
	}//success
	,complete:function(){
			
	}//complete
			
	});//ajax
}

function change_tax()
{	
	var tax=$("#tax").val(); 

	if(tax==16)
	{
		var r=confirm("Προσοχή! Αγορές με ΦΠΑ 16% απαιτούν έκδοση Τιμολογίου και μπορούν να κάνουν ΜΟΝΟ εταιρίες η ελεύθεροι επαγγελματίες με έδρα παραμεθόριες περιοχές όπως αυτές προβλέπονται από τον ΚΒΣ. Σε κάθε άλλη περίπτωση η παραγγελία σας είναι άκυρη");
	
		if(r==true)
		{
			$.ajax({
				type: "GET",
				dataType: "json",
				url: "/cart/dispatch.php",
				data: ({action:'tax',tax:tax}),
				beforeSend:function(){
						
				}//start
				,success: function (j) {
					if (j.msg) {
						$.jGrowl(j.msg, {
							header: 'Λάθος'
						});
					}
					else
					{	
						window.location.href=window.location.href;	
					}	
				}//success
				,complete:function(){
						
				}//complete
						
				});//ajax
		}
		else
		{
			
		}
	}
	else
	{
			$.ajax({
				type: "GET",
				dataType: "json",
				url: "/cart/dispatch.php",
				data: ({action:'tax',tax:tax}),
				beforeSend:function(){
						
				}//start
				,success: function (j) {
					if (j.msg) {
						$.jGrowl(j.msg, {
							header: 'Λάθος'
						});
					}
					else
					{	
						window.location.href=window.location.href;	
					}	
				}//success
				,complete:function(){
						
				}//complete
						
				});//ajax
	}

}


function change_shipping()
{	
	var shippingMethod=$("#shippingMethod").val(); 

	
			$.ajax({
				type: "GET",
				dataType: "json",
				url: "/cart/dispatch.php",
				data: ({action:'shippingMethod',shippingMethod:shippingMethod}),
				beforeSend:function(){
						
				}//start
				,success: function (j) {
					if (j.msg) {
						$.jGrowl(j.msg, {
							header: 'Λάθος'
						});
					}
					else
					{	
						window.location.href=window.location.href;	
					}	
				}//success
				,complete:function(){
						
				}//complete
						
				});//ajax
	

}

function setInvoice()
{	
	var invoiceType=$(("#invoiceTypeHtml input:radio:checked")).val();
		
	if (invoiceType == 1) {
		$("#invoiceDataHtml").hide();
		$("#invoiceDataForm").hide("");
		$.ajax({
		type: "GET",
		dataType: "json",
		url: "/checkout/dispatch.php",
		data: ({action:'setInvoice',invoiceType:invoiceType}),
		beforeSend:function(){
				
		}//start
		,success: function (j) {	
			
		}//success
		,complete:function(){
				
		}//complete
				
		});//ajax
    }
	
	if (invoiceType == 2 ) {
		
		if($("#invoiceDataForm").is(":hidden"))
		{
			$("#invoiceDataHtml").show();
			$("#invoiceDataForm").load("/checkout/invoice.php");
			$("#invoiceDataForm").show();
		}
	}
	
	
	
}

function invoiceData()
{	
	var formData=$("#invoiceForm").serialize(); 
	formData+="&action=invoiceData";
	
	$.ajax({
	type: "POST",
	dataType: "json",
	url: "/checkout/dispatch.php",
	data: (formData),
	beforeSend:function(){
			 
	}//start
	,success: function (j) {	
		var r=j.r;	
		
		var status=j.status;
		if(status==1)
		{
			$("#invoiceDataHtml").html(j.html);	
			$("#invoiceDataHtml").show();	
			$("#invoiceDataForm").html("");	
		}
		
			
	}//success
	,complete:function(){
			
	}//complete
			
	});//ajax
}

