/* Notes: these function is design to the templates which using ajax technic*/
//add multiple menu item
function addOption(selectId,optionId,txt,val)
{
    var objOption = new Option(txt,val);
    objOption.id = optionId;
    document.getElementById(selectId).options.add(objOption);
}

//clear multiple menu item
function clearOptions(selectId)
{
    var obj;
    obj = document.getElementById(selectId);
    if(obj.options.length>0){
	    for (var i=0; i<obj.options.length; i++){
    		obj.options[i] = null;
    		i--;
    	}
	  }
}

//remove multiple menu item
function removeOptions(selectId)
{
    var obj;
    var sel = false;
    obj = document.getElementById(selectId);
    if(obj.options.length>0){
		  for (i=0;i<obj.options.length;i++){
		  	var current = obj.options[i];
		  	if (current.selected){
		  		sel = true;
		  		obj.options[i] = null;
		  		i--;
		  	}
		  }
	  }
	  if (!sel) alert ('you have not select anything yet!');
}

//menu items copy 
function copyToList(from,to) //from:source to:destination
{
	fromList = document.getElementById(from);
	toList = document.getElementById(to);
  if (toList.options.length > 0 && toList.options[0].value == 'temp')
  {
    toList.options.length = 0;
  }
  var sel = false;
  var tagExisted = false;
  for (i=0;i<fromList.options.length;i++)
  {
    var current = fromList.options[i];
    if (current.selected)
    {
      sel = true;
      txt = current.text;
      val = current.value;
      
      for (j=0;j<toList.options.length;j++)
  		{
  			var tmpOption = toList.options[j];
  			if(tmpOption.value == val){
  				tagExisted = true;
  				break;
  			}
  		}
  		
  		if (tagExisted)
      {
        alert ('this item has been existed!');
        return;
      }
      else{
      	toList.options[toList.length] = new Option(txt,val);
    	}
//      fromList.options[i] = null;
//      i--;
    }
  }
  if (!sel) alert ('you have not select anything yet!');
}

function allSelect() //
{
  List = document.forms[0].chosen;
  if (List.length && List.options[0].value == 'temp') return;
  for (i=0;i<List.length;i++)
  {
     List.options[i].selected = true;
  }
}

//set item value
function setItemValue(form,id,val)
{
    document.forms[form].elements[id].value = val;
}

//assign opener obj item inner html
function assignOpenerItemInnerHTML(form,id,val)
{
//    alert("testt");
		objItem=window.opener.document.getElementById(id);
		objItem.innerHTML = val;
//    alert(window.opener.document.forms[form].elements[id].innerHTML);
//    window.opener.document.forms[form].elements[id].innerHTML = val;
}

//set selected item on a dropdown list
function setSelectedItemByValue(form,id,objValue)
{
  objList=document.getElementById(id);
  if (objList.length<0) return;
	for (i=0;i<objList.length;i++)
	{
   if(objList.options[i].value == objValue){
	   	objList.options[i].selected = true;
   }
	}
}

//set checkbox checked
function checkedItem(form,id)
{
    objBox=document.getElementById(id);
    if(objBox != null){
    	objBox.checked = 'checked';
    }
}

//set checkbox unchecked
function unCheckedItem(form,id)
{
    objBox=document.getElementById(id);
    objBox.checked=false;
}

//set item radio
function setItemRadio(form,id,val)
{
    var radios=document.forms[form].elements[id];
//    alert(radios.length);
    for (var i=0; i < radios.length; i++){
			if(radios[i].value == val){
				radios[i].checked = true;
			}
		}
}

//shift different pages
function GoPage(pageNum,val){
//alert(document.forms['DataListForm1'].elements[pageNum].value);
//	document.forms['DataListForm1'].elements[pageNum].value = val;
//	alert('[Debug1]: ' + document.forms['DataListForm1'].elements['nfDB_next_page'].value);
	DataListForm1.elements[pageNum].value = val;
	DisplayStartProcessing('indicator');
	xajax_LoadDataList(xajax.getFormValues(DataListForm1),1);
}

//display start processing...
function DisplayStartProcessing(tagdiv)
{
    Element.show(tagdiv);
}

//display end processing...
function DisplayEndProcessing(tagdiv)
{
    Element.hide(tagdiv);
}

//set item value
function DebugAlert(val)
{
    alert(val);
}

function SetStylePadding(id,padding){
	objBox=document.getElementById(id);
    objBox.style.padding=padding;
}
