//remove options from select list
function removeOptions(id)
{
	var sel = document.getElementById(id);
	for (var i = sel.options.length - 1; i>=0; i--) {
		sel.remove(i);
	}
}
//add one option to select list
function addOption(id,text,value)
{
	var newOp = document.createElement('option');
	newOp.text = text;
	newOp.value = value;
	var sel = document.getElementById(id);
	try {
		sel.add(newOp, null); // standards compliant; doesn't work in IE
	}
	catch(ex) {
		sel.add(newOp); // IE only
	}
}
//add options to select list  * used for multible select list
function addOptions(id,text)
{
	var start=0;
	var end=0;
	var txt="";
	var val="";
	for(var i=end;i<text.length;i++){
		if(text.charAt(i)=="#"){
			end=i;
			txt = text.substr(start,end-start);
			start=end+1;
		}
		if(text.charAt(i)=="|"){
			end=i;
			val = text.substr(start,end-start);
			start=end+1;
		}
		if(txt!=""&&val!=""){
			addOption(id,txt,val);
			txt="";
			val="";
		}
	}
}
//if select field allow multible select
function mselect(id,base_path){
  var selectedArray = new Array();
  var selObj = document.getElementById("edit-field-department-"+id+"-nids");
  var i;
  var j;
  var k;
  var elem;
if(id==0){
	elem = "edit-field-course-nids";
}
else{
	elem="edit-field-course-"+(id-1)+"-nids"
}
  var elem2 = elem+"-label";
  //remove select options before add new ones
  removeOptions(elem);//*********************************************
  addOptions(elem,document.getElementById("hidden").value);
 
  for (i=0; i<selObj.options.length; i++) {
    if (selObj.options[i].selected) {
      updateContentPages(selObj.options[i].value,(id+19),base_path);
    }
  }
  document.getElementById(elem2).style.display="";
  document.getElementById(elem).style.display="";
}
// function ajax call updateContentPages.php 
function updateContentPages(val,id,base_path){
document.getElementById("hidden2").value=val;
	var ajaxRequest;  // The variable that makes Ajax possible!
	try
	{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	}
	catch (e)
	{
		// Internet Explorer Browsers
		try
		{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) 
		{
			try
			{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch (e)
			{
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function()
	{
		if(ajaxRequest.readyState == 4)
		{
			if(ajaxRequest.responseText=="0")
			{
				alert("You Are Not authorized, register or login");
			}
			else
			{	var elem;
				var elem2;
				var helem1;
				var helem2;
				//var helem3;
				//var helem4;
				//var helem5;
				//var helem6;
				if(id==1){
					elem="edit-field-university-nids";
					elem2=elem+"-label";
					removeOptions(elem);//*********************************************
					addOption(elem,"-----","0");
					addOptions(elem,ajaxRequest.responseText);
					document.getElementById(elem2).style.display="";
					document.getElementById(elem).style.display="";
				}
				if(id==2||id==3||id==4||id==5||id==6||id==7){
					elem="edit-field-university-"+(id-2)+"-nids";
					elem2=elem+"-label";
					removeOptions(elem);//*********************************************
					addOption(elem,"-----","0");
					addOptions(elem,ajaxRequest.responseText);
					document.getElementById(elem).style.display="";
					document.getElementById(elem2).style.display="";
					//hide other fields
					if(id==2){
						helem1="edit-field-faculty-nids";
						helem2="edit-field-faculty-nids-label";
						document.getElementById(helem1).style.display="none";
						document.getElementById(helem2).style.display="none";
					}
					if(id==3){
						helem1="edit-field-faculty-0-nids";
						helem2="edit-field-faculty-0-nids-label";
						//helem3="edit-field-department-nids";
						//helem4="edit-field-department-nids-label";
						document.getElementById(helem1).style.display="none";
						document.getElementById(helem2).style.display="none";
						//document.getElementById(helem3).style.display="none";
						//document.getElementById(helem4).style.display="none";
					}
					if(id==4||id==5||id==6||id==7){
						helem1="edit-field-faculty-"+(id-3)+"-nids";
						helem2="edit-field-faculty-"+(id-3)+"-nids-label";
						//helem3="edit-field-department-"+(id-4)+"-nids";
						//helem4="edit-field-department-"+(id-4)+"-nids-label";
						//if(id==4){
						//	helem5="edit-field-course-nids";
						//	helem6="edit-field-course-nids-label";
						//}
						//else{
							//helem5="edit-field-course-"+(id-5)+"-nids";
							//helem6="edit-field-course-"+(id-5)+"-nids-label";
						//}
						document.getElementById(helem1).style.display="none";
						document.getElementById(helem2).style.display="none";
						//document.getElementById(helem3).style.display="none";
						//document.getElementById(helem4).style.display="none";
						//document.getElementById(helem5).style.display="none";
						//document.getElementById(helem6).style.display="none";
					}
				}
				/*if(id==8){
					elem="edit-field-faculty-nids";
					elem2=elem+"-label";
					removeOptions(elem);//*********************************************
					addOption(elem,"-----","0");
					addOptions(elem,ajaxRequest.responseText);
					document.getElementById(elem).style.display="";
					document.getElementById(elem2).style.display="";

				}*/
				if(id==9||id==10||id==11||id==12||id==13){
					elem="edit-field-faculty-"+(id-9)+"-nids";
					elem2=elem+"-label";
					removeOptions(elem);//*********************************************
					addOption(elem,"-----","0");
					addOptions(elem,ajaxRequest.responseText);
					document.getElementById(elem).style.display="";
					document.getElementById(elem2).style.display="";
					//hide other fields
					/*if(id==9){
						helem3="edit-field-department-nids";
						helem4="edit-field-department-nids-label";
						document.getElementById(helem3).style.display="none";
						document.getElementById(helem4).style.display="none";
					}
					else{
						helem3="edit-field-department-"+(id-10)+"-nids";
						helem4="edit-field-department-"+(id-10)+"-nids-label";
						if(id==10){
							helem5="edit-field-course-nids";
							helem6="edit-field-course-nids-label";
						}
						else{
							helem5="edit-field-course-"+(id-11)+"-nids";
							helem6="edit-field-course-"+(id-11)+"-nids-label";
						}
						document.getElementById(helem3).style.display="none";
						document.getElementById(helem4).style.display="none";
						document.getElementById(helem5).style.display="none";
						document.getElementById(helem6).style.display="none";
					}*/
				}
				/*if(id==14){
					elem="edit-field-department-nids";
					elem2=elem+"-label";
					removeOptions(elem);//*********************************************
					addOption(elem,"-----","0");
					addOptions(elem,ajaxRequest.responseText);
					document.getElementById(elem).style.display="";
					document.getElementById(elem2).style.display="";

				}*/
				/*if(id==15||id==16||id==17||id==18){
										
					elem="edit-field-department-"+(id-15)+"-nids";
					elem2=elem+"-label";
					removeOptions(elem);//*********************************************
					//addOption(elem,"-----","0");
					addOptions(elem,ajaxRequest.responseText);
					document.getElementById(elem).style.display="";
					document.getElementById(elem2).style.display="";
					//alert("value2 "+val);
					updateContentPages(document.getElementById("hidden2").value,id+8,base_path);
				}*/
				/*if(id==23||id==24||id==25||id==26){
					if(id==23){
						helem5="edit-field-course-nids";
						helem6="edit-field-course-nids-label";
					}
					else{
						helem5="edit-field-course-"+(id-24)+"-nids";
						helem6="edit-field-course-"+(id-24)+"-nids-label";
					}
					//alert(ajaxRequest.responseText);		
					removeOptions(helem5);//*********************************************
					addOptions(helem5,ajaxRequest.responseText);
					document.getElementById("hidden").value=ajaxRequest.responseText;
					document.getElementById(helem5).style.display="";
					document.getElementById(helem6).style.display="";
				}*/
				/*if(id==19){
 					var elem="edit-field-course-nids";
					elem2=elem+"-label";
					var courses = document.getElementById(elem);
					var text = ajaxRequest.responseText;
					var ops=new Array();
					var start=0;
					var end=0;
					var i;
					var txt="";
					var val="";
					//extract nid and title from response text
					for(i=end;i<text.length;i++){
						var flag=0;
						if(text.charAt(i)=="#"){
							end=i;
							txt = text.substr(start,end-start);
							start=end+1;
						}
						if(text.charAt(i)=="|"){
							end=i;
							val = text.substr(start,end-start);
							start=end+1;
						}
						if(txt!=""&&val!=""){
							for (j=0; j<courses.options.length; j++){
								if(courses.options[j].value==val){
									flag=1;
									break;
								}
							}
							if(flag==0){
								addOption(elem,txt,val);
							}
						}
					}
				}*/
				/*if(id==20||id==21||id==22){
 					var elem="edit-field-course-"+(id-20)+"-nids";
					elem2=elem+"-label";
					var courses = document.getElementById(elem);
					var text = ajaxRequest.responseText;
					var ops=new Array();
					var start=0;
					var end=0;
					var i;
					var txt="";
					var val="";
					//extract nid and title from response text
					for(i=end;i<text.length;i++){
						var flag=0;
						if(text.charAt(i)=="#"){
							end=i;
							txt = text.substr(start,end-start);
							start=end+1;
						}
						if(text.charAt(i)=="|"){
							end=i;
							val = text.substr(start,end-start);
							start=end+1;
						}
						if(txt!=""&&val!=""){
							for (j=0; j<courses.options.length; j++){
								if(courses.options[j].value==val){
									flag=1;
									break;
								}
							}
							if(flag==0){
								addOption(elem,txt,val);
							}
						}
					}
				}*/
			}
		}
	}
	ajaxRequest.open("GET", base_path+"updateContentPages.php"+"?val="+val+"&id="+id, true);
	ajaxRequest.send(null);
}

function view_other(value){
	elem="edit-profile-other";
	elem2=elem+"-label";
	if(value==document.getElementById("other_university_node_id").value){
		document.getElementById(elem).style.display="";
		document.getElementById(elem2).style.display="";	
	}
	else{
		document.getElementById(elem).style.display="none";
		document.getElementById(elem2).style.display="none";
	}
}
// function ajax call updateProfilePage.php 
function updateProfilePage(val,id,base_path){
	var ajaxRequest;  // The variable that makes Ajax possible!
	try
	{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	}
	catch (e)
	{
		// Internet Explorer Browsers
		try
		{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) 
		{
			try
			{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch (e)
			{
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function()
	{
		if(ajaxRequest.readyState == 4)
		{
			if(ajaxRequest.responseText=="0")
			{
				alert("You Are Not authorized, register or login");
			}

			else{
				if(id==1){
					elem="edit-profile-university";
					//elem2=elem+"-label";
					elem3="edit-profile-other";
					elem4=elem3+"-label";
					
					
					document.getElementById(elem3).style.display="none";
					document.getElementById(elem4).style.display="none";
					
					//elem3="edit-profile-department";
					//elem4=elem3+"-label";
					removeOptions(elem);//*********************************************
					//addOption(elem,"-----","0");
					addOptions(elem,ajaxRequest.responseText);
					
					var otherNodeId = ajaxRequest.responseText.split("#");
					document.getElementById("other_university_node_id").value = otherNodeId[otherNodeId.length-1];
					//alert(otherNodeId[otherNodeId.length-1]);
					//document.getElementById(elem).style.display="";
					//document.getElementById(elem2).style.display="";
					//document.getElementById(elem3).style.display="none";
					//document.getElementById(elem4).style.display="none";
				}
				/*else if(id==2){
					//elem="edit-profile-department";
					//elem2=elem+"-label";
					/*removeOptions(elem);//*********************************************
					addOption(elem,"-----","0");
					addOptions(elem,ajaxRequest.responseText);
					document.getElementById(elem2).style.display="";
					document.getElementById(elem).style.display="";
				}*/
			}
		}
	}
	ajaxRequest.open("GET", base_path+"updateProfilePage.php"+"?val="+val+"&id="+id, true);
	ajaxRequest.send(null);
}
function checkComment()
{
	var count=0;
	var str = document.getElementById("edit-comment").value;
	var len = document.getElementById("len").value;
	for(var i=0;i<str.length;i++)
	{
		if(str.charAt(i)==" "||str.charAt(i)=="\n"){
			if(str.charAt(i-1)!=" " && str.charAt(i-1)!="\n")
				count++;
		}
		if(count>=len){
			str = str.substr(0,i);
			document.getElementById("edit-comment").value = str;
			break;
		}
	}	
	//alert(len);
	//alert(document.getElementById("edit-comment").value.length);
	if(count>=len){
		alert("No more words");
	}	
	else{
		document.getElementById("length").innerHTML=len-count;
	}
}
function showcontentDivs(id,size,name){
	for(var i=1;i<=size;i++){
		document.getElementById(name+i).style.display="none";
	}	
	document.getElementById(name+id).style.display="";
	
	
	///////////////////////////////////////////////////////////////////////////////
	var query = "";
		
	if((id-1) > 0){
		query += '<a href="javascript:showcontentDivs('+(id-1)+','+size+',\''+name+'\')">&lt;--</a>&nbsp;&nbsp;';
	}
	if((id-2) > 0){
		query += '<a href="javascript:showcontentDivs('+(id-2)+','+size+',\''+name+'\')">'+(id-2)+'</a>&nbsp;&nbsp;';
	}
	if((id-1) > 0){
		query += '<a href="javascript:showcontentDivs('+(id-1)+','+size+',\''+name+'\')">'+(id-1)+'</a>&nbsp;&nbsp;';
	}
	query += "<b>"+id+"</b>&nbsp;&nbsp;";
	if((id+1) <= size){
		query += '<a href="javascript:showcontentDivs('+(id+1)+','+size+',\''+name+'\')">'+(id+1)+'</a>&nbsp;&nbsp;';
	}
	if((id+2) <= size){
		query += '<a href="javascript:showcontentDivs('+(id+2)+','+size+',\''+name+'\')">'+(id+2)+'</a>&nbsp;&nbsp;';
	}
	if((id+1) <= size){
		query += '<a href="javascript:showcontentDivs('+(id+1)+','+size+',\''+name+'\')">--&gt;</a>&nbsp;&nbsp;';
	}
	
	document.getElementById("pagging").innerHTML = query;
	///////////////////////////////////////////////////////////////////////////////
	
}
function makeProfilePrivate(id,uid,base_path){
	var ajaxRequest;  // The variable that makes Ajax possible!
	try
	{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	}
	catch (e)
	{
		// Internet Explorer Browsers
		try
		{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) 
		{
			try
			{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch (e)
			{
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function()
	{
		if(ajaxRequest.readyState == 4)
		{
			if(id == 1){
				document.getElementById('profilestatues').innerHTML = '<a href="javascript:makeProfilePrivate( 0 , '+uid+' ,\''+base_path+'\');">Make your profile Public</a>';		
			}
			else{
				document.getElementById('profilestatues').innerHTML = '<a href="javascript:makeProfilePrivate( 1 , '+uid+' ,\''+base_path+'\');">Make your profile Private</a>';
			}
		}
	}
	ajaxRequest.open("GET", base_path+"makeProfilePrivate.php"+"?uid="+uid+"&id="+id, true);
	ajaxRequest.send(null);
}
function updateSuggistOptions(value,base_path){
	var ajaxRequest;  // The variable that makes Ajax possible!
	try
	{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	}
	catch (e)
	{
		// Internet Explorer Browsers
		try
		{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) 
		{
			try
			{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch (e)
			{
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function()
	{
		if(ajaxRequest.readyState == 4)
		{
			removeOptions("suggistOptions");
			addOptions("suggistOptions",ajaxRequest.responseText);
			document.getElementById("suggistOptions").style.display="";
			
			
			/*else{
			sug.style.display="none";
			}*/
		}
	}
	ajaxRequest.open("GET", base_path+"updateSuggistOptions.php"+"?val="+value, true);
	ajaxRequest.send(null);
}



//**********************************************************************************************************************//

/*
var arr=new Array();
var input="";
var temp="";
*/

function selectOption(value,base_path,e){
	sug=document.getElementById("suggistOptions");
	if(e.keyCode==40)
	{
		sug.style.display="";
		sug.focus();
		sug.selectedIndex=0;
		//document.getElementById("searchContentTextBox") = sug.value;
		/*saveTextValue();
		updateText(sug,1);*/
	}
	else if(e.keyCode==13||e.keyCode==38){}
	else{
		//sug=document.getElementById("suggistOptions");
		if(value!="")
			updateSuggistOptions(value,base_path);
		else
			sug.style.display="none";
		
	}
}
/*
function updateText(list,f){
	var tmpCh=document.getElementById('searchContentTextBox').value;
	var start = 0;
	for(i=0;i<tmpCh.length;i++){
		if(tmpCh.charAt(i)==' '){
			start=i;
		}
	}
	var arabic = tmpCh.substr(0,start);
	var english = tmpCh.substr(start,tmpCh.length);
	if(f==1){
		temp = english;
	}
	english=list.value;
	text.value=arabic+english;
	
}

function saveTextValue(f){
    	input=document.getElementById('searchContentTextBox').value;
}
*/
function choose(e){
	sug = document.getElementById("suggistOptions");
	txt = document.getElementById('searchContentTextBox');
	if(e.keyCode==13)
	{
		sug.style.display="none";
		txt.focus();
		txt.value = sug.value;
	}
	if(sug.selectedIndex==0&&e.keyCode==38)
	{
		sug.style.display="none";
		txt.focus();
	}
}


function approveuniversity(cid, title, uid, id, newtitle, base_path){
	var ajaxRequest;  // The variable that makes Ajax possible!
	try
	{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	}
	catch (e)
	{
		// Internet Explorer Browsers
		try
		{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) 
		{
			try
			{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch (e)
			{
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function()
	{
		if(ajaxRequest.readyState == 4)
		{
			var status = 'Approved';
			var color = 'blue';
			if(id == 0){
				status = 'Not Approved';
				color = 'red';
			}
			document.getElementById(cid+"_"+title+"_"+uid).innerHTML = '<font color="'+color+'"><b>'+status+'</b></font>';
		}
	}
	ajaxRequest.open("GET", base_path+"approveuniversity.php"+"?title="+newtitle+"&uid="+uid+"&cid="+cid+"&id="+id, true);
	ajaxRequest.send(null);
}

function approve(buttonid, base_path){
	var str = document.getElementById(buttonid).id;
	var arr = str.split("_");
	var title = document.getElementById("uni_"+arr[0]+"_"+arr[1]+"_"+arr[2]).value;
	//alert(title);
	//alert(arr[0]+" - "+arr[1]+" - "+arr[2]+" - "+arr[3]);
	approveuniversity(arr[0], arr[1], arr[2], arr[3], title, base_path);
}
