function createPermanentCookie(name,value,domain){days=30*365;var date=new Date();date.setTime(date.getTime()+(days*24*60*60*1000));var expires="; expires="+date.toGMTString();if(domain!=null)
domain="; domain="+domain;else
domain="";document.cookie=name+"="+value+expires+"; path=/";}
function readCookie(cookiename){var cookiestring=""+document.cookie;var index1=cookiestring.indexOf(cookiename);if(index1==-1||cookiename=="")return"";var index2=cookiestring.indexOf(';',index1);if(index2==-1)index2=cookiestring.length;return unescape(cookiestring.substring(index1+cookiename.length+1,index2));}
function setCountry(id,name,domain){createPermanentCookie("countryId",id,domain);if(document.getElementById('countryName')){document.getElementById('countryName').innerHTML=name;}}
function setAllRegions(domain){if(document.getElementById('regionSeparator')){document.getElementById('regionSeparator').innerHTML='';}
if(document.getElementById('regionName')){document.getElementById('regionName').innerHTML='';}
createPermanentCookie("regionId",'-1',domain);}
function setRegion(id,name,domain){if(document.getElementById('regionSeparator')){document.getElementById('regionSeparator').innerHTML='&raquo;';}
if(document.getElementById('regionName')){document.getElementById('regionName').innerHTML=name;}
createPermanentCookie("regionId",id,domain);}
function setRegionChoosen(isChoosen){createPermanentCookie("regionChoosen",isChoosen);}
function selectedOption(selectId){selectElement=document.getElementById(selectId);return selectElement.options[selectElement.selectedIndex];}
function chooseRegionCountry(urlMap,appDomain){inMap='';sa=urlMap.split(',');selectedRegionOption=selectedOption('regionSelection');selectedCountryOption=selectedOption('countrySelection');for(var i=0;i<=sa.length-3;i+=3){if(selectedCountryOption.value==sa[i+1]&&selectedRegionOption.value==sa[i+2]){inMap=sa[i];break;}}
inMapCount=0;if(inMap!=''){for(var i=0;i<=sa.length-3;i+=3){if(inMap==sa[i]){inMapCount++;}}}
if(inMap==''){host='//'+window.location.hostname;if(window.location.port!='')
host=host+':'+window.location.port;if(appDomain.indexOf(host)>=0){chooseCountry();chooseRegion();}else{country=selectedOption('countrySelection').value;region=selectedOption('regionSelection').value;loc=location.protocol+appDomain+location.pathname;if(loc.indexOf("?")>-1){loc=loc+'&cid='+country+'&&rid='+region;}else{loc=loc+'?cid='+country+'&&rid='+region;}
location.href=loc;}}else{country=selectedOption('countrySelection').value;region=selectedOption('regionSelection').value;loc=location.protocol+'//'+inMap+location.pathname;if(inMapCount>0){if(loc.indexOf("?")>-1){loc=loc+'&cid='+country+'&&rid='+region;}else{loc=loc+'?cid='+country+'&&rid='+region;}}
window.location.href=loc;}}
function chooseRegion(domain){selectedRegionOption=selectedOption('regionSelection');if(selectedRegionOption.value==-1){setAllRegions(domain);}
else{setRegion(selectedRegionOption.value,selectedRegionOption.text,domain);}
setRegionChoosen(true);}
function chooseCountry(domain){selectedCountryOption=selectedOption('countrySelection');setCountry(selectedCountryOption.value,selectedCountryOption.text,domain);setAllRegions(domain);setRegionChoosen(true);}
function closeRegionChangeForm(){document.getElementById("regionChangeForm").style.display='none';if('function'==typeof onRegionChoose){onRegionChoose();}
}
function refresh(){window.location=unescape(window.location.pathname);}

function showRegionChange()

{ 

	getRegionVieport();
	
	document.getElementById('regionDiv').innerHTML='';
	
	new Ajax.Request('/choose-region.html', {method:'get',onSuccess:function(transport) 
	
	{var response=transport.responseText||""; 
	
	document.getElementById('regionDiv').innerHTML='';}});
	
	var value=document.getElementById("hidInput").value;if(value=="false"){document.getElementById("regionChangeForm").style.display='block';}
	
	else{document.getElementById("regionChangeForm").style.display='none';}}
	
	var viewportwidth;var viewportheight;
	
	function getRegionVieport(){if(typeof window.innerWidth!='undefined'){viewportwidth=window.innerWidth,viewportheight=window.innerHeight}
else if(typeof document.documentElement!='undefined'&&typeof document.documentElement.clientWidth!='undefined'&&document.documentElement.clientWidth!=0){viewportwidth=document.documentElement.clientWidth,viewportheight=document.documentElement.clientHeight}
else{viewportwidth=document.getElementsByTagName('body')[0].clientWidth,viewportheight=document.getElementsByTagName('body')[0].clientHeight}
var elem=$('regionChangeForm');if(elem!=null){var offsetY=(viewportheight/2)-97;var offsetX=(viewportwidth/2)-197;var scrolledY=0;var scrolledX=0;if(window.pageYOffset)scrolledY=window.pageYOffset;if(window.pageXOffset)scrolledX=window.pageXOffset;else{if(document.documentElement.scrollTop)
scrolledY=document.documentElement.scrollTop;if(document.documentElement.scrollLeft)
scrolledX=document.documentElement.scrollLeft;}
elem.style.top=offsetY+scrolledY+'px';elem.style.left=offsetX+scrolledX+'px';}}
Event.observe(window,'load',getRegionVieport,false);Event.observe(window,'resize',getRegionVieport,false);Event.observe(window,'scroll',getRegionVieport,false);