/******
 * Copyright: Bernardini & Schnyder GmbH
 * Author: Sebastian Haller
 * Date: 2005
 * javascript
 */

// allow only numbers to be entered in sone fields
function goodchars(e, chars)
{
	var key, keychar;

	if (window.event)
		key = window.event.keyCode;
	else if (e)
		key = e.which;
	else
		return true;

	keychar = String.fromCharCode(key);
	keychar = keychar.toLowerCase();
	chars = chars.toLowerCase();

	if (chars.indexOf(keychar) != -1)
		return true;

	// control keys
	if ( key==null || key==0 || key==8 || key==9 || key==13 || key==27 )
		return true;

	return false;
}

// new window
function newwindow(url, name, w, h, features) {
	if(screen.width)
	{
		var winl = (screen.width-w)/2;
		var wint = (screen.height-h)/2;
	}
	else
	{
		winl = 0;
		wint = 0;
	}
	if (winl < 0)
		winl = 0;
	if (wint < 0)
		wint = 0;
	//var settings = 'scrollbars=yes, resizable=yes, ';
	var settings = '';
	settings += 'height=' + h + ', ';
	settings += 'width=' + w + ', ';
	//settings += 'top=' + wint + ', ';
	//settings += 'left=' + winl + ', ';
	settings += features;
	win = window.open(url, name, settings);
	win.resizeTo(w, h);
	win.focus();
	// do not uncomment this. it is used in links containing
	// a href for users without javascript and an onclick="return newwindow(...);" for users with javascript
	// we do not want the href action to be excuted on success!
	return false;
}

// flush basket
function basket_flush(link, text)
{
	var random = 'random=' + Math.round(Math.random() * 1000);
	var is_confirmed  = confirm(text);
	if (!is_confirmed)
		return false;

	// thanks to: http://jibbering.com/2002/4/httprequest.html
	var xmlhttp=false;
	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
	// JScript gives us Conditional compilation, we can cope with old IE versions.
	// and security blocked creation of the objects.
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			xmlhttp = false;
		}
	}
	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
		xmlhttp = new XMLHttpRequest();
	}

	// direct redirect, if no xmlhttp available
	if(!xmlhttp) {
		return true;
	}

	xmlhttp.open("GET", link+'&'+random, true);
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4) {
			//alert(xmlhttp.responseText);
			document.getElementById("basket").innerHTML = xmlhttp.responseText;
			document.getElementById("basket_mouse_content").innerHTML = xmlhttp.responseText;
			basket_mouse_show();
		}
	}
	xmlhttp.send(null);
	return false;
}

// add size to basket
function basket_add(link,o)
{
	
	var random = 'random=' + Math.round(Math.random() * 1000);
	var query = '';
	var first = true;
	for (var i=0; i<o.elements.length; i++)
	{
		if (o.elements[i].type == 'text' || o.elements[i].type == 'hidden')
		{
			if (!first)
				query = query + '&';
			else
				first = false;
			query = query + o.elements[i].name + '=' + o.elements[i].value;
		}
	}
	
	// thanks to: http://jibbering.com/2002/4/httprequest.html
	var xmlhttp=false;
	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
	// JScript gives us Conditional compilation, we can cope with old IE versions.
	// and security blocked creation of the objects.
	 try {
	  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	 } catch (e) {
	  try {
	   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	  } catch (E) {
	   xmlhttp = false;
	  }
	 }
	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
		try {
			xmlhttp = new XMLHttpRequest();
		} catch (e) {
			xmlhttp=false;
		}
	}
	if (!xmlhttp && window.createRequest) {
		try {
			xmlhttp = window.createRequest();
		} catch (e) {
			xmlhttp=false;
		}
	}


	// direct redirect, if no xmlhttp available
	if(!xmlhttp) {
		return true;
	}
	try {
		xmlhttp.open("GET", link+"&"+query, true);
		//xmlhttp.open("GET","http://www.google.com/", true);
	} catch (e) {
			alert(e.description)
		}
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4) {
			//alert(xmlhttp.responseText);
			document.getElementById("basket").innerHTML = xmlhttp.responseText;
			document.getElementById("basket_mouse_content").innerHTML = xmlhttp.responseText;
			basket_mouse_show();
		}
	}	
	xmlhttp.send(null);
	return false;
}

var basket_mouse_t;
function basket_mouse_show()
{
	clearTimeout(basket_mouse_t);
	basket_mouse_t = setTimeout('basket_mouse_hide()', 3000);
	o = document.getElementById("basket_mouse");
	o.style.visibility = "visible";
	var y = (document.body.clientWidth-o.offsetWidth)/2;
	if (document.all)
	{
		if (document.compatMode && document.compatMode!="BackCompat")
		{
			o.style.left = (document.documentElement.scrollLeft + y) + "px";
			o.style.top = (document.documentElement.scrollTop + 230) + "px";
		}
		else
		{
			o.style.left = (document.body.scrollLeft + y) + "px";
			o.style.top = (document.body.scrollTop + 230) + "px";
		}
	}
	else
	{
		o.style.left = (window.pageXOffset + y) + "px";
		o.style.top = (window.pageYOffset + 230) + "px";
	}
}

function basket_mouse_hide()
{
	clearTimeout(basket_mouse_t);
	document.getElementById("basket_mouse").style.visibility = "hidden";
}

function quickorder_for_omniweb()
{
	for (var i=0; i<=last; i++)
	{
		quickorder_chg_num(i, document.getElementById('num_'+i));
	}
	quickorder_chg_quantity(last, document.getElementById('quantity_'+last));
}

function quickorder_chg_num(link,i, o)
{
	if (o.value.length != 4)
		return;

	// check if content changed. this caused problems when pressing tab and going through all fields
	if (content[i] == o.value)
		return;

	content[i] = o.value;

	// thanks to: http://jibbering.com/2002/4/httprequest.html
	var xmlhttp=false;
	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
	// JScript gives us Conditional compilation, we can cope with old IE versions.
	// and security blocked creation of the objects.
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			xmlhttp = true;
		}
	}
	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
		xmlhttp = new XMLHttpRequest();
	}

	// direct redirect, if no xmlhttp available
	if(!xmlhttp) {
		return false;
	}

	xmlhttp.open("GET", link+'?num='+o.value+'&i='+i, true);
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4) {
			//alert(xmlhttp.responseText);
			document.getElementById("size"+i).innerHTML = xmlhttp.responseText;
		}
	}
	xmlhttp.send(null);
	return false;
}

function quickorder_chg_quantity(i, o)
{
	if (o.value.length == 0)
		return;

	if (i!=last)
		return;

	if (i>=99)
		return;

	i++;
	last++;

	var e = document.createElement("div");
	var o = document.getElementById("entries");
	o.appendChild(e);
	e.innerHTML = '<div id="entry'+i+'" class="quickorderentry">'
		+ '	<div id="num'+i+'" class="quickorderentrycol1">'
		+ '		<input maxlength="4" class="ordernumber" type="text" size="4" id="num_'+i+'" name="num['+i+']" value="" onKeyup="quickorder_chg_num('+i+', this);"/>'
		+ '	</div>'
		+ '	<div id="size'+i+'" class="quickorderentrycol2">'
		+ emptytext
		+ '	</div>'
		+ '	<div id="quantity'+i+'" class="quickorderentrycol3">'
		+ '		<input class="number" type="text" size="4" id="quantity_'+i+'" name="quantity['+i+']" value="" onKeypress="return goodchars(event,\'0123456789\');" onKeyup="quickorder_chg_quantity('+i+', this);" />'
		+ '	</div>'
		+ '</div>';

	// not in IE 6.0 and below
	//var a = document.createAttribute("style");
	//a.nodeValue = "background-color: #abcdef;";
	//e.setAttributeNode(a);
}

// used to confirm a link before sending request (i.e. for delete links)
function confirmlink(text, obj)
{
	var is_confirmed  = confirm(text);
	if (is_confirmed) {
		obj.href += '&js_confirmed=1';
	}

	return is_confirmed;
}

// changes content of a div
function setcontent(obj, content)
{
	document.getElementById(obj).innerHTML = content;
	return false;
}

// show product
function showproduct(index)
{
	for (var i=0; i<10000; i++)
	{
		if (document.getElementById("product_sbox_"+i) != undefined)
			showdiv("product_sbox_"+i);
		else
			break;
		if (document.getElementById("product_box_"+i) != undefined)
			hidediv("product_box_"+i);
		else
			break;
	}
	if (document.getElementById("product_sbox_"+index) != undefined)
		hidediv("product_sbox_"+index);
	if (document.getElementById("product_box_"+index) != undefined)
		showdiv("product_box_"+index);

	return false;
}

// if we change content of basket
function basketchanged()
{
	hidediv("basket_total_price");
	showdiv("basket_total_link");
	return false;
}

// show shipping method
function showcostmethod(index)
{
	for (var i=1; i<10000; i++)
	{
		if (document.getElementById("costs_"+i) != undefined)
			hidediv("costs_"+i);
		else
			break;
	}
	if (document.getElementById("costs_"+index) != undefined)
		showdiv("costs_"+index);

	return true;
}

function orderaddressbeqs()
{
	if (document.getElementById("beqs") != undefined)
	{
		o = document.getElementById("beqs");
		if (o.checked)
			hidediv("orderaddressbeqs");
		else
			showdiv("orderaddressbeqs");
	}

	return true;
}

// hide div
function hidediv(obj)
{
	o = document.getElementById(obj);
	o.style.display='none';
}

// show div
function showdiv(obj)
{
	o = document.getElementById(obj);
	o.style.display='block';
}

// flush basket
function mybook_flush(link, text)
{
	var random = 'random=' + Math.round(Math.random() * 1000);
	var is_confirmed  = confirm(text);
	if (!is_confirmed)
		return false;

	// thanks to: http://jibbering.com/2002/4/httprequest.html
	var xmlhttp=false;
	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
	// JScript gives us Conditional compilation, we can cope with old IE versions.
	// and security blocked creation of the objects.
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			xmlhttp = false;
		}
	}
	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
		xmlhttp = new XMLHttpRequest();
	}

	// direct redirect, if no xmlhttp available
	if(!xmlhttp) {
		return true;
	}
	xmlhttp.open("GET", link+'&'+random, true);
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4) {
			//alert(xmlhttp.responseText);
			document.getElementById("mybook").innerHTML = xmlhttp.responseText;
			document.getElementById("mybook_mouse_content").innerHTML = xmlhttp.responseText;
			if(xmlhttp.responseText!=""){
				mybook_mouse_show();
			}

		}
	}
	xmlhttp.send(null);
	return false;
}


// add size to basket
function mybook_add(link,o)
{
	var random = 'random=' + Math.round(Math.random() * 1000);
	var query = '';
	var first = true;
	for (var i=0; i<o.elements.length; i++)
	{
		if (o.elements[i].type == 'text' || o.elements[i].type == 'hidden' || (o.elements[i].type == 'checkbox' && o.elements[i].checked) )
		{
			if (!first)
				query = query + '&';
			else
				first = false;
			query = query + o.elements[i].name + '=' + o.elements[i].value;
		}
	}
	
	// thanks to: http://jibbering.com/2002/4/httprequest.html
	var xmlhttp=false;
	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
	// JScript gives us Conditional compilation, we can cope with old IE versions.
	// and security blocked creation of the objects.
	 try {
	  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	 } catch (e) {
	  try {
	   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	  } catch (E) {
	   xmlhttp = false;
	  }
	 }
	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
		try {
			xmlhttp = new XMLHttpRequest();
		} catch (e) {
			xmlhttp=false;
		}
	}
	if (!xmlhttp && window.createRequest) {
		try {
			xmlhttp = window.createRequest();
		} catch (e) {
			xmlhttp=false;
		}
	}


	// direct redirect, if no xmlhttp available
	if(!xmlhttp) {
		return true;
	}
	try {
		//alert(link+"&"+query)
		xmlhttp.open("GET", link+"&"+query, true);
		//xmlhttp.open("GET","http://www.google.com/", true);
	} catch (e) {
			alert(e.description)
		}
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4) {
			//alert(xmlhttp.responseText);
			document.getElementById("mybook").innerHTML = xmlhttp.responseText;
			document.getElementById("mybook_mouse_content").innerHTML = xmlhttp.responseText;
			if(xmlhttp.responseText!=""){
				mybook_mouse_show();
			}
		}
	}	
	xmlhttp.send(null);
	return false;
}

//  remove chapter from mybook
function mybook_removeChapter(link,o)
{
	
	var random = 'random=' + Math.round(Math.random() * 1000);
	
	// thanks to: http://jibbering.com/2002/4/httprequest.html
	var xmlhttp=false;
	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
	// JScript gives us Conditional compilation, we can cope with old IE versions.
	// and security blocked creation of the objects.
	 try {
	  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	 } catch (e) {
	  try {
	   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	  } catch (E) {
	   xmlhttp = false;
	  }
	 }
	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
		try {
			xmlhttp = new XMLHttpRequest();
		} catch (e) {
			xmlhttp=false;
		}
	}
	if (!xmlhttp && window.createRequest) {
		try {
			xmlhttp = window.createRequest();
		} catch (e) {
			xmlhttp=false;
		}
	}


	// direct redirect, if no xmlhttp available
	if(!xmlhttp) {
		return true;
	}
	try {
		
		xmlhttp.open("GET", link, true);
		//xmlhttp.open("GET","http://www.google.com/", true);
	} catch (e) {
			alert(e.description)
		}
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4) {
			//alert(xmlhttp.responseText);
			document.getElementById("mybook").innerHTML = xmlhttp.responseText;
			document.getElementById("mybook_mouse_content").innerHTML = xmlhttp.responseText;
			if(xmlhttp.responseText!=""){
				mybook_mouse_show();
			}
		}
	}	
	xmlhttp.send(null);
	return false;
}

//  remove chapter from mybook
function mybook_new(link,o)
{
	
	var random = 'random=' + Math.round(Math.random() * 1000);
	
	// thanks to: http://jibbering.com/2002/4/httprequest.html
	var xmlhttp=false;
	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
	// JScript gives us Conditional compilation, we can cope with old IE versions.
	// and security blocked creation of the objects.
	 try {
	  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	 } catch (e) {
	  try {
	   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	  } catch (E) {
	   xmlhttp = false;
	  }
	 }
	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
		try {
			xmlhttp = new XMLHttpRequest();
		} catch (e) {
			xmlhttp=false;
		}
	}
	if (!xmlhttp && window.createRequest) {
		try {
			xmlhttp = window.createRequest();
		} catch (e) {
			xmlhttp=false;
		}
	}


	// direct redirect, if no xmlhttp available
	if(!xmlhttp) {
		return true;
	}
	try {
		
		xmlhttp.open("GET", link, true);
		//xmlhttp.open("GET","http://www.google.com/", true);
	} catch (e) {
			alert(e.description)
		}
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4) {
			//alert(xmlhttp.responseText);
			document.getElementById("mybook").innerHTML = xmlhttp.responseText;
			document.getElementById("mybook_mouse_content").innerHTML = xmlhttp.responseText;
			if(xmlhttp.responseText!=""){
				mybook_mouse_show();
			}
		}
	}	
	xmlhttp.send(null);
	return false;
}

function mybook_update(link,o)
{
	var random = '&random=' + Math.round(Math.random() * 1000);
	var query = '';
	var first = true;
	for (var i=0; i<o.elements.length; i++)
	{
		if (o.elements[i].type == 'text' || o.elements[i].type == 'hidden' || (o.elements[i].type == 'checkbox' && o.elements[i].checked) 
			  || o.elements[i].type == 'select-one' )
		{
			
			if (!first)
				query = query + '&';
			else
				first = false;
			query = query + o.elements[i].name + '=' + o.elements[i].value;
		}
	}
	// thanks to: http://jibbering.com/2002/4/httprequest.html
	var xmlhttp=false;
	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
	// JScript gives us Conditional compilation, we can cope with old IE versions.
	// and security blocked creation of the objects.
	 try {
	  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	 } catch (e) {
	  try {
	   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	  } catch (E) {
	   xmlhttp = false;
	  }
	 }
	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
		try {
			xmlhttp = new XMLHttpRequest();
		} catch (e) {
			xmlhttp=false;
		}
	}
	if (!xmlhttp && window.createRequest) {
		try {
			xmlhttp = window.createRequest();
		} catch (e) {
			xmlhttp=false;
		}
	}


	// direct redirect, if no xmlhttp available
	if(!xmlhttp) {
		return true;
	}
	try {
		
		xmlhttp.open("GET", link+"&"+query+random, true);
		//xmlhttp.open("GET","http://www.google.com/", true);
	} catch (e) {
			alert(e.description)
		}
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4) {
			//alert(xmlhttp.responseText);
			document.getElementById("mybook").innerHTML = xmlhttp.responseText;
			
		}
	}	
	xmlhttp.send(null);
	return false;
}

var mybook_mouse_t
function mybook_mouse_show()
{
	clearTimeout(mybook_mouse_t);
	mybook_mouse_t = setTimeout('mybook_mouse_hide()', 3000);
	o = document.getElementById("mybook_mouse");
	o.style.visibility = "visible";
	var y = (document.body.clientWidth-o.offsetWidth)/2;
	if (document.all)
	{
		if (document.compatMode && document.compatMode!="BackCompat")
		{
			o.style.left = (document.documentElement.scrollLeft + y) + "px";
			o.style.top = (document.documentElement.scrollTop + 230) + "px";
		}
		else
		{
			o.style.left = (document.body.scrollLeft + y) + "px";
			o.style.top = (document.body.scrollTop + 230) + "px";
		}
	}
	else
	{
		o.style.left = (window.pageXOffset + y) + "px";
		o.style.top = (window.pageYOffset + 230) + "px";
	}
}

function mybook_mouse_hide()
{
	clearTimeout(mybook_mouse_t);
	document.getElementById("mybook_mouse_content").innerHTML = "";
	document.getElementById("mybook_mouse").style.visibility = "hidden";
}



function mybook_link(link)
{
	
	var query = 'random=' + Math.round(Math.random() * 1000);
	// thanks to: http://jibbering.com/2002/4/httprequest.html
	var xmlhttp=false;
	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
	// JScript gives us Conditional compilation, we can cope with old IE versions.
	// and security blocked creation of the objects.
	 try {
	  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	 } catch (e) {
	  try {
	   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	  } catch (E) {
	   xmlhttp = false;
	  }
	 }
	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
		try {
			xmlhttp = new XMLHttpRequest();
		} catch (e) {
			xmlhttp=false;
		}
	}
	if (!xmlhttp && window.createRequest) {
		try {
			xmlhttp = window.createRequest();
		} catch (e) {
			xmlhttp=false;
		}
	}


	// direct redirect, if no xmlhttp available
	if(!xmlhttp) {
		return true;
	}
	try {
		xmlhttp.open("GET", link+"&"+query, true);
		//xmlhttp.open("GET","http://www.google.com/", true);
	} catch (e) {
			alert(e.description)
		}
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4) {
			//alert(xmlhttp.responseText);
			document.getElementById("mybook").innerHTML = xmlhttp.responseText;
			document.getElementById("mybook_mouse_content").innerHTML = xmlhttp.responseText;
			if(xmlhttp.responseText!=""){
				mybook_mouse_show();
			}
		}
	}	
	xmlhttp.send(null);
	return false;
}
function skipSelection() {
	var subjectsCombo = document.getElementById("subject");
	var subjectId = subjectsCombo.value;
	var levelsCombo = document.getElementById("level");
	// SUBJECTS
	// 20 -> Hohere Fachschule Pflege
	// 21 -> Berufsfachschule FaGe
	
	// LEVELS
	// 4 -> Sekundarstufe
	// 5 -> Tertiaerstufe
	if (20 == subjectId) {
		levelsCombo.value = 5;
	} else if (21 == subjectId) {
		levelsCombo.value = 4;
	}
	submitBookSearch();
}
