// ALL CODE CONTAINED IN FUNCTIONS

// JS MAIN INDEX PAGE PROCESSING CODE
function indexLanguageInit()
{
	var url = "http://" +getURL();

	// add the lang cookie var ie: us_us
	var location_dir = readLocaleCookie('webkinzLocaleCookie');
	var url_page_orig_location = window.location;

	// add a default
	if( (location_dir == undefined) || (location_dir != 'ca_fr' && location_dir != 'fr_fr' && location_dir != 'es_es' && location_dir != 'it_it') )
	{
		location_dir = "us_en";
	}

	// set index or redirection page for the current lang
	var item_id = getQueryVariable('item_id');
	var page    = location_dir+"_index.php?item_id="+item_id;

	url = url+"/"+page;
	// only redirect if necessary
	if(url != url_page_orig_location)
	{
		window.location=url;
	}
}

function getQueryVariable(variable)
{
	var query = window.location.search.substring(1);
	var vars = query.split("&");
	for (var i=0;i<vars.length;i++)
	{
		var pair = vars[i].split("=");
		if (pair[0] == variable)
		{
			return pair[1];
		}
	}
}

function popup()
{
    var location_dir = readLocaleCookie('webkinzLocaleCookie');
	// add a default
    if(location_dir == undefined)
    {
		location_dir = getLocalLangDir(3);
		if(location_dir.length != 5 || location_dir.indexOf("_") != 2)
        {  // give it a default value
			location_dir = "us_en";
        }
	}
    var live_ip = "66.48.69.113";
	var live_url = 'storelocator.webkinz.com';
	var url = "";
	if(checkURL(live_ip) || checkURL(live_url)) // on live has an extra dir
    {
     url = "http://customerservice.webkinz.com/collectionscatalog";
	}
	else
	{
	 url = "http://dt.sandbox.webkinz.com";
	}
	url = url+"/"+location_dir+"/ClothingRestrictions/index.html";

	window.open(url,"clothingRestrictions","left="+(screen.width-817)/2+",top="+(screen.height-800)/2+",height=488,width=600,toolbar=0,titlebar=0,location=0,status=0,directories=0,menubar=0,scrollbars=1,resizable=0");
}

/**
* Return url
* author: Konstantin Glazov
*/
function getURL()
{
	var location = window.location.href;
	//alert(location);
	var location_array = location.split("/");
	return location_array[2];
}

function getLocalLangDir(url_part_index)
{
	var location = window.location.href;
	var location_array = location.split("/");

	//us_en_index.php?item_id=WE000163

	var filename_array = location_array[url_part_index].split("_index.php");
	//alert(filename_array[0]);

	return filename_array[0];
}


/**
* Return bool if string is found to be part of url
* author: David Toporowski
*/
function checkURL(needle_string)
{
	var return_bool_sting_found = true;
	if (window.location.href.indexOf(needle_string)==-1)
	{
		return_bool_sting_found = false;
	}
    return return_bool_sting_found;
}

function readLocaleCookie(name)
{
	var nameEQ=name+"=";
	var value;
	var ca=document.cookie.split(';');
	for(var i=0;i<ca.length;i++)
	{
		var c=ca[i];
		while(c.charAt(0)==' ')
			c=c.substring(1,c.length);
		if(c.indexOf(nameEQ)==0)
			value=c.substring(nameEQ.length,c.length);
	}
	return value;
}

function internationalDistributors()
{
	// lang var (it_it,fr_fr etc)
	var location_dir = readLocaleCookie('webkinzLocaleCookie');

    // add a default
    if(location_dir == undefined)
    {
		location_dir = getLocalLangDir(3);
		if(location_dir.length != 5 || location_dir.indexOf("_") != 2)
        {  // give it a default value
			location_dir = "us_en";
        }
	}

	var idlurl = "";
    //if( !checkURL("66.48.69.113") )
	if( checkURL("sandbox") )
	{
		//var idlurl = "http://dev129.sandbox.webkinz.com/";
        // for url = "http://integration83.sandbox.webkinz.com/"; use register32
		idlurl = "http://register30.sandbox.webkinz.com/idl/"+location_dir+"/";
    }
	else //url == "66.48.69.113" - must be live
	{
  		//idlurl = "http://www.webkinz.com/idl/"+location_dir+"/";
        idlurl = "http://mci2.webkinz.com/idl/"+location_dir+"/";
    }

  	window.open(idlurl, "idl","left="+(screen.width-640)/2+",top="+(screen.height-484)/2+",height=484,width=640,toolbar=0,titlebar=0,location=0,status=0,directories=0,menubar=0,scrollbars=0,resizable=0");
}

