function intval (mixed_var, base) {
    var tmp;
    var type = typeof( mixed_var );
    if (type === 'boolean') {
        return (mixed_var) ? 1 : 0;
    } else if (type === 'string') {
        tmp = parseInt(mixed_var, base || 10);
        return (isNaN(tmp) || !isFinite(tmp)) ? 0 : tmp;
    } else if (type === 'number' && isFinite(mixed_var) ) {
        return Math.floor(mixed_var);
    } else {
        return 0;
    }
}
var sendFormViaAjax = function(form,loadIn,loader){
	$(loader).show();
	$.ajax({
		type: "POST",
		url: "?page=host_domain_ajax&showtemplate=false",
		data: $(form).serialize(),
		success: function(msg){
			$(loader).hide();
			$(loadIn).html(msg);
		}
	});
}
var validateSearchDomain = function(){
	var valid = true;
	var err   = '';
	if($("#domain_input").val() == ''){
		err = err + "لطفا نام دامنه را وارد کنيد.";
		valid = false;
	}
	if($("input.domainTypeCheckbox:checked").length == 0){
		err = err + "\nشما بايد حداقل يک پسوند انتخاب کنيد.";
		valid = false;
	}
	if(valid == false)
		alert(err);
	return valid;
}
var updateSubmitInput = function(){
	if($(".ml_domain_checkbox").length > 0)
		$("#step1_button").removeAttr('disabled');
	else
		$("#step1_button").attr('disabled','disabled');
}
var step1FormSubmit = function(){
	if($(".ml_domain_checkbox:checked").length == 0)
		return false;
	else
		document.getElementById("ml_moduleform_2").submit();
}
var update_unit_price = function(){
	$.ajax({
		type: "POST",
		url: "?page=host_domain_ajax&q=host_price&host_id="+$("#host_pack").val()+"&showtemplate=false",
		data: "",
		success: function(msg){
			rate = msg / 1;
			year = $("select[name='ml_years']").val() / 1;
			$("#host_rate").val(rate).change();
		}
	});
}
var update_host_packs = function(el){
	$.ajax({
		type: "POST",
		url: "?page=host_domain_ajax&q=host_packs_option&server_type="+$(el).val()+"&showtemplate=false",
		data: "",
		success: function(msg){
			$("#host_pack").html(msg).change();
		}
	});
}
var showHostFeatures = function(){
	$("#loading_host_image").show();
	$.ajax({
		type: "POST",
		url: "?page=host_domain_ajax&q=host_pack_features&pack="+$("#host_pack").val()+"&showtemplate=false",
		data: "",
		success: function(msg){
			$("#host_features").html(msg);
			$("#loading_host_image").hide();
		}
	});
}
var updatePrices = function(){
	var year 			= $("select[name='ml_years']").val() / 1;
	var domainPercent 	= $("#setting_domain_discount_"+year).val() / 1;
	var hostPercent 	= $("#setting_host_discount_"+year).val() / 1;
	
	var thisTotal		= 0;
	
	$("div.domain_item").each(function(i){
		thisDomainPrice 		= $(this).children("input[name='ml_price[]']").val() / 1;
		thisDomainForYears 		= thisDomainPrice * year;
		thisDiscount 			= intval(( thisDomainForYears * domainPercent ) / 100);
		thisTotal 				= thisDomainForYears - thisDiscount;
		
		$("#domain_price_"+i).val(thisDomainForYears);
		$("#domain_discount_"+i).val(thisDiscount);
		/*$("#domain_total_"+i).val(thisTotal);*/
	});
	
	var host_rate 		= $("#host_rate").val() / 1;
	var host_price 		= host_rate * year;
	var host_discount 	= intval(( host_price * hostPercent ) / 100);
	var host_total 		= host_price - host_discount;
	$("#host_price").val(host_price);
	$("#host_discount").val(host_discount);
	/*$("#host_total").val(host_total);*/
	
	var total = 0;
	$("input.input_price").not('#total,#total_discount,#final_total').each(function(i){
		total = total + $(this).val() / 1;
	});
	
	var totalDiscount = 0;
	$("input.input_discount").not('#total,#total_discount,#final_total').each(function(i){
		totalDiscount = totalDiscount + $(this).val() / 1;
	});
	var codeDiscount 	 = $("#giftcode_discount_rate").val() / 1;
	codeDiscount 		+= $("#referal_discount").val() / 1;
	/*var totalDiscount = intval((total * codeDiscount) / 100);*/
	var topay = total - totalDiscount;
	$("#total").val(total);
	$("#total_discount").val(totalDiscount);
	$("#final_total").val(topay);
	
	return true;
}
var enterGiftcode = function(){
	if($("#ml_giftcode").val() == '')
	{
		alert('كد تخفيف را وارد كنيد.');
		return false;
	}
	$("#loading_image").show();
	$.ajax({
		type: "POST",
		url: "?page=host_domain_ajax&q=get_giftcode&code="+$("#ml_giftcode").val()+"&showtemplate=false",
		data: "",
		success: function(msg){
			if(msg == 'err'){
				alert('كد وارد شده اشتباه است.');
				$("#ml_giftcode").val('');
				$("#giftcode_discount_rate").val('0');
			}else{
				$("#giftcode_discount_rate").val(msg);
				updatePrices();
			}
			$("#loading_image").hide();
		}
	});
}
var login = function(form){
	//var data = $(form).serialize();
	//alert(data);
	data = "username="+$("input#ml_username").val()+"&password="+$("input#ml_password").val();
	$("#loading_login").show();
	$.ajax({
		type: "POST",
		url: "?page=host_domain_ajax&q=do_login&showtemplate=false",
		data: data,
		success: function(msg){
			if(msg == 'err'){
				$("#loggedin").val('0').change();
				alert('نام كاربري و يا رمزعبور اشتباه است');
			}else{
				$("#loggedin").val('1').change();
				$("#login_form").html(msg);
			}
			$("#loading_login").hide();
		}
	});
}
var update_submit_step3 = function(){
	if($("#loggedin").val() == '1')
		$("#step3_button").removeAttr('disabled');
	else
		$("#step3_button").attr('disabled','disabled');
}
var remove_billing_item = function(i,t){
	if(!confirm("آيا مطمئن هستيد؟") || $("td.amount").length < 3)
		return false;
	$(t).parent().parent().fadeOut('normal',function(){
		$(this).remove();
		var discountPercent = $("#code_discount").val() / 1;
		var intTotal = 0;
		$("td.amount").each(function(i){
			intTotal = intTotal + ($(this).html() / 1);
		});
		var discounts = (discountPercent * intTotal) / 100;
		var toPay	  = intTotal - discounts;
		$("td#total").html('<b>'+intTotal+'</b>');
		$("b#discount").html(discounts);
		$("b#billing").html(toPay);
		// =========
		if($("td.amount").length < 3)
			$("a.delLink").remove();
		// =========
	});
}
var check_form_step3 = function(t){
	if($("#loggedin").val() != '1'){
		alert('لطفا با استفاده از فرم ورود به حساب خود وارد شويد');
		return false;
	}else
		$(t).parents('form').submit();
}
var renewUpdatePrices = function(){
	var year = $("select[name='ml_years']").val() / 1;
	var total = 0;
	var totalDiscount = 0;
	$("input.domainCheckbox:checked").each(function(){
		dID = $(this).attr('domain_id');
		thisPrice = ($("input#domain_price_"+dID).val() / 1) * year;
		thisDiscount = (thisPrice * ($("input#setting_renew_domain_discount_"+year).val() / 1)) / 100;
		totalDiscount += thisDiscount;
		total += thisPrice;
	});
	thisPrice = ($("input#host_unit_price").val() / 1) * year;
	thisDiscount = (thisPrice * ($("input#setting_renew_host_discount_"+year).val() / 1)) / 100;
	totalDiscount += thisDiscount;
	total += thisPrice;
	//-- Code Discount
	totalDiscount += (total * ($("input#giftcode_percent").val() / 1)) / 100;
	var toPay = total - totalDiscount;
	$("#discounts").val(totalDiscount);
	$("#total").val(total);
	$("#to_pay").val(total - totalDiscount);
}
var enterGiftcodeRenew = function(){
	if($("#ml_giftcode").val() == '')
	{
		alert('كد تخفيف را وارد كنيد.');
		return false;
	}
	$("#loading_image").show();
	$.ajax({
		type: "POST",
		url: "?page=host_domain_ajax&q=get_giftcode&code="+$("#ml_giftcode").val()+"&showtemplate=false",
		data: "",
		success: function(msg){
			if(msg == 'err'){
				alert('كد وارد شده اشتباه است.');
				$("#ml_giftcode").val('');
				$("#giftcode_percent").val('0');
			}else{
				$("#giftcode_percent").val(msg);
				renewUpdatePrices();
			}
			$("#loading_image").hide();
		}
	});
}
var checkRenewForm = function(){
	//ml_submit
	if($("input.domainCheckbox:checked").length == 0){
		$("#ml_submit").attr('disabled','disabled');
		$("div#domain_error").fadeIn();
	}else{
		$("#ml_submit").removeAttr('disabled');
		$("div#domain_error").fadeOut();
	}
}
var _loadWithAjax_do = function(url , data , returnTo , method){
	var thisH = $(returnTo).height();
	if(thisH < 100)
		thisH = 100;
	$(returnTo).fadeOut('normal',function(){
		$(this).html("<br/><br/><div style='text-align:center'><img src='modules/Hosting/images/loading_large.gif'/></div><br/><br/>").css('height',thisH+'px').fadeIn('normal',function(){
			$.ajax({
				type: method,
				url: url,
				data: data,
				success: function(msg){
					$(returnTo).fadeOut('normal',function(){
						$(this).css('height','auto').html(msg).fadeIn('normal');
					});
				}
			});
		});
	});
}
var loadWithAjax = function(el,returnTo){
	if($(el).is('a')){
		var url = $(el).attr('href')+'&showtemplate=false';
		var data = '';
		var method = 'GET';
		_loadWithAjax_do(url , data , returnTo , method);
		if($(el).parent().is('li'))
		{
			//alert('test');
			//alert($(el).parent().html());
			$(el).parent('li').addClass('selected').siblings('li.selected').removeClass('selected');
		}
	}
	
	return false;
};
$(document).ready(function() {
	//$('.datepicker').datepicker();
	if($("div#domain_search_input > input#domain_input").val() != '' && $("div#domain_search_input > input#domain_input").val() != undefined && $("div#domain_search_input > input#domain_input").val() != 'undefined'){
		if(validateSearchDomain() == true)
			sendFormViaAjax("#domain_search","#resultHolder","#loadingImage");
	}
	$("table.pre_billing tr:even td, table.list tr:even td").addClass('odd');
	//$("table.pre_billing tr , table.list tr").hover(function(){ $(this).children('td').css('background-color','#DFF7FF'); } , function(){ $(this).children('td').removeAttr('style'); });
	
	$("a.ajax").live('click',function(){
		return loadWithAjax(this,'#userpage');
	});;
	
});
