function FillProvince(mf,ship)
{
	if (mf==null) mf=jQuery('form:eq(0)').attr('id');
	if(!ship){
		var province = GetElement('address_state', mf);			//pour le Hidden	
		var temp=GetElement("province_list",mf);
	}else{
		var province = document.getElementById('shipstate').getElementsByTagName('input')[0];
		var temp=GetElement("province_listShip",mf);	
	}
	var selecteditem = 0;
	if (temp != null)
	{
		ResetSelect(temp);		
		for (k=1;k<=provinceid.length;k++)
			temp.options[k]=new Option(provincename[k-1],provinceid[k-1]);

		if (province!=null)
			for (k=1;k<=provinceid.length;k++)
			{
				if (temp.options[k].text == province.value)
				{
					temp.options[k].selected=true;
					selecteditem = k;				
				}
			}
	}
}

function FillArea(CityIndex,mf,ship)
{
	if (mf==null) mf=jQuery('form:eq(0)').attr('id');
	//Ctrl d'enregistrement
	if(!ship){
		var province = GetElement('address_state', mf);
		var city = GetElement('address_city', mf);
		var area = GetElement('address_line3', mf);		
		var temp=GetElement("area_list",mf);	
	}else{
		var province = document.getElementById('shipstate').getElementsByTagName('input')[0];
		var city = document.getElementById('shipcity').getElementsByTagName('input')[0];
		var area = document.getElementById('address_line3Ship').getElementsByTagName('input')[0];	
		var temp=GetElement("area_listShip",mf);
	}
		
	if (temp != null)
	{
		ResetSelect(temp);
		if (CityIndex != 0)//Cas sans ville
		{		
			//initialisation
			temp.length=areaid.length;
			var j=1;
			for (var i=0;i<areaid.length;i++)
			{
				if (areaid[i].substr(0,4)==CityIndex)
				{
					temp.options[j].text=areaname[i];
					temp.options[j].value=areaid[i];
					j=j+1;
				}			
			}
			if (j==1)
			{
				temp.length=1;
				document.getElementById("DivAreal").style.display="none";
			}
			else
			{
				temp.length=j;
				document.getElementById("DivAreal").style.display="block";
			}
		}
		else
			document.getElementById("DivAreal").style.display="none";

	}
}

function FillCity(ProvincIndex,mf,ship)
{
	if (mf==null) mf=jQuery('form:eq(0)').attr('id');
	var group=new Array();
	if(!ship){
		var province = GetElement('address_state', mf);	
		var city = GetElement('address_city', mf);				//pour le Hidden
		var quartier = GetElement('address_line3', mf);
		var temp=GetElement("city_list",mf);
	}else{
		var province =document.getElementById('shipstate').getElementsByTagName('input')[0];
		var city = document.getElementById('shipcity').getElementsByTagName('input')[0];	//pour le Hidden
		var quartier = document.getElementById('address_line3Ship').getElementsByTagName('input')[0];
		var temp = GetElement("city_listShip",mf);
	}
	var nboptions = 0; 
	
	if (temp != null)
	{
		ResetSelect(temp);
		
		
		
		//remplissage des groups
		group[0]= new Array();
		for (var k=0;k<provinceid.length;k++)
		{
			group[k+1]= new Array();
			for (var i=0;i<cityid.length;i++)
				if (cityid[i].substr(0,2)==provinceid[k]) 						
					group[k+1].push(new Option(cityname[i],cityid[i]));
			group[k+1].push(new Option("其它 ","其它 ")); //<option value="Other">Other</option>
		}
		
		if (ProvincIndex != null)
		{
			
			//Vider la liste pour les quartiers
			FillArea(0,mf,ship);
			if(!ship){
				SaveElement2("address_line3","",ProvincIndex,mf);}
			else
			{
				SaveElementShip(quartier,"",ProvincIndex,mf);
			}
		
			if (ProvincIndex == 0)
			{
				if(!ship){
					SaveElement2("address_city","",ProvincIndex,mf);
					FillArea(ProvincIndex,mf);
					SaveElement2("address_line3","",ProvincIndex,mf);
				}
				else
				{	
					SaveElementShip(city,"",ProvincIndex,mf);
					FillArea(ProvincIndex,mf,ship);
					SaveElementShip(quartier,"",ProvincIndex,mf);
				}
			}
				
			nboptions  = group[ProvincIndex].length;
			if (city!=null)
				temp.options[0].selected = true;
				for (k=0;k<nboptions;k++)
				{
					temp.options[k+1] = group[ProvincIndex][k];
					if (temp.options[k+1].text == city.value)
					{
						temp.options[k+1].selected=true;
					}
				}

		}
		//Mise à Jour lors d'un postback
		else if (province != null && province.value != "" && city != null)
		{
			var Other = true; //Pour le traitement du Other
			if(!ship){
				var temp2=GetElement("province_list",mf);
			}
			else
			{
				var temp2=GetElement("province_listShip",mf);
			}
			nboptions  = group[temp2.options.selectedIndex].length;					
			for (k=0;k<nboptions;k++)
			{
				temp.options[k+1] = group[temp2.options.selectedIndex][k];
				if (temp.options[k+1].text == city.value)
				{
					temp.options[k+1].selected=true;
					Other = false;							
				}
			}
			if (Other)
			{
				if (city.value != "")
				{
					temp.options[nboptions].selected=true; //Pour le Other
					//FillOtherCity(mf);
				}
				
				
			}
		}
	}			
}

function SetArea(mf,ship)
{
	if(!ship){
		var area_list	= GetElement("area_list",mf);
		var area		= GetElement('address_line3', mf);	
	}
	else
	{
		var area_list	= GetElement("area_listShip",mf);
		var area		= GetElement('address_line3Ship', mf);	
	}
	
	if (area_list != null)
	{
		
				SelectList(area_list,area.value);
			
	}
}

//------------------------------------------------//
// This function resets a select.                 //
//------------------------------------------------//
function ResetSelect(selectlist)
{
	// Remove options
	while (selectlist.options.length != 1)
	{
		selectlist.options[1] = null;
	}
}

function SelectList(objList,idstring)
{
	for (var i=0;i<objList.length;i++)
	{
		if (objList.options[i].text==idstring)
		{
			objList.options[i].selected = true;
			return;
		}
	}
	
}


function SaveElement2(ElementObject,ElementValue,Index,mf)
{
	if (mf==null) mf=jQuery('form:eq(0)').attr('id');
	var element  = GetElement(ElementObject,mf);
		
		if (element!=null) 
		{
			if (Index != 0) //Pour ne pas mettre le "selectionner dans l'element"
				element.value = ElementValue;
			else
				element.value = "";
		}	
}

/** special "shipping" **/
function SaveElementShip(element,ElementValue,Index,mf)
{
	if (mf==null) mf=jQuery('form:eq(0)').attr('id');
	//var element  = GetElement(ElementObject,mf);
		
		if (element!=null) 
		{
			if (Index != 0) //Pour ne pas mettre le "selectionner dans l'element"
				element.value = ElementValue;
			else
				element.value = "";
		}	
}


function SaveElement(ElementObject,ElementValue,mf)
{
	if (mf==null) mf=jQuery('form:eq(0)').attr('id');
	var element  = GetElement(ElementObject,mf);
	if (element!=null) 
			element.value = ElementValue;
}

/************************************************************************/
/*						Used to validate the Area						*/
/************************************************************************/
function ValidateArea(mf)
{
	if (mf==null) return;
	var ProvinceList = GetElement("province_list",mf)
	var AreaList = GetElement("area_list",mf)
	var Login = GetElement("login",mf)
	
	
	if (AreaList.selectedIndex == 0 
	&& ProvinceList.selectedIndex != 0 
	&& AreaList.options.length > 1) 
	{
		document.getElementById("AreaValid").style.color = "red";
		document.getElementById("AreaValid").style.fontWeight = "bold";
		window.scrollTo(0,0);
	}
	else
	{
		document.getElementById("AreaValid").style.color = "";
		document.getElementById("AreaValid").style.fontWeight = "normal";
				
		document.forms[mf].submit();
	}
}






