// JS MAIN PROCESSING CODE

// add dir name for live env
// http://customerservice.webkinz.com/collectionscatalog/us_en/index.html
var app_dir = "collectionscatalog/";
var live_url = "customerservice";
if(!checkURL(live_url)) // on live has an extra dir
{
 app_dir = "";
}
var url = "http://" +getURL()+"/"+app_dir;

// 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 = getLocalLangDir(3);			
	if(location_dir.length != 5 || location_dir.indexOf("_") != 2)
    {  // give it a default value	    
		location_dir = "us_en";       
    }
}

// set index or redirection page for the current lang         
var page  = "product_lines.html";
if(!checkURL(page)) 
{
	page  = "index.html";
}

var clothingRestDir = "ClothingRestrictions";
if(checkURL(clothingRestDir)) 
{
	location_dir = location_dir + "/" + clothingRestDir;
	page  = "index.html";
}

var clothingPageNeedle = "clothing.html"
if(checkURL(clothingPageNeedle)) 
{	
	page  = clothingPageNeedle;
}

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

// FUNCTIONS
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_url = "customerservice";
	if(checkURL(live_url)) // on live has an extra dir
    {
	 location_dir = "collectionscatalog/" + location_dir;
	}

    // set index page for the current lang
    var url = "http://" +getURL()+"/"+location_dir+"/ClothingRestrictions/index.html";
    //alert(url);

	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;
	var location_array = location.split("/");        
	return location_array[2];
}

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


/**
* 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 = "us_en"; 
    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"; 
        }
	}
	// set index page for the current lang
	var index_pre = "";
	
    if( !checkURL("customerservice") )
	{
		//var idlurl = "http://dev129.sandbox.webkinz.com/";
        // for url = "http://integration83.sandbox.webkinz.com/"; use register32
		var idlurl = "http://register34.sandbox.webkinz.com/idl/"+location_dir+"/";
    }
	else //url == "customerservice.webkinz.com" - 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");
}

