//alert('framework.js');
var isIE = /*@cc_on!@*/!1;

//sort
function sort(table,field)
{
	var type;
	//alert(getCookie("sort"));

	if (getCookie("_field") != field)
		type = "asc";
	else if (getCookie("_type") == "asc")
		type = "desc";
	else
		type = "asc";

	setCookie("_table",table);
	setCookie("_field",field);
	setCookie("_type",type);

	//url = url + "&_sort=" + field + "&_type=" + type;
	//url = url + "/_sort," + field + "/_type," + type + "/";


	url = location;
	//alert(url);
	location = url;

	return false;
}

function disableSort()
{
	tags = document.getElementsByTagName("a");

	for(i=0;i<tags.length;i++)
	{
		if (tags[i].className == "sort")
		{
			tags[i].style.color = "gray";
			tags[i].disabled = true;
			tags[i].onclick = function(){return false;}
		}
	}
}

//pager
function setPage(i)
{
	setCookie("_page",i);

	url = new String(location);
	var array = url.split('/');
	var p = array.length - 1;

	if(array[p] == "" && p > 7 && !isNaN(array[p-1]))
	{
		page = array[p];
		res = p-1;
	}
	else
	{
		page = array[p];
		res = p;
	}


	//if(!isNaN(page))
	//{
		var url = "";
		for (var j = 0; j <= res; j++)
		{
			if(j == res)
				array[j] = i;
				
			url = url + array[j] + "/";
		}
	//}
	
	location = url;
	return false;
}

function setPageNext(i)
{
	return setPage(i+1);
}

function setPagePerv(i)
{
	return setPage(i-1);
}

//table
function addRow(table,source,adjust,i)
{
	//source = 1; //source row
	//adjust = 2; //adjust row

	rows = document.getElementById(table).rows;
	source = rows[2];

	if (i == undefined)
		i = document.getElementById(table).rows.length - adjust;

	row = document.getElementById(table).insertRow(i);

	for(var i=0;i<source.cells.length;i++)
	{
		cell = row.insertCell(-1);
		html = source.cells[i].innerHTML;
		html = html.replace(/value=\S+\s/,"value=''");
		html = html.replace(/>.+</,"><"); //for textarea
		cell.innerHTML = html;
	}
}

function deleteRow(table,reserve,adjust,i)
{
	//reserve = 1; //reserve row
	//adjust = 3; //adjust row

	if (i == undefined)
		i = document.getElementById(table).rows.length - adjust;

	if (i == reserve)
		return;

	//alert(i);
	document.getElementById(table).deleteRow(i);
}

function setTableWidth(id,width)
{
	if (id != undefined && id != "")
	{
		table = document.getElementById(id);
		tags = Array(1);
		tags[0] = table;
	}
	else
	{
		tags = document.getElementsByTagName("table");
	}

	for(i=0;i<tags.length;i++)
	{
		table = tags[i];
		table.width = width;
	}
}

function changeRowColor(id,color,type)
{
	if (type == undefined)
		type = 0;

	if (id != undefined && id != "")
	{
		table = document.getElementById(id);
		tags = Array(1);
		tags[0] = table;
	}
	else
	{
		tags = document.getElementsByTagName("table");
	}

	for(i=0;i<tags.length;i++)
	{
		table = tags[i];

		for(var j=1;j<table.rows.length;j++)
		{
			if (j%2 == type)
			{
				table.rows[j].style.backgroundColor = color;
			}
		}
	}
}

//display
function hide(csv)
{
	var array = csv.split(',');
	var obj;
	
	for(i=0;i<array.length;i++)
	{
		obj = getElementByName(array[i]);
		
		if (obj)
		{
			obj.style.display = "none";
		}
	}
}

function toggleDisplay(obj)
{
	if (obj.style.display != "none")
		obj.style.display = "none";
	else
		obj.style.display = "";
}

function toggleColumn(id,csv)
{
	table = document.getElementById(id);
	var array = csv.split(',');

	for(var i=0;i<table.rows.length;i++)
	{
		for(j=0;j<array.length;j++)
		{
			toggleDisplay(table.rows[i].cells[array[j]]);
		}
	}
}

function toggleRow(id,csv)
{
	table = document.getElementById(id);
	var array = csv.split(',');

	for(var i=0;i<table.rows.length;i++)
	{
		for(j=0;j<array.length;j++)
		{
			toggleDisplay(table.rows[array[j]]);
		}
	}
}

function toggleRowById(table_id,id)
{
	table = document.getElementById(table);

	for(var i=0;i<table.rows.length;i++)
	{
		if (table.rows[i].id == id)
		{
			toggleDisplay(table.rows[i]);
		}
	}
}

function toggleElementsByTagName(tag,name,vis)
{
	tags = document.getElementsByTagName(tag);
	for(i=0;i<tags.length;i++)
	{
		if (tags[i] == undefined)
			tags[i].style.display = vis;
		else if (tags[i].name == name)
			tags[i].style.display = vis;
	}
}

//color
function setBackgroundColorByName(name,color)
{
	objs = document.getElementsByName(name);
	if (objs)
	{
		for(i=0;i<objs.length;i++)
		{
			objs[i].style.backgroundColor = color;
		}
	}
}

function setBorderColorByName(name,color,color2)
{
	objs = document.getElementsByName(name);
	if (objs)
	{
		for(i=0;i<objs.length;i++)
		{
			objs[i].style.borderColor = color;
			if (objs[i].type == "select-one" || objs[i].type == "select-multiple")
				objs[i].style.backgroundColor = color2;
		}
	}
}


function setBackgroundColorById(id,color)
{
	obj = document.getElementById(id);
	if (obj)
	{
		obj.style.backgroundColor = color;
	}
}

function setBorderColorById(id,color,color2)
{
	obj = document.getElementById(id);
	if (obj)
	{
		obj.style.borderColor = color;
		if (obj.type == "select-one" || obj.type == "select-multiple")
			obj.style.backgroundColor = color2;
	}
}


//form
function clearForm(form)
{
	objs = form.elements;

	for(i=0;i<objs.length;i++)
	{
		if (objs[i].type == "text")
			objs[i].value = "";
		else if (obj.type == "select-one" || obj.type == "select-multiple")
			objs[i].selectedIndex = 0;
	}
}

function clearFormInputSpace(form)
{
	objs = form.elements;

	for(i=0;i<objs.length;i++)
	{
		if (objs[i].type == "text")
		{
			if (isEmpty(objs[i].value))
				objs[i].value = "";
		}
	}
}

//form - select option
function getSelectOption(id,sep)
{
	if (sep == undefined)
		sep = ",";
	
	var select = document.getElementById(id);
	var array = Array(select.options.length);
	
	for(i=0;i<select.options.length;i++)
	{
		array[i] = select.options[i].value;
	}
	
	return array.join(sep);
}
	
function setSelectOption(id,csv)
{
	var select = document.getElementById(id);
	select.options.length = 0;
	
	if (csv == "")
		return;

	var options = csv.split(',');

	if (options.length > 0 && options[0] == ' ')
		options[0] = '';

	for(i=0;i<options.length;i++)
	{
		//alert(options[i]);
		select.options[i]=new Option(options[i],options[i]);
	}
	select.options.length = options.length;
}

function setSelectOptionById(id,csv)
{
	setSelectOption(id,csv);
}

function selectAllOption(id)
{
  var select = document.getElementById(id);
  for (var i=0; i<select.options.length; i++) 
  {
    select.options[i].selected = true;
  }
}

function selectOption(id,csv)
{
  var select = document.getElementById(id);
  var options = csv.split(',');
  
  for (var i=0; i<select.options.length; i++) 
  {
  	  for(j=0;j<options.length;j++)
  	  {
  	  	  if(select.options[i].value == options[j])
  			select.options[i].selected = true;
  	  }
  }
}


//form - radio button
function setRadio(name,csv)
{
	var options = csv.split(',');

	objs = document.getElementsByName(name);
	for(i=0;i<objs.length;i++)
	{
		for(j=0;j<options.length;j++)
		{
			if (objs[i].type == "radio")
			{
			 	if (objs[i].value == options[j])
					objs[i].checked = true;
			}
		}
	}
}

function setRadioByName(name,csv)
{
	setRadio(name,csv);
}


function isRadioChecked(name)
{

	objs = document.getElementsByName(name);
	for(i=0;i<objs.length;i++)
	{
		if (objs[i].type == "radio")
		{
			if (objs[i].checked)
				return true;
		}
	}

	return false;
}

//form - checkbox
function checkAll(form,name)
{
	if (!form) return;

	objs = form.elements;

    for(i=0;i<objs.length;i++)
	{
		if (objs[i].type == "checkbox")
		{
			if (name == undefined || name == objs[i].name)
			{
				if (objs[i].checked == false)
				{
					objs[i].checked = true;
					if (objs[i].onclick)
						objs[i].onclick();
				}
			}
		}
	}
}

function uncheckAll(form,name)
{
	if (!form) return;

    objs = form.elements;

    for(i=0;i<objs.length;i++)
	{
		if (objs[i].type == "checkbox")
		{
			if (name == undefined || name == objs[i].name)
				objs[i].checked = false;
		}
	}
}

function setCheckbox(name,csv)
{
	var options = csv.split(',');

	objs = document.getElementsByName(name);
	for(i=0;i<objs.length;i++)
	{
		for(j=0;j<options.length;j++)
		{
			if (objs[i].type == "checkbox" && objs[i].value == options[j])
				objs[i].checked = true;
		}
	}
}


function setCheckboxByName(name,csv)
{
	setCheckbox(name,csv);
}


function setHiddenByCheckbox(hidden,checkbox)
{
	hiddens = document.getElementsByName(hidden);
	checkboxs = document.getElementsByName(checkbox);

	for(i=0;i<checkboxs.length;i++)
	{
		if (checkboxs[i].checked)
		{
			hiddens[i].value = checkboxs[i].value;
		}
	}
}

function checkForm(color,csv)
{
	if(check_field(color,csv))
		return true;
	else
		return false;
}

function check_field(color,csv)
{
	var arr = csv.split(',');
	for(i=0;i<arr.length;i++)
	{
		obj = getElementByName(arr[i]);
		if(obj.value == "")
		{
			obj.style.backgroundColor = color;
			var error=true;
		}
		else
			obj.style.backgroundColor = '';
	}

	if(error)
		return false;
	else
		return true;

}


//string
function trim(s)
{
	if(s == "")
		return s;

	var str = String(s);
	str = str.replace(/^\s+/, "");
	str = str.replace(/\s+$/, "");

	return str;
}

function isEmpty(s)
{
	return s.match(/^\s*$/);
}

//css
function changeCssStyle(theClass,element,value)
{
	var cssRules;
	if (document.all)
	{
		cssRules = 'rules';
	}
	else if (document.getElementById)
	{
		cssRules = 'cssRules';
	}

	for (var i = 0; i < document.styleSheets.length; i++)
	{
		for (var j = 0; j < document.styleSheets[i][cssRules].length; j++)
		{
			if (document.styleSheets[i][cssRules][j].selectorText == theClass)
			{
				document.styleSheets[i][cssRules][j].style[element] = value;
			}
		}
	}
}

//array
function inArray(need,array)
{
	for (i=0;i<array.length;i++)
	{
		if (need == array[i])
			return true;
	}
	return false;
}

//get value
function getElementsByTagName(tag)
{
	return document.getElementsByTagName(tag);
}

function getElementsByName(name)
{
	return document.getElementsByName(name);
}

function getElementById(id)
{
	return document.getElementById(id);
}

function getElementsByClass(css)
{
    var objs = new Array();
    var els = document.getElementsByTagName("*");
    var elsLen = els.length;
    var pattern = new RegExp("\b"+css+"\b");
    for (i = 0, j = 0; i < elsLen; i++)
    {
         if ( pattern.test(els[i].className) )
         {
             objs[j] = els[i];
             j++;
         }
    }
    return objs;
}

function getElementByName(name)
{
	objs = document.getElementsByName(name);
	if (objs)
		return objs[0];
	else
		return null;
}

function getValueByName(name)
{
	obj = getElementByName(name);

	if (obj)
		return obj.value;
	else
		return null;
}

function getValueById(id)
{
	obj = getElementById(id);

	if (obj)
		return obj.value;
	else
		return null;
}

//set value
function setNameValue(name,value)
{
	objs = document.getElementsByName(name);
	for(i=0;i<objs.length;i++)
	{
		objs[i].value = value;
	}
}

function setIdValue(id,value)
{
	obj = document.getElementById(id);
	if (obj)
	{
		obj.value = value;
	}
}

function setValueByName(name,value)
{
	getElementByName(name).value;
}

function setValueById(id,value)
{
	getElementById(id).value = value;
}

//copy value
function copyValueByName(source,target)
{
	getElementByName(target).value = getElementByName(source).value;
}

function copyValueById(source,target)
{
	getElementById(target).value = getElementById(source).value;
}

//calendar
function calendar(obj)
{
	//dhtml
	popUpCalendar(obj, obj, 'yyyy-mm-dd',-1,-1);
}

function inCSV(name,csv)
{
	var array = csv.split(',');

	for(i=0;i<array.length;i++)
	{
		if(name == array[i])
			return true;
	}
	return false;
}

function isRole(role,role_csv)
{
	return inCSV(role,role_csv);
}

function assignValue(name,value)
{
	if (name.match(/\w+\[\d+\]/))
	{
		var array = name.match(/(\w+)\[(\d+)\]/);

		/*
		alert(array[0]);
		alert(array[1]);
		alert(array[2]);
		*/

		name = array[1];
		i = array[2];

		objs = getElementsByName(name + "[]");
		obj = objs[i];
	}
	else
	{
		obj = getElementByName(name);
	}

	if (obj)
	{
		if (obj.type == "text")
		{
	       	obj.value = value;
		}
		else if (obj.type == "checkbox")
		{
			obj.checked = true;
		}
		else if (obj.type == "select-one" || obj.type == "select-multiple")
		{
			obj.value = value;

			if (obj.value == "" && value != "")
			{
				obj.add(new Option(value,value));
				obj.value = value;
			}
		}
		else if (obj.type  == "textarea")
		{
			obj.value = value;
		}
	}

}
function get_quick_search_id()
{
	var search_id = document.getElementById('quick_search_id').value;
	return(search_id);
}
/*
function import(path,type,title){
var s,i;
if(type=="js"){
var ss=document.getElementsByTagName("script");
for(i=0;i<ss.length;i++){
if(ss[i].src && ss[i].src.indexOf(path)!=-1)return;
}
s=document.createElement("script");
s.type="text/javascript";
s.src=path;
}else if(type=="css"){
var ls=document.getElementsByTagName("link");
for(i=0;i<ls.length;i++){
if(ls[i].href && ls[i].href.indexOf(path)!=-1)return;
}
s=document.createElement("link");
s.rel="alternate stylesheet";
s.type="text/css";
s.href=path;
s.title=title;
s.disabled=false;
}
else return;
var head=document.getElementsByTagName("head")[0];
head.appendChild(s);
}
*/
