var orders;
var dist;

function pageload()
{
	dist = "east";
	var f = document.getElementById("mainform").elements;
	for (var i=0; i<f.length; i++)
	{
		var e = f[i];
		if (e.type=="checkbox")
			e.onclick = new Function("productSelected(this);");
	}
	
	var cookie = readCookie('order');
	if (cookie == null){
		orders = new dictionary(); 
		return;
	}
	
	eval(cookie);
	if (orders == null){
		orders = new dictionary(); 
	}
	
	// Distributor
	cookie = readCookie('distributor');
	if (cookie == null){
		//dist = "east"; 
		return;
	}
	
	eval(cookie);
	/*
	if (dist == null){
		dist = "east"; 
	}
	*/
	
}

function productSelected(id)
{
	updateList(id.value,id.checked);
}

function updateList(productCode,checked)
{
  //alert(productCode+" "+checked);
  productCode = "prod"+productCode;
  var o = orders.Lookup(productCode);
  //if (o==null)
  //  alert('not found');
	
  if (checked){
	  //alert('Set');
	  orders.Set(productCode,checked);
  }
  else {
	  //alert('Delete');
	  orders.Delete(productCode);
  }
  doSerialize();
}

function clearList()
{
	orders = new dictionary();
	doSerialize();
	dist = null;
	doSerializeDist();
	var f = document.getElementById("mainform").elements;
	for (var i=0; i<f.length; i++)
	{
		var e = f[i];
		if (e.type=="checkbox")
			e.checked = false;
	}

}

function printList()
{
	/*
	var cookie = readCookie('distributor');
	if (cookie == null||cookie==""){
		window.open('getdistributor.html');
		return;
	}

	eval(cookie);
	if (dist==null||cookie==""){
		window.open('getdistributor.html');
	}
	*/
	window.open('List.html')
}

function selectDistributor(value)
{
	
	if (value=="DistributorEast.html")
		dist="east";
	else
		dist="west";
	doSerializeDist();
	document.location.href=value;
}

function doSerialize()
{
  var objSerializer = new JSSerializer();
  objSerializer.Serialize(orders);
  var orderString = objSerializer.GetJSString('orders');
  //alert(orderString);
  writeCookie('order',orderString);
}

function doSerializeDist()
{
  var objSerializer = new JSSerializer();
  objSerializer.Serialize(dist);
  var distString = objSerializer.GetJSString('dist');
  //alert(orderString);
  writeCookie('distributor',distString);
}

function readCookie(name)
{
  var cookieValue = "";
  var search = name + "=";

  if(document.cookie.length > 0)
  { 
    offset = document.cookie.indexOf(search);

    if (offset != -1)
    { 
      offset += search.length;
      end = document.cookie.indexOf(";", offset);
      if (end == -1) end = document.cookie.length;
      cookieValue = unescape(document.cookie.substring(offset, end))
    }
  }

  return cookieValue;

}

// Example:

// writeCookie("myCookie", "my name", 24);

// Stores the string "my name" in the cookie "myCookie" which expires after 24 hours.

function writeCookie(name, value, hours)
{
  var expire = "";

  if(hours != null)
  {
    expire = new Date((new Date()).getTime() + hours * 3600000);
    expire = "; expires=" + expire.toGMTString();
  }

  document.cookie = name + "=" + escape(value) + expire;
}

var popUpWin=0;

function popUpWindow(URLStr, left, top, width, height)
{

  if(popUpWin)
  {
    if(!popUpWin.closed) popUpWin.close();
  }
  popUpWin = open(URLStr, 'popUpWin', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=yes,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'');

}

function printPage() { print(document); }

function dictionary()
{
   this.Lookup = function(strKeyName) {
    return(this[strKeyName]);
   }
   
   this.Set = function(strKeyName,strValue) {
	   var str = "this['"+strKeyName+"'] = " + strValue;
       eval(str);
   }

   this.Add = function() {
    for (c=0; c < arguments.length; c+=2) {
      this[arguments[c]] = arguments[c+1];
    }
  }
  
   this.Delete = function(strKeyName) {
    for (c=0; c < arguments.length; c++) {
      this[arguments[c]] = null;
    }
  }
}
