var cursor = 'pointer';
// screen size
var sw;
var sh;
var cart_visible;
function showWait() {
	showElement('wait');
}
function getScreenSize() {
	if ( navigator.appName == "Microsoft Internet Explorer" ) {
		sw = document.body.clientWidth;
		sh = document.body.clientHeight;
	} else {
		sw = window.innerWidth;
		sh = window.innerHeight;
	}
}
function centreElement(div,w,h) {
	o=document.getElementById(div);
	o.style.left=(sw-w)/2+"px";
	o.style.top=(sh-h)/2+"px";
}
function positionElement(div,l,t) {
	o=document.getElementById(div);
	o.style.left=l+"px";
	o.style.top=t+"px";
}
function showElement(div) {
	o=document.getElementById(div);
	o.style.visibility='visible';
	return true;
}
function hideElement(div) {
	o=document.getElementById(div);
	o.style.visibility='hidden';
	return true;
}
function Browser() {
	var bName = navigator.appName;
	switch ( bName ) {
	case "Netscape":
		return "NN";
		break;
	case "Konqueror":
		return "KK";
		break;
	default:
		return "IE";
	}
}

function SetDefaultFocus(FormName) {
	var i;
	if ( document.forms[FormName].elements == null ) return;
	var flength = document.forms[FormName].elements.length;
	var type;
	if (  flength > 0 ) {
		for ( i = 0; i < flength; i++ ) {
			type = document.forms[FormName].elements[i].type;
			if (  type == "text" || type == "password" || type == "textarea" || type == "file" ) {
				document.forms[FormName].elements[i].focus();
				break;
			}
		}
	}
}

function SetCursor() {
	if ( cursor == 'pointer' ) {
		cursor = 'hand';
		document.style.cursor = 'hand';
	} else {
		cursor = 'pointer';
		document.style.cursor = 'pointer';
	}
	return true;
}
function LinkPopupWindow(address,pWidth,pHeight,r,t,m,s,l) {
	PopupWindow(address,pWidth,pHeight,r,t,m,s,l);
}

function ShowHand(id) {
	o = document.getElementById(id);
	o.style.cursor='hand';
	return true;
}
function iif(cond,first,last) {
	if ( cond )
		return first;
	else
		return last;
}
function EncodeConfirm(message) {
	return confirm(unescape(message));
}
function ResetSearchformFields() {
	document.form1.s_title.value = "";
	document.form1.s_author.value = "";
	document.form1.s_publisher.value = "";
	document.form1.s_keywords.value = "";
	document.form1.s_isbn.value = "";
	document.form1.s_binding[0].selected = true;
	document.form1.s_signed.checked = false;
	document.form1.s_dustjacket.checked = false;
	document.form1.s_edition.checked = false;
	document.form1.s_minprice.value = "";
	document.form1.s_maxprice.value = "";
	document.form1.s_listlength.value = "20";
	document.form1.s_sortfield[2].selected = true;
	document.form1.s_bookid.value = "";
}

function ChangeAction(formname,act) {
	eval('document.'+formname+'.a.value='+act);
	return true;
}
function SubmitSimpleSearch(stype) {
	document.ssform.sstype.value = stype;
	document.ssform.submit();
	return true;
}
var ajax = {
	request : '',
	params : '',
	showWait : false,
	sendMethod : 'GET',
	http : Object,
	init : function(request,showWait,sendMethod) {
		if ( sendMethod == null ) {
			this.sendMethod = "GET";
		} else {
			this.sendMethod = sendMethod;
		}
		if ( this.sendMethod == "GET" ) {
			this.request = request;
		} else {
			var pos = request.indexOf('?');
			if (pos == -1) {
				this.request = request;
				this.params = '';
			} else {
				this.request = request.substring(0,pos);
				this.params = request.substr(pos+1);
			}
		}
		if ( showWait == null ) {
			this.showWait = false;
		} else {
			this.showWait = showWait;
		}
		this.createRequestObject();
		this.sendRequest();
	},
	createRequestObject : function() {
		if ( navigator.appName == "Microsoft Internet Explorer" ) {
			this.http = new ActiveXObject("Microsoft.XMLHTTP");
		} else {
			this.http = new XMLHttpRequest();
		}
	},
	sendRequest : function() {
		if ( this.showWait ) {
			// display the Wait icon
			document.getElementById('wait').style.display="block";
		}
		if ( this.sendMethod == 'POST' ) {
			this.http.open('POST', this.request,true);
		} else {
			this.http.open('GET', this.request,true);
		}
		this.http.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
		this.http.onreadystatechange = ajax.getResponse;
		if ( this.sendMethod == 'POST' ) {
			this.http.send(this.params);
		} else {
			this.http.send(null);
		}
	},
	getResponse : function() {
		if ( ajax.http.readyState == 4 ) {
			if ( ajax.showWait ) document.getElementById('wait').style.display="none";
			//alert(ajax.http.responseText);
			eval(ajax.http.responseText);
			ajax.http = null;
		}
	}
}
function AJAX(request,showWait,sendMethod) {
	/*	PARAMETERS
		request		-	php script + parameters to run on server
		showWait	-	true/false display the wait icon (default: false)
		sendMethod	-	"GET" or "POST" (default: "GET")
		All parameters must already be urlencoded
	*/
	ajax.init(request,showWait,sendMethod);
}
var Base64 = {

	// private property
	_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

	// public method for encoding
	encode : function (input) {
		var output = "";
		var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
		var i = 0;

		input = Base64._utf8_encode(input);

		while (i < input.length) {

			chr1 = input.charCodeAt(i++);
			chr2 = input.charCodeAt(i++);
			chr3 = input.charCodeAt(i++);

			enc1 = chr1 >> 2;
			enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
			enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
			enc4 = chr3 & 63;

			if (isNaN(chr2)) {
				enc3 = enc4 = 64;
			} else if (isNaN(chr3)) {
				enc4 = 64;
			}

			output = output +
			this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
			this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);

		}

		return output;
	},

	// public method for decoding
	decode : function (input) {
		var output = "";
		var chr1, chr2, chr3;
		var enc1, enc2, enc3, enc4;
		var i = 0;

		input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

		while (i < input.length) {

			enc1 = this._keyStr.indexOf(input.charAt(i++));
			enc2 = this._keyStr.indexOf(input.charAt(i++));
			enc3 = this._keyStr.indexOf(input.charAt(i++));
			enc4 = this._keyStr.indexOf(input.charAt(i++));

			chr1 = (enc1 << 2) | (enc2 >> 4);
			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
			chr3 = ((enc3 & 3) << 6) | enc4;

			output = output + String.fromCharCode(chr1);

			if (enc3 != 64) {
				output = output + String.fromCharCode(chr2);
			}
			if (enc4 != 64) {
				output = output + String.fromCharCode(chr3);
			}

		}

		output = Base64._utf8_decode(output);
		return output;

	},

	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";

		for (var n = 0; n < string.length; n++) {

			var c = string.charCodeAt(n);

			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}

		}

		return utftext;
	},

	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;

		while ( i < utftext.length ) {

			c = utftext.charCodeAt(i);

			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}

		}

		return string;
	}
}
function formPostContent(form_name) {
	var submit_query = '';
	var form_element;
	var last_element_name = '';

	for (i = 0; i < document.forms[form_name].elements.length; i++) {
		form_element = document.forms[form_name].elements[i];
		switch (form_element.type) {
 			// Text fields, hidden form elements
 			case 'text':
 			case 'hidden':
 			case 'password':
 			case 'textarea':
 			case 'select-one':
 				submit_query += form_element.name + '=' + realescape(form_element.value) + '&';
 				break;
			 // Radio buttons
 			case 'radio':
 				if (form_element.checked) {
 					submit_query += form_element.name + '=' + realescape(form_element.value) + '&';
 				}
 				break;
 			// Checkboxes
 			case 'checkbox':
 				if (form_element.checked) {
 					// Continuing multiple, same-name checkboxes
 					if (form_element.name == last_element_name) {
 						// Strip of end ampersand if there is one
 						if (submit_query.lastIndexOf('&') == submit_query.length - 1) {
 							submit_query = submit_query.substr( 0, submit_query.length - 1);
 						}
 						// Append value as comma-delimited string
 						submit_query += ',' + realescape(form_element.value);
 					} else {
 						submit_query += form_element.name + '=' + realescape(form_element.value);
 					}
 					submit_query += '&';
 					last_element_name = form_element.name;
 				}
 				break;
 		}
 	}
 	// Remove trailing separator
 	submit_query = submit_query.substr(0, submit_query.length - 1);
 	return submit_query;
}
function realescape(str) {
	// urlencodes "+" signs
	str = escape(str);
	return str.replace(/\+/g,'%2B');
}
/* Abbreviation container */
function ShowAbbr(Id) {
	AJAX('popups.php?a=8&id='+Id,false,'GET');
}
function HideAbbr() {
	document.getElementById('tip').style.visibility="hidden";
}
function revealCart() {
	if ( cart_visible ) {
		hideCart();
	} else {
		showCart();
	}
	return false;
}
function showCart() {
	o=document.getElementById('showcart');
	s=document.getElementById('search_results');
	o.style.height='0px';
	o.style.visibility='visible';
	for ( var i=0; i < 30; i+=0.01 ) {
		o.style.height=i+'px';
		s.style.top+=i;
	}
	cart_visible=true;
}
function hideCart() {
	o=document.getElementById('showcart');
	s=document.getElementById('search_results');
	for ( var i=30; i=0; i-=0.01 ) {
		o.style.height=i+'px';
		s.style.top-=i;
	}
	o.style.visibility='hidden';
	cart_visible=false;
}
function submitOrderAddressForm() {
	var v="'form1'";
	v+= ",'RefundRead','You must acknowledged that you have read the Refund Policy','isChecked'";
	if ( ValidateForm(v) ) {
		document.form1.submit();
		return true;
	}
	return false;
}
function saveSearch() {
	/* save search criteria */
	if ( document.form1.save_name.value=="" ) {
		alert('You must give a Name to these search criteria');
	} else {
		document.form1.a.value="78";
		document.form1.submit();
	}
}
/* mouse */
var mouseX = 0;
var mouseY = 0;
var IE = document.all?true:false;
// If NS -- that is, !IE -- then set up for mouse capture
if (!IE) document.captureEvents(Event.MOUSEMOVE);
// Set-up to use getMouseXY function onMouseMove
document.onmousemove = getMouseXY;
// Main function to retrieve mouse x-y pos.s
function getMouseXY(e) {
	// Temporary variables to hold mouse x-y pos.s
	var tempX = 0;
	var tempY = 0;
  	if (IE) {
  		// grab the x-y pos.s if browser is IE
    	tempX = event.clientX + document.body.scrollLeft;
    	tempY = event.clientY + document.body.scrollTop;
  	} else {
  		// grab the x-y pos.s if browser is NS
    	tempX = e.pageX;
    	tempY = e.pageY;
  	}
  	// for strict mode only
	if( IE && document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		tempX += document.documentElement.scrollLeft;
		tempY += document.documentElement.scrollTop;
  	}
  	// catch possible negative values in NS4
  	if (tempX < 0){tempX = 0;}
  	if (tempY < 0){tempY = 0;}
  	mouseX = tempX;
  	mouseY = tempY;
  	return true;
}
function getScrollXY() {
	var scrOfX = 0, scrOfY = 0;
	if( typeof( window.pageYOffset ) == 'number' ) {
		//Netscape compliant
		scrOfY = window.pageYOffset;
		scrOfX = window.pageXOffset;
	} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		//DOM compliant
		scrOfY = document.body.scrollTop;
		scrOfX = document.body.scrollLeft;
	} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		//IE6 standards compliant mode
		scrOfY = document.documentElement.scrollTop;
		scrOfX = document.documentElement.scrollLeft;
	}
	return [ scrOfX, scrOfY ];
}
/* end of mouse */
function insertAnchor(form, field) {
    var fldObj = document.forms[form].elements[field];
    var link = '<a href="url">text</a>';

    //IE support
    if (document.selection) {
        fldObj.focus();
        sel = document.selection.createRange();
        sel.text = link;
        document.forms[form].insert.focus();
    }
    //MOZILLA/NETSCAPE support
    else if (document.forms[form].elements[field].selectionStart || document.forms[form].elements[field].selectionStart == "0") {
        var startPos = document.forms[form].elements[field].selectionStart;
        var endPos = document.forms[form].elements[field].selectionEnd;
        var chaineSql = document.forms[form].elements[field].value;

        fldObj.value = chaineSql.substring(0, startPos) + link + chaineSql.substring(endPos, chaineSql.length);
    } else {
        fldObj.value += link;
    }
    return false;
}
function insertValueQuery() {
    var myQuery = document.sqlform.sql_query;
    var myListBox = document.sqlform.dummy;

    if(myListBox.options.length > 0) {
        sql_box_locked = true;
        var chaineAj = "";
        var NbSelect = 0;
        for(var i=0; i<myListBox.options.length; i++) {
            if (myListBox.options[i].selected){
                NbSelect++;
                if (NbSelect > 1)
                    chaineAj += ", ";
                chaineAj += myListBox.options[i].value;
            }
        }

        //IE support
        if (document.selection) {
            myQuery.focus();
            sel = document.selection.createRange();
            sel.text = chaineAj;
            document.sqlform.insert.focus();
        }
        //MOZILLA/NETSCAPE support
        else if (document.sqlform.sql_query.selectionStart || document.sqlform.sql_query.selectionStart == "0") {
            var startPos = document.sqlform.sql_query.selectionStart;
            var endPos = document.sqlform.sql_query.selectionEnd;
            var chaineSql = document.sqlform.sql_query.value;

	        myQuery.value = chaineSql.substring(0, startPos) + chaineAj + chaineSql.substring(endPos, chaineSql.length);
        } else {
            myQuery.value += chaineAj;
        }
        sql_box_locked = false;
    }
}
function randomID() {
	// generate a random 32 character string
	var str = 'abcdefghijklmnopqrstuvwxyz';
	var rnd = str.substr(Math.floor(Math.random()*26),1);
	for ( var i = 1; i < 32; i++ ) rnd += str.substr(Math.floor(Math.random()*26),1);
	return rnd;
}



