var gTempTypeName ="All";
var gTempSubTypeName ="All";
var gMainSubName ="All";
var gSubSubName ="All";
  
function resetForm()
{
	document.getElementById("frmAdvSearch").reset();
}

function SelectAllSearchPlaces(allPlaces)
{
	if (allPlaces.value == 0)
	{
		allPlaces.value = 1;
				
		document.getElementById("allPlaces").checked = 1;
		document.getElementById("place_Title").checked = 1;
		document.getElementById("place_Summary").checked = 1;
		document.getElementById("place_Text").checked = 1;
		
		document.getElementById("place_Title").value = 1;
		document.getElementById("place_Summary").value = 1;
		document.getElementById("place_Text").value = 1;
		
		document.getElementById("place_Title").disabled=1;
		document.getElementById("place_Summary").disabled=1;
		document.getElementById("place_Text").disabled=1;
	} 
	else 
	{
		document.getElementById("allPlaces").checked = 0;
		
		document.getElementById("allPlaces").value = 0;
		
		document.getElementById("place_Title").disabled=0;
		document.getElementById("place_Summary").disabled=0;
		document.getElementById("place_Text").disabled=0;
	} 
}

function ChangeSelect(bFlag)
{
	var id = "" ;
	var ManiList	= document.all.mainSubject;
	var SubListName = document.all.mainSubject;
	var MainHidden	= document.all.MainSubID;
	var SubHidden	= document.all.subSubID;
	var MainName	= "" , SubName ="";

	if(bFlag == 1)
	{
		ManiList	= document.all.template_type;
		SubListName = document.all.template_type;
		MainHidden  = document.all.temp_Type;
		SubHidden   = document.all.template_sub_type;
	}
	//var CurrentValue = ManiList.value;
	var CurrentValue =  ManiList.options[ManiList.selectedIndex].value;

	if(CurrentValue == "*")
	{
		if(bFlag == 1)
		{
			gTempTypeName		= ManiList.options[ManiList.selectedIndex].text;
//			gTempSubTypeName	= "الجميع";
		//	gTempSubTypeName	= unescape("%u0627%u0644%u062C%u0645%u064A%u0639");;
            gTempSubTypeName    ="All" ;
		
		}
		else
		{
			gMainSubName = ManiList.options[ManiList.selectedIndex].text;
//			gSubSubName	= "الجميع";
			//gSubSubName	= unescape("%u0627%u0644%u062C%u0645%u064A%u0639");;
		     gSubSubName    ="All";
		} 
				
		return 1;
	}

	for(i=0; i < ManiList.length - 1 ; i++)
	{	
		id = ManiList.id + parseInt(ManiList.item(i+1).value) ;
		if( document.all[id] != null ) document.all[id].className = "hide" ;
	
	}
	
	//show the corresponding sub list
	id = ManiList.id + parseInt(ManiList.item(ManiList.selectedIndex).value);
	if( document.all[id] != null ) document.all[id].className = "showTemplate" ;
	if( bFlag == 1)
	{
		gTempTypeName		= ManiList.options[ManiList.selectedIndex].text;
		if( document.all[id] != null )
			gTempSubTypeName	= document.all[id].options[document.all[id].selectedIndex].text;
		//if( document.all.item(id) != null )
		//gTempSubTypeName	= document.all.item(id).options[document.all[id].selectedIndex].text;
	}
	else
	{
		gMainSubName		= ManiList.options[ManiList.selectedIndex].text;
		if( document.all[id] != null )
			gSubSubName			= document.all[id].options[document.all[id].selectedIndex].text;
	}
		
	if( document.all[MainHidden.id] != null )document.all[MainHidden.id].value = CurrentValue ;
	if(document.all[id] != null && document.all[SubHidden.id] != null )
	{
		document.all[SubHidden.id].value = document.all[id].value ;
	}
			
	return 1;
}//end of ChangeSelect(bFlag)

function Enable_Date(bCondition)
{
	document.getElementById("DurationType").value = bCondition;
	document.getElementById("DurationFlag").value = !bCondition;
	document.getElementById("DateFlag").value = bCondition;
	
	//document.getElementById("DurationFlag").checked = !bCondition;
	//document.getElementById("DateFlag").checked = bCondition;
	
	document.getElementById("Duration_List").disabled = bCondition;
	document.getElementById("fromDay1").disabled = !bCondition;
	document.getElementById("fromMonth1").disabled = !bCondition;
	document.getElementById("fromYear1").disabled = !bCondition;
	document.getElementById("toDay1").disabled = !bCondition;
	document.getElementById("toMonth1").disabled = !bCondition;
	document.getElementById("toYear1").disabled = !bCondition;
}

function ValidateData1()
{  
    //alert("testtttt ...");
	var lang = get_lang();
	var collectData;
	
	GetSelDate();
		
	if (trim(document.getElementById("advSearchText").value) == "")
	{
		alert("Please insert at least one word.");
		document.getElementById("advSearchText").value = "";
		document.getElementById("advSearchText").focus();
		return false;
	}

	if (validateSearchText() == 1)
	{
		alert("Please insert at least one word.");
		return false;
	}
	
	if(document.all.AllFlag)
	{
		if(!document.all.AllFlag.checked)
		{
			if(document.all.AndFlag.checked)
			{
				if((document.all.Countries1.options[document.all.Countries1.selectedIndex].value == 0) &&
				(document.all.Countries2.options[document.all.Countries2.selectedIndex].value == 0) &&
				(document.all.Countries3.options[document.all.Countries3.selectedIndex].value == 0))
				{
					alert("Please select one country at least.");
					return false;
				}
			}
			
			if(document.all.OrFlag.checked)
			{
				if((document.all.Countries4.options[document.all.Countries4.selectedIndex].value == 0) &&
				(document.all.Countries5.options[document.all.Countries5.selectedIndex].value == 0) &&
				(document.all.Countries6.options[document.all.Countries6.selectedIndex].value == 0))
				{
					alert("Please select one country at least.");
					return false;
				}
			}
		}
	}
	

	if(CheckDates() == false)
	{
		alert("\"Release from\" date must be less than or equal to \"Release to\" date.");
		return false;
	}

	if (document.getElementById("place_Title").checked)
		document.getElementById("plc_Title").value = 1;
	else
		document.getElementById("plc_Title").value = 0;

	if (document.getElementById("place_Summary").checked)
		document.getElementById("plc_Summary").value = 1;
	else
		document.getElementById("plc_Summary").value = 0;

	if (document.getElementById("place_Text").checked)
		document.getElementById("plc_Text").value = 1;
	else
		document.getElementById("plc_Text").value=0;

	collectData = GetAllData();

	collectData += "&AdvSearch=1";

	document.location.href= "/topics/search.asp?"+collectData + "&lng=" + lang + "&num=2";
}


function CheckDates()
{	
	var intFromYear = Math.ceil(document.all.fromYear1.value);
	var intFromMonth = Math.ceil(document.all.fromMonth1.value);
	var intFromDay = Math.ceil(document.all.fromDay1.value);
	
	var intToYear = Math.ceil(document.all.toYear1.value);
	var intToMonth = Math.ceil(document.all.toMonth1.value);
	var intToDay = Math.ceil(document.all.toDay1.value);
	
	
	if(document.all.DurationType)
	{
		if(document.all.DurationType.value == 0)
			return true;
		else
		{
			if(intFromYear == 0 || intFromMonth == 0 || intFromDay == 0 || intToYear == 0 || intToMonth == 0 || intToDay == 0)
				return false;
				
			if(intFromYear > intToYear)
				return false;
			else
				if(intFromYear < intToYear)
					return true;
				else
				{
					if(intFromMonth > intToMonth)
						return false;
					else
						if(intFromMonth < intToMonth)
							return true;
						else
						{
							if(intFromDay > intToDay)
								return false;
							else
								if(intFromDay < intToDay)
									return true;
						}
				}
			
		}
	}
	
	return true;
}

function get_lang()
{
	var lang;

	if (document.getElementById("lang"))
		lang = document.getElementById("lang").value;
	
	return lang;
}

function GetSelDate()
{
	var SearchDateType	 = document.getElementById("DurationType").value;
	var Duration = document.getElementById("Duration").value;
	var FromDate = "";

	if (SearchDateType == "0")
	{
		var TempDate = new Date();
		FromDate = Duration;

		if (Duration == "1") FromDate = GetY_M_D(0, 0, 1);
		if (Duration == "2") FromDate = GetY_M_D(0, 0, 7);
		if (Duration == "3") FromDate = GetY_M_D(0, 1, 0);
		if (Duration == "4") FromDate = GetY_M_D(1 ,0 , 0);
	}
	else
	{
		FromDate = document.getElementById("fromDay1").value + "/" + document.getElementById("fromMonth1").value + "/" +  document.getElementById("fromYear1").value;
		document.getElementById("ToDate").value = document.getElementById("toDay1").value + "/" + document.getElementById("toMonth1").value + "/" +  document.getElementById("toYear1").value;
	}
	document.getElementById("FromDate").value = FromDate;
}

function GetY_M_D(Y,M,D)
{
	var currDate = new Date();
	var d = new Date(currDate.getFullYear()-Y,currDate.getMonth()-M,currDate.getDate()-D);
	return (d.getDate()<10 ? "0"+d.getDate() : d.getDate()) + "/" + (d.getMonth()+1<10 ? "0"+(d.getMonth()+1) : d.getMonth()+1) + "/" + d.getFullYear();
}


function validateSearchText()
{
	var bFlag = 0;
	var Counter = 0;
	var tempText = "";
	var strSearch = "";
			
	strSearch = document.getElementById("advSearchText").value;
	document.getElementById("text").value = document.getElementById("advSearchText").value;
	
	for(Counter; Counter<strSearch.length; Counter++)
	{
		tempText += document.getElementById("text").value.charAt(Counter);
				
		if (document.getElementById("text").value.charAt(Counter) == "+")
			break;
		else
			if (document.getElementById("text").value.charAt(Counter) == "/")
			{
				bFlag = 1;
				document.getElementById("Sign").value = 1;
				break;
			}
	}
			
	RetVal = GetTextSearch(bFlag,Counter,strSearch);
	
	if (RetVal == 1)
		return RetVal;
		
	document.getElementById("text").value = RetVal;
			
	return 0;
}

function GetTextSearch(bFlag,start,strSearch)
{
	var Counter = start;
	var cOperator = "+";
	var cOperator2 = "/";
	var temp = strSearch.substr(0, start);

	if (bFlag)
	{
		cOperator = "/";
		cOperator2 = "+";
	}
	
	for (Counter; Counter<strSearch.length; Counter++)
		if (strSearch.charAt(Counter) == cOperator)
		{
			if (Counter && (strSearch.charAt(Counter-1) != cOperator))
				temp += " ";
		}
		else
			if (strSearch.charAt(Counter) == cOperator2)
				return 1;
			else
				temp += strSearch.charAt(Counter);

	return temp;
}

function Navigate(typeNav)
{
	var collectData = "";
	var record_Count	= 0;
	var titlesCount = 0;
	var buf = 10;
	var count = 1;

	collectData = GetAllData();
	
	if (document.getElementById("record_Count"))
		record_Count = parseInt(document.getElementById("record_Count").value);

	if (record_Count <= 0)
	{
		alert("no records");
		return;
	}
			
	if (document.getElementById("TitlesCount"))
		titlesCount = parseInt(document.getElementById("TitlesCount").value);

	if (document.getElementById("BUFF"))
		buf = document.getElementById("BUFF").value;

	if (document.getElementById("counter"))
		count = parseInt(document.getElementById("counter").value);

	collectData +="&BUFF=" + buf;
					
	switch (typeNav)
	{
		//Move to first records
		case 1:
			collectData += "&START_POINT=0&Direction=f";
			collectData += "&First=1&Prev=1";
			break;
			
		//Move to next records
		case 3:
			if (document.getElementById("START_POINT"))
			{
				document.getElementById("START_POINT").value = count;
				if (parseInt(record_Count) < parseInt(document.getElementById("START_POINT").value,10) + parseInt(document.getElementById("BUFF").value))
					collectData +="&Next=1&Last=1&Direction=f";

				collectData += "&START_POINT=" + document.getElementById("START_POINT").value;
			}
			
			collectData += "&First=0&Prev=0&counter=" + count;
			break;
			
		//Move to previous records
		case 2:
			count = count - titlesCount - buf;
			
			if (count <= 0)
			{				
				Navigate(1);
				return;
			}

			if (document.getElementById("START_POINT"))
			{
				document.getElementById("START_POINT").value = count;
				collectData += "&START_POINT=" + document.getElementById("START_POINT").value;
				
				if (parseInt(document.getElementById("START_POINT").value,10) >= buf)
					collectData +="&First=0&Prev=0";
				else
					collectData +="&First=1&Prev=1";
			}
			
			collectData += "&Direction=f&counter=" + count;
			break;

		//Move to last records
		case 4:
			var nRemainder = parseInt(record_Count) % parseInt(buf) == 0 ? 1 : 0;
			var nDiff = parseInt( parseInt(record_Count) / parseInt(buf) ) - nRemainder;
			count = nDiff * parseInt(buf) + 1;
			document.getElementById("START_POINT").value = count;
			collectData += "&First=0&Last=1&Prev=0&Next=1&counter=" + count;
			collectData += "&Direction=f&START_POINT=" + count;			
			break;	
	}
	
	document.location.href = "/topics/search.asp?" + collectData + "&num=2";
}

function GetAllData()
{
    var cu_no=1;
    var dataCollection = "cu_no=1";
    
	dataCollection += "&Sign=";
	if (document.getElementById("Sign")) dataCollection += document.getElementById("Sign").value;
	
	dataCollection += "&text=";
	if (document.getElementById("text")) 
		if (document.all)
			dataCollection += escape(document.getElementById("text").value);
		else
			dataCollection += document.getElementById("text").value;
	
	dataCollection += "&plc_Title=";
	if (document.getElementById("plc_Title")) dataCollection += document.getElementById("plc_Title").value;
	
	dataCollection += "&plc_Summary=";
	if (document.getElementById("plc_Summary")) dataCollection += document.getElementById("plc_Summary").value;
	
	dataCollection += "&plc_Text=";
	if (document.getElementById("plc_Text")) dataCollection += document.getElementById("plc_Text").value;
	
	dataCollection += "&temp_Type=";
	if (document.getElementById("temp_Type")) dataCollection += document.getElementById("temp_Type").value;
	
	dataCollection += "&template_sub_type=";
	if (document.getElementById("template_sub_type")) dataCollection += document.getElementById("template_sub_type").value;
	
	dataCollection += "&MainSubID=";
	if (document.getElementById("MainSubID")) dataCollection += document.getElementById("MainSubID").value;
	
	dataCollection += "&subSubID=";
	if (document.getElementById("subSubID")) dataCollection += document.getElementById("subSubID").value;
	
	dataCollection += "&DurationType=";
	if (document.getElementById("DurationType")) dataCollection += document.getElementById("DurationType").value;
	
	dataCollection += "&FromDate=";
	if (document.getElementById("FromDate")) dataCollection += document.getElementById("FromDate").value;
	
	dataCollection += "&ToDate=";
	if (document.getElementById("ToDate")) dataCollection += document.getElementById("ToDate").value;
	
	dataCollection += "&Countriesflag=";
	if (document.getElementById("CountriesFlag")) dataCollection += document.getElementById("CountriesFlag").value;
	
	dataCollection += GetSelCountries();
	
	if (document.getElementById("titlesPerPage")) dataCollection += "&BUFF=" + document.getElementById("titlesPerPage").value;
	if (document.getElementById("titlesOrder")) dataCollection += "&order_Type=" + document.getElementById("titlesOrder").value;
	if (document.getElementById("advSearch")) dataCollection += "&advSearch=" + document.getElementById("advSearch").value;
	
	return dataCollection;
}

function GetSelCountries()
{
	var SelCountry1 = "0";
	var SelCountry2 = "0";
	var SelCountry3 = "0";
	
	if (document.getElementById("CountriesFlag") && document.getElementById("CountriesFlag").value == "0")
	{ 
		if (document.getElementById("Countries1") && document.getElementById("Countries2") && document.getElementById("Countries3"))
		{
			SelCountry1 = document.getElementById("Countries1").value;
			SelCountry2 = document.getElementById("Countries2").value;
			SelCountry3 = document.getElementById("Countries3").value;
		}
	}
	else
		if (document.getElementById("CountriesFlag") && document.getElementById("CountriesFlag").value == "1")
			if (document.getElementById("Countries4") && document.getElementById("Countries5") && document.getElementById("Countries6"))
			{
				SelCountry1 = document.getElementById("Countries4").value;
				SelCountry2 = document.getElementById("Countries5").value;
				SelCountry3 = document.getElementById("Countries6").value;
			}
		
	if (document.getElementById("SelCountry1") && document.getElementById("SelCountry2") && document.getElementById("SelCountry3"))
	{
		SelCountry1 = document.getElementById("SelCountry1").value;
		SelCountry2 = document.getElementById("SelCountry2").value;
		SelCountry3 = document.getElementById("SelCountry3").value;
	}	
	
	return "&SelCountry1=" + SelCountry1 + "&SelCountry2=" + SelCountry2 + "&SelCountry3=" + SelCountry3;
}


function Enable_Date(bCondition)
{
	document.all.DurationType.value 	= bCondition;
	document.all.DurationFlag.value 	= !bCondition;//bCkecked;
	document.all.DurationFlag.checked 	= !bCondition;//bCkecked;
				
	document.all.Duration_List.disabled = bCondition;//!bCkecked;

	document.all.DateFlag.value 	= bCondition;//!bCkecked;
	document.all.DateFlag.checked 	= bCondition;//!bCkecked;

	document.all.fromDay1.disabled	= !bCondition;//bDisabled;
	document.all.fromMonth1.disabled	= !bCondition;//bDisabled;
	document.all.fromYear1.disabled	= !bCondition;//bDisabled;
				
	document.all.toDay1.disabled	= !bCondition;//bDisabled;
	document.all.toMonth1.disabled	= !bCondition;//bDisabled;
	document.all.toYear1.disabled	= !bCondition;//bDisabled;
				
}//end of Enable_Date(bCondition)
function EnableCountries(bCondition)
{
	if(bCondition == 0 || bCondition == 1)
	{
		document.all.CountriesFlag.value =bCondition;// 0;
		document.all.AllFlag.checked = false;

		document.all.AndFlag.value	= !bCondition;//1;
		document.all.AndFlag.checked = !bCondition;//true;
			
		document.all.Countries1.disabled = bCondition;//false;
		document.all.Countries2.disabled = bCondition;//false;
		document.all.Countries3.disabled = bCondition;//false;

		document.all.OrFlag.value = bCondition;//0;
		document.all.OrFlag.checked = bCondition;//false;
				
		document.all.Countries4.disabled = !bCondition;//true;
		document.all.Countries5.disabled = !bCondition;//true;
		document.all.Countries6.disabled = !bCondition;//true;
	}
	else // case of bCondition = 2
	{
		document.all.AllFlag.checked = true;

		document.all.AndFlag.checked = false;

		document.all.Countries1.disabled = true;
		document.all.Countries2.disabled = true;
		document.all.Countries3.disabled = true;

					
		document.all.CountriesFlag.value = "*";
		document.all.OrFlag.checked = false;
					
		document.all.Countries4.disabled = true;
		document.all.Countries5.disabled = true;
		document.all.Countries6.disabled = true;
				
	}
				
}