
function loadJs(paths, important)
{
	var important = important || false;

	if(!$.isArray(paths)) paths = new Array(paths);
	
	$.each(paths, function(key, data){
		var $el = $('head script').filter(function(){
			if(typeof $(this).attr('src') != 'undefined' && $(this).attr('src').match(new RegExp(data+"$", "i"))) return true;
			return false;
		});

		if($el.length == 0)
		{
			$('head').append('<script type="text/javascript" src="' + data + '"></script>');
		}
		else if(important == true)
		{
			document.write('<script type="text/javascript" src="' + data + '"></script>');
		}
	});
}

function loadCss(paths, important)
{
	var important = important || false;

	if(!$.isArray(paths)) paths = new Array(paths);
	
	$.each(paths, function(key, data){
		var $el = $('head link').filter(function(){
			if(typeof $(this).attr('href') != 'undefined' && $(this).attr('href').match(new RegExp(data+"$", "i"))) return true;
			return false;
		});
	
		if($el.length == 0)
		{
			$('head').append('<link type="text/css" rel="stylesheet" href="' + data + '" />');
		}
		else if(important == true)
		{
			document.write('<link type="text/css" rel="stylesheet" href="' + data + '" />');
		}
	});
}

//---- Global Message Box
var globalMsgTimeout;
var globalMsgHtml = '<div id="global_message" class="msg_box" style="position:fixed;top:0;z-index:9999;width:400px;display:none;"><div class="message info"></div></div>';

function showGlobalMsg(msg, msgType, autoHide)
{
	var msg = msg || null;
	var msgType = msgType || 'info';
	var autoHide = autoHide || false;
	
	if($('#global_message').length < 1)
	{
		var $globalMsgEl = $(globalMsgHtml).appendTo('body');
		$globalMsgEl.hover(function(){
			clearTimeout(globalMsgTimeout);
		}, function(){
			hideGlobalMsg();
		});
		
		var bodyWidth = $('body').innerWidth();
		var globalMsgWidth = $globalMsgEl.outerWidth();
		
		// Set center
		$globalMsgEl.css('left', ((bodyWidth/2) - (globalMsgWidth/2)) + 'px');
	}

	clearTimeout(globalMsgTimeout);

	if(msg != null)
	{
		$('#global_message .message:first').attr('class','message').addClass(msgType).html(msg);
	}
	
	$('#global_message').stop(true).show();
	
	if(autoHide == true) hideGlobalMsg();
}

function hideGlobalMsg(delay)
{
	var delay = delay || 5000;
	clearTimeout(globalMsgTimeout);
	globalMsgTimeout = setTimeout(function(){
		$('#global_message').hide();
	}, delay);
}
//---- End Global Message Box


function addDistrictOption($targetEl, provinceId, callback)
{
	var callback = callback || null;
	
	$.ajax({
		'type'		: "GET",
		'dataType'	: "json",
		'url'		: "district.json.php?province_id=" + provinceId,
		'success'	: function(data)
		{
			//---- Check for add first default empty option
			var firstVal = $targetEl.find('option:first').val();
			
			if(firstVal == "")
			{
				var firstOption = $targetEl.find('option:first');
			}
			//---- End Check for add first default empty option
			
			// Clear old option
			$targetEl.empty();
			
			//---- Add first default empty option
			if(typeof firstOption != 'undefined')
			{
				$targetEl.append(firstOption);
			}
			//---- End Add first default empty option

			if(data != null)
			{
				$.each(data, function(key, val){
					$targetEl.append('<option value="'+key+'">'+val+'</option>');
				});
			}
			
			$targetEl.attr('disabled', false);
			
			if(callback != null)
				callback();
		}
	});
}


function addFormButtonClass()
{
	$(document).ready(function(){
		
		//--------- Change form link buttons to buttons
		
		/**
		 * a.submit_form	: onclick parent form submit
		 * a.reset_form		: onclick parent form reset
		 */
		$('form').each(function(){
			//------- Submit form button
			
			var form = $(this);
			
			$(this).find('input').keydown(function(e){
				if (window.event) keycode = window.event.keyCode;
				else if (e) keycode = e.which;

				if(keycode == 13)
				{
					e.preventDefault();
					$(form).submit();
				}
			});

			$(this).find('a.submit_form').click(function(e){
				e.preventDefault();
				
				var msg = $(this).attr('title');

				if(!msg.match(/^\s*$/))
				{
					if($(this).hasClass('confirm'))
					{
						if(!confirm(msg)) return false;
					}
					else if($(this).hasClass('alert'))
					{
						alert(msg);
					}
				}

				var form_action = $(form).attr('action');
				
				if($(this).attr('href') && $(this).attr('href') != '#')
				{
					$(form).attr('action', $(this).attr('href'));
				}
				
				$(form).submit();
				
				$(form).attr('action', form_action);
			});
			//------- End Submit form button

			//------- Reset form button
			$(this).find('a.reset_form').click(function(e){
				e.preventDefault();
				clearForm($(form));
			});
			//------- End Reset form button
		});
		//--------- End Change form link buttons to buttons
		
	});
}


function clearForm(el)
{
    $(el).find(':input').each(function(){
        switch(this.type) {
            case 'password':
            case 'select-multiple':
            case 'select-one':
            case 'text':
            case 'textarea':
                $(this).val('');
                break;
            case 'checkbox':
            case 'radio':
                this.checked = false;
        }
    });
}


function setFormValue(objName, strValue, frmName){
	var frmName = frmName || null;
	if(frmName == null){
		var arrObj = document.getElementsByName(objName);
	}else{
		var arrObj = [document[frmName][objName]];
	}
	for(i=0;i<arrObj.length;i++){
		arrObjCheck = arrObj[i];
		if(arrObjCheck.type == "text" || arrObjCheck.type == "textarea" || arrObjCheck.type == "hidden"){ arrObjCheck.value = strValue;}
		if(arrObjCheck.type == "select-one") for(j=0;j<arrObjCheck.length;j++){ if(arrObjCheck.options[j].value == strValue){ arrObjCheck.options[j].selected = true;}else if(arrObjCheck.options[j].text == strValue){arrObjCheck.options[j].selected = true;}}
		if(arrObjCheck.type == "select-multiple") for(j=0;j<arrObjCheck.length;j++){ if(strValue.indexOf("|"+arrObjCheck.options[j].value+"|")  > -1){ arrObjCheck.options[j].selected = true;}else if(strValue.indexOf("|"+arrObjCheck.options[j].text+"|")  > -1){arrObjCheck.options[j].selected = true;}}
		if(arrObjCheck.type == "radio") if(arrObjCheck.value == strValue){ arrObjCheck.checked = true;}
		if(arrObjCheck.type == "checkbox") if(arrObjCheck.value == strValue){ arrObjCheck.checked = true;}
	}
}


function addSelectBoxTitle(){
	$(document).ready(function(){
		if($.browser.msie)
		{
			$("select").mouseover(function(){
				$(this).find("option").each(function(){
					$(this).attr('title', $(this).text());
				});
			});
		}
	});
}


function hideSelectBox()
{
	if($.browser.msie && $.browser.version.substr(0,1) < 7)
	{
		$('select').each(function(){
			if($(this).css('visibility') == 'inherit' || $(this).hasClass('hideSelectBox'))
			{
				$(this).css('visibility', 'hidden');
				$(this).addClass('hideSelectBox');
			}
		});
	}
}

function showSelectBox()
{
	if($.browser.msie && $.browser.version.substr(0,1) < 7)
	{
		$('select.hideSelectBox').css('visibility', 'visible');
	}
}
