// JavaScript Document
/*.............................................................................*/
var txt_all="All";
var isFa = 0;
var LangFa=0;
/*.............................................................................*/
function setLang (Lang) 
{
  if (Lang==LangFa) isFa=1;
  else isFa=0;
}
/*----------------------------------------------*/
function jv_get_urtl () 
{
  t1   = location.href.lastIndexOf("/");
  from = location.href.substring (t1+1, location.href.length);
  return from;
}
/*----------------------------------------------*/
function SelectCity(city) {
 var obj_prov;
 var obj_city;
 obj_city = document.getElementById("cmb_city");
 obj_prov = document.getElementById("cmb_province");

 for (i=1; i<Cities.length; i++) {
  for (j=0; j<Cities[i].length; j++)  {
    if (Cities[i][j].text==city) {
	    obj_prov.selectedIndex=i;
      ComboBox_Changed("Alle",obj_prov);  
      obj_city.selectedIndex= (Cities[i].length==1)? 0: j+1;
	    break;
     }
    }
  }
	jv_search_start();  
}
/*----------------------------------------------*/  
function SelectCityByID(firstLine, ID) {
 var obj_prov;
 var obj_city;
 obj_city = document.getElementById("cmb_city");
 obj_prov = document.getElementById("cmb_province");
 selctedProvince =obj_prov.selectedIndex;
 obj_city.options[0]= new Option(firstLine, -1);
 if (selctedProvince==0)  {
   for (i=0; i<AllCities.length; i++) obj_city.options[i+1]= AllCities[i];
   return;
   }
 ComboBox_Changed(firstLine,obj_prov);
 for (i=0; i<obj_city.length; i++) 
     if (obj_city[i].value==ID) {
	  obj_city.selectedIndex=i;
	   break;
	  }
return;	  
}
/*----------------------------------------------*/  
function SelectSubcatByID(firstLine, ID) {
 var obj_prov;
 var obj_city;
 obj_subCat = document.getElementById("cmb_subCat");
 obj_mainCat = document.getElementById("cmb_mainCat");
 selectedMainCat =obj_mainCat.selectedIndex;
 
 obj_subCat.options[0]= new Option(firstLine, -1);
 if (selectedMainCat==0)  {
    if (isFa)  for (i=0; i<All_subCat_array_fa.length; i++) obj_subCat.options[i+1]= All_subCat_array_fa[i];
  else {
    for (i=0; i<All_subCat_array.length; i++) obj_subCat.options[i+1]= All_subCat_array[i];
    }
   return;
   }
 ComboBox_Changed(firstLine,obj_mainCat);
 for (i=0; i<obj_subCat.length; i++) 
     if (obj_subCat[i].value==ID) {
	  obj_subCat.selectedIndex=i;
	   break;
	  }
return;	   

}
/*.............................................................................*/
function ComboBox_Changed(firstLine, obj)
{
var subObj;
txt_all = firstLine;
var mainObj;
 switch (obj.id) {
	 case "cmb_mainCat" :  //......................................................	 
	   subObj  = document.getElementById('cmb_subCat');
	   if (isFa==1) pvt_mainCmb_changed(obj, subObj,All_subCat_array_fa, subCat_array_fa); 
     else pvt_mainCmb_changed(obj, subObj,All_subCat_array, subCat_array);
	   break;
	 case "cmb_province" :  //......................................................
     subObj  = document.getElementById('cmb_city');
     pvt_mainCmb_changed(obj, subObj,AllCities, Cities);
	   break;
	 case "cmb_subCat" :  //......................................................
      var selectValue = obj.options[obj.selectedIndex].value;           
	    mainObj  = document.getElementById('cmb_mainCat');	     
	         
      if (mainObj.selectedIndex==0) { 
        if (isFa==1) pvt_subCmb_changed(mainObj, obj, subCat_array_fa, sub_2_main);
        else  pvt_subCmb_changed(mainObj, obj, subCat_array, sub_2_main);
        }
      break;
	 case "cmb_city" :  //......................................................
	    mainObj  = document.getElementById('cmb_province');
	    if (mainObj.selectedIndex==0) pvt_subCmb_changed( mainObj, obj, Cities, city_2_province);     
      break;
   } 
}
/*.............................................................................*/
function pvt_mainCmb_changed(mainObj, subObj, allSubItems, subItems)
{
 var i, offset;
 var ix;  
 ix = mainObj.selectedIndex;    
 for (i=subObj.options.length-1; i>=0; i--) subObj.options[i]=null;	/* clear sub list */
   if (ix == 0) {
     subObj.options[0] = new Option (txt_all, -1);     
 	   for (i=0; i<allSubItems.length; i++)  { 	    
      subObj.options[i+1] = allSubItems[i];            
      }
     subObj.selectedIndex = 0;      
     return;
     }     
   offset = 0;
   if (subItems[ix].length>1) {
     subObj.options[0] = new Option (txt_all, -1);
     offset = 1;
     }  
   for ( i=0; i<subItems[ix].length; i++) {  /* add items of sub category */
      subObj.options[i+offset] = subItems[ix][i];      
      }
   if (subItems[ix]==0) return;  
   subObj.selectedIndex = 0;
   subObj.disabled=false;  

}
/*.............................................................................*/

function pvt_subCmb_changed( mainObj, subObj, subItems, sub_2_main)
{
//if (mainObj.selectedIndex==0) pvt_subCmb_changed( mainObj, obj, Cities, city_2_province);
	var selectValue = subObj.options[subObj.selectedIndex].value;     
  var mainID =-1;
  for (i=0; i<sub_2_main.length; i+=2) 
  if (sub_2_main[i]==subObj.options[subObj.selectedIndex].value)  {
     mainID=sub_2_main[i+1];
		 break;
		 }
  if (mainID<=0) alert ("mainID not found");
  for (i=0; i<mainObj.options.length; i++)  
    if (mainObj.options[i].value == mainID) {    
		  mainObj.selectedIndex = i;
		  break;
		  }
  for (i=subObj.options.length-1; i>=0; i--) subObj.options[i]=null;
  subObj.options[0]= new Option (txt_all, 0);
	for (i=0; i<subItems[mainObj.selectedIndex].length; i++) 	  subObj.options[i+1]=subItems[mainObj.selectedIndex][i];		
	for (i=0; i<subItems[mainObj.selectedIndex].length+1; i++)
    if (subObj.options[i].value == selectValue) {
    subObj.selectedIndex = i; 
    break;
	  }
}
/*.............................................................................*/


