
// Check cate
function checkCate(p){
	var strCatid	= '0-';
	var strCatname	= '';
	$("input[name=name_cate]").each(function()
	{
		if(this.checked){	
			strCatid	+= this.value + '-';
			strCatname	+= this.alt + '-';
		}
	
	});
	
	var strColunm	= '';
	$("input[name=name_colunm]").each(function()
	{
		if(this.checked){	
			strColunm	+= this.value + '-';			
		}
	
	});
	
	
	if(strCatid){
		url	= URL_ROOT + "index.php?page=ajax&action=urlbuild";
		$.post(url, {
			catid: strCatid,
			catname: strCatname,
			colunm: strColunm
			
		},
		function(data){
			
			if(p)
				data	= data.replace(/website-templates/gi,p);
			
			window.location.href	= data;
		});
	}
}	

 
function showTemplate(obj){
	
	$("div.box-tem").fadeOut('slow', function () {
		$("div.box-tem .tem-big img").attr({src:obj.src});
		$("div.box-tem .title-tem-big").text(obj.name);
		$("div.box-tem .view-detail-tem a").attr({href:obj.href});
		$("div.box-tem .tem-big a").attr({href:obj.href});
        $("div.box-tem").fadeIn("slow");		
      });	
	return false;
	
}

function showTemplateImg(obj){	
	$("div.box-tem").fadeOut('slow', function () {
		
		$("div.box-tem .tem-big img").attr({src:obj.img});
        $("div.box-tem").fadeIn("slow");
		//$(".btn-page").addClass('btn-page-active');
		//$(this).addClass('btn-page-active');
		
      });	
	return false;
	
}


function chooseDomain(id){
	if(!id) return false;
		
	$("div.class-domain-register").hide('fast', function () {
		
	  });	
	$("div#"+id).show("slow");	
}

function checkDomain(){
	var dm	= $("#id-iput-domain-name").val();
	if(!dm){
		addError(lang_put_domain_name);	
		return false;
	}
	else{
		removeError();
		var i=0;
		// Send check domain
		var str_result;
		$("input[name=name-ext]").each(function(){
				if(this.checked) i++;
			
		});
		if(!i){
			addError(lang_choose_domain_ext);	
			return false;
		}
		$("#result-domain-result").html('');
		$("input[name=name-ext]").each(function(){
			var domain	= dm + '.' + this.value;
			var id_tmp	= domain;
			var id_tmp	= id_tmp.replace('.','');	
			id_tmp	= id_tmp.replace('.','');
			if(this.checked){
				$("#result-domain-result").html($("#result-domain-result").html() + '<div id="'+id_tmp+'"> <IMG src="'+URL_ROOT+'public/image/ajax-loader-1.gif"> '+domain+'</div><div class="whois" id="whois-'+id_tmp+'" style="display:none"></div>');				
				ajaxCheckDomain(domain, id_tmp);
				
			}
			
		});
	
	}
}

function ajaxCheckDomain(domain, dm){	
	$.post(URL_ROOT + 'plugin/checkdomain.php?domain=' + domain, {
   		
	 },
	function(data){		
		$("#"+dm).html($("#"+dm).html().replace('<IMG src="'+URL_ROOT+'public/image/ajax-loader-1.gif">',''));
		$("#"+dm).html($("#"+dm).html().replace('<img src="'+URL_ROOT+'public/image/ajax-loader-1.gif">',''));
		if(data==1){
			if(typeof list_domain_choose_post != 'undefined'){
				if(list_domain_choose_post.indexOf(',' + domain) != -1)
					$("#"+dm).html('<input type="checkbox" name="name-domain-selected" value="'+domain+'" checked="checked" />' + $("#"+dm).html() + '<a href="javascript:addToCart(\'domain_arr\', \''+domain+'\')">(Add to cart)</a>');
				else
					$("#"+dm).html('<input type="checkbox" name="name-domain-selected" value="'+domain+'" />' + $("#"+dm).html() + '<a href="javascript:addToCart(\'domain_arr\', \''+domain+'\')">(Add to cart)</a>');
			}
			else
				$("#"+dm).html('<input type="checkbox" name="name-domain-selected" value="'+domain+'" />' + $("#"+dm).html()  + '<a href="javascript:addToCart(\'domain_arr\', \''+domain+'\')">(Add to cart)</a>');
		}
		else
			$("#"+dm).html('<img src="'+URL_ROOT+'public/image/checkbox_gray.gif" />' + $("#"+dm).html() + '(<a href="javascript:void(0);" onclick="ajaxWhoIs(\''+domain+'\', \'contentDialog\')" >WhoIs</a>)');
	});	
}

function ajaxWhoIs(domain, dm){
	$('#dialog').dialog('open');				
	$('#dialog').dialog('option', 'title','');
	$('#dialog').dialog('option', 'width', 500);
	$('#dialog').dialog('option', 'position', 'top');
	$('#dialog').dialog('option', 'buttons', { 'Close' : function() { $(this).dialog("close"); } });

	$("#"+dm).html(who_is_loading);
	$("#"+dm).show("slow");
	$.post(URL_ROOT + 'plugin/whois.php?domain=' + domain + '&cmd=getwhois', {
   		
	 },
	function(data){		
		$("#"+dm).css('display','none');
		$("#"+dm).html(domain + '<p></p>' + data);
		$("#"+dm).show("slow");
	});	
}


function addError(str_error, id_error){	
	if(typeof id_error =='undefined')
		id_error	= 'result-error';
	var html_error	= $("#"+id_error).html();

	if(html_error.indexOf(str_error) ==-1){
		$("#"+id_error).html(html_error + '<li>'+str_error+'</li>');
		$("#"+id_error).css('color','red');
		$("#"+id_error).show("slow");
		
	}
} 

function removeError(str_error, id_error){
	
	if(typeof id_error =='undefined')
		id_error	= 'result-error';
	
	if(typeof str_error =='undefined'){
		$("#"+id_error).html('');		
		$("#"+id_error).hide("fast");		
		return false;	
	}
	else if(!str_error){
		$("#"+id_error).html('');		
		$("#"+id_error).hide("fast");		
		return false;
	}
	
	str_error	= '<li>'+str_error+'</li>'
	var html_error	= $("#"+id_error).html();

	if(html_error.indexOf(str_error) !=-1){
		html_error.replace(str_error,'');
		$("#"+id_error).html(html_error);
		$("#"+id_error).css('color','red');
		$("#"+id_error).show("slow");
	}
	
}

function autoShowHide(id){
	var me	= $(id);
	if(me.css('display') == 'none'){
		me.show("slow");	
	}
	else{
		me.hide("fast");	
	}
}
// Check go to page
function check_goto(step){
	switch(step){
		case 'step3':
			var list_hosting	= '';
			$("input[name=name-hosting]").each(function(){
						
				if(this.checked){
					list_hosting	+= ',' + $(this).val();					
				}				
			});
			
			if(list_hosting !='')
				return true;
			
		break;
		
		case 'step4':
			var list_webadmin	= '';
			$("input[name=name-webadmin]").each(function(){
				
				if(this.checked){
					list_webadmin	+= ',' + $(this).val();					
				}
				
			});
			
			if(list_webadmin !='')
				return true;
			
		break;
	
	}
	
	return false;
	
}
// GO TO pages

function goto(step, obj){
	
	switch(step){
		case 'step2':
			
			var list_module	= '';
			$("input[name=name-module]").each(function(){
				
				if(this.checked){
					list_module	+= ',' + $(this).val();					
				}
				
			});
			
			// Save step
			$.post(URL_ROOT + 'index.php?page=cart-online&action=save&step=step1', {
   				list_module_post: list_module
			},
			function(data){				
				window.location.href	= obj.url;	
				
			});
			
		break;
		
		
		case 'step3':
			// Define
			var domain_type	= ''; // domain type
			var list_domain_ext	= ''; // list domain ext
			var list_domain_choose	= ''; // list domain ext
			var domain_name	= '';
			var domainexists_name	= '';
			
			domain_name	= $('input[name=name_domain_name]').val();
			domainexists_name	= $('input[name=name_domainexists_name]').val();			
			domain_type = jQuery('input[name=domain_type]:radio:checked').val();
			if(domain_type == 'without'){
				domain_name	= '';
				list_domain_ext = '';
			}
			
								
			if(domain_type == 'register')
			{
				removeError('','result-error');
				if(domain_name)
				{
					
					
					$("input[name=name-ext]").each(function(){
						
						if(this.checked){
							list_domain_ext	+= ',' + $(this).val();
							
						}
						
					});
					
					var dd	= 0;
					$("input[name=name-domain-selected]").each(function(){
						
						if(this.checked){
							list_domain_choose	+= ',' + $(this).val();
							dd++;
						}
						
					});
					if(!dd){
						addError(lang_chosse_aleat_domain);
						return false;
					}
				}
				else
				{
					addError(lang_put_domain_name);	
					return false;
				}
			}
			
			if(domain_type == 'exists'){				
				removeError('','result-error-exists');
				
				if(!domainexists_name || domainexists_name.indexOf('.') == -1){
					addError(lang_put_domain_name_full_with_ext, 'result-error-exists');
					return false;
				}
				list_domain_ext	= '';
			}
				
			
			// Hosting
			var list_hosting	= '';
			$("input[name=name-hosting]").each(function(){
						
				if(this.checked){
					list_hosting	+= ',' + $(this).val();					
				}				
			});
			
			// Save step
			$.post(URL_ROOT + 'index.php?page=cart-online&action=save&step=step2', {
   				domain_type_post: domain_type,
				list_domain_ext_post: list_domain_ext,
				list_domain_choose_post: list_domain_choose,
				domain_name_post: domain_name,
				domainexists_name_post: domainexists_name,
				list_hosting_post: list_hosting
			},
			function(data){		
				
				// Redirect to step2
				window.location.href	= obj.url;	
				
			});
			
			
		break;
		
		case 'step4':
			var list_webadmin	= '';
			$("input[name=name-webadmin]").each(function(){
				
				if(this.checked){
					list_webadmin	+= ',' + $(this).val();					
				}
				
			});
			
			// Save step
			$.post(URL_ROOT + 'index.php?page=cart-online&action=save&step=step3', {
   				list_webadmin_post: list_webadmin
			},
			function(data){
				// Redirect to step2
				window.location.href	= obj.url;
				
			});
			
		break;
		
		case 'step5':
		
			window.location.href	= URL_ROOT + 'cart-online/payment-website-online/' + obj.code + '.html';
		break;
		
		case 'step6':
		
			window.location.href	= URL_ROOT + 'cart-online/review-cart/' + obj.code + '.html';
		break;
		
		case 'finish':
			// Save step
			$.post(URL_ROOT + 'index.php?page=cart-online&action=save&step=finish', {
   				
			},
			function(data){
				// Redirect to step2
				//window.location.href	= obj.url;
				
			});
			
		break;
		
	}
	
}

function deleteCartItem(param1, param2, id){
	$.post(URL_ROOT + 'index.php?page=cart-online&action=delete', {
		param1: param1,
		param2: param2
	},
	function(data){
		// Redirect to step2
		document.getElementById(id).style.display	= 'none';
		jQuery('.id-sum-price-scroll').html(data);		
		document.getElementById('id-sum-price').innerHTML	= data;
		if(data=='0&nbsp;VND')
			window.location.reload();
		
	});
}

function deleteCart(){
	$.post(URL_ROOT + 'index.php?page=cart-online&action=deleteCart', {
		
	},
	function(data){
		window.location.href	= URL_ROOT;		
	});
}

function viewOrderItem(id, dm){
	$('#dialog').dialog('open');				
	$('#dialog').dialog('option', 'title','#'+id);
	$('#dialog').dialog('option', 'width', 500);
	$('#dialog').dialog('option', 'position', 'top');
	$('#dialog').dialog('option', 'buttons', { "Đóng": function() { $(this).dialog("close"); } });

	$("#"+dm).html('loading...');
	$("#"+dm).show("slow");
	$.post(URL_ROOT + 'index.php?page=customer&action=viewOrderItem', {
			id: id
	},
	function(data){
		$("#"+dm).css('display','none');
		$("#"+dm).html(data);
		$("#"+dm).show("slow");
	});
}

function deleteOrderItem(id, idHidden){
	if(confirm('Bạn muốn xóa đơn hàng?')){
		$.post(URL_ROOT + 'index.php?page=customer&action=deleteOrderItem', {
			id: id
		},
		function(data){
			//document.getElementById(idHidden).style.display	= 'none';
			loadCustpage('index.php?page=customer&action=vieworder','fragment-vieworder');
		});
	}else{
		
	}
	
}

function deleteOrderItemFromInfo(id, idHidden){
	if(confirm('Bạn muốn xóa đơn hàng?')){
		$.post(URL_ROOT + 'index.php?page=customer&action=deleteOrderItem', {
			id: id
		},
		function(data){
			//document.getElementById(idHidden).style.display	= 'none';
			loadCustpage('index.php?page=customer&action=viewinfo','fragment-viewinfo');
		});
	}else{
		
	}
	
}


function addToCart(type, id){
	showMessage({'title':'Thông báo','message':'Đang thêm vào giỏ hàng...','autoClose':false});
	$.post(URL_ROOT + 'index.php?page=cart-online&action=save&step=addToCart', {
		list_hosting_post: id,
		type: type
	},
	function(data){
		showMessage({'title':'Thông báo','message':'Sản phẩm đã thêm vào giỏ hàng','autoClose':true});
	});
	
}




function showMessage(obj){
	obj.w	= obj.w?obj.w:300;
	obj.h	= obj.h?obj.h:150;	
	jQuery('#dialog').dialog('open');				
	jQuery('#dialog').dialog('option', 'title',obj.title);
	jQuery('#dialog').dialog('option', 'width', obj.w);
	jQuery('#dialog').dialog('option', 'height', obj.h);
	jQuery('#dialog').dialog('option', 'resizable', true);
	//jQuery('#dialog').dialog('option', 'position', 'top');
	if(obj.autoClose != true)
		jQuery('#dialog').dialog('option', 'buttons', { "Đóng": function() { jQuery(this).dialog("close"); } });

	jQuery("#contentDialog").html(obj.message);
	jQuery("#contentDialog").show("slow");
	if(obj.autoClose == true)
		setTimeout("jQuery('#dialog').dialog('close');",3000);
}

function showForComment(obj){
	
	jQuery('#dialog').dialog('open');				
	jQuery('#dialog').dialog('option', 'title',obj.title);
	jQuery('#dialog').dialog('option', 'width', 700);
	jQuery('#dialog').dialog('option', 'height', 600);
	jQuery('#dialog').dialog('option', 'resizable', true);
	//jQuery('#dialog').dialog('option', 'position', 'top');
	if(obj.autoClose != true)
		jQuery('#dialog').dialog('option', 'buttons', { "Đóng": function() { jQuery(this).dialog("close"); } });

	jQuery("#contentDialog").html(obj.message);
	jQuery("#contentDialog").show("slow");
	if(obj.autoClose == true)
		setTimeout("jQuery('#dialog').dialog('close');",3000);
}

function htecomUrlBuild(obj){
		
}
/**
*	Ajax functions
*
*/

function ajaxPost(url, idResult, arrResult, callback){
	
	$.post(url, {
   		
	 },
	function(data){
		
		if(typeof(idResult) != "undefined")
		if(idResult)
			document.getElementById(idResult).innerHTML	= data;
	
		if(callback)
			eval(callback);
	});

}

function jsonPost(url, idResult, arrResult, callback){
	
	$.post(url, {   		
	 },
	function(data){
				
		if(data.error){			
			alert(data.error);
			return false;
		}
		
		/*if(data.success == 'update_status'){
			if(typeof(idResult) != "undefined"){
				if(idResult){
					document.getElementById(idResult).innerHTML	= data.active_unactive;		
					if(data.active=='0')
						$("#tr-"+idResult).addClass("unactive");			
					else
						$("#tr-"+idResult).removeClass("unactive");				
				}
			}
			
		}
		
		else*/
		if(typeof(idResult) != "undefined")
		if(idResult)
			document.getElementById(idResult).innerHTML	= data.message;
		
			
		if(callback)
			eval(callback);
	},'json');

}


//** Site Logo/ Watermark Script- (c) Dynamic Drive DHTML code library: http://www.dynamicdrive.com.
//** Available/ usage terms at http://www.dynamicdrive.com
//** v2.0 (April 19th, 09')


var absContent={
	setting: {orientation:4, visibleduration:0, fadeduration:[1000, 500]}, //orientation=1|2|3|4, duration=millisec or 0, fadedurations=millisecs
	offsets: {x:0, y:0},//offset of logo relative to window corner
	logoHTML: '<a href="http://www.dynamicdrive.com" title="Dynamic Drive"><img src="http://i40.tinypic.com/e7xmvm.gif" style="width:50px; height:47px; border:0" /></a>', //HTML for logo, which is auto wrapped in DIV w/ ID="mysitelogo"

	coord: {},

	keepfixed:function(){
		if (!this.cssfixedsupport){
			var $window=jQuery(window)
			var is1or3=/^[13]$/.test(this.setting.orientation)
			var is1or2=/^[12]$/.test(this.setting.orientation)
			var x=$window.scrollLeft() + (is1or3? this.offsets.x : $window.width()-this.$control.width()-this.offsets.x)
			var y=$window.scrollTop() + (is1or2? this.offsets.y : $window.height()-this.$control.height()-this.offsets.y)
			this.$control.css({left:x+'px', top:y+'px'})
		}
	},

	showlogo:function(){
		var mainobj=absContent
		this.$control.animate({opacity:1}, this.setting.fadeduration[0])
		if (this.setting.visibleduration>0){
			setTimeout(function(){
				mainobj.$control.stop().animate({opacity:0}, mainobj.setting.fadeduration[1], function(){
					jQuery(window).unbind('scroll.fixed resize.fixed')
				})
			}, this.setting.visibleduration+this.setting.fadeduration[0])
		}
	},
	
	init:function(obj){
		jQuery(document).ready(function($){
			var mainobj=absContent
			var iebrws=document.all
			mainobj.cssfixedsupport=!iebrws || iebrws && document.compatMode=="CSS1Compat" && window.XMLHttpRequest //not IE or IE7+ browsers in standards mode
			if (mainobj.cssfixedsupport){
				mainobj.coord[(/^[13]$/.test(mainobj.setting.orientation))? 'left' : 'right']=mainobj.offsets.x
				mainobj.coord[(/^[12]$/.test(mainobj.setting.orientation))? 'top' : 'bottom']=mainobj.offsets.y
			}
			mainobj.$control=$('<div id="mysitelogo">'+obj.content+'</div>')
				.css({position:mainobj.cssfixedsupport? 'fixed' : 'absolute', opacity:0})
				.css(mainobj.coord)
				.appendTo('body')
			if (document.all && !window.XMLHttpRequest && mainobj.$control.text()!='') //loose check for IE6 and below, plus whether control contains any text
				mainobj.$control.css({width:mainobj.$control.width()}) //IE6- seems to require an explicit width on a DIV containing text
			mainobj.keepfixed()
			mainobj.showlogo()
			$(window).bind('scroll.fixed resize.fixed', function(){mainobj.keepfixed()})
		})
	}
}