// Sniffer based on http://www.mozilla.org/docs/web-developer/sniffer/browser_type.html
var uagent    = navigator.userAgent.toLowerCase();
var is_safari = ( (uagent.indexOf('safari') != -1) || (navigator.vendor == "Apple Computer, Inc.") );
var is_opera  = (uagent.indexOf('opera') != -1);
var is_webtv  = (uagent.indexOf('webtv') != -1);
var is_ie     = ( (uagent.indexOf('msie') != -1) && (!is_opera) && (!is_safari) && (!is_webtv) );
var is_ie4    = ( (is_ie) && (uagent.indexOf("msie 4.") != -1) );
var is_moz    = ( (navigator.product == 'Gecko')  && (!is_opera) && (!is_webtv) && (!is_safari) );
var is_ns     = ( (uagent.indexOf('compatible') == -1) && (uagent.indexOf('mozilla') != -1) && (!is_opera) && (!is_webtv) && (!is_safari) );
var is_ns4    = ( (is_ns) && (parseInt(navigator.appVersion) == 4) );
var is_kon    = (uagent.indexOf('konqueror') != -1);
var is_win    =  ( (uagent.indexOf("win") != -1) || (uagent.indexOf("16bit") !=- 1) );
var is_mac    = ( (uagent.indexOf("mac") != -1) || (navigator.vendor == "Apple Computer, Inc.") );
var ua_vers   = parseInt(navigator.appVersion);

// catch possible bugs with WebTV and other older browsers
var is_regexp = (window.RegExp) ? true : false;

function fetch_object(idname)
{
	if (document.getElementById)
	{
		return document.getElementById(idname);
	}
	else if (document.all)
	{
	
		return document.all[idname];
	}
	else if (document.layers)
	{
		return document.layers[idname];
	}
	else
	{
		return null;
	}
}


function expand_height(boxid, pixelvalue)
{
	var box = fetch_object(boxid);
	var boxheight = parseInt(box.style.height);
	var newheight = boxheight + pixelvalue;
	if (newheight > 0)
	{
		box.style.height = newheight + "px";
	}
	return false;
}
function enable_check(obj, handle)
{
	handle.disabled = (obj.checked == true) ? false : true;    
}

function XMLDecode(str)
{
     //str = str.replace("&amp;","&");
     str = str.replace("&lt;","<");
     str = str.replace("&gt;",">");
     str = str.replace("&apos;","'");
     str = str.replace("&quot;","\"");
     return str;
}

function delete_list(the_url){
	if (selected_item()){
		var question = confirm_submit(delete_question)
		if (question == true){
			the_form.action = the_url;
			the_form.submit();
		}
	}
}

function updateForm(the_url){
	if (selected_item()){
		the_form.action = the_url;
		the_form.submit();
	}
}

function selected_item(){
	var name_count = the_form.length;
	for (i=0;i<name_count;i++){
		if (the_form.elements[i].checked){
			return true;
		}
	}
	alert(choose_one);
	return false;
}


function checkMaxchar(field, maxlength) 
{
	if (field.value.length > maxlength) 
	{
		alert("Bạn chỉ được nhập tối đa là " + maxlength + " ký tự trong phần này!");
		return false;
	}
	return true;
}
function checknumber(x)
{
	var anum=/(^\d+$)|(^\d+\.\d+$)/;
	if (anum.test(x))
	{
		testresult=true;
	}
	else
	{
		testresult=false;
	}
	return (testresult)
}

function checkEmail(field) 
{ 
	var str = field.value; 
	var pattern = /^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+/; 
	var flag = pattern.test(str); 
 	if(!flag) 
 	{ 
 	 	return false; 
 	} 
 return true; 
}

function isEmail(email)
{
	// Valid format: "a@b.cd"
	invalidChars = " /;,:";
	if (email=="")
	{
		return false;
	}
	for (var i=0; i<invalidChars.length; i++)
	{
		badChar=invalidChars.charAt(i);
		if (email.indexOf(badChar,0) > -1)
		{
			return false;
		}
	}
	atPos = email.indexOf("@",1);
	// there must be one "@" symbol
	if (atPos == -1)
	{
		return false;
	}
	if (email.indexOf("@", atPos+1)!=-1)
	{
		// and only one "@" symbol
		return false;
	}
		periodPos = email.indexOf(".",atPos)
	if (periodPos == -1)
	{
		// and at least one "." after the "@"
		return false;
	}
	if (atPos+2>periodPos)
	{
		// and at least one character between "@" and "."
		return false;
	}
	if (periodPos+3>email.length)
	{
		return false;
	}
	return true;
}

function check_url(field)
{
	var is_protocol_ok=field.value.indexOf('http://');
	if (is_protocol_ok==-1)
 	{ 
		return false;
 	}
	return true;
}

function isUrl(s) {
 	var url = /http:\/\/[A-Za-z0-9\.-]{3,}\.[A-Za-z]{3}/;
 	return url.test(s);
}

