////////////////////////////////////////////////////
// Annick Goutal Common Javascript
//
// Copyright (C) 2006 - 2007 Neue Media
//
// Notes:
// 
////////////////////////////////////////////////////
/*
// multi function onload function
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

// attached page element handling
function attachPageHandlers () {

	var term = document.getElementById("term");
	term.onfocus = function() {
		
	}
}

addLoadEvent (attachFormHandlers);
addLoadEvent (attachPageHandlers);
*/

////////////////////////////////////////////////////
// Pop ups
////////////////////////////////////////////////////
function AGPopUp(url, winName, width1, height1, scrolls1)
{ 
	var width1, height1, scrolls1;
	var features='toolbar=no,close=no,location=no,directories=no,status=no,menubar=no,scrollbars='+scrolls1+',status=no,width='+width1+',height='+height1+',left=400,top=200';
	window.open(url, winName, features);
}

////////////////////////////////////////////////////
// Dynamic site resizing
////////////////////////////////////////////////////

function sizeDetect()
{
	//alert('yep resized');

	//var width1 		= document.body.offsetWidth;
	var bodytag 	= document.getElementsByTagName("body")[0];
	var flashmovie 	= document.getElementById("flashmovie");
	var embedmovie 	= document.getElementById("embedmovie");

	var x,y;
	if (self.innerHeight) // all except Explorer
	{
		x = self.innerWidth;
		y = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
		// Explorer 6 Strict Mode
	{
		x = document.documentElement.clientWidth;
		y = document.documentElement.clientHeight;
	}
	else if (document.body) // other Explorers
	{
		x = document.body.clientWidth;
		y = document.body.clientHeight;
	}

	//alert(x);
	if (x < 985) 
	{ 
		//document.body.setAttribute("class", "small");
		bodytag.className = 'small';
		//flashmovie.setAttribute("width", 560);
		//embedmovie.setAttribute("width", 560);
	}
	else
	{
		//document.body.setAttribute("class", "");
		bodytag.className = '';
		//flashmovie.setAttribute("width", 783);
		//embedmovie.setAttribute("width", 783);
	}


} 

////////////////////////////////////////////////////
// Language / Country Lightbox
////////////////////////////////////////////////////

// show languages
function showLanguages()
{ 
	var language_class = document.getElementById('lightbox-dropdown-language').className;

	// close the language drop down if open
	if (language_class == 'dropdown')
	{
		document.getElementById('lightbox-dropdown-language').className = 'dropdown-selected';
	}
	else
	{
		document.getElementById('lightbox-dropdown-language').className = 'dropdown';
	}
	
	// always close the country drop down if the laguage is clicked
	document.getElementById('lightbox-dropdown-country').className = 'dropdown';

}

// select a language
function selectLanguage(lang_id, lang_name)
{ 
	var lang_id;
	var lang_name;
	
	// close language drop down
	document.getElementById('lightbox-dropdown-language').className = 'dropdown';

	// set language drop down to the language name
	document.getElementById('lightbox-dropdown-language-link').innerHTML = lang_name;
	document.getElementById('lang_id').value = lang_id;
		
}

// show countries
function showLocales()
{ 
	var locales_class = document.getElementById('lightbox-dropdown-country').className;

	// close the language drop down if open
	if (locales_class == 'dropdown')
	{
		document.getElementById('lightbox-dropdown-country').className = 'dropdown-selected';
	}
	else
	{
		document.getElementById('lightbox-dropdown-country').className = 'dropdown';
	}
		
	// always close the language drop down if the laguage is clicked
	document.getElementById('lightbox-dropdown-language').className = 'dropdown';
}


// select a country
function selectLocale(locale_id, locale_name)
{ 
	var locale_id;
	var locale_name;
	
	// close language drop down
	document.getElementById('lightbox-dropdown-country').className = 'dropdown';

	// set language drop down to the language name
	document.getElementById('lightbox-dropdown-country-link').innerHTML = locale_name;
	document.getElementById('locale_id').value = locale_id;
		
}

////////////////////////////////////////////////////
// Product page drop downs
////////////////////////////////////////////////////

// products - show sort items drop down
function showSortedItems()
{ 
	var sort_class = document.getElementById('sort-items-by').className;

	if (sort_class == 'null')
	{
		document.getElementById('sort-items-by').className = 'selected';
	}
	else
	{
		document.getElementById('sort-items-by').className = 'null';
	}
		
}

// products - show items per page drop down
function showItemsPerPage()
{ 
	var perpage_class = document.getElementById('items-per-page').className;

	if (perpage_class == 'null')
	{
		document.getElementById('items-per-page').className = 'selected';
	}
	else
	{
		document.getElementById('items-per-page').className = 'null';
	}
		
}


////////////////////////////////////////////////////
// Generic boutique finder
////////////////////////////////////////////////////

// stores - show boutique country dropdown
function showBoutiqueCountries()
{ 
	var countries_class = document.getElementById('dropdown-country').className;
	var locations_class = document.getElementById('dropdown-location').className;

	// close the location drop down if open
	if (locations_class == 'dropdown-selected')
	{
		document.getElementById('dropdown-location').className = 'dropdown';
	}

	if (countries_class == 'dropdown')
	{
		document.getElementById('dropdown-country').className = 'dropdown-selected';
	}
	else
	{
		document.getElementById('dropdown-country').className = 'dropdown';
	}
		
}

// stores - get store locations based on country id
function getBoutiqueLocations(country_id, country_name)
{ 
	var country_id;
	var country_name;
	var container;
	var url;
	
	// close boutique country drop down
	document.getElementById('dropdown-country').className = 'dropdown';

	// set boutique country drop down to the country name
	document.getElementById('dropdown-country-link').innerHTML = country_name;
	document.getElementById('dropdown-country-link').setAttribute("rel",country_id);

	// set boutique location first element to 'Choose a location'
	document.getElementById('dropdown-location-link').innerHTML = "Choose a location";

	// ajax request for the associate locations
	//alert(country_id);
	container = "dropdown-location-container";
	url = MAIN_URL + "get+boutique+locations/" + country_id;
	//alert(url);
	new Ajax.Updater(container, url, {
	
										onComplete:function(request){showBoutiqueLocations();
																		
																	}, 
	
										asynchronous:true
									 });
	
		
}

// stores - show boutique location dropdown
function showBoutiqueLocations()
{ 
	var locations_class = document.getElementById('dropdown-location').className;

	if (locations_class == 'dropdown')
	{
		document.getElementById('dropdown-location').className = 'dropdown-selected';
	}
	else
	{
		document.getElementById('dropdown-location').className = 'dropdown';
	}
		
}

// stores - set store locations based on one selected
function setBoutiqueLocation(location_name)
{ 
	var location_name;

	// close boutique country drop down
	document.getElementById('dropdown-location').className = 'dropdown';

	// set boutique country drop down to the country name
	document.getElementById('dropdown-location-link').innerHTML = location_name;

	// set boutique button
	document.getElementById('boutique-button').style.display = 'block';

}

// stores - go to stores page
function gotoBoutiques()
{ 
	var country_id;
	var url;
	var location;
	
	country_id 	= document.getElementById('dropdown-country-link').getAttribute("rel");
	location 	= document.getElementById('dropdown-location-link').innerHTML;
	url 		= MAIN_URL + "find+a+boutique/" + country_id + "/" + location; 
	
	window.location = url;
}

////////////////////////////////////////////////////
// Stores page boutique finder
////////////////////////////////////////////////////

// stores - show boutique country dropdown
function showStoresBoutiqueCountries()
{ 
	var countries_class = document.getElementById('stores-dropdown-country').className;
	var locations_class = document.getElementById('stores-dropdown-location').className;

// close the location drop down if open
	if (locations_class == 'dropdown-selected')
	{
		document.getElementById('stores-dropdown-location').className = 'dropdown';
	}

	if (countries_class == 'dropdown')
	{
		document.getElementById('stores-dropdown-country').className = 'dropdown-selected';
	}
	else
	{
		document.getElementById('stores-dropdown-country').className = 'dropdown';
	}
		
}

// stores - get store locations based on country id
function getStoresBoutiqueLocations(country_id, country_name)
{ 
	var country_id;
	var country_name;
	var container;
	var url;
	
	// close boutique country drop down
	document.getElementById('stores-dropdown-country').className = 'dropdown';

	// set boutique country drop down to the country name
	document.getElementById('stores-dropdown-country-link').innerHTML = country_name;
	document.getElementById('stores-dropdown-country-link').setAttribute("rel",country_id);

	// set boutique location first element to 'Choose a location'
	document.getElementById('stores-dropdown-location-link').innerHTML = "Choose a location";

	// ajax request for the associate locations
	//alert(country_id);
	container = "dropdown-stores-location-container";
	url = MAIN_URL + "get+stores+boutique+locations/" + country_id;
	//alert(url);
	new Ajax.Updater(container, url, {
	
										onComplete:function(request){showStoresBoutiqueLocations();
																		
																	}, 
	
										asynchronous:true
									 });
	
		
}

// stores - show boutique location dropdown
function showStoresBoutiqueLocations()
{ 
	var locations_class = document.getElementById('stores-dropdown-location').className;

	if (locations_class == 'dropdown')
	{
		document.getElementById('stores-dropdown-location').className = 'dropdown-selected';
	}
	else
	{
		document.getElementById('stores-dropdown-location').className = 'dropdown';
	}
		
}

// stores - set store locations based on one selected
function setStoresBoutiqueLocation(location_name)
{ 
	var location_name;

	// close boutique country drop down
	document.getElementById('stores-dropdown-location').className = 'dropdown';

	// set boutique country drop down to the country name
	document.getElementById('stores-dropdown-location-link').innerHTML = location_name;

	// set boutique button display
	document.getElementById('boutique-button').style.display = 'block';


}


// stores - show boutique types dropdown
function showStoresBoutiqueTypes()
{ 
	var types_class = document.getElementById('stores-dropdown-show').className;

	if (types_class == 'dropdown')
	{
		document.getElementById('stores-dropdown-show').className = 'dropdown-selected';
	}
	else
	{
		document.getElementById('stores-dropdown-show').className = 'dropdown';
	}
		
}

// stores - get store types
function getStoresBoutiqueTypes(type_id, type_name)
{ 
	var type_id;
	var type_name;

// close boutique country drop down
	document.getElementById('stores-dropdown-show').className = 'dropdown';

	// set boutique country drop down to the country name
	document.getElementById('stores-dropdown-show-link').innerHTML = type_name;
	document.getElementById('stores-dropdown-show-link').setAttribute("rel",type_id);
}

// stores - go to stores page
function gotoStoresBoutiques()
{ 
	var country_id;
	var location;
	var type;
	var url;

	country_id 	= document.getElementById('stores-dropdown-country-link').getAttribute("rel");
	location 	= document.getElementById('stores-dropdown-location-link').innerHTML;
	type	 	= document.getElementById('stores-dropdown-show-link').getAttribute("rel");

	if(country_id == '' || location == 'Choose%20a%20location') return;
	if(country_id == '' || location == 'Sélectionnez une ville') return;
	
	url 		= MAIN_URL + "find+a+boutique/" + country_id + "/" + location;
	if (type)
	{
		url 	= MAIN_URL + "find+a+boutique/" + country_id + "/" + location + "/" + type;
	}
	
	window.location = url;
}

////////////////////////////////////////////////////
// Stores
////////////////////////////////////////////////////

// show the store image
function showStoreImage(image_id)
{
	
	var image_id;
	var path;
	var img	= document.getElementById("storeimage");



	path  = MAIN_URL + STORES_IMAGES_MAIN + image_id + ".jpg";

	img.setAttribute("src", path);
}

////////////////////////////////////////////////////
// Gift wizard
////////////////////////////////////////////////////

// gifts - show receivers dropdwon
function showGiftReceivers()
{ 
	var receivers_class = document.getElementById('dropdown-receiver').className;
	var occasions_class = document.getElementById('dropdown-occasions').className;

	// close the occasion drop down if open
	if (occasions_class == 'dropdown-selected')
	{
		document.getElementById('dropdown-occasions').className = 'dropdown';
	}

	if (receivers_class == 'dropdown')
	{
		document.getElementById('dropdown-receiver').className = 'dropdown-selected';
	}
	else
	{
		document.getElementById('dropdown-receiver').className = 'dropdown';
	}
		
}

// gifts - show gift occasions dropdown
function showGiftOccasions()
{ 
	var locations_class = document.getElementById('dropdown-occasions').className;

	if (locations_class == 'dropdown')
	{
		document.getElementById('dropdown-occasions').className = 'dropdown-selected';
	}
	else
	{
		document.getElementById('dropdown-occasions').className = 'dropdown';
	}
		
}

// gift - get gift receiver
function getGiftReceivers(type_id, type_name)
{ 
	var type_id;
	var type_name;

// close gift receiver drop down
	document.getElementById('dropdown-receiver').className = 'dropdown';

	// set gift receiver drop down to the gift receiver name
	document.getElementById('dropdown-receiver-link').innerHTML = type_name;
	document.getElementById('dropdown-receiver-link').setAttribute("rel",type_id);
}

// gift - get gift occasion
function getGiftOccasions(type_id, type_name)
{ 
	var type_id;
	var type_name;

	// close gift occasion drop down
	document.getElementById('dropdown-occasions').className = 'dropdown';

	// set gift occasion drop down to the gift occasion name
	document.getElementById('dropdown-occasions-link').innerHTML = type_name;
	document.getElementById('dropdown-occasions-link').setAttribute("rel",type_id);
}

// stores - go to stores page
function gotoGiftProducts()
{ 
	var receiver_id;
	var occasions_id;
	var url;

	receiver_id 	= document.getElementById('dropdown-receiver-link').getAttribute("rel");
	occasions_id 	= document.getElementById('dropdown-occasions-link').getAttribute("rel");

	if(receiver_id == '' || occasions_id == '') return;
	
	url 		= MAIN_URL + "gift+products/" + receiver_id + "/" + occasions_id;
	
	window.location = url;
}

////////////////////////////////////////////////////
// Check out page
////////////////////////////////////////////////////

// load a selected billing address into the checkout page
function loadBillingAddress()
{

	var b_select 		= document.getElementById("b_use_address");
	var b1_title		= b_checkout_addresses[b_select.selectedIndex]['title'];
	var b1_firstname	= b_checkout_addresses[b_select.selectedIndex]['firstname'];
	var b1_surname		= b_checkout_addresses[b_select.selectedIndex]['surname'];
	var b1_address1		= b_checkout_addresses[b_select.selectedIndex]['address1'];
	var b1_address2		= b_checkout_addresses[b_select.selectedIndex]['address2'];
	var b1_city			= b_checkout_addresses[b_select.selectedIndex]['city'];
	var b1_county		= b_checkout_addresses[b_select.selectedIndex]['county'];
	var b1_postcode		= b_checkout_addresses[b_select.selectedIndex]['postcode'];
	var b1_country_id	= b_checkout_addresses[b_select.selectedIndex]['country_id'];
	var b1_phone		= b_checkout_addresses[b_select.selectedIndex]['phone'];
	var b1_fax			= b_checkout_addresses[b_select.selectedIndex]['fax'];
	var b1_hash			= b_checkout_addresses[b_select.selectedIndex]['hash'];

	// get billing address elements
	var b_title 		= document.getElementById("b_title");
	var b_firstname 	= document.getElementById("b_firstname");
	var b_surname 		= document.getElementById("b_surname");
	var b_address1	 	= document.getElementById("b_address1");
	var b_address2	 	= document.getElementById("b_address2");
	var b_city		 	= document.getElementById("b_city");
	var b_county	 	= document.getElementById("b_county");
	var b_postcode	 	= document.getElementById("b_postcode");
	var b_country_id 	= document.getElementById("b_country_id");
	var b_phone		 	= document.getElementById("b_phone");
	var b_fax		 	= document.getElementById("b_fax");
	var b_id		 	= document.getElementById("b_id");

	b_title.setAttribute("value", b1_title);
	b_firstname.setAttribute("value", b1_firstname);
	b_surname.setAttribute("value", b1_surname);
	b_address1.setAttribute("value", b1_address1);
	b_address2.setAttribute("value", b1_address2);
	b_city.setAttribute("value", b1_city);
	b_county.setAttribute("value", b1_county);
	b_postcode.setAttribute("value", b1_postcode);
	b_country_id.setAttribute("value", b_country_id);
	b_phone.setAttribute("value", b1_phone);
	b_fax.setAttribute("value", b1_fax);
	b_id.setAttribute("value", b1_hash);

}

// load a selected shipping address into the checkout page
function loadShippingAddress(form)
{

	var s_select 		= document.getElementById("s_use_address");

	// if the first option just exit
	if (s_select.selectedIndex == 0) return;
	
	// if same as billing address is selected - get the billing address details
	if (s_select.selectedIndex == 1)
	{	
		copyBillingAddress(form);
		return;
	}

	var s1_title		= s_checkout_addresses[s_select.selectedIndex]['title'];
	var s1_firstname	= s_checkout_addresses[s_select.selectedIndex]['firstname'];
	var s1_surname		= s_checkout_addresses[s_select.selectedIndex]['surname'];
	var s1_address1		= s_checkout_addresses[s_select.selectedIndex]['address1'];
	var s1_address2		= s_checkout_addresses[s_select.selectedIndex]['address2'];
	var s1_city			= s_checkout_addresses[s_select.selectedIndex]['city'];
	var s1_county		= s_checkout_addresses[s_select.selectedIndex]['county'];
	var s1_postcode		= s_checkout_addresses[s_select.selectedIndex]['postcode'];
	var s1_country_id	= s_checkout_addresses[s_select.selectedIndex]['country_id'];
	var s1_phone		= s_checkout_addresses[s_select.selectedIndex]['phone'];
	var s1_fax			= s_checkout_addresses[s_select.selectedIndex]['fax'];
	var s1_hash			= s_checkout_addresses[s_select.selectedIndex]['hash'];

	// get billing address elements
	var s_title 		= document.getElementById("s_title");
	var s_firstname 	= document.getElementById("s_firstname");
	var s_surname 		= document.getElementById("s_surname");
	var s_address1	 	= document.getElementById("s_address1");
	var s_address2	 	= document.getElementById("s_address2");
	var s_city		 	= document.getElementById("s_city");
	var s_county	 	= document.getElementById("s_county");
	var s_postcode	 	= document.getElementById("s_postcode");
	var s_phone		 	= document.getElementById("s_phone");
	var s_fax		 	= document.getElementById("s_fax");
	var s_id		 	= document.getElementById("s_id");

	s_title.setAttribute("value", s1_title);
	s_firstname.setAttribute("value", s1_firstname);
	s_surname.setAttribute("value", s1_surname);
	s_address1.setAttribute("value", s1_address1);
	s_address2.setAttribute("value", s1_address2);
	s_city.setAttribute("value", s1_city);
	s_county.setAttribute("value", s1_county);
	s_postcode.setAttribute("value", s1_postcode);
	s_phone.setAttribute("value", s1_phone);
	s_fax.setAttribute("value", s1_fax);
	s_id.setAttribute("value", s1_hash);

}

// copy the billing address to the shipping address
function copyBillingAddress(form)
{
	// get shipping address info
	var s_title 		= document.getElementById("s_title");
	var s_firstname 	= document.getElementById("s_firstname");
	var s_surname 		= document.getElementById("s_surname");
	var s_company 		= document.getElementById("s_company");
	var s_address1	 	= document.getElementById("s_address1");
	var s_address2	 	= document.getElementById("s_address2");
	var s_city		 	= document.getElementById("s_city");
	var s_county	 	= document.getElementById("s_county");
	var s_postcode	 	= document.getElementById("s_postcode");
	var s_int_code	 	= document.getElementById("s_int_code");
	var s_phone		 	= document.getElementById("s_phone");
	var s_fax		 	= document.getElementById("s_fax");
	var s_id		 	= document.getElementById("s_id");

	// copy over
	s_title.setAttribute("value", form.b_title.value);
	s_firstname.setAttribute("value", form.b_firstname.value);
	s_surname.setAttribute("value", form.b_surname.value);
	s_company.setAttribute("value", form.b_company.value);
	s_address1.setAttribute("value", form.b_address1.value);
	s_address2.setAttribute("value", form.b_address2.value);
	s_city.setAttribute("value", form.b_city.value);
	s_county.setAttribute("value", form.b_county.value);
	s_postcode.setAttribute("value", form.b_postcode.value);
	s_int_code.setAttribute("value", form.b_int_code.value);
	s_phone.setAttribute("value", form.b_phone.value);
	s_fax.setAttribute("value", form.b_fax.value);
	s_id.setAttribute("value", form.b_id.value);

}

