var NS4 = (navigator.appName == "Netscape" && parseInt(navigator.appVersion) < 5);

function addOption(theSel, theText, theValue)
{
  var newOpt = new Option(theText, theValue);
  var selLength = theSel.length;
  theSel.options[selLength] = newOpt;
}

function deleteOption(theSel, theIndex)
{ 
  var selLength = theSel.length;
  if(selLength>0)
  {
    theSel.options[theIndex] = null;
  }
}

function moveOptions_section(theSelFrom, theSelTo)
{
	
	var selLength = theSelFrom.length;
	var selectedText = new Array();
	var selectedValues = new Array();
	var selectedCount = 0;
	
	var i;
	var j = 0;
	// Find the selected Options in reverse order
	// and delete them from the 'from' Select.
	for(i=selLength-1; i>=0; i--)
	{
		if(theSelFrom.options[i].selected)
		{    //displayinfo[i]=new Array(destList.options[count].value,window.document.forms[0].section_id.value,destList.options[count].text);
			scriptAr[j++]=new Array(theSelFrom.options[i].value,1,theSelFrom.options[i].text,'#',1,1);
	  		selectedText[selectedCount] = theSelFrom.options[i].text;
	  		selectedValues[selectedCount] = theSelFrom.options[i].value;
	  		deleteOption(theSelFrom, i);
	  		selectedCount++;
		}
	}
	
	// Add the selected text/values in reverse order.
	// This will add the Options to the 'to' Select
	// in the same order as they were in the 'from' Select.
	for(i=selectedCount-1; i>=0; i--)
	{
		addOption(theSelTo, selectedText[i], selectedValues[i]);
	}
	
	if(NS4) history.go(0);
}

///////////////////////
function setValue() 
{ 	
	document.forms[0].pagearr.value = document.forms[0].pagearr.value + scriptAr.toString() + '|';
} 

function moveOptions_subsection(theSelFrom, theSelTo,ctype)
{
	var selLength = theSelFrom.length;
	var selectedText = new Array();
	var selectedValues = new Array();
	var selectedCount = 0;
	
	var i;
	var j = 0;
	var parent, ptype;
	// Find the selected Options in reverse order
	// and delete them from the 'from' Select.
	for(i=selLength-1; i>=0; i--)
	{
		if(theSelFrom.options[i].selected)
		{    //displayinfo[i]=new Array(destList.options[count].value,window.document.forms[0].section_id.value,destList.options[count].text);
			parent=document.forms[0].src_section.options[document.forms[0].src_section.selectedIndex].value;
			ptype = viewtype1(parent);
			if(ptype>=ctype){
				alert("The parent page type must be higher.");
				return false;
			}
			scriptAr[j++]=new Array(theSelFrom.options[i].value,parent,theSelFrom.options[i].text,'#',ctype,1);
			selectedText[selectedCount] = theSelFrom.options[i].text;
			selectedValues[selectedCount] = theSelFrom.options[i].value;
			deleteOption(theSelFrom, i);
			selectedCount++;
		}
	}
  
  // Add the selected text/values in reverse order.
  // This will add the Options to the 'to' Select
  // in the same order as they were in the 'from' Select.
	for(i=selectedCount-1; i>=0; i--)
	{
		addOption(theSelTo, selectedText[i], selectedValues[i]);
	}
  
  	if(NS4) history.go(0);
}

function moveOptions_listpage(theSelFrom, theSelTo,ctype)
{

	var selLength = theSelFrom.length;
	var selectedText = new Array();
	var selectedValues = new Array();
	var selectedCount = 0;
	
	var i;
	var j = 0;
	var parent, ptype;
	// Find the selected Options in reverse order
	// and delete them from the 'from' Select.
	for(i=selLength-1; i>=0; i--)
	{
		if(theSelFrom.options[i].selected)
		{    //displayinfo[i]=new Array(destList.options[count].value,window.document.forms[0].section_id.value,destList.options[count].text);
	
			parent=document.forms[0].src_subsection.options[document.forms[0].src_subsection.selectedIndex].value;	
			ptype = viewtype1(parent);
			if(ptype>=ctype){
				alert("The parent page type must be higher.");
				return false;
			}
			scriptAr[j++]=new Array(theSelFrom.options[i].value,parent,theSelFrom.options[i].text,'#',ctype,1);
			selectedText[selectedCount] = theSelFrom.options[i].text;
	  		selectedValues[selectedCount] = theSelFrom.options[i].value;
	  		deleteOption(theSelFrom, i);
	  		selectedCount++;
		}
	}
	
	// Add the selected text/values in reverse order.
	// This will add the Options to the 'to' Select
	// in the same order as they were in the 'from' Select.
	for(i=selectedCount-1; i>=0; i--)
	{
		addOption(theSelTo, selectedText[i], selectedValues[i]);
	}
	
	if(NS4) history.go(0);
}



function moveOptions_content(theSelFrom, theSelTo,ctype)
{	
	var selLength = theSelFrom.length;
	var selectedText = new Array();
	var selectedValues = new Array();
	var selectedCount = 0;
	
	var i;
	var j = 0;
	var parent, ptype;
	// Find the selected Options in reverse order
	// and delete them from the 'from' Select.
	for(i=selLength-1; i>=0; i--)
	{
		if(theSelFrom.options[i].selected)
		{    //displayinfo[i]=new Array(destList.options[count].value,window.document.forms[0].section_id.value,destList.options[count].text);
			parent=document.forms[0].src_listpages.options[document.forms[0].src_listpages.selectedIndex].value;	
			ptype = viewtype1(parent);
			if(ptype>=ctype){
				alert("The parent page type must be higher.");
				return false;
			}
			scriptAr[j++]=new Array(theSelFrom.options[i].value,parent,theSelFrom.options[i].text,'#',ctype,1);
	  		selectedText[selectedCount] = theSelFrom.options[i].text;
			selectedValues[selectedCount] = theSelFrom.options[i].value;
			deleteOption(theSelFrom, i);
			selectedCount++;
		}
	}
	
	// Add the selected text/values in reverse order.
	// This will add the Options to the 'to' Select
	// in the same order as they were in the 'from' Select.
	for(i=selectedCount-1; i>=0; i--)
	{
		addOption(theSelTo, selectedText[i], selectedValues[i]);
	}
	
	if(NS4) history.go(0);
}



function moveOptions(theSelFrom, theSelTo)
{

  var selLength = theSelFrom.length;
  var selectedText = new Array();
  var selectedValues = new Array();
  var selectedCount = 0;
  
  var i;

  // Find the selected Options in reverse order
  // and delete them from the 'from' Select.
  for(i=selLength-1; i>=0; i--)
  {
    if(theSelFrom.options[i].selected)
    {    //displayinfo[i]=new Array(destList.options[count].value,window.document.forms[0].section_id.value,destList.options[count].text);

      selectedText[selectedCount] = theSelFrom.options[i].text;
      selectedValues[selectedCount] = theSelFrom.options[i].value;
      deleteOption(theSelFrom, i);
      selectedCount++;
    }
  }
  
  // Add the selected text/values in reverse order.
  // This will add the Options to the 'to' Select
  // in the same order as they were in the 'from' Select.
  for(i=selectedCount-1; i>=0; i--)
  {
    addOption(theSelTo, selectedText[i], selectedValues[i]);
  }
  
  if(NS4) history.go(0);
}
//************************************************************************************
function Initselections(){ 
	//Initialize  exist structure
	var temp=new Array();
	var v,sec_parent,sectionid,subsectionid,listpagesid;
	sec_parent=1; 
	for (v=0; v<displayinfo.length; v++){
		temp=displayinfo[v];
		if(temp[4]==2) { sectionid=temp[0];   break; }
	}
        
	for (v=0; v<displayinfo.length; v++){
		temp=displayinfo[v];
		if((temp[4]==3) &&(temp[1]==sectionid) ) { subsectionid=temp[0];  break; }
	}

	for (v=0; v<displayinfo.length; v++){
		temp=displayinfo[v];
		if((temp[4]==4) &&(temp[1]==subsectionid) ) { listpagesid=temp[0];   break; }
	}

	fillSelectFromArray_byparent(window.document.forms[0].src_section,displayinfo,sec_parent);
	if(window.document.forms[0].src_section.options[0]){
		window.document.forms[0].src_section.options[0].selected=true;
		viewtype(window.document.forms[0].src_section.options[0].value,'div1');
	}
	else
		cleartype('div1');
	
	fillSelectFromArray_byparent(window.document.forms[0].src_subsection,displayinfo,sectionid);
	if(window.document.forms[0].src_subsection.options[0]){
		window.document.forms[0].src_subsection.options[0].selected=true;
		viewtype(window.document.forms[0].src_subsection.options[0].value,'div2');
	}
	else 
		cleartype('div2');
	
	fillSelectFromArray_byparent(window.document.forms[0].src_listpages,displayinfo,subsectionid);
	if(window.document.forms[0].src_listpages.options[0]) {
		window.document.forms[0].src_listpages.options[0].selected=true;
		viewtype(window.document.forms[0].src_listpages.options[0].value,'div3');
	}
	else 
		cleartype('div3');
	
	fillSelectFromArray_byparent(window.document.forms[0].src_contents,displayinfo,listpagesid);
	if(window.document.forms[0].src_contents.options[0]){
		window.document.forms[0].src_contents.options[0].selected=true;
		viewtype(window.document.forms[0].src_contents.options[0].value,'div4');
	}
	else 
		cleartype('div4');

		
		//Initialize Non assigned Section
        fillSelectFromArray_bytype(window.document.forms[0].dest_section,nonassignedinfo,2);
        fillSelectFromArray_bytype(window.document.forms[0].dest_subsection,nonassignedinfo,3);
        fillSelectFromArray_bytype(window.document.forms[0].dest_listpages,nonassignedinfo,4);
        fillSelectFromArray_bytype(window.document.forms[0].dest_contents,nonassignedinfo,5);
		fillSelectFromArray_bytype(window.document.forms[0].dest_undefined,nonassignedinfo,0);

}


function change_section(id){
	var temp=new Array();
	var v,sectionid,subsectionid,listpagesid;

	for (v=0; v<displayinfo.length; v++){
		temp=displayinfo[v];
		if((temp[4]==3) &&(temp[1]==id) ) { subsectionid=temp[0];   break; }
	}

	for (v=0; v<displayinfo.length; v++){
		temp=displayinfo[v];
		if((temp[4]==4) &&(temp[1]==subsectionid) ) { listpagesid=temp[0];   break; }
	}

	fillSelectFromArray_byparent(window.document.forms[0].src_subsection,displayinfo,id);
	if(window.document.forms[0].src_subsection.options[0]){
		window.document.forms[0].src_subsection.options[0].selected=true;
		viewtype(window.document.forms[0].src_subsection.options[0].value,'div2');
	}
	else 
		cleartype('div2');

	fillSelectFromArray_byparent(window.document.forms[0].src_listpages,displayinfo,subsectionid);
    if(window.document.forms[0].src_listpages.options[0]){
		window.document.forms[0].src_listpages.options[0].selected=true;
		viewtype(window.document.forms[0].src_listpages.options[0].value,'div3');
	}
	else 
		cleartype('div3');
        
	fillSelectFromArray_byparent(window.document.forms[0].src_contents,displayinfo,listpagesid);
	if(window.document.forms[0].src_contents.options[0]){
		window.document.forms[0].src_contents.options[0].selected=true;
		viewtype(window.document.forms[0].src_contents.options[0].value,'div4');
	}
	else 
		cleartype('div4');
}


function change_subsection(id){
	var temp=new Array();
	var v,sectionid,subsectionid,listpagesid;

	for (v=0; v<displayinfo.length; v++){
		temp=displayinfo[v];
		if(temp[1]==id){ listpagesid=temp[0];   break; }
	}

	fillSelectFromArray_byparent(window.document.forms[0].src_listpages,displayinfo,id);
	if(window.document.forms[0].src_listpages.options[0]){
		window.document.forms[0].src_listpages.options[0].selected=true;
		viewtype(window.document.forms[0].src_listpages.options[0].value,'div3');
	}
	else 
		cleartype('div3');

	fillSelectFromArray_byparent(window.document.forms[0].src_contents,displayinfo,listpagesid);
	if(window.document.forms[0].src_contents.options[0]){
		window.document.forms[0].src_contents.options[0].selected=true;
		viewtype(window.document.forms[0].src_contents.options[0].value,'div4');
	}
	else 
		cleartype('div4');
}




function fillSelectFromArray_byparent(selectCtrl, itemArray,parentid) {
	var i, j;
	var prompt;

	// empty existing items
	for (i = selectCtrl.options.length; i >= 0; i--) {
		selectCtrl.options[i] = null;
	}


/*	prompt = (itemArray != null) ? goodPrompt : badPrompt;
	if (prompt == null) {
		j = 0;
	} else {		
		selectCtrl.options[0] = new Option(prompt);
		j = 1;
	}      */  j=0;
	if (itemArray != null) {
		// add new items
		for (i = 0; i < itemArray.length; i++) {   
			if (itemArray[i][1] == parentid) { //alert(parentid);
				selectCtrl.options[j] = new Option(itemArray[i][2]);
				if (itemArray[i][0] != null) {
					selectCtrl.options[j].value = itemArray[i][0];					
			} 
                         j++;
                        }

		}
		// select first item (prompt) for sub list
	//	selectCtrl.options[0].selected = true;
	}
//maral
	if(window.document.forms[0].src_contents.options[0]){
		window.document.forms[0].src_contents.options[0].selected=true;
		viewtype(window.document.forms[0].src_contents.options[0].value,'div4');
	}
	else 
		cleartype('div4');

}



function addOptionbefore(theSel, theText, theValue,theSelFrom)
{
  var elSel = theSel;
 if(theText==null){ alert('Nothing selected'); return}
  if (elSel.selectedIndex >= 0) {
    var elOptNew = document.createElement('option');
    elOptNew.text = theText;
    elOptNew.value =theValue;
    var elOptOld = elSel.options[elSel.selectedIndex];  
    try {
      elSel.add(elOptNew, elOptOld); // standards compliant; doesn't work in IE
    }
    catch(ex) {
      elSel.add(elOptNew, elSel.selectedIndex); // IE only
    }
  }
  
  
  
    for (i = theSelFrom.length - 1; i>=0; i--) {
    if (theSelFrom.options[i].selected) {
      theSelFrom.remove(i);
    }
  }
  
  

}
//********************************************************************************************************
function fillSelectFromArray_bytype(selectCtrl, itemArray,typeid) {
	var i, j;
	var prompt;

	// empty existing items
	for (i = selectCtrl.options.length; i >= 0; i--) {
		selectCtrl.options[i] = null;
	}

/*	prompt = (itemArray != null) ? goodPrompt : badPrompt;
	if (prompt == null) {
		j = 0;
	} else {		
		selectCtrl.options[0] = new Option(prompt);
		j = 1;
	}      */  
	j=0;
	if (itemArray != null) {
		// add new items
		for (i = 0; i < itemArray.length; i++) {
		       	if (itemArray[i][4] == typeid) {
			selectCtrl.options[j] = new Option(itemArray[i][2]);
			if (itemArray[i][0] != null) {
				selectCtrl.options[j].value = itemArray[i][0];
			} 
                         j++;
                        }

		}
		// select first item (prompt) for sub list
	//	selectCtrl.options[0].selected = true;
	}

}

//**************************************************************************************
function controldives(id){
if(id==2){
     document.getElementById('section_div').style.visibility='visible';
     document.getElementById('subsection_div').style.visibility='hidden';
     document.getElementById('listpages_div').style.visibility='hidden';
     document.getElementById('contentpages_div').style.visibility='hidden';
	 document.getElementById('undefined_div').style.visibility='hidden';

}else 
if(id==3){
    document.getElementById('section_div').style.visibility='hidden';
     document.getElementById('subsection_div').style.visibility='visible';
     document.getElementById('listpages_div').style.visibility='hidden';
     document.getElementById('contentpages_div').style.visibility='hidden';
	 document.getElementById('undefined_div').style.visibility='hidden';
}else
if(id==4){
     document.getElementById('section_div').style.visibility='hidden';
     document.getElementById('subsection_div').style.visibility='hidden';
     document.getElementById('listpages_div').style.visibility='visible';
     document.getElementById('contentpages_div').style.visibility='hidden';
	 document.getElementById('undefined_div').style.visibility='hidden';
}else
if(id==5){
     document.getElementById('section_div').style.visibility='hidden';
     document.getElementById('subsection_div').style.visibility='hidden';
     document.getElementById('listpages_div').style.visibility='hidden';
     document.getElementById('contentpages_div').style.visibility='visible';
	 document.getElementById('undefined_div').style.visibility='hidden';
}else
if(id==0){
     document.getElementById('section_div').style.visibility='hidden';
     document.getElementById('subsection_div').style.visibility='hidden';
     document.getElementById('listpages_div').style.visibility='hidden';
     document.getElementById('contentpages_div').style.visibility='hidden';
	 document.getElementById('undefined_div').style.visibility='visible';
}

}

//*****************************************************************************
function findParent(id)
{
	var temp = new Array();

	for(var i = 0, len = displayinfo.length; i < len; i++)
	{
		temp=displayinfo[i];
		if(temp[0] == id)
		{
			window.document.getElementById('location').value=window.document.getElementById('location').value + temp[1] +',';
			window.document.getElementById('pageid').value=temp[0];
			window.document.getElementById('pagetype').value=temp[4];
			//return true;
		}		
	}
	return false;
}

//*****************************************************************************
function clearLoc()
{
	window.document.getElementById('location').value='';
	window.document.getElementById('pageid').value='';
	window.document.getElementById('pagetype').value='';
}

//*****************************************************************************
function viewtype(id,divename){
	var temp=new Array();
	var v,ctype,cname, cname1;

	for (v=0; v<displayinfo.length; v++){
		temp=displayinfo[v];
		if(temp[0]==id){ ctype=temp[4];   break; }
	}

	if(ctype==2)	cname='&nbsp;&bull;&nbsp;Section page<br>&nbsp;<a href="index.php?module=Display&type=admin&func=editSection&pageid='+id+'" class=\"text-rose-light\"><img src="modules/Display/pnimages/edit.gif" alt="edit"></a>&nbsp;<a href="index.php?module=Display&type=admin&func=deletePage&pageid='+id+'" class="text-rose-light"><img src="modules/Display/pnimages/del.gif" alt="delete"></a>';
	else if(ctype==3)  cname='&nbsp;&bull;&nbsp;SubSection page<br>&nbsp;<a href="index.php?module=Display&type=admin&func=editSubsection&pageid='+id+'" class=\"text-rose-light\"><img src="modules/Display/pnimages/edit.gif" alt="edit"></a>&nbsp;<a href="index.php?module=Display&type=admin&func=deletePage&pageid='+id+'" class="text-rose-light"><img src="modules/Display/pnimages/del.gif" alt="delete"></a>&nbsp;<a href=# onClick="javascript:findParent('+id+');window.open(\'modules/Display/tree_sub.html\',\'mywindow\',\'width=350,height=500,status=yes,scrollbars=yes,resizable=yes\');"><img src="modules/Display/pnimages/loc.gif" alt="location"></a>';
	else if(ctype==4)  cname='&nbsp;&bull;&nbsp;List page<br>&nbsp;<a href="index.php?module=Display&type=admin&func=editList&pageid='+id+'" class=\"text-rose-light\"><img src="modules/Display/pnimages/edit.gif" alt="edit"></a>&nbsp;<a href="index.php?module=Display&type=admin&func=deletePage&pageid='+id+'" class="text-rose-light"><img src="modules/Display/pnimages/del.gif" alt="delete"></a>&nbsp;<a href=# onClick="javascript:findParent('+id+');window.open(\'modules/Display/tree_list.html\',\'mywindow\',\'width=350,height=500,status=yes,scrollbars=yes,resizable=yes\');"><img src="modules/Display/pnimages/loc.gif" alt="location"></a>';
	else if(ctype==5)	cname='&nbsp;&bull;&nbsp;Content page<br>&nbsp;<a href="index.php?module=Display&type=admin&func=editContent&pageid='+id+'" class=\"text-rose-light\"><img src="modules/Display/pnimages/edit.gif" alt="edit"></a>&nbsp;<a href="index.php?module=Display&type=admin&func=deletePage&pageid='+id+'" class="text-rose-light"><img src="modules/Display/pnimages/del.gif" alt="delete"></a>&nbsp;<a href=# onClick="javascript:findParent('+id+');window.open(\'modules/Display/tree_cont.html\',\'mywindow\',\'width=350,height=500,status=yes,scrollbars=yes,resizable=yes\');"><img src="modules/Display/pnimages/loc.gif" alt="location"></a>';
	else	cname='';
	//else   cname='Content';

	//alert(cname);
	if(ctype!=1)
  		//window.document.getElementById(divename).innerHTML ='&nbsp;&bull;&nbsp;'+cname+'';
		window.document.getElementById(divename).innerHTML = cname;
//&nbsp;<a href="#" onClick="javascript:window.open("index.php?module=Display&type=admin&func=getTree&t=tree2.html","mywindow","width=350, height=500 resizable=yes scrollbars=yes")">loc</a>

}

function viewtype1(id){
	var temp=new Array();
	var v,ctype;

	for (v=0; v<displayinfo.length; v++){
		temp=displayinfo[v];
		if(temp[0]==id){ ctype=temp[4];   break; }
	}

	if(ctype!=1)
		return ctype;
}

function cleartype(ddiv){
     window.document.getElementById(ddiv).innerHTML ='';
}
