/* products_cookies.js
 * by patrick schmider v1.0 000101
 * Copyright (c) 2000 Patrick Schmider. All Rights Reserved.
 * License to use is granted if and only if this entire
 * copyright notice is included.
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
*/

// global variables - 
// used by functions that call some of the functions below
var search_type = 0;
var details_type = 1;

var cookie_names = new Array();
var location_names = new Array();
var window_widths = new Array();
var window_heights = new Array();
var window_urls = new Array();
window_urls["de"] = new Array();
window_urls["fr"] = new Array();
window_urls["en"] = new Array();

//cookie names
cookie_names[search_type] = "cookie_search";
cookie_names[details_type] = "cookie_details";
//location strings
location_names[search_type] = "location_search";
location_names[details_type] = "location_details";
//window width of popUp Window
window_widths[search_type] = 700;
window_widths[details_type] = 760;
//window height of popUp Window
window_heights[search_type] = 480;
window_heights[details_type] = 560;
//window urls 
window_urls["de"][search_type] = "../../products/html/prod_search_de.htm";
window_urls["fr"][search_type] = "../../products/html/prod_search_en.htm";
window_urls["en"][search_type] = "../../products/html/prod_search_en.htm";
window_urls["de"][details_type] = "../../products/html/prod_details_de.htm";
window_urls["fr"][details_type] = "../../products/html/prod_details_en.htm";
window_urls["en"][details_type] = "../../products/html/prod_details_en.htm";


//---------------------------------------------------------
//---------------------------------------------------------
//---------------------------------------------------------
function getStringfromLocation(loc, val_name) {
  /*
  	loc 	 = the location.href property of calling document
  	val_name = exp:
  		location.href - location.search = url
    		location.href 	= www.xyz.com/a/test.html?a_string=foo
    		location.search	= ?a_string=foo
    		val_name 	= "a_string"
    		val		= "foo"
  */
  
  var the_string = "";
  var location_header = "?"+val_name+"=";
  
  var val_start = loc.indexOf(location_header) + location_header.length;
  if (val_start == location_header.length-1)
    return the_string; // string not found -> return empty string
    
  var val_end   = loc.indexOf(";",val_start);
  if (val_end   == -1)
    val_end = loc.length;
  
  the_string = loc.substring(val_start,val_end);
  return unescape(the_string);
}


//---------------------------------------------------------
//---------------------------------------------------------
//---------------------------------------------------------
function getCookieValue(val_id) {
 var val = "";
 if(document.cookie) {
  var val_name = val_id+"=";
  var val_start = document.cookie.indexOf(val_name) + val_name.length;
  var val_end   = document.cookie.indexOf(";",val_start);
  if (val_end   == -1)
   val_end = document.cookie.length;
  val = document.cookie.substring(val_start,val_end);
 }
 return unescape(val);
}


//---------------------------------------------------------
//---------------------------------------------------------
//---------------------------------------------------------
function setCookieValue(name, value, expire, path, domain, secure) {
 if ( 	(name == null) ||
 	(value == null) ||
 	(expire == null) ) {
 	alert ("Error - Could not set cookie !");
 	return 0;
 }
 var now = new Date();
 var expires = new Date(now.getTime() + expire);
 var cookie_string = "";
 cookie_string += name + "=" + escape(value) +
 	((expires) ? "; expires=" + expires.toGMTString() : "") +
 	((path) ? "; path=" + path : "") +
	((domain) ? "; domain=" + domain : "") +
	((secure) ? "; secure" : "");
 document.cookie = cookie_string;
}


//---------------------------------------------------------
//---------------------------------------------------------
//---------------------------------------------------------
function testCookie() {
  if (navigator.cookieEnabled) return 1;
  var now = new Date();
  var val_in = new Date(now.getTime());
  var val_out = "";
  var cookie_id = "dummy";
  var cookie_exp = 10000; //10 seconds
  setCookieValue(cookie_id, val_in, cookie_exp);
  val_out = getCookieValue(cookie_id);
  //alert("in: "+val_in+"; out: "+val_out);
  if (val_out == val_in) {
    return 1;
  } else {
    return 0;
  }
}


//---------------------------------------------------------
//---------------------------------------------------------
//---------------------------------------------------------
function callWindow(type, lang, value, window_id, expire, path) {
  /*
  Function to forward a parameter (value) by ...
  a) setting a cookie of type 'type' with given value
  b) opening a new window and adding the search String of type 'type' with given value
  
  The called/opened window can access the value by using 
  one of the functions provided above:
  a) getCookieValue(val_id)
  b) getStringfromLocation(loc, val_name)
  
  type 		= one of the global types defined above (at the top)
  lang 		= "de", "fr" or "en"
  value 	= parameter value to provide cookie / opened window with
  Optional Parameters:
  window_id	= id for the opened window (default: location_names[type])
  expire	= cookie expire time (default: 3 min)
  path 		= path where cookie will be accessible (default: "/" -> everywhere in project)
  
  
  */
  
  //Test required parameters
  if ( 	(type == null) || 
  	( (lang != "de") && (lang != "fr") && (lang != "en") ) ||
  	(value == null) ) {
    alert("ERROR - Given parameters invalid !");
    return 0;
  }
 
  //Set optional parameters 
  (window_id) ? null : window_id = location_names[type];
  (expire) ? 	null : expire = 1000 * 60 * 3; //3 Minutes
  (path) ? 	null : path = "/"; //root of current project
  
  //A) Set cookie (if possible)
  //=============
  if (testCookie()) {
  //test if cookies are available
    var cookie_name = cookie_names[type]; //globally defined at the top
    setCookieValue(cookie_name, value, expire, path);
  }
  
  //B) Call window
  //==============
  var search_header = "?"+location_names[type]+"="; //globally defined at the top
  var new_window_href = window_urls[lang][type];
  var new_window = new_window_href + search_header + escape(value);
  //var popUp_options = 'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=700,height=480';
  var popUp_options = "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=";
  popUp_options += window_widths[type] + ",height=";
  //popUp_options += window_heights[type];
  if (screen.availHeight > 870) { //1024x768, 1280x960 -> we need at least a height of 720 pixels
    popUp_options += 720;
  } else {
    popUp_options += screen.availHeight-40; //maximize window height
  }
  var temp_window = window.open(new_window, window_id, popUp_options);
  //temp_window.location.reload();
  //temp_window.focus();
}

