function isEmail( text )
{
	var pattern = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
	var regex = new RegExp( pattern );
	return regex.test( text );
}

function isNumber(field) {
	var re = /^[0-9-'.'-',']*$/;
	if (!re.test(field.value)){
		field.value = field.value.replace(/[^0-9-'.'-',']/g,"");
	}
}
	
function searchOnFocus(field,value)
{
	if(field.value==value){
		field.value = '';
	}
}

function searchOnBlur(field,value)
{
	if(field.value==''){
		field.value = value;
	}
}

function checkAll(fmobj) {
  for (var i=0;i<fmobj.elements.length;i++) 
  {
    var e = fmobj.elements[i];
    if ( (e.name != 'allbox')  && (e.type=='checkbox') && (!e.disabled) ) 
	{
      e.checked = fmobj.allbox.checked;
    }
  }
}

function checkSpecialCharacter(str)
{
  var iChars = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?";
  for (var i = 0; i < str.length; i++) {
	if (iChars.indexOf(str.charAt(i)) != -1) {
	 return false;
	}
  }
  return true;
}

function postAjax(dataString,url,targetId,hidden)
{
	var type		= 	"POST";	
	var	dataType	=	"html";
	loadAjax(type,dataType,dataString,url,targetId,hidden);
}

function getAjax(dataString,url,targetId,hidden)
{
	var type		= 	"GET";	
	var	dataType	=	"html";
	loadAjax(type,dataType,dataString,url,targetId,hidden);
}

function loadAjax(type,dataType,dataString,url,targetId,hidden)
{
	var JQuery = jQuery.noConflict();
	if(hidden==1)
	{
		 JQuery.ajax({
			   type:type,
			   dataType:dataType,
			   url:url,
			   data:dataString,
			   success: function(html){
					jQuery("#"+targetId).css({backgroundColor:"#40B220"});
					jQuery("#"+targetId).css({color:"#FFFFFF"});
					jQuery("#"+targetId).html(html)
					.fadeIn(1500,'')
					.fadeOut(2500,'');
				}
		});
	}
	else{
		  jQuery.ajax({
			   type:type,
			   dataType:dataType,
			   url:url,
			   data:dataString,
			   success: function(html){
					jQuery("#"+targetId).html(html)
				}
			});
		}
}

/*
	Perpose: set value for Element has an ID
*/

function setElementValue(obj_id,value)
{
	document.getElementById(obj_id).value=value;
}

/*
	Get Extension of file
	Input a file
	Affter executive return the extension of a file
	@Author:Nguyen Duc Manh
	@ Email: drducmanh@gmail.com
*/
function getExt(str)
{
	var latdot=str.lastIndexOf(".");
	var len=str.length;
	var temp=""
	for(i=latdot+1;i<str.length;i++)
	{
	  temp+=str.charAt(i);
	}
	temp=temp.toLowerCase();
	return temp;
}

/*
	Javascript function checkImage
	Perpose: Check type of image.
	Example: var imageRestrict="jpg,bmp,png,gif";
			 var imageId=image;
			 <input type='file' id='image' onchange='javascript:checkImage(this.id);' />
	When you change image then call function checkImage('image','jpg,bmp,png,gif');
	@Author: Nguyen Duc Manh
	@ Email: drducmanh@gmail.com
*/

function checkImage(imageId,imageRestrict)
{
	var JQuery = jQuery.noConflict();
	var filename=jQuery('#'+imageId).attr("value");
	if(filename)
	{
		var ext=getExt(filename);
		ext=ext.toLowerCase();
		var temp=imageRestrict.indexOf(ext);
		if(temp<0){
			alert("File dinh dang khong dung!");
			jQuery('#'+imageId).attr("value","");
			return false;
		}	
		else return true;
	}	
}

function fadeIn(target_id)
{
	var JQuery = jQuery.noConflict();
	jQuery('#'+target_id).fadeIn("slow");
}
function fadeOut(target_id)
{
	var JQuery = jQuery.noConflict();
	jQuery('#'+target_id).fadeOut("slow");
}


function saveField(value,url,dataString,targetId,hidden)
{
	loadAjax("GET","html",dataString,url,targetId,hidden);
}


//++++++ chuan hoa string
function trimText(text,text_id)
{
	var len = 	text.length;
	var str	=	"";
	if(len)
	{
		for(i=0;i<len;i++)	
		{
			if(text.charAt(i)==" ")	
				str+= "";	
			else
				str+= text.charAt(i);	
		}
	}
	document.getElementById(text_id).value = str;	
}


/** 
	Add/remove form elements using jQuery
*/	

function addFormField(target_id,inputName,inputId,totalId) {
	var JQuery 	= jQuery.noConflict();
	var total = JQuery("#"+totalId).val();
	
	total++;
	if(total<10){
		list_number	=	"0"+total;		
	}
	else{
		list_number	=	total;		
	}
	JQuery("#"+target_id).append("<p id='row_" + total + "'><label for='"+inputId+"["+total+"]'>" + list_number + "&nbsp;&nbsp;<input type='text' size='30' name='"+inputName+"[]' id='"+inputId+"["+total+"]'  >&nbsp;&nbsp<a href='javascript:void(0);' onClick='removeFormField(\"row_" + total + "\"); return false;'>Xóa</a><p>");
	
	JQuery("#"+totalId).val(total);
	/*JQuery('#'+totalId).highlightFade({
		speed:1000
	});*/
}

function removeFormField(id) {
	var JQuery = jQuery.noConflict();
	JQuery("#"+id).remove();
}


function clear(id)
{
	jQuery("#"+id).val("");
}	

function gotoTop()
{
	jQuery(document).ready(function() {
			jQuery('html, body').animate({scrollTop:0}, 'slow');
		});	
}

function popupCenter(pageURL, title,w,h) 
{
	var left = (screen.width/2)-(w/2);
	var top = (screen.height/2)-(h/2);
	var targetWin = window.open (pageURL, '', 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, copyhistory=no, width='+w+', height='+h+', top='+top+', left='+left);
} 

// Popup images
function openNewImage(file, imgText) {	 	
	if (file.lang == 'no-popup') return;
	picfile = new Image();
	picfile.src =(file.src);
	width=picfile.width;
	height=picfile.height;
	
	if (imgText!='' && height>0) {
		height += 40;
	}
	else if (height==0) {
		height = screen.height;
	}

	winDef = 'status=no,resizable=yes,scrollbars=no,toolbar=no,location=no,fullscreen=no,titlebar=yes,height='.concat(height).concat(',').concat('width=').concat(width).concat(',');
	winDef = winDef.concat('top=').concat((screen.height - height)/2).concat(',');
	winDef = winDef.concat('left=').concat((screen.width - width)/2);
	newwin = open('', '_blank', winDef);

	newwin.document.writeln('<style>a:visited{color:blue;text-decoration:none}</style>');
	newwin.document.writeln('<body topmargin="0" leftmargin="0" marginheight="0" marginwidth="0">');
	newwin.document.writeln('<div style="width:100%;height:100%;overflow:auto;"><a style="cursor:pointer" href="javascript:window.close()"><img src="', file.src, '" title="Click to close this window" border=0></a>');
	if (imgText != '') {
		newwin.document.writeln('<div align="center" style="padding-top:5px;font-weight:bold;font-family:arial,Verdana,Tahoma;color:blue">', imgText , '</div></div>');
	}
	newwin.document.writeln('</body>');
	newwin.document.close();
}

//++++ Smooth popup using Jquery
function jqPopup(title,content)
{
	jQuery.popup.show(title, content);
}

//++++ Tinny Advance config: _skin = default, o2k7
function tinyAdvanceInit(_element,_width,_height,_skin)
{
	tinyMCE.init({
		// General options
		mode : "exact",
		elements : _element,
		theme : "advanced",
		skin : _skin,
		plugins : "pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,wordcount,advlist,autosave,imagemanager",
	
		// Theme options
		theme_advanced_buttons1 : "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,styleselect,formatselect,fontselect,fontsizeselect",
		theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
		theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen",
		theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,pagebreak,restoredraft,|,insertimage",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		theme_advanced_statusbar_location : "bottom",
		theme_advanced_resizing : true,
		// Style formats
		style_formats : [
			{title : 'Bold text', inline : 'b'},
			{title : 'Red text', inline : 'span', styles : {color : '#ff0000'}},
			{title : 'Red header', block : 'h1', styles : {color : '#ff0000'}},
			{title : 'Example 1', inline : 'span', classes : 'example1'},
			{title : 'Example 2', inline : 'span', classes : 'example2'},
			{title : 'Table styles'},
			{title : 'Table row 1', selector : 'tr', classes : 'tablerow1'}
		],
		width:_width,
		height:_height
	});
}

//+++++ Tinny Simple config
function tinySimpleInit(_element,_width,_height,_skin)
{
	tinyMCE.init({
		// General options
		mode : "exact",
		elements : _element,
		theme : "simple",
		skin : _skin,
		width:_width,
		height:_height
	});
}

//++++ Include javascript
function incJS(jsFile)
{
  document.write('<script type="text/javascript" src="'
    + jsFile + '"></scr' + 'ipt>'); 
}


function refreshParent() {
  window.opener.location.href = window.opener.location.href;
  if (window.opener.progressWindow){
    window.opener.progressWindow.close()
  }
}

function setUrlParent(url)
{
	window.opener.location.href = url;
	/*if (window.opener.progressWindow){
		window.opener.progressWindow.close()
	 }*/
}
