var bil_country='';
var shp_country='';
var payment_type='';
var cc_type='';
var data='';
var error=0;
//----------------------------------
function affiliate_send(id) {
	k=$("#all_cost").html();
	$.ajax({
		type:		"GET",
		url:		"./affiliate/sale.php",
		data:		"idev_tracking="+id+"&varname="+k.substring(1),
		beforeSend:	function() {
			alert('This transaction has been approved.'+"\n"+"\n"+'order #'+id);
		},
		success:	function(msg) {
			switch(payment_type) {
				case 'p-cc':
				$("#th-ord").html('<b>'+id+'</b>');
				setTimeout('$("#part-4").fadeIn("slow")',600);
				break;
				case 'p-ml':
				$("#th-ml-ord").html('<b>'+id+'</b>');
				setTimeout('$("#part-5").fadeIn("slow")',600);
				setTimeout('window.print()',2000);
				break;
				case 'p-pp':

				break;
			}
		}
	});
}
//----------------------------------
function copy_bil_to_shp() {
	$("#shp-fn").val($("#bil-fn").val());
	$("#shp-ln").val($("#bil-ln").val());
	$("#shp-adr").val($("#bil-adr").val());
	$("#shp-city").val($("#bil-city").val());
	$("#shp-state").val($("#bil-state").val());
	$("#shp-zip").val($("#bil-zip").val());
	$("#shp-ph").val($("#bil-ph").val());
	$("#shp-eml").val($("#bil-eml").val());
	$("#shp-eml2").val($("#bil-eml2").val());
}
//----------------------------------
function check_data() {
	error=0;
	//-----------------
	if($("#bil-fn").val()=='') {
		error=1;
	}
	if($("#bil-ln").val()=='') {
		error=1;
	}
	if($("#bil-adr").val()=='') {
		error=1;
	}
	if($("#bil-city").val()=='') {
		error=1;
	}
	if($("#bil-state").val()=='') {
		error=1;
	}
	if($("#bil-zip").val()=='') {
		error=1;
	}
	if($("#bil-ph").val()=='') {
		error=1;
	}
	if($("#bil-eml").val()=='') {
		error=1;
	}
	if($("#bil-eml").val()!=$("#bil-eml2").val()) {
		error=1;
	}
	if($("#bil-ccnumber").val()=='') {
		error=1;
	}
	if($("#bil-ccexp").val()=='') {
		error=1;
	}
	if($("#bil-cccvv").val()=='') {
		error=1;
	}
	//-----------------
	if(error==0) {
		if(shp_country!='') {
			if($("#shp-fn").val()=='') {
				error=1;
			}
			if($("#shp-ln").val()=='') {
				error=1;
			}
			if($("#shp-adr").val()=='') {
				error=1;
			}
			if($("#shp-city").val()=='') {
				error=1;
			}
			if($("#shp-state").val()=='') {
				error=1;
			}
			if($("#shp-zip").val()=='') {
				error=1;
			}
			if($("#shp-ph").val()=='') {
				error=1;
			}
			if($("#shp-eml").val()=='') {
				error=1;
			}
			if($("#shp-eml").val()!=$("#shp-eml2").val()) {
				error=1;
			}
			if(error==0) {
			} else {
				alert('Please, fill all the blank shipping fields');
			}
		}
	} else {
		alert('Please, fill all the blank billing fields');
	}
	if(error==0) {
		return true;
	} else {
		return false;
	}
}
//----------------------------------
function make_data() {
	if(error==0) {
		$("#th-name").html($("#bil-fn").val()+" "+$("#bil-ln").val());
		$("#th-adr").html($("#bil-adr").val());
		$("#th-city").html($("#bil-city").val());
		$("#th-state").html($("#bil-state").val());
		$("#th-zip").html($("#bil-zip").val());
		$("#th-cn").html(bil_country);
		$("#th-eml").html($("#bil-eml").val());
		$("#th-subtotal").html(product);
		$("#th-cd").html($("#cd_cost").html());
		$("#th-shipping").html($("#shipping_cost").html());
		$("#th-total").html($("#all_cost").html());


		$("#th-ml-name").html($("#bil-fn").val()+" "+$("#bil-ln").val());
		$("#th-ml-name1").html($("#th-ml-name").html());
		$("#th-ml-adr").html($("#bil-adr").val());
		$("#th-ml-city").html($("#bil-city").val());
		$("#th-ml-state").html($("#bil-state").val());
		$("#th-ml-zip").html($("#bil-zip").val());
		$("#th-ml-cn").html(bil_country);
		$("#th-ml-eml").html($("#bil-eml").val());
		$("#th-ml-subtotal").html(product);
		$("#th-ml-cd").html($("#cd_cost").html());
		$("#th-ml-shipping").html($("#shipping_cost").html());
		$("#th-ml-total").html($("#all_cost").html());
		switch(payment_type) {
			case 'p-cc':
			$("#th-pay").html('Credit Card');
			break;
			case 'p-ml':
			$("#th-ml-pay").html('Mail');
			break;
			case 'p-pp':
			$("#th-pay").html('PayPal');
			break;
		}

		data=
		"&bil_fn="+$("#bil-fn").val()+
		"&bil_ln="+$("#bil-ln").val()+
		"&bil_adr="+$("#bil-adr").val()+
		"&bil_city="+$("#bil-city").val()+
		"&bil_state="+$("#bil-state").val()+
		"&bil_zip="+$("#bil-zip").val()+
		"&bil_cn="+bil_country+
		"&bil_ph="+$("#bil-ph").val()+
		"&bil_eml="+$("#bil-eml").val()+
		"&bil_cctype="+cc_type+
		"&bil_ccnumber="+$("#bil-ccnumber").val()+
		"&bil_ccexp="+$("#bil-ccexp").val()+
		"&bil_cccvv="+$("#bil-cccvv").val()+
		"&shp_fn="+$("#shp-fn").val()+
		"&shp_ln="+$("#shp-ln").val()+
		"&shp_adr="+$("#shp-adr").val()+
		"&shp_city="+$("#shp-city").val()+
		"&shp_state="+$("#shp-state").val()+
		"&shp_zip="+$("#shp-zip").val()+
		"&shp_cn="+shp_country+
		"&shp_ph="+$("#shp-ph").val()+
		"&shp_eml="+$("#shp-eml").val();
		if(document.getElementById("mt-cd").checked==true) {
			data+="&cd="+cd;
			$("#th-pr").html(pr_name+"CD Version");
			$("#th-ml-pr").html(pr_name+"CD Version");
		} else {
			data+="&cd=0";
			$("#th-pr").html(pr_name+"Download Version");
			$("#th-ml-pr").html(pr_name+"Download Version");
		}

		$.ajax({
			type:		"POST",
			url:		"index.php",
			data:		"task=authorize&type="+payment_type+data,
			beforeSend:	function() {
				$("#part-2").hide();
				$("#part-3").show();
			},
			complete:	function() {
				$("#part-3").hide();
			},
			success:	function(msg) {
				if(msg.substring(0,4)=='EMS-') {
					affiliate_send(msg);
				} else {
					if(msg=='error: decline') {
						alert('This transaction has been declined');
					} else {
						alert(msg);
					}
					$("#blok-index-bottom-order").click();
				}
			}
		});
	}
}
//----------------------------------
$(document).ready(function() {
	//-------
	$("#dsp").css('display','');
	//-------
	$("#ertui").hide();
	$("#product_name").html(pr_name);
	$("#product_name_cost").html($("#product_name").html());
	$("#product_price").html('$'+product);
	$("#product_cost").html($("#product_price").html());
	$("#product_total_price").html('$'+product);
	$("#cd_price").html('$'+cd);
	$("#shipping_total_price").html('$'+shipping);
	$("#all_total_price").html('$'+product);
	$("#cd_cost").html('$0.00');
	$("#shipping_cost").html('$0.00');
	//-------
	$("#part-1").fadeIn('slow');
	$("#part-2").hide();
	$("#part-3").hide();
	$("#part-4").hide();
	$("#part-5").hide();
	$("#countries").hide();
	$("#blok-index-bottom-order").hide();
	$("#blok-order-by-phone").hide();
	//-------
	$(".p-method").click(function() {
		id=$(this).attr('id');
		if(document.getElementById(id).checked==true) {
			switch(id) {
				case 'p-pp':
				document.getElementById('p-cc').checked=false;
				document.getElementById('p-ml').checked=false;
				document.getElementById('p-ph').checked=false;
				break;
				case 'p-cc':
				document.getElementById('p-pp').checked=false;
				document.getElementById('p-ml').checked=false;
				document.getElementById('p-ph').checked=false;
				break;
				case 'p-ml':
				document.getElementById('p-pp').checked=false;
				document.getElementById('p-cc').checked=false;
				document.getElementById('p-ph').checked=false;
				break;
				case 'p-ph':
				document.getElementById('p-pp').checked=false;
				document.getElementById('p-cc').checked=false;
				document.getElementById('p-ml').checked=false;
				break;
			}
			/*
			if(id=='p-ph') {
				$("#blok-order-by-phone").fadeIn('slow');
				$("#blok-index-bottom-order").hide();
			} else {
				$("#blok-order-by-phone").hide();
				payment_type=id;
				$("#blok-index-bottom-order").fadeIn('normal');
			}
			*/
			layer_show("blayer");
		} else {
			if(id=='p-ph') {
				$("#blok-order-by-phone").fadeOut('fast');
			} else {
				payment_type='';
				$("#blok-index-bottom-order").fadeOut('fast');
			}
		}
	});
	//-------
	$("#ship_country").change(function() {
		var i=document.getElementById('ship_country');
		shp_country=i[i.selectedIndex].value;
		$("#shp-cn").val(shp_country);
	});
	//-------
	$("#bil_cn").change(function() {
		var i=document.getElementById('bil_cn');
		bil_country=i[i.selectedIndex].value;
	});
	//-------
	$("#mt-cd").click(function() {
		if(document.getElementById("mt-cd").checked==true) {
			$("#countries").fadeIn('fast');
			$("#cd_total_price").html('$'+cd);
			$("#cd_cost").html($("#cd_total_price").html());
			$("#shipping_cost").html($("#shipping_total_price").html());
			$("#all_total_price").html('$'+(product+shipping+cd));
			$("#ship_country").change();
		} else {
			$("#countries").hide();
			$("#cd_total_price").html('');
			$("#all_total_price").html('$'+product);
			$("#cd_cost").html('$0.00');
			$("#shipping_cost").html('$0.00');
		}
	});
	//-------
	$(".mn-itm-c").click(function() {
		id=$(this).attr('id').substring(5);
		if(id=='order') {
			$("#part-1").fadeIn('slow');
			$("#part-2").hide();
			$("#part-3").hide();
			$("#part-4").hide();
			$("#part-5").hide();
		} else {
			window.location=id+'.html';
		}
	});
	//-------
	$("#blok-index-bottom-order").click(function() {
		$("#bil_cn").change();
		$("#all_cost").html($("#all_total_price").html());
		$("#part-1").fadeOut('fast');
		$("#left_img").attr('src','../img/014.png');
		switch(payment_type) {
			case 'p-cc':
			$("#part-2").fadeIn('slow');
			$("#ml_information").hide();
			$("#billing_information").show();
			$("#payment_information").show();
			if(document.getElementById("mt-cd").checked==false) {
				$("#shipping_information").hide();
			} else {
				$("#shipping_information").show();
			}
			break;
			case 'p-ml':
			$("#part-2").fadeIn('slow');
			$("#ml_information").show();
			$("#billing_information").show();
			$("#billing_information_subtitle").hide();
			$("#billing_information_title").html('The following information is required for the completion of your order');
			if(document.getElementById("mt-cd").checked==false) {
				$("#shipping_information").hide();
			} else {
				$("#shipping_information").show();
			}
			$("#payment_information").hide();
			break;
			case 'p-pp':
			$("#part-2").hide();
			$("#itemfrm-invoice").val("EMS-0");
			$("#itemfrm-price").val(product);
			$("#itemfrm-name").val(pr_name);
			$("#itemfrm-shipping").val(shipping);
			if(document.getElementById("mt-cd").checked==true) {
				$("#itemfrm-shipping").val(shipping);
				$("#itemfrm-price").val((product+cd));
			} else {
				$("#itemfrm-shipping").val('0');
				$("#itemfrm-price").val(product);
			}
			$(itemfrm).submit();
			break;
		}
	});
	//-------
	$("#blok-index-bottom-order, #place_order, #blok-submit").mouseover(function() {
		$(this).css('cursor','pointer').css('border','4px solid red');
	});
	$("#blok-index-bottom-order, #place_order, #blok-submit").mouseout(function() {
		$(this).css('border','4px solid green');
	});
	//-------
	$("#place_order").click(function() {
		$("#part-2").fadeOut('fast');
		$("#part-1").fadeIn('slow');
	});
	//-------
	$("#shp-same").click(function() {
		if(document.getElementById("shp-same").checked==true) {
			copy_bil_to_shp();
		}
	});
	//-------
	$(".check-info-cctype").click(function() {
		cc_type=$(this).val();
	});
	//-------
	$("#blok-submit").click(function() {
		switch(payment_type) {
			case 'p-cc':
			if(!document.getElementById("mt-cd").checked) {
				copy_bil_to_shp();
			}
			if(check_data()) {
				if(cc_type=='') {
					alert('Check Card Type');
				} else {
					make_data();
				}
			}
			break;
			case 'p-ml':
			if(!document.getElementById("mt-cd").checked) {
				copy_bil_to_shp();
			}
			$("#bil-ccnumber").val('0000000000000');
			$("#bil-ccexp").val('00/00');
			$("#bil-cccvv").val('000');
			if(check_data()) {
				make_data();
			}
			break;
			case 'p-pp':
			make_data();
			break;
		}
	});
	//-------
	$("#what_is_this").click(function() {
		$("#ertui").fadeIn('slow');
		return false;
	});
	//-------
	$(".layer-x").click(function() {
		$("#ertui").fadeOut('slow');
	});
	$(".layer-x").mouseover(function() {
		$(this).css('cursor','pointer');
	});

	//-------
});
//-----------------------------------
