﻿function postwith (qu) {
  var myForm = document.createElement("form");
  myForm.method="post" ;
  myForm.action = "runsearch.php" ;

	var myInput = document.createElement("input");
   	myInput.setAttribute("name", 'Qstring') ;
   	myInput.setAttribute("value", 'cnt=10&q='+ qu);
    myForm.appendChild(myInput) ;

document.body.appendChild(myForm) ;
  myForm.submit() ;
  document.body.removeChild(myForm) ;

}

function onkey(e)  {      

	if (e.keyCode == 13) {
		GetSearchData();
		}
	 }
function checkFilling() {
	var ClassificationSel = document.getElementById('Classification').selectedIndex;
	var DateToSel = document.getElementById('DateTo').selectedIndex;
	var DateFromSel = document.getElementById('DateFrom').selectedIndex;
	var RAndDSel = document.getElementById('RAndD').selectedIndex;
	var InstitutionSel = document.getElementById('Institution').selectedIndex;
	var FundedSel = document.getElementById('Funded').selectedIndex;
	if ((ClassificationSel == 0) && (DateToSel == 0) && (DateFromSel == 0) && (RAndDSel == 0) && (InstitutionSel == 0) && (FundedSel == 0)) {
		return true;
	} else {
		return false;
	}
}



var ShowHide = true;
function showHideAdvncdSearch() {
	if (ShowHide) {
		document.getElementById('AdvncdSearch').style.display = 'block';
		ShowHide = false;
	} else {
		document.getElementById('AdvncdSearch').style.display = 'none';
		ShowHide = true;
	}

}

var strContent = document.getElementById("contentTable1").innerHTML;
function resetdata() {
	if (ShowHide){
document.getElementById("contentTable1").innerHTML = "";
document.getElementById("contentTable1").innerHTML = strContent;
document.getElementById('AdvncdSearch').style.display = 'none';
FillDateFromTo()
	}else {
document.getElementById("contentTable1").innerHTML = "";
document.getElementById("contentTable1").innerHTML = strContent;
document.getElementById('AdvncdSearch').style.display = 'block';
FillDateFromTo()
		}
}

function GetSearchData() {
	var SearchField = document.getElementById('SearchField').value;
	if (checkFilling()) {
		if (SearchField == "") {
			alert('Your search query is empty, please enter the text you want to search.');
			return false;
		}
	} else {
	}
	if (document.getElementById('AdvncdSearch').style.display == 'block') {
		GetData();
	} else {
		this.location = 'runsearch.php?cnt=10&q='+SearchField;
		//postwith (SearchField)
	}
}




function showHideClassification() {
	document.getElementById('Natural sciences').style.display = 'none';
	document.getElementById('Engineering and technology').style.display = 'none';
	document.getElementById('Medical and Health sciences').style.display = 'none';
	document.getElementById('Agricultural sciences').style.display = 'none';
	document.getElementById('Social sciences').style.display = 'none';
	document.getElementById('Humanities').style.display = 'none';

	var ClassficationValue = document.getElementById('Classification').value;
	document.getElementById(ClassficationValue).style.display = 'block';
}

function addOption(selectbox, text, value) {
	var optn = document.createElement('OPTION');
	optn.title = String(text);
	optn.text = text;
	optn.value = value;

	selectbox.options.add(optn);
}



function FillClassification(n) {
	//debugger;
	var classSelect = document.getElementById('SubClass');
	removeAllOptions(classSelect);
	switch (n) {
		case 'Natural sciences' :
			classSelect.style.display = '';
			addOption(classSelect,'1.1 Mathematics','ClsMathematics');
			addOption(classSelect,'1.2 Computer and information sciences','ClsComputerinformationsciences');
			addOption(classSelect,'1.3 Physical science','ClsPhysicalscience');
			addOption(classSelect,'1.4 Chemical sciences','ClsChemicalsciences');
			addOption(classSelect,'1.5 Earth and related Environmental sciences','ClsEarthrelatedEnvironmentalsciences');
			addOption(classSelect,'1.6 Biological science','ClsBiologicalscience');
			addOption(classSelect,'1.7 Other natural sciences','ClsOthernaturalsciences');
			break;

		case 'Engineering and technology' :
			classSelect.style.display = '';
			addOption(classSelect,'2.1 Civil engineering','ClsCivilengineering');
			addOption(classSelect,'2.2 Electrical engineering, Electronic engineering, Information engineering','ClsElectricalengineeringElectronicengineeringInformationenginee');
			addOption(classSelect,'2.3 Mechanical engineering','ClsMechanicalengineering');
			addOption(classSelect,'2.4 Chemical engineering','ClsChemicalengineering');
			addOption(classSelect,'2.5 Materials engineering','ClsMaterialsengineering');
			addOption(classSelect,'2.6 Medical engineering','ClsMedicalengineering');
			addOption(classSelect,'2.7 Environmental engineering','ClsEnvironmentalengineering');
			addOption(classSelect,'2.8 Environmental biotechnology','ClsEnvironmentalbiotechnology');
			addOption(classSelect,'2.9 Industrial biotechnology','ClsIndustrialbiotechnology');
			addOption(classSelect,'2.10 Nano-technology','ClsNano-technology');
			addOption(classSelect,'2.11 Other engineering and technologies','ClsOtherengineeringtechnologies');
			break;

		case 'Medical and Health sciences' :
			classSelect.style.display = '';
			addOption(classSelect,'3.1 Basic medicine','ClsBasicmedicine');
			addOption(classSelect,'3.2 Clinical medicine','ClsClinicalmedicine');
			addOption(classSelect,'3.3 Health Sciences','ClsHealthSciences');
			addOption(classSelect,'3.4 Medical biotechnology','ClsMedicalbiotechnology');
			addOption(classSelect,'3.5 Other medical sciences','ClsOthermedicalsciences');
			break;

		case 'Agricultural sciences' :
			classSelect.style.display = '';
			addOption(classSelect,'4.1 Agriculture, Forestry, and Fisheries','ClsAgricultureForestryFisheries');
			addOption(classSelect,'4.2 Animal and Dairy science','ClsAnimalDairyscience');
			addOption(classSelect,'4.3 Veterinary science','ClsVeterinaryscience');
			addOption(classSelect,'4.4 Agricultural biotechnology','ClsAgriculturalbiotechnology');
			addOption(classSelect,'4.5 Other agricultural sciences','ClsOtheragriculturalsciences');
			break;

		case 'Social sciences' :
			classSelect.style.display = '';
			addOption(classSelect,'5.1 Psychology','ClsPsychology');
			addOption(classSelect,'5.2 Economies and Business','ClsEconomiesBusiness');
			addOption(classSelect,'5.3 Educational sciences','ClsEducationalsciences');
			addOption(classSelect,'5.4 Sociology','ClsSociology');
			addOption(classSelect,'5.5 Law','ClsLaw');
			addOption(classSelect,'5.6 Political science','ClsPoliticalscience');
			addOption(classSelect,'5.7 Social and economic geography','ClsSocialeconomicgeography');
			addOption(classSelect,'5.8 Media and communications','ClsMediacommunications');
			addOption(classSelect,'5.9 Other social sciences','ClsOthersocialsciences');
			break;

		case 'Humanities' :
			classSelect.style.display = '';
			addOption(classSelect,'6.1 History and Archaeology','ClsHistoryArchaeology');
			addOption(classSelect,'6.2 Languages and Literature','ClsLanguagesLiterature');
			addOption(classSelect,'6.3 Philosophy, Ethics and Religion','ClsPhilosophyEthicsReligion');
			addOption(classSelect,'6.4 Arts','ClsArts');
			addOption(classSelect,'6.5 Other humanities','ClsOtherhumanities');
			break;

		case 'ALLClassification' :
			classSelect.style.display = 'none';
			break;

	}
}
function FillRandD(n) {
	var RandDSelect = document.getElementById('RAndDSub');
	removeAllOptions(RandDSelect);
	switch (n) {
		case 'Type of Research' :
			RandDSelect.style.display = '';
			addOption(RandDSelect,'Basic research','ResearchTypeBasicresearch');
			addOption(RandDSelect,'Applied research','ResearchTypeAppliedresearch');
			addOption(RandDSelect,'Experimental development','ResearchTypeExperimentaldevelopment');
			addOption(RandDSelect,'Patent','ResearchTypePatent');
			//addOption(RandDSelect, 'Doctorate Thesis |  رسالة دكتوراه','patent');
			//addOption(RandDSelect, 'Master’s thesis  | رسالة  ماجستير','patent');
			break;

		case 'Applied research' :
			RandDSelect.style.display = '';
			addOption(RandDSelect,'Book','ResearchTypebook');
			addOption(RandDSelect,'Doctorate Thesis','ResearchTypeDoctorateThesis');
			addOption(RandDSelect,'Master’s thesis','ResearchTypeMastersthesis');
			break;

		case 'AllRAndD' :
			RandDSelect.style.display = 'none';
			break;
	}
}
function FillInstitution(n) {
	var InstitutioSelect = document.getElementById('InstitutionSub');
	removeAllOptions(InstitutioSelect);
	switch (n) {
		case 'Universities' :
			InstitutioSelect.style.display = '';
			addOption(InstitutioSelect,'All','AllUniversities');
			addOption(InstitutioSelect,'Carnegie Mellon University in Qatar CMUQ','InstitutionCarnegieMellonUniversityinQatar"OR"Institutionكارنيجيميلونفيقطر');
			addOption(InstitutioSelect,'Qatar University','InstitutionQatarUniversity"OR"Institutionجامعةقطر');
			addOption(InstitutioSelect,'Weill Cornell Medical College-Qatar','InstitutionWeillCornellMedicalCollege-Qatar"OR"Institutionجامعةوايلكورنيلفيقطر');
			addOption(InstitutioSelect,'Texas A & M University at Qatar','InstitutionTexasA&MUniversityatQatar"OR"InstitutionجامعةتكساسA&Mفيقطر');
			addOption(InstitutioSelect,'Virginia Commonwealth University in Qatar','InstitutionVirginiaCommonwealthUniversityinQatar"OR"Institutionجامعةفيرجينياكومونويلثفيقطر');
			addOption(InstitutioSelect,'Carnegie Mellon University Qatar','InstitutionCarnegieMellonUniversityQatar"OR"Institutionكارنيجيميلونفيقطر');
			addOption(InstitutioSelect,'Georgetown University School of Foreign Service in Qatar','InstitutionGeorgetownUniversitySchoolofForeignServiceinQatar"OR"Institutionجامعةجورجتاونكليةالشؤونالدوليةفيقطر');
			break;

		case 'Other institutions' :
			InstitutioSelect.style.display = '';
			addOption(InstitutioSelect,'All','AllOtherInstitution');
			addOption(InstitutioSelect,'Hamad Medical Corporation','InstitutionHamadMedicalCorporation"OR"Institutionمؤسسةحمدالطبية');

			addOption(InstitutioSelect,'Qatar Diabetes Association','InstitutionQatarDiabetesAssociation"OR"Institutionالجمعيةالقطريةللسكري');
			addOption(InstitutioSelect,'Qatar Petroleum','InstitutionQatarPetroleum"OR"Institutionقطرللبترول');
			addOption(InstitutioSelect,'University of Pittsburgh  and Hamad Medical Corporation','InstitutionUniversityofPittsburghHamadMedicalCorporation"OR"InstitutionمؤسسةحمدالطبيةوUPMC');
			addOption(InstitutioSelect,'Gulf Organiztion for Industrial Consulting','InstitutionGulfOrganiztionforIndustrialConsulting"OR"Institutionمنظمةالخليجللاستشاراتالصناعية');
			addOption(InstitutioSelect,'Shafallah Center for Children with Special Needs','InstitutionShafallahCenterforChildrenwithSpecialNeeds"OR"Institutionمركزالشفلحلذويالاحتياجاتالخاصة');
			addOption(InstitutioSelect,'Institute of Administrative Development','InstitutionInstituteofAdministrativeDevelopment"OR"Institutionمعهدالتنميةالادارية');
			addOption(InstitutioSelect,'Permanent Population Committee','InstitutionPermanentPopulationCommittee"OR"Institutionاللجنةالدائمةللسكان');
			addOption(InstitutioSelect,'National Council for Culture, Arts and Heritage','InstitutionNationalCouncilforCulture,ArtsHeritage"OR"Institutionالمجلسالوطنيللثقافةوالفنونوالتراث');
			addOption(InstitutioSelect,'Qatar Television','InstitutionQatarTelevision"OR"Institutionتلفزيونقطر');
			addOption(InstitutioSelect,'Supreme Education Council','InstitutionSupremeEducationCouncil"OR"Institutionهيئةالتعليمالعالي/المجلسالأعلىللتعليم');
			addOption(InstitutioSelect,'Ministry of Municipality and Urban Planning - Agricultural & Water Research Department','InstitutionMinistryofMunicipalityUrbanPlanning-Agricultural&WaterResearchDepartment"OR"Institutionوزارةالشؤونالبلديةوالزراعة-إدارةالبحوثالزراعيةوالمائية');
			addOption(InstitutioSelect,'Gulf Organiztion for Industrial Consulting','InstitutionGulfOrganiztionforIndustrialConsulting"OR"Institutionمنظمةالخليجللاستشاراتالصناعية');
			addOption(InstitutioSelect,'Waqfiat Sheikh.Ali Bin Abdulla Al-Thani for Studies','InstitutionWaqfiatSheikh.AliBinAbdullaAl-ThaniforStudies"OR"Institutionجائزةالشيخعليبنعبداللهآلثانيارالإسلامي');
			addOption(InstitutioSelect,'Moza Almaliki center for International Training and Qualification','InstitutionMozaAlmalikicenterforInternationalTrainingQualification"OR"Institutionمركزموزةالمالكيللتدريبوالتأهيلالدولي');
			addOption(InstitutioSelect,'Agricultural & Water Research Department','InstitutionAgricultural&WaterResearchDepartment"OR"Institutionإدارةالبحوثالزراعيةوالمائية');
			addOption(InstitutioSelect,'Plant Tissues laboratory','InstitutionPlantTissueslaboratory"OR"Institutionمختبرزراعةالأنسجةالنباتية');
			addOption(InstitutioSelect,'RAND-Qatar Policy Institute','InstitutionRAND-QatarPolicyInstitute"OR"Institutionمعهدراند-قطرللسياسات');
			addOption(InstitutioSelect,'اللجنة الوطنية القطرية للتربية والثقافة والعلوم','Institutionاللجنةالوطنيةالقطريةللتربيةوالثقافةوالعلوم');
			addOption(InstitutioSelect,'حولية كلية الإنسانيات والعلوم الاجتماعية','Institutionحوليةكليةالإنسانياتوالعلومالاجتماعية');
			addOption(InstitutioSelect,'المجموعة العربية للأجهزة العليا للرقابة المالية والمحاسبية','Institutionالمجموعةالعربيةللأجهزةالعلياللرقابةالماليةوالمحاسبية');
			addOption(InstitutioSelect,'الدار المصرية اللبنانية','AllOtherInstitution');
			addOption(InstitutioSelect,'الدارالمصريةاللبنانية','Institutionالدارالمصريةاللبنانية');
			addOption(InstitutioSelect,'إدارة أحياء التراث الإسلامي','Institutionإدارةأحياءالتراثالإسلامي');
			addOption(InstitutioSelect,'مجلس التخطيط','Institutionمجلسالتخطيط');
			addOption(InstitutioSelect,'وزارة شؤون الخدمة المدنية والإسكان','Institutionوزارةشؤونالخدمةالمدنيةوالإسكان');
			addOption(InstitutioSelect,'لجنة من ضمنها ممثل لادارة نظم الخدمة المدنية','Institutionلجنةمنضمنهاممثللادارةنظمالخدمةالمدنية');
			addOption(InstitutioSelect,'رئاسة المحاكم الشرعية','Institutionرئاسةالمحاكمالشرعية');
			addOption(InstitutioSelect,'الجمعية القطرية لذوي الاحتياجات الخاصة','Institutionالجمعيةالقطريةلذويالاحتياجاتالخاصة');
			addOption(InstitutioSelect,'دار المعرفة الجامعية الإسكندرية','Institutionدارالمعرفةالجامعيةالإسكندرية');
			addOption(InstitutioSelect,'الهلال الاحمر القطري','Institutionالهلالالاحمرالقطري');
			addOption(InstitutioSelect,'إدارة قضايا الدولة','Institutionإدارةقضاياالدولة');
			addOption(InstitutioSelect,'مجلة الرقابة المالية','Institutionمجلةالرقابةالمالية');
			addOption(InstitutioSelect,'سلسلة كتاب الأمة','Institutionسلسلةكتابالأمة');
			addOption(InstitutioSelect,'المجلس الاعلى لشؤون الاسرة','Institutionالمجلسالاعلىلشؤونالاسرة');
			addOption(InstitutioSelect,'وزارة الخارجية','Institutionوزارةالخارجية');
			break;

		case 'AllInstitution' :
			InstitutioSelect.style.display = 'none';
			break;
	}
}
function removeAllOptions(selectbox) {
	var i;
	for (i=selectbox.options.length-1; i>=0; i--) {
		selectbox.remove(i);
	}
}
function FillDateFromTo() {
	var DateTo = document.getElementById('DateTo');
	var DateFrom = document.getElementById('DateFrom');
	var CurrentDate = new Date();
	var CurrentYear = CurrentDate.getFullYear();
	var i, FillYear = 0;
	addOption(DateTo,'','');
	addOption(DateFrom,'','');
	for (i=0; i<38; i++) {
		FillYear = CurrentYear-2-i;
		addOption(DateTo,FillYear,FillYear);
		addOption(DateFrom,FillYear,FillYear);

	}
}
setTimeout('FillDateFromTo()',1);

function GetData() {

	var ClassificationSel = document.getElementById('Classification');
	var Classification = ClassificationSel.options[ClassificationSel.selectedIndex].value+' ';
	if (Classification == 'ALLClassification ') {
		Classification = '';
	}
	var SubClassSel = document.getElementById('SubClass');
	var SubClass;
	if (SubClassSel.options.length>0) {
		SubClass = '("'+SubClassSel.options[SubClassSel.selectedIndex].value+'")';
	} else {
		SubClass = '';
	}
	var DateToSel = document.getElementById('DateTo');
	var DateTo = DateToSel.options[DateToSel.selectedIndex].value;
	if (DateTo) {
		DateTo = DateTo;
	}
	var DateFromSel = document.getElementById('DateFrom');
	var DateFrom = DateFromSel.options[DateFromSel.selectedIndex].value;
	if (DateFrom) {
		DateFrom = DateFrom;
	}
	var RAndDSel = document.getElementById('RAndD');
	var RAndD = RAndDSel.options[RAndDSel.selectedIndex].value+' ';
	if (RAndD == 'AllRAndD ') {
		RAndD = '';
	} else {
		RAndD = '("'+RAndD+'")';
	}
	var RAndDSubSel = document.getElementById('RAndDSub');
	var RAndDSub;
	if (RAndDSubSel.options.length>0) {
		RAndDSub = '("'+RAndDSubSel.options[RAndDSubSel.selectedIndex].value+'")';
	} else {
		RAndDSub = '';
	}
	var InstitutionSel = document.getElementById('Institution');
	var Institution = InstitutionSel.options[InstitutionSel.selectedIndex].value+' ';
	if (Institution == 'AllInstitution ') {
		Institution = '';
	}
	var InstitutionSubnSel = document.getElementById('InstitutionSub');
	var InstitutionSub;
	if (InstitutionSubnSel.options.length>0) {
		var InstitutionSub = '("'+InstitutionSubnSel.options[InstitutionSubnSel.selectedIndex].value+'")';
	} else {
		InstitutionSub = '';
	}
	var FundedSel = document.getElementById('Funded');
	var Funded = FundedSel.options[FundedSel.selectedIndex].value;
	if (Funded == 'ALLFunded') {
		Funded = '';
	} else {
		Funded = '("'+Funded+'")';
	}

	var txtSearch = trim(document.getElementById('SearchField').value);

	if (txtSearch != "") {
		txtSearch = "("+txtSearch+")";
	}
	//Build Search Query  
	var searchQuery;
	searchQuery = txtSearch;
	//Classification 
	searchQuery = BuildQuery(searchQuery, SubClass);
	// R&D
	searchQuery = BuildQuery(searchQuery, RAndDSub);

	// Institution
	searchQuery = BuildQuery(searchQuery, InstitutionSub);
	// Funded
	searchQuery = BuildQuery(searchQuery, Funded);

	//*******************
    var strDate 
	strDate = GetDateWuery (DateTo, DateFrom)
		if (parseInt(DateTo ) > parseInt(DateFrom))
		{
			alert ("The From Date date should be less than the To Date")
			return;
			}
	searchQuery = BuildQuery(searchQuery, strDate );
	var strSearchData = searchQuery
	//alert(strSearchData)
	//this.location = 'sessearch.cgi?cnt=10&q='+strSearchData;
	this.location = 'runsearch.php?cnt=10&q='+strSearchData;
	//postwith (strSearchData)

}

function GetDateWuery (FromD,ToD)
{
	var strDate
	var i
	strDate = ''
	i= FromD

	for (i=FromD; i <= ToD; i++)
	{

		strDate += '"' + 'Year' + i + '"'
		if (i < ToD)
		{
			strDate += "OR"
		}
		
	}
	if (strDate == '"Year"')
	{
		strDate = ''
	}
	else 
	{
		strDate = '(' + strDate + ')'
	}
 	 
	 return strDate;
	 
	
}

function BuildQuery(CurrentValue, AddedValue) {
	if (CurrentValue != "" && AddedValue != "") {
		CurrentValue += " and "+AddedValue;
	} else {
		CurrentValue += AddedValue;
	}
	return CurrentValue;
}

//String . Trim 
 function trim(str)
	   {
		 s = str.replace(/^(\s)*/, '');
		 s = s.replace(/(\s)*$/, '');

		 return s;
	   }