////////////////////////////////////
//Common Javascript functions
//Function related to page
//function ChangeCountry(language,type,uCountry,uState,uCity,font,form)function ChangeCountry(language,type,uCountry,font,form)
  { 	//language= "en";
	
	if(uCountry=="pleasecheck")
	{
		if(type=="delivery")		{									uCountry = form.delivery_Country[form.delivery_Country.selectedIndex].value;
				
			countryInst = new CountryInstance(language,type,uCountry*1+'','','',font,form);					}
		else if(type=="user")		{
			uCountry = form.user_Country[form.user_Country.selectedIndex].value;
			countryInst = new CountryInstance(language,type,uCountry*1+'','','',font,form);
		}
	}	else	{	
		if(type=="delivery"){
            // Added 20091228 - BYF - if there is a "delivery country selection" config, do not use intialDeliveryAddr
            if (typeof nonCountry[0] !='undefined' && typeof countryDisplay != 'undefined' && countryDisplay.length > 0) {
                // note: countryDisplay.length is a temp solution for getting CountryID value from "delivery country selection" config.
                // The "delivery country selection" config looks like this: countryDisplay[156]="1";
                countryInst = new CountryInstance(language,type, (countryDisplay.length-1)*1+'','','',font,form);
            }
            else{
                countryInst = new CountryInstance(language,type,intialDeliveryAddr[0]*1+'',intialDeliveryAddr[1],intialDeliveryAddr[2],font,form);
            }  
            // End - Added 20091228 - BYF - if there is a "delivery country selection" config, do not use intialDeliveryAddr            
        }
		else if(type=="user")			countryInst = new CountryInstance(language,type,intialUserAddr[0]*1+'',intialUserAddr[1],intialUserAddr[2],font,form);				}
	
	//load country

	if(rls.loadCountryFinished == 0)
	{
		var url = serverName+"/getCountries.jsp?language="+language;
		//var request = new AJAXRequest(url, 'setCountries');
		//request.send();
		
		callToServer(url,"setCountries");
	} 
	else
	{
		BuildCountrySelectBox();
		BuildStateCities(countryInst.language,countryInst.country);
	}			   	
}

function BuildCountrySelectBox()
{

    if((countryInst.type == "delivery")&&(rls.builddeliveryCountrySelectBox != "0")) return;    
    if((countryInst.type == "user")&&(rls.builduserCountrySelectBox != "0")) return;
		
	var td_country = document.getElementById(countryInst.type+"Addr_country");		    td_country.innerHTML = "<font class='"+countryInst.font+"'>"+countryTitle+"</font>:";            var td_countrylist = document.getElementById(countryInst.type+"Addr_countrylist");      var tr_town = document.getElementById(countryInst.type+"Addr_townTr");
        if(countryInst.type == "delivery")
    {		
		if (typeof countryInst.form.delivery_Country != 'undefined')   			 	countryInst.form.delivery_Country.length = 0; 
		 
		var jj =0; 			for(var i=0;i<country[1000].length;i++)			{				
						    if(((typeof nonCountry[0] !='undefined')&&(typeof countryDisplay[country[1000][i]] !='undefined'))|| ((typeof nonCountry[0] =='undefined')&&(typeof countryNonDisplay[country[1000][i]] =='undefined')) )
			    {			     
				  countryInst.form.delivery_Country.options[jj] = new Option(country[country[1000][i]][0],StringFMT(country[1000][i],3));		
							      if(country[1000][i] == countryInst.country)	
			      {								 countryInst.form.delivery_Country.options[jj].selected = true;				 				  }
				  jj++;	 				}					
			}	
		rls.builddeliveryCountrySelectBox ="1";	
	}
	else if(countryInst.type == "user")
	{
			
		if (typeof countryInst.form.user_Country != 'undefined')   	
			 	countryInst.form.user_Country.length = 0; 
		 
		for(var i=0;i<country[1000].length;i++)			{				countryInst.form.user_Country.options[i] = new Option(country[country[1000][i]][0],StringFMT(country[1000][i],3));										if(country[1000][i] == countryInst.country)
				{									countryInst.form.user_Country.options[i].selected = true;						}
			}
		rls.builduserCountrySelectBox ="1";			
	}	
	
}

function StringFMT(_String,_length)
{
	var retStr = _String+'';
	
	while(retStr.length<_length)
	   retStr = '0'+ retStr;
	
	return   retStr; 
	
}


function BuildStateCitySelectBox()
{
	
	//////refresh state cities select box////////       
    var td_state = document.getElementById(countryInst.type+"Addr_state");    var td_statelist = document.getElementById(countryInst.type+"Addr_statelist");
    var td_city = document.getElementById(countryInst.type+"Addr_city");    var td_citylist = document.getElementById(countryInst.type+"Addr_citylist");    
    var td_stateTr = document.getElementById(countryInst.type+"Addr_stateTr");
        var td_cityTr  = document.getElementById(countryInst.type+"Addr_cityTr");

    var tr_town = document.getElementById(countryInst.type+"Addr_townTr");        var townStar = document.getElementById(countryInst.type+"Addr_townStar");    
    var zipStar  = document.getElementById(countryInst.type+"Addr_zipStar");    
            //if country is singapore, no city name needed to fill
    if(countryInst.country=="702")					  			tr_town.style.display="none";  //hide
	else 
			tr_town.style.display="";  //display
    /////if country is usa, town is a must filled field//    if(countryInst.country=="840")       townStar.innerHTML = "*";    else   
       townStar.innerHTML = "";       
    ///////if country is US or Canada, zipcode is a must filled field//////    if((countryInst.country=="840"||countryInst.country=="124"))        zipStar.innerHTML = "*";    else       zipStar.innerHTML = "";
    //build statelist select box    
            
    if(country[countryInst.country][1]<3)
    {
		//td_state_tr.innerHTML = "";        //td_state.innerHTML = "";       //if(navigator.userAgent.indexOf("Firefox")!=-1) //firefox   broser   
       //{			if(countryInst.type=="delivery")			{
				countryInst.form.delivery_State.length=0;
				countryInst.form.delivery_State.options[0] = new Option(state[country[countryInst.country][3][0]],country[countryInst.country][3][0]);							countryInst.form.delivery_State.options[0].selected = true;
								
    
			}
			else
			{				countryInst.form.user_State.length=0;	
				countryInst.form.user_State.options[0] = new Option(state[country[countryInst.country][3][0]],country[countryInst.country][3][0]);							countryInst.form.user_State.options[0].selected = true;
				
			}			td_stateTr.style.display="none";		//}
       //else		 //   td_statelist.innerHTML = "<input type=\"hidden\" name=\""+countryInst.type+"_State\" value=\""+country[countryInst.country][3][0]+"\">";
       	       }
    else   
    {       		
						td_stateTr.style.display="";  //display
			td_state.innerHTML = "<font class='"+countryInst.font+"'>"+country[countryInst.country][2]+":</font>";					
			if(countryInst.type == "delivery")
			{				countryInst.form.delivery_State.length=0;				
				var jj =0;				for(var i=0;i<country[countryInst.country][3].length;i++)				{
				   if(((typeof nonState[countryInst.country] !='undefined')&&(typeof stateDisplay[country[countryInst.country][3][i]] !='undefined'))
				   || ((typeof nonState[countryInst.country] =='undefined')&&(typeof stateNonDisplay[country[countryInst.country][3][i]] =='undefined')))				   {
					 countryInst.form.delivery_State.options[jj] = new Option(state[country[countryInst.country][3][i]],country[countryInst.country][3][i]);
					 if(country[countryInst.country][3][i] == countryInst.state)						countryInst.form.delivery_State.options[jj].selected = true;					 jj++;	
				   }					}															}			else if(countryInst.type  == "user" )			{							countryInst.form.user_State.length=0;				
				for(var i=0;i<country[countryInst.country][3].length;i++)				{
					countryInst.form.user_State.options[i] = new Option(state[country[countryInst.country][3][i]],country[countryInst.country][3][i]);
					if(country[countryInst.country][3][i] == countryInst.state)						countryInst.form.user_State.options[i].selected = true;				}					}												
    }
     //build city list selectbox;
    if((country[countryInst.country][1]>1)&&(countryInst.country != '840'))
    {
			if(countryInst.state=="")  countryInst.state = country[countryInst.country][3][0];		td_cityTr.style.display="";				
				if(!(countryInst.country == '0'))  // non other country
			td_city.innerHTML = "<font class='"+countryInst.font+"'>"+cityTitle+"</font>:";		else				td_city.innerHTML = "";
		
				if(countryInst.type == "delivery")			{				countryInst.form.delivery_City.length=0;				
				var kk = 0;
				for(var i=0;i<states[countryInst.state].length;i++)				{				  				   if(((typeof nonCity[countryInst.state] !='undefined')&&(typeof cityDisplay[states[countryInst.state][i]] !='undefined'))
				   || ((typeof nonCity[countryInst.state] =='undefined')&&(typeof cityNonDisplay[states[countryInst.state][i]] =='undefined')))
				   {		
						countryInst.form.delivery_City.options[kk] = new Option(city[states[countryInst.state][i]],states[countryInst.state][i]);						if(states[countryInst.state][i] == countryInst.city)							countryInst.form.delivery_City.options[kk].selected = true;											kk++;	
					}				}																}		else if (countryInst.type == "user")					{				countryInst.form.user_City.length=0;
				for(var i=0;i<states[countryInst.state].length;i++)				{
					countryInst.form.user_City.options[i] = new Option(city[states[countryInst.state][i]],states[countryInst.state][i]);
					if(states[countryInst.state][i] == countryInst.city)						countryInst.form.user_City.options[i].selected = true;				}
					}
			
						
	}		
    else  // SG(type=1), HK(type=1), USA (840) etc
    {				 		 td_cityTr.style.display="none";	
		 				
		 						 if(countryInst.type=="delivery")		  {
				countryInst.form.delivery_City.length=0;				if(countryInst.country != '840')					
					countryInst.form.delivery_City.options[0] = new Option(city[states[country[countryInst.country][3][0]][0]],states[country[countryInst.country][3][0]][0]);			
				else
					countryInst.form.delivery_City.options[0] = new Option("","");							countryInst.form.delivery_City.options[0].selected = true;
				
		  }
		 else
		  {				countryInst.form.user_City.length=0;				if(countryInst.country != '840')					
					countryInst.form.user_City.options[0] = new Option(city[states[country[countryInst.country][3][0]][0]],states[country[countryInst.country][3][0]][0]);			
				else
					countryInst.form.user_City.options[0] = new Option("","");							countryInst.form.user_City.options[0].selected = true;
	
		  }					
		 
    }
  
    
    if(rls.loadNextAddressBox=="0")      
    {
		ChangeCountry(countryInst.language,"user",countryInst.country,countryInst.font,countryInst.form);
		rls.loadNextAddressBox="1";
	}
		

}

function setCountries(str)
{     
	
	var countries = str;
	for(var i=1; i<countries.length; i++)		
	{	
	    country[countries[i][0]] = [countries[i][1],countries[i][2],countries[i][3]];
	}
	    //countries[i][1]: name
	    //countries[i][2]: structtype
	    //countries[i][3]: state/prinvice name type
	country[1000] = countries[0]; // al country id list array

	rls.loadCountryFinished =1;
    //alert("1234")
	BuildCountrySelectBox();
	BuildStateCities(countryInst.language,countryInst.country);
	
}


function BuildStateCities(language,ucountry)
{


	if (typeof rls.loadStateCityFinished[countryInst.uCountry] != 'undefined')   
	{
		BuildStateCitySelectBox();
	}
	else
	{
		var url = serverName+"/getStateCityByCountry.jsp?countryID="+countryInst.country+"&language="+countryInst.language;				

		//var request = new AJAXRequest(url,setStateCities);	
	
		//request.send();
		callToServer(url,"setStateCities");	
	}

}


function setStateCities(str)
  {     
    //Cities str format: (returned from Ajax Server)
    //[[1, Beijing], [2, Shanghai]]
    //var stateCities = eval(str);    var stateCities = str;
	country[countryInst.country][3] = stateCities[0];
     for(var i=1;i<stateCities.length;i++)
    {		//all states		//stateCities[i]
		state[stateCities[i][0]]  = stateCities[i][1]; 	//state[stateid] = state name	;		var citylist = stateCities[i][2];  // city list 		
		states[stateCities[i][0]] = new Array;  //  stateCities[i][0] = stateid;		for(var j = 0;j <citylist.length; j++)		{
			city[citylist[j][0]] = citylist[j][1];   //	city[cityid]  = cityname;				
			states[stateCities[i][0]][j] = citylist[j][0];  // states[stateCities[i][0]] = city id list under this state;		 		}	 		
    }
    	BuildStateCitySelectBox();
  }


  
 function changeState(type,form)
	{
	  if(type=="delivery")
	  {
          var stateChosen= form.delivery_State[form.delivery_State.selectedIndex].value;
          form.delivery_City.length = 0;
          var jj=0;
          for(var ii =0; ii<states[stateChosen].length;ii++)
          {
          
          	if(((typeof nonCity[stateChosen] !='undefined')&&(typeof cityDisplay[states[stateChosen][ii]] !='undefined'))
				 || ((typeof nonCity[stateChosen] =='undefined')&&(typeof cityNonDisplay[states[stateChosen][ii]] =='undefined')))
            {
				form.delivery_City.options[jj] = new Option(city[states[stateChosen][ii]],states[stateChosen][ii]);
				jj++;
			}
		  }
                    
      }
      else
      {
		   var stateChosen= form.user_State[form.user_State.selectedIndex].value;
           form.user_City.length = 0;
           for(var ii =0; ii<states[stateChosen].length;ii++)
			 form.user_City.options[ii] = new Option(city[states[stateChosen][ii]],states[stateChosen][ii]);
      
      }
	}	

var currentCountryId;
var loadCountryFinished	= 0;
var country = new Array;
//i				:  countryId integer ; no "00" in front of string
//country[i][0] :  country name
//country[i][1] :  struct type 
//country[i][2] :  state title
//country[i][3] :  state id list array
var loadStateCityFinished = new Array;
var state = new Array;;
//state[i]		: i is state id, value is state name;	
var states = new Array; 
// states[i]	: i is state id, value of states[i] is "city id list" array in this state
var city   = new Array;;
// city[i]		: i is city id , value of city[i] is city name
var ajaxthreadfinished = 0;
////////////////////////////////////
var countryInst = new CountryInstance('en','delivery','156','','','',this.form);


function CountryInstance(uLanguage,uType,uCountry,uState,uCity,uFont,uForm)
{
	this.language = uLanguage;
	this.type	  = uType;
	this.country  = uCountry;
	this.state	  = uState;
	this.city	  = uCity;
	this.font	  = uFont;
	this.form     = uForm;
}
//alert("000");

function RegionLoadStatus(rLoadCountryFinished)
{	
	this.loadCountryFinished = rLoadCountryFinished;
	this.loadStateCityFinished = new Array;	
	this.builddeliveryCountrySelectBox = "0";
	this.builduserCountrySelectBox = "0";
	this.loadNextAddressBox  = "0";
	
}

var rls = new RegionLoadStatus(0); 
var PageCache = new Array;


function RefreshObj(lan,cateID,mallID,storeID,regionID,sCateID,itemID,totalNo,startNo,endNo,objType,objName,targetObjType,targetObjName)
{

	var objString = document.getElementById(targetObjType+":"+targetObjName);	
	modifiedLocation ="section:"+lan;
	modifiedLocation +=":"+cateID;
	modifiedLocation +=":"+mallID;
					modifiedLocation +=":"+storeID;
					modifiedLocation +=":"+regionID;
					modifiedLocation +=":"+sCateID;
					modifiedLocation +=":"+itemID;
					modifiedLocation +=":"+totalNo;
					modifiedLocation +=":"+startNo;
					modifiedLocation +=":"+endNo;
					modifiedLocation +=":"+objType;
					modifiedLocation +=":"+objName;
					modifiedLocation +=":"+targetObjType;
					modifiedLocation +=":"+targetObjName;
	
	if(typeof PageCache[sCateID*1] != 'undefined')
	{
	
		if(typeof PageCache[sCateID*1][startNo*1] != 'undefined')
		{	

			for(var i=0;i<PageCache[sCateID*1][startNo*1].length;i++)
			{
				if(PageCache[sCateID*1][startNo*1][i].objKey == (objType+":"+objName))
				{
					objString.innerHTML = PageCache[sCateID*1][startNo*1][i].PageString;					
					//////////add into browser history//////////////																		
					dhtmlHistory.add(modifiedLocation,"1");
					///////////////////////
					return;
				}
			}			
		}
		else 
			PageCache[sCateID*1][startNo*1] = new Array;						
	}
	else 
	{
		PageCache[sCateID*1] = new Array;
		PageCache[sCateID*1][startNo*1] = new Array;
	}
			
	objString.innerHTML = "<table width=100%><tr><td width=50></td><td><br><br><br><img src=\"/image/"+lan+"/loading.jpg\"></td></tr></table>";	
	var url  = "/getObject.jsp?language="+lan;    
		url += "&cateID="+cateID;
		url += "&mall_id="+mallID;		
		url += "&storeID="+storeID;    	
		url += "&regionID="+regionID;		
		url += "&scateID="+sCateID;
		url += "&itemID="+itemID;
		url += "&total="+totalNo;
		url += "&startNo="+startNo;
		url += "&endNo="+endNo;
		url += "&objType="+objType;
		url += "&objName="+objName;
		url += "&targetObjType="+targetObjType;
		url += "&targetObjName="+targetObjName;
    
    
    //alert(targetObjType+":"+targetObjName);
    //alert(objString);

	callToServer(url,"RefreshObjHandler");	
	
}
function RefreshObjHandler(displayString,targetObjType,targetObjName,sCateID,startNo,ObjectKey)
{
   
    var refreshBlock = document.getElementById(targetObjType+":"+targetObjName);    
	refreshBlock.innerHTML  = displayString;				
	
	PageCache[sCateID*1][startNo*1][PageCache[sCateID*1][startNo*1].length] = new PageCacheUnit(ObjectKey,displayString) ;					
	dhtmlHistory.add(modifiedLocation,"1");
}

function PageCacheUnit(ObjectKey,PageString)
{
	this.PageString = PageString;
	this.objKey		= ObjectKey;
}

function buildQueryString(theFormName) {
  var theForm = document.forms[theFormName];
  var qs = ''
  for (e=0;e<theForm.elements.length;e++) {
    if (theForm.elements[e].name!='') {
      qs+=(qs=='')?'?':'&'
      qs+=theForm.elements[e].name+'='+escape(theForm.elements[e].value)
      }
    }
  return qs
}



