function Cookie(document, name, hours, path, domain, secure) {
	// Set up cookie.
	this.$document = document;
	this.$name = name;
	if (hours)
		this.$expiration = new Date((new Date()).getTIme() + hours * 3600000);
	else
		this.$expiration = null;
	if (path) 
		this.$path = path;
	else
		this.$path = null;
	if (domain)
		this.$domain = domain;
	else
		this.$domain = null;
	if (secure)
		this.$secure = true;
	else
		this.$secure = false;
}

function _Cookie_store() {
	// Put together proper cookie components from the cookie object.
	var cookieval = "";
	for (var prop in this) {
		if ((prop.charAt() == '$') || ((typeof this[prop]) == 'function')) continue;
		if (cookieval != "") cookieval += '&';
		cookieval += prop + ':' + escape(this[prop]);
	}

	// Build the complete cookie string.
	var cookie = this.$name + '=' + cookieval;
	if (this.$expiration) cookie += '; expires=' + this.$expiration.toGMTString();
	if (this.$path) cookie += '; path=' + this.$path;
	if (this.$domain) cookie += '; domain=' + this.$domain;
	if (this.$secure) cookie += '; secure';

	// Store the cookie.
	this.$document.cookie = cookie;
}

function _Cookie_load() {
	// Get all cookies for this document.
	var allcookies = this.$document.cookie;
	if (allcookies == "") return false;
	
	// Extract the named cookie.
	var start = allcookies.indexOf(this.$name + '=');
	if (start == -1) return false;
	start += this.$name.length + 1;
	var end = allcookies.indexOf(';', start);
	if (end == -1) end = allcookies.length;
	var cookieval = allcookies.substring(start, end);

	// Parse the cookie values.
	var a = cookieval.split('&');
	for (var i = 0; i < a.length; i++) {
		a[i] = a[i].split(':');
	}

	// Set variables from cookie.
	for (var i = 0; i < a.length; i++) {
		this[a[i][0]] = unescape(a[i][1]);
	}

	// Done.
	return true;		
}

function _Cookie_remove() {
	var cookie;
	cookie = this.$name + '=';
	if (this.$path) cookie += '; path=' + this.$path;
	if (this.$domain) cookie += '; domain=' + this.$domain;
	cookie += '; expires=Fri, 02-Jan-1970 00:00:00 GMT';
	
	// Store the cookie.
	this.$document.cookie = cookie;
}

// Create dummy cookie object.
new Cookie();
Cookie.prototype.store = _Cookie_store;
Cookie.prototype.load = _Cookie_load;
Cookie.prototype.remove = _Cookie_remove;

//ako
//added because IE < 5.5 doesn't have Array.push...
function push() {
	var sub = this.length;
	
	for (var i = 0; i < push.arguments.length; ++i) {
		this[sub] = push.arguments[i];
			sub++;
	}
	
	return sub;
}

//added for splice...
function isNumber(a) {
  return typeof a == 'number' && isFinite(a);
}

//added for splice...
function isUndefined(a) {
  return typeof a == 'undefined';
}

//added because IE < 5.5 doesn't have Array.splice...
function splice(s, d) {
  var max = Math.max,
    min = Math.min,
    a = [], // The return value array
    e,  // element
    i = max(arguments.length - 2, 0),   // insert count
    k = 0,
    l = this.length,
    n,  // new length
    v,  // delta
    x;  // shift count

  s = s || 0;
  if (s < 0) {
    s += l;
  }

  //alert("got here 1...");
  s = max(min(s, l), 0);  // start point
  //object expected
  //d = Math.max(Math.min(isNumber(d) ? d : l, l - s), 0);    // delete count

  //alert("got here 2...");
  //var myMin = Math.min(isNumber(d) ? d : l, l - s);
  var myMin = 0;

  if (isNumber(d)) {
    myMin = Math.min(d, l - s);
  } else {
    myMin = Math.min(l, l - s);
  }          

  d = Math.max(myMin, 0);

  v = i - d;
  n = l + v;

  while (k < d) {
    e = this[s + k];

    if (! isUndefined(e)) {
      a[k] = e;
    }
    k += 1;
  }

  x = l - s - d;

  if (v < 0) {
    k = s + i;
    while (x) {
      this[k] = this[k - v];
      k += 1;
      x -= 1;
    }
    this.length = n;
  } else if (v > 0) {
    k = 1;
    while (x) {
      this[n - k] = this[l - k];
      k += 1;
      x -= 1;
    }
  }

  for (k = 0; k < i; ++k) {
    this[s + k] = arguments[k + 2];
  }
  return a;
}


function isIE() {
	if (navigator.appName == "Microsoft Internet Explorer") {
		return true;
	} else {
		return false;
	}
}

function browserVersion() {
	msieIndex = navigator.appVersion.indexOf("MSIE") + 5;
	return (parseFloat(navigator.appVersion.substr(msieIndex, 3)));
}

//check to see whether we're running antiquated IE and define Array.push
//and Array.splice if we are...
if (isIE() && browserVersion() >= 5.5) {
} else {
	Array.prototype.push = push;
	Array.prototype.splice = splice;
}
//end ako


function addRemoveBasket(thisItem,thisBasket,thisBasketSize) {
	//ako
	//alert("In addRemoveBasket, thisItem = " + thisItem + ", thisBasket = " + thisBasket + ", thisBasketSize = " + thisBasketSize);

	var allcookies = document.cookie;
	var pos = allcookies.indexOf("basket=");
	var thisString = "";
	var value = "";
	var thisLength = 0;
	var x = 0;
	var y = false;

  //ako
  //alert("pos = " + pos + ", allcookies = '" + allcookies + "'");

	if (pos != -1) {
		var start = pos + 7;
		var end = allcookies.indexOf(";", start);
		if (end == -1) end = allcookies.length;
		value = allcookies.substring(start,end);
		value = unescape(value);
	}
	
//	a = thisBasket.value.split(",");
	//alert('Cookie before operation: ' + value);
	a = value.split(",");
	a.sort();

	for (i = 0; i < a.length; i++) {
		if (a[i] == thisItem.value) {
			x = i;
			y = true;
			break;
		}
	}
	
	//ako
	//alert("y = " + y + ", a = " + a + ", thisItem.checked = " + thisItem.checked);

	if (thisItem.checked) {
		if (! y) {
			//ako
			//alert("about to push a");
			
			//ako -- IE 5.1 javascript halts on this line!
			a.push(thisItem.value);
			//ako
			//alert("pushed a; a = " + a + ", thisItem.value = " + thisItem.value);

			thisLength = parseInt(thisBasketSize[0].value) + 1;
			thisBasketSize[0].value = thisLength;
			thisBasketSize[1].value = thisLength;
      //alert(thisItem.value + ' added to your basket.');
		}
	} else {
		if (y) {
			//ako error here in IE
			a.splice(x, 1);
			thisLength = parseInt(thisBasketSize[0].value) - 1;
			thisBasketSize[0].value = thisLength;
			thisBasketSize[1].value = thisLength;
			//alert(thisItem.value + ' removed from your basket.');
		}
	}
//	thisBasketSize.value = a.length;
	a.sort();
	thisString = "";
	for (i = 0; i < a.length; i++) {
		if (a[i].length > 0) {
			if (thisString == "") {
				thisString = a[i];
			} else {
				thisString = thisString + "," + a[i];
			}
		}
	}
	thisBasket.value = thisString;
//	alert('cookie is ' + thisBasket.value);
	document.cookie = "basket=" + escape(thisString) + "; path=/";
}

function addRemoveBasket2(thisItem,thisBasket,thisBasketSize) {
var allcookies = document.cookie;
var pos = allcookies.indexOf("basket=");
var thisString = "";
var value = "";
var thisLength = 0;
var x = 0;
var y = false;
var basketCookie = new Cookie (document,download,24,'/');
basketCookie.load();
	if (pos != -1) {
		var start = pos + 7;
		var end = allcookies.indexOf(";", start);
		if (end == -1) end = allcookies.length;
		value = allcookies.substring(start,end);
		value = unescape(value);
	}
	a = value.split(",");
	a.sort();
	for (i = 0; i < a.length; i++) {
		if (a[i] == thisItem.value) {
			x = i;
			y = true;
			break;
		}
	}
	if (thisItem.checked) {
		if (!y) {
			a.push(thisItem.value);
			thisLength = parseInt(thisBasketSize[0].value) + 1;
			thisBasketSize[0].value = thisLength;
			thisBasketSize[1].value = thisLength;
//			alert(thisItem.value + ' added to your basket.');
		}
	} else {
		if (y) {
			a.splice(x,1);
			thisLength = parseInt(thisBasketSize[0].value) - 1;
			thisBasketSize[0].value = thisLength;
			thisBasketSize[1].value = thisLength;
//			alert(thisItem.value + ' removed from your basket.');
		}
	}
//	thisBasketSize.value = a.length;
	a.sort();
	thisString = "";
	for (i = 0; i < a.length; i++) {
		if (a[i].length > 0) {
			if (thisString == "") {
				thisString = a[i];
			} else {
				thisString = thisString + "," + a[i];
			}
		}
	}
	thisBasket.value = thisString;
//	alert('cookie is ' + thisBasket.value);
	document.cookie = "basket=" + escape(thisString);
}

function validateSave(thisForm) {
	switch(thisForm.displayInfo.value) {
		case "content":
			selectAll(thisForm.awardsList);
			selectAll(thisForm.reviewsList);
			break;
		case "author":
			selectAll(thisForm.awardsList);
			selectAll(thisForm.appearanceList);
			selectAll(thisForm.interviewsList);
			break;
		default:
			break;
	}
}

function checkSave(url) {
var c = false;
var f = this.window.document.form;
var e, n, v, m;
var w = 0;
var y = "";
var x = "";
var regexp = /=@-/ig;
var oldtext = "";
var newtext = "";
var oldcompare = "";
var newcompare = "";
for (i = 0; i < f.elements.length; i++) {
	if (f.elements[i].name.substr(0,4) == "old_") {
		e = f.elements[i].name.substr(4,f.elements[i].name.length);
		if ((f.elements[e].name != undefined)) {
			if (f.elements[e].value != null) {
				n = f.elements[e].name;
				if ((n == "bisac_content_description")
					|| (n == "table_of_contents")
					|| (n == "excerpt")
					|| (n == "backlist_body_text")
					|| (n == "other_awards")
					|| (n == "electronic_version")
					|| (n == "author_bio")) {
					oldtext = f.elements[i].value;
					newtext = oldtext.replace(regexp,'"');
					f.elements[i].value = newtext;
				}
				if (f.elements[e].type == "select-multiple") {
					oldcompare = f.elements[i].value;
					a = new Array();
					for (var si = 0; si < f.elements[e].length; si++) {
						a[si] = f.elements[e].options[si].value;
					}
					newcompare = a.join();
				} else {
					oldcompare = f.elements[i].value;
					newcompare = f.elements[e].value;
				}
			 	if (newcompare != oldcompare) {
					c = true;
//				
//					alert(n + " changed.  Old Length: " + f.elements[i].value.length + " New Length:" + f.elements[e].value.length);
//					alert("oldcompare: " + oldcompare + " newcomare:" + newcompare);
//					if (f.elements[i].value.length != f.elements[e].value.length) {
//					if (1 == 1) {
//						m = Math.max(f.elements[i].value.length,f.elements[e].value.length);
//						for (j = 0; j < m; j++) {
//							if (f.elements[i].value.substr(j,1) != f.elements[e].value.substr(j,1)) {
//								if (w == 0) {
//									w = j;
//									break;
//								}
//							}
//						}
//						y = f.elements[i].value.substr(w,25);
//						z = f.elements[e].value.substr(w,25);
//						alert("Difference Begins: " + w);
//						alert("Old: " + y + " New: " + z);
//					}
//
					break;
				}
			}
		}
	}
} 
if (c && confirm("You have made changes to this page.  Do you want to save them?")) {
	this.window.document.form.nextLocation.value=url;
	this.window.document.form.submit[0].click();
	}
else {
	this.location.href=url;
	}
}

function openEdit(inWindow,inIsbn,inTitleNumber,inDescription,inDisplayInfo,inItemCode,inAuthorID) {
var w = 625;
var h = 400;
var winl = (screen.width - w) / 2;
var wint = (screen.height - h) / 2;
var winprops = 'height=' + h + ',width=' + w + ',top=' + wint + ',left=' + winl + ',resizable=yes,scrollbars';
	if (inItemCode == undefined) {
		inItemCode = 0;
	}
	w=window.open('tolText.cfm?isbn=' + inIsbn + '&titleNumber=' + inTitleNumber + '&description=' + inDescription + '&displayInfo=' + inDisplayInfo + '&itemCode=' + inItemCode + '&authorID=' + inAuthorID,inWindow,winprops);
	if (parseInt(navigator.appVersion) >= 4) { w.window.focus(); }
}

function moveSel(dnDir, formObj, selectObj) {
  var srcText = srcValue = destText = destValue = "";
  var srcIndex = destIndex = 0;

  docForm = eval('document.' + formObj.name);
  docFormName = 'document.' + formObj.name;

  with (docForm) {
    for (i = 0; i < length; i++) {
      if ((elements[i].type == "select-one") || (elements[i].type == "select-multiple")) {
        selName = elements[i].name;
        break;
      }
    } 
  }
  selObj = eval(docFormName + '.'+ selectObj);
  srcIndex = selObj.selectedIndex;          

  switch (dnDir) {
    case "t":
       if (srcIndex == 0) return;
       destIndex = 0;
       break;
    case "b":
       if (srcIndex == selObj.length) return;
       destIndex = selObj.length - 1;
      break;
        
    case true:
       increment = 1
       if (srcIndex +1 == selObj.length) 
          return;
       destIndex = srcIndex + increment;
       break;
    case false:
       increment = -1
       if (srcIndex < 1) return;
       destIndex = srcIndex + increment;
       break;
  }

  with (selObj) {
    srcText  = options[srcIndex].text;
    srcValue = options[srcIndex].value;

    destText  = options[destIndex].text;
    destValue = options[destIndex].value;

    options[destIndex].text = srcText;
    options[destIndex].value = srcValue;

    options[srcIndex].text = destText;
    options[srcIndex].value = destValue;
  
    selectedIndex = destIndex;
  } 
}

function addNewOption(oSelectFrom,oSelectInto,oMulti,oSync,oSyncList) {
	oNewOption = new Option();
	if (oSelectFrom.selectedIndex >= 0) {
		for (i = 0; i < oSelectFrom.length; i++) {
			if (oSelectFrom.options[i].selected == true) {
				oNewOption = new Option();
				oNewOption.value = oSelectFrom.options[i].value;
				oNewOption.text = oSelectFrom.options[i].text;
				oSelectInto[oSelectInto.length] = oNewOption;
			}
	    } 
		for (i = oSelectFrom.length-1; i > -1; i--) {
			if (oSelectFrom.options[i].selected == true) {
				if (oMulti)
					oSelectFrom.options[i].selected = false;
				else
					oSelectFrom.options[i] = null;
			}
		}
		if (!oMulti) 
			sortList(oSelectFrom);
		if (oSync) {
			selectAll(oSelectInto);
			syncList(oSelectInto,oSyncList);
			oSelectInto.selectedIndex=-1;
		}
	}
}

function removeOption(oSelectFrom,oSelectInto,oMulti,oSync,oSyncList) {
	oNewOption = new Option();
	if (oSelectInto.selectedIndex >= 0) {
		for (i = oSelectInto.length-1; i > -1; i--) {
			if (oSelectInto.options[i].selected == true) {
				if (!oMulti) {
					oNewOption = new Option();
					oNewOption.value = oSelectInto.options[i].value;
					oNewOption.text = oSelectInto.options[i].text;
					oSelectFrom[oSelectFrom.length] = oNewOption;
				}
				oSelectInto.options[i].selected = false;
				oSelectInto.options[i] = null;
			}
	    } 
		if (!oMulti) 
			sortList(oSelectFrom);
		if (oSync) {
			selectAll(oSelectInto);
			syncList(oSelectInto,oSyncList);
			oSelectInto.selectedIndex=-1;
		}
	}
}

function selectAll(oSelectAllList) {
	if (oSelectAllList.length > 0)
	{
		for (i = 0; i < oSelectAllList.length; i++)
	    {
			oSelectAllList.options[i].selected = true;
	    } 
	}
}

function syncList(sSyncIn,sSyncOut) {
	var oSaveIndex = 0;
	oSaveOption = new Option();
	oSaveOption.value = sSyncOut.options[sSyncOut.selectedIndex].value;
	oSaveOption.text = sSyncOut.options[sSyncOut.selectedIndex].text;
	sSyncOut.length = 0;
	addNewOption(sSyncIn,sSyncOut,true,false,'')
	for (i = 0; i < sSyncOut.length; i++) {
		if (sSyncOut.options[i].value == oSaveOption.value) {
			oSaveIndex = i;
		}
    } 
	sSyncOut.selectedIndex = oSaveIndex;
}

function sortList(sSort)  {
	var temp_opts = new Array();
	var temp = new Object();
	for (var i=0; i<sSort.options.length; i++) {
		temp_opts[i] = sSort.options[i];
	}
	for (var x=0; x<temp_opts.length-1; x++) {
		for (var y=(x+1); y<temp_opts.length; y++) {
			if (temp_opts[x].text > temp_opts[y].text) {
				temp = temp_opts[x].text;
				temp_opts[x].text = temp_opts[y].text;
				temp_opts[y].text = temp;
				temp = temp_opts[x].value;
				temp_opts[x].value = temp_opts[y].value;
				temp_opts[y].value = temp;
			}
		}
	}
	for (var i=0; i<sSort.options.length; i++) {
		sSort.options[i].value = temp_opts[i].value;
		sSort.options[i].text = temp_opts[i].text;
	}
}	

function textCounter(field, countfield, maxlimit) {
	if (field.value.length > maxlimit) {
		countfield.value = field.value.length;
		// if too long...trim it!
		field.value = field.value.substring(0, maxlimit);
		alert('The message is too long.  You have exceeded ' + maxlimit + ' characters.');
		// otherwise, update 'msg_length' counter
	}	
	else {
		countfield.value = field.value.length;
	}
}


