

var preloadFlag = false;

function newImage(arg) {
	if (document.images) {
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}

function changeImages() {
	//alert(preloadFlag);
	//if (document.images && (preloadFlag == true)) {
		
		for (var i=0; i<changeImages.arguments.length; i+=2) {
			document[changeImages.arguments[i]].src = changeImages.arguments[i+1];
		}
	//}
}



function setCookie(name,value,days) {
  if (days!="")
  {
      var date= new Date();
      date.setTime(date.getTime()+(days*24*60*60*1000));
      var expires="; expires="+date.toGMTString();
  }
  else var expires="";
  document.cookie=name+"="+value+expires+";"+"path=/; domain=www.rockynook.com"; 
}

function readCookie(name)
{
  var nameEQ= name + "=";
  var ca= document.cookie.split(';');
  for (var i=0; i< ca.length; i++)
    {
        var c= ca[i];
	while (c.charAt(0)==' ') c=c.substring(1,c.length);
	if (c.indexOf(nameEQ) ==0 ) return c.substring(nameEQ,c.length);
	
    }
    return null;
}

function eraseCookie(name)
 {
  setCookie(name,"",-1);
 }
 
 function trim(c)
 {
   while (c.charAt(0)==' ') c=c.substring(1,c.length);
   return c;
 }
function classBook(ID,Amount) {
	this.ID=ID;
	this.Amount=Amount;
}

function basketDebug() {
        var ca=document.cookie.split(';');
	
	
	for (var i=0;i<ca.length;i++) {
		//Book=BooksList[i];
		//alert(\"ID: \"+Book.ID+\", Menge: \"+Book.Amount);
	}
}

function basketAdd(id,type) {
  if (type==2) id=id+"_studie";
//  alert("id="+id);
    var n=readCookie(id);
        if (n!=null)
           {
	    
	    var aux=n.split('=');
	    var aux1=trim(aux[1]);
	    eraseCookie(id);
	    var aux2=parseInt(aux1)+parseInt('1');
	    setCookie(id,aux2,"");
	   }
	 else
	   setCookie(id,1,"");  
	   
	/*var Book=new classBook(id,1);
	var Ok=new Boolean(true);
	var i=(-1);
	while (BooksList[++i]) {
		var tempBook=BooksList[i];
		if (tempBook.ID==id) {
			Ok=false;
			return;
		}
	}
	BooksList.push(Book);
	BooksList[i]=Book; */
}

function basketRemove(id,type) {
    if (type == 2) id=id+"_studie";
     eraseCookie(id);
	/*var tempList=new Array();
	var i=(-1);
	var c=-1;
	while (BooksList[++i]) {
		if (BooksList[i].ID!=id) tempList[++c]=BooksList[i];
	}
	BooksList=tempList;*/
	if (document.cookie!=null) basketShow(1,type);
	else document.history.back();
	
}

function basketEmpty() {
	//BooksList=new Array();
	document.cookie=null;
}

function basketShow(mode,type) {
        
	if (document.cookie==null) {
		alert("The shopping cart is empty!");
		return 0;
	}
//	alert(document.cookie);
	var i=(-1);
	var IDString=":";
	var ca=document.cookie.split(';');
	while (ca[++i]) {
		var book=ca[i].split('=');
		book[0]=trim(book[0]);
		if ( (book[0].match("_studie")!=null)&& (type==2) )
                	IDString=IDString+book[0].substr(0,book[0].length-7)+"="+trim(book[1])+":";
                 else
		 if ( (book[0].match("_studie")==null) && (type==1))			
         		IDString=IDString+trim(book[0])+"="+trim(book[1])+":";
	}
	/*if (IDString.compareTo( ":")==0)
	{
	  alert("Warenkorb ist leer!");
	  return 0;
	}*/
	if (mode==1)
	  {
	   if (type ==1)
	  	document.location.replace("/books/warenkorb?"+IDString);
           else
	   if (type ==2)
	      document.location.replace("/books/warenkorbStudie?"+IDString);		  
	  }
	else
		document.location.href="/books/warenkorb?"+IDString;
}

function basketOrder(type) {
	if (document.cookie==null) {
		alert("The shopping cart is empty!");
		return 0;
	}
	var i=(-1);
	var IDString=":";
	var ca=document.cookie.split(';');
	while (ca[++i]) {
		var book=ca[i].split('=');
		book[0]=trim(book[0]);
		if ( (book[0].match("_studie")!=null) && (type==2) )
		   IDString=IDString+book[0].substr(0,book[0].length-7)+"="+trim(book[1])+":";
		 else
		 if ( (book[0].match("_studie")==null) && (type==1) )  
			IDString=IDString+trim(book[0])+"="+trim(book[1])+":";
	}
	 /*if (IDString == ":")
	  {
	     alert("Warenkorb ist leer!");
	     return 0;
	  }*/
	  if (type==1)
	      document.location.href="/books/bestellung?"+IDString;
	   else
	     if (type==2)
	       document.location.href="/books/bestellungStudie?"+IDString;      
}

