$(document).ready(function(){
	$("#btn_subcrible").click(function(event){
								   
		if(document.getElementById('no_bg_captcha').value=='')
		{
			alert('Chưa nhập mã bảo vệ');
			return false;
		}
		
		if(ValidateEmail($("#subcribleemail").val()))
		{
			ajaxload('subcrible', '/resources/regis-newsletter.php?email='+$("#subcribleemail").val()+'&unsubcrible='+$("#unsubcrible").val()+'&captcha='+$("#no_bg_captcha").val());
		}
	});
});

function ValidateEmail(element)
{
	var email = element;
	var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
	if (!filter.test(email))
	{
		alert('Địa chỉ Email không hợp lệ');
		return false;
	}else
	{
		return true;
	}
}

function checkboxStatus(field)
{
	var aa= document.getElementById(field);
	
	for (var i =0; i < aa.elements.length; i++) 
	{
		if(aa.elements[i].checked == true)
		return true;
	}
	
	alert("Bạn phải chọn ít nhất 1 tên miền để đăng ký.");
	return false;
}

function select_box(name, value)
{
	for (i = 0; document.getElementById(name).options[i]; i++)
	{
		if (document.getElementById(name).options[i].value == value)
		{
			document.getElementById(name).options[i].selected = true;
			break;
		}
	}
}

function tabs(tabid, contentid, selectedclass, selectedtab, selectedcontent)
{
	i=1;
	while(document.getElementById(tabid+i))
	{
		$("#"+contentid+i).hide();
		$("#"+tabid+i).removeClass(selectedclass);
		
		i++;
	}
	
	$("#"+selectedtab).addClass(selectedclass) 
	$("#"+selectedcontent).fadeIn();

}

function showclock(hour, minute, second)
{
   var clock = new Array();
   clock[0] = second;
   clock[1] = minute;
   clock[2] = hour;
   
   if(clock[2]<10) hour = '0'+clock[2];
   if(clock[1]<10) minute = '0'+clock[1];
   if(clock[0]<10) second = '0'+clock[0];
   
   document.getElementById('hour').innerHTML = hour;
   document.getElementById('minute').innerHTML = minute;
   
   document.getElementById('hour1').innerHTML = hour;
   document.getElementById('minute1').innerHTML = minute;
   document.getElementById('second1').innerHTML = second;
   
   if(clock[0] >= 59)
   {
		clock[0] = 0;
		clock[1] = clock[1]+1;
		if(clock[1] >= 59)
		{
			clock[1] = 0;
			clock[2] = clock[2]+1;
			if(clock[2] > 23)
			{
				clock[2] = 0;
			}
		}
   }else
   {
	   clock[0] = clock[0] + 1;
   }
   // alert(clock[0]);
   
   window.setTimeout('showclock(' + clock[2] + ', '+ clock[1]+', '+ clock[0] +')', 1000);
}


function whoisDomain(contentId, url_dynamic_page)
{
	$(function(){
		var jContent = $( "#"+contentId );
		$.ajax(
			{
				url: url_dynamic_page,
				type: "get",
				dataType: "html",
				
				error: function(){
					jContent.html( "<p>Server busy</p>" );
				},
				
				beforeSend: function(){
					jContent.html( '<td colspan="4" style="text-align:center"><img src="/ajax-loader.gif" /></td>' );
				},
				
				complete: function(){
					//jContent.html( "<p>Server busy</p>" );
				},
				
				success: function( strData ){
					jContent.html( strData );
				}
			}							
		);
	}
	);
}


function ajaxload(contentId, url_dynamic_page)
{
	$(function(){
		var jContent = $( "#"+contentId );
		$.ajax(
			{
				url: url_dynamic_page,
				type: "get",
				dataType: "html",
				
				error: function(){
					jContent.html( "<p>Server busy</p>" );
				},
				
				beforeSend: function(){
					jContent.html( '<img src="/loading.gif" />' );
				},
				
				complete: function(){
					//jContent.html( "<p>Server busy</p>" );
				},
				
				success: function( strData ){
					jContent.html( strData );
				}
			}							
		);
	}
	);
}
//page = Trang hiện tại
//str = Điều kiện GET hiện tại
//url = link trang xử lý hiện tại.
function timedLocation(page, str, url)
{
	str = str.replace("?", "");
	str = removeSubStr(str, "page");
	var real_link = url + '/?'+str+'&page=' + page;

	setTimeout("location.href='" + real_link + "'" ,1000);
}

function linkTo(view, str, url)
{
	str = str.replace("?", "");
	str = removeSubStr(str, "view");
	location.href= url + '/?'+str+'&view=' + view;		
}

function cart(contentId, menusId, foodId, action, lang, mnfoodId)
{
	$(function(){
		var jContent = $( "#"+contentId );
		$.ajax(
			{
				url: "/tools/cart.php?menus_id=" + menusId + "&food_id=" + foodId + "&action=" + action + "&lang=" + lang + "&group_id=" + mnfoodId,
				type: "get",
				dataType: "html",
				
				error: function(){
					jContent.html( "<p>Server busy</p>" );
				},
				
				beforeSend: function(){
					//jContent.html( '<img src="/ajax-loader.gif" />' );
				},
				
				complete: function(){
					//jContent.html( "<p>Server busy</p>" );
				},
				
				success: function( strData ){
					jContent.html( strData );
				}
			}							
		);
	}
	);
}

function removeSubStr(str_main, str_remove)
{ 
	var arr = str_main.split("&");
	var str = "";
	
	for (x in arr)
	{
	   str_check = ";" + arr[x];
	   
	   if(subStrFind(str_check, ";", "=") == str_remove)
	   {
			arr.splice(x,0);
	   }else if(str!="")
	   {
		  str += "&" + arr[x];
	   }else
	   {
		  str += arr[x];
	   }
	}
	
	return str;
}

function subStrFind(str_main, start_char, end_char)
{
	var arr1 = new Array();
	var arr2 = new Array();
	
	arr1 = str_main.split(start_char, 2);
	arr2 = arr1[1].split(end_char, 2);
	
	return arr2[0];
}

function reloadC () {
		var f = document.getElementById("captcha");
		f.src = f.src + "?" + Math.random();
	}
	
function doSaveAs(){
	if (document.execCommand){
		document.execCommand("SaveAs")
	}
	else {
		alert("Save-feature available only in Internet Exlorer 5.x.")
	}
}

function openWindow(widthm, heightm, url)
{
	screen_w = screen.width;
	w = screen_w/2 - widthm/2;
	windowx = window.open (url, 'result',"location=1,status=1,scrollbars=1,width="+widthm+",height="+heightm);
	windowx.moveTo(w,0);
}

function changewt(id){
    $('/tools/weather.php?id='+id,'noidung');
}

function weather(contentId, id)
{
	$(function(){
		var jContent = $( "#"+contentId );
		$.ajax(
			{
				url: "/tools/weather.php?id="+id,
				type: "get",
				dataType: "html",
				
				error: function(){
					jContent.html( "<p>Server busy</p>" );
				},
				
				beforeSend: function(){
					jContent.html( '<img src="/loading.gif" />' );
				},
				
				complete: function(){
					//jContent.html( "<p>Server busy</p>" );
				},
				
				success: function( strData ){
					jContent.html( strData );
				}
			}							
		);
	}
	);
}
