
function init_onup() {

   var ajaxRequest;  // The variable that makes Ajax possible!
	
	try {
		
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} 
	
	catch (e) {
		// Internet Explorer Browsers
		try {
		
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		
		catch (e)  {
		
			try {
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			
			catch (e) {
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	
	return ajaxRequest;
}


/**
*	This will show book list that i Want
**/

function GetBooksIWant() {	
	
	var ajaxRequest;
	ajaxRequest=init_onup();	
	
	ajaxRequest.onreadystatechange = function(){
		
		if(ajaxRequest.readyState == 4) {
			
			//alert(ajaxRequest.responseText);
			document.getElementById("booksiwant").innerHTML = ajaxRequest.responseText;
		}					
		
	} //call back function			
	
	rand = Math.random();
	ajaxRequest.open("GET", "/ajaxcontent/booksiwant.php?rand="+rand, true);
	ajaxRequest.send(null);
	
	
}  //ShowScore

function DelBooksIWant(val) {	
	
	var ajaxRequest;
	ajaxRequest=init_onup();	
	
	ajaxRequest.onreadystatechange = function(){
		
		if(ajaxRequest.readyState == 4) {
			
			alert(ajaxRequest.responseText);
			GetBooksIWant();
			GetBookMatchList()
			//document.getElementById("booksiwant").innerHTML = ajaxRequest.responseText;
		}					
		
	} //call back function			
	
	if(confirm("Do you really want delete it ?")) {
		
		rand = Math.random();
		ajaxRequest.open("GET", "/controller/del_wantlist_ctl.php?rand="+rand+"&asin="+val, true);
		ajaxRequest.send(null);
		
	}
	
	
}  //DelBooksIWant



function DelBooksIHave(val) {	
	
	var ajaxRequest;
	ajaxRequest=init_onup();	
	
	ajaxRequest.onreadystatechange = function(){
		
		if(ajaxRequest.readyState == 4) {
			
			alert(ajaxRequest.responseText);
			GetBooksIHave();
			//document.getElementById("booksiwant").innerHTML = ajaxRequest.responseText;
		}					
		
	} //call back function			
	
	if(confirm("Do you really want delete it ?")) {
		
		rand = Math.random();
		ajaxRequest.open("GET", "/controller/del_havelist_ctl.php?rand="+rand+"&asin="+val, true);
		ajaxRequest.send(null);
		
	}
	
	
}  //DelBooksIHave


/**
*	This will show book list that i have
**/

function GetBooksIHave() {	
	
	var ajaxRequest;
	ajaxRequest=init_onup();	
	
	ajaxRequest.onreadystatechange = function(){
		
		if(ajaxRequest.readyState == 4) {
			
			//alert(ajaxRequest.responseText);
			document.getElementById("booksihave").innerHTML = ajaxRequest.responseText;
		}					
		
	} //call back function			
	
	rand = Math.random();
	ajaxRequest.open("GET", "/ajaxcontent/booksihave.php?rand="+rand, true);
	ajaxRequest.send(null);
	
	
}  //ShowScore

function GetTradeRequest() {	
	
	var ajaxRequest;
	ajaxRequest=init_onup();	
	
	ajaxRequest.onreadystatechange = function(){
		
		if(ajaxRequest.readyState == 4) {
			
			//alert(ajaxRequest.responseText);
			document.getElementById("traderequest").innerHTML = ajaxRequest.responseText;
		}					
		
	} //call back function			
	
	rand = Math.random();
	ajaxRequest.open("GET", "/ajaxcontent/traderequest.php?rand="+rand, true);
	ajaxRequest.send(null);
	
	
}  //ShowScore

function fakeEsc() {
	window.event.keyCode = 27;	
}

function AddBook(type,evt) {	
	
	var ajaxRequest;
	ajaxRequest=init_onup();	
	
	ajaxRequest.onreadystatechange = function(){
		
		if(ajaxRequest.readyState == 4) {
			
			alert(ajaxRequest.responseText);
			
			if(type=="1") {
				
				GetBooksIHave();
				GetTradeRequest();
				
			}
			
			if(type=="2") {GetBooksIWant();GetBookMatchList()}
			//document.getElementById("score_board").innerHTML = ajaxRequest.responseText;

			//alert("Being closed");
			//window.onkeypress = fakeEsc;
		}					
		
	} //call back function			
	
	var book_asin, small_img, medium_img,book_title,book_author,points,index,condition,param,rand;
	book_asin = document.getElementById("asin").value;
	small_img = document.getElementById("small_img").value;
	medium_img = document.getElementById("medium_img").value;
	//alert("Hello = "+ book_asin);
	//alert("Medium Image = "+ medium_img);
	
	book_title = escape(document.getElementById("book_title").innerHTML);
	book_author = escape(document.getElementById("book_author").innerHTML);
	points = document.getElementById("points").innerHTML;
	index = document.getElementById("condition").selectedIndex;
	condition = document.getElementById("condition").options[index].value;
	
	param = "simg="+small_img+"&mimg="+medium_img+"&asin="+book_asin+"&title="+book_title+"&author="+             book_author+"&points="+points+"&condition="+condition;
	//alert(param);
	rand = Math.random();
	
	if(type=="1") ajaxRequest.open("GET", "/controller/addbook_ctl.php?rand="+rand+"&"+param, true);
	if(type=="2") ajaxRequest.open("GET", "/controller/addbook2_ctl.php?rand="+rand+"&"+param, true);		
	
	ajaxRequest.send(null);
	
	
}  //ShowScore

function Next(Keywords,ItemPage,Page) {	
	
	var ajaxRequest;
	ajaxRequest=init_onup();	
	
	ajaxRequest.onreadystatechange = function(){
		
		if(ajaxRequest.readyState == 4) {
			
			//alert(ajaxRequest.responseText);
			document.getElementById("SearchPageItem").innerHTML = ajaxRequest.responseText;
		}else {
			
				document.getElementById("SearchPageItem").innerHTML = "<div align=center style='margin:100px 0 0 0'><img src=/images/loadingAnimation.gif></div>";
			
		}					
		
	} //call back function			
	
	rand = Math.random();
	

	ajaxRequest.open("GET", "/ajaxcontent/"+Page+"?rand="+rand+"&ItemPage="+ItemPage+"&book="+Keywords, true);
	ajaxRequest.send(null);
	
	
}  //Next

function Previous(Keywords,ItemPage,Page) {	
	
	var ajaxRequest;
	ajaxRequest=init_onup();	
	
	ajaxRequest.onreadystatechange = function(){
		
		if(ajaxRequest.readyState == 4) {
			
			//alert(ajaxRequest.responseText);
			document.getElementById("SearchPageItem").innerHTML = ajaxRequest.responseText;
		}else {
			
				document.getElementById("SearchPageItem").innerHTML = "<div align=center style='margin:100px 0 0 0'><img src=/images/loadingAnimation.gif></div>";
			
		}					
		
	} //call back function			
	
	rand = Math.random();
	ajaxRequest.open("GET", "/ajaxcontent/"+Page+"?rand="+rand+"&ItemPage="+ItemPage+"&book="+Keywords, true);
	ajaxRequest.send(null);
	
	
}  //Next

function GetBooksMatch() {	
	
	var ajaxRequest;
	ajaxRequest=init_onup();	
	
	ajaxRequest.onreadystatechange = function(){
		
		if(ajaxRequest.readyState == 4) {
			
			//alert(ajaxRequest.responseText);
			document.getElementById("bookmatch").innerHTML = ajaxRequest.responseText;
		}					
		
	} //call back function			
	
	rand = Math.random();
	ajaxRequest.open("GET", "/ajaxcontent/booksmatch.php?rand="+rand, true);
	ajaxRequest.send(null);
	
	
}  //GetBooksMatch

function BooksoftheMonth() {	
	
	var ajaxRequest;
	ajaxRequest=init_onup();	
	
	ajaxRequest.onreadystatechange = function(){
		
		if(ajaxRequest.readyState == 4) {
			
			//alert(ajaxRequest.responseText);
			document.getElementById("booksofthemonth").innerHTML = ajaxRequest.responseText;
		}else {
			
				document.getElementById("booksofthemonth").innerHTML = "<img src=/images/ajax-loader.gif>";
			
		}					
		
	} //call back function			
	
	rand = Math.random();
	ajaxRequest.open("GET", "/ajaxcontent/booksofthemonth.php?rand="+rand, true);
	ajaxRequest.send(null);
	
	
}  //BooksoftheMonth

function RequestAccept(tid) {	
	
	var ajaxRequest;
	ajaxRequest=init_onup();	
	
	ajaxRequest.onreadystatechange = function(){
		
		if(ajaxRequest.readyState == 4) {
			
			//alert(ajaxRequest.responseText);
			if(ajaxRequest.responseText==1){
				
				document.getElementById("accept").style.display = "block";			
				document.getElementById("accept2").style.display = "none";
				document.getElementById("accept3").style.display = "none";
			
			} if(ajaxRequest.responseText==0){
				
				document.getElementById("accept2").style.display = "block";
				document.getElementById("accept").style.display = "none";
				document.getElementById("accept3").style.display = "none";
				
			}if(ajaxRequest.responseText==3){
				
				document.getElementById("accept3").style.display = "block";
				document.getElementById("accept").style.display = "none";
				document.getElementById("accept2").style.display = "none";
				
			}
		}					
		
	} //call back function			
	
	rand = Math.random();
	ajaxRequest.open("GET", "/controller/accept_ctl.php?rand="+rand+"&tid="+tid, true);
	ajaxRequest.send(null);
	
	
}  //RequestAccept

function BooksCounter() {	
	
	var ajaxRequest;
	ajaxRequest=init_onup();	
	
	ajaxRequest.onreadystatechange = function(){
		
		if(ajaxRequest.readyState == 4) {
			
			//alert(ajaxRequest.responseText);
			document.getElementById("counter").innerHTML = ajaxRequest.responseText;			
			//int=window.clearInterval(int);			
			
		} else {
			
				document.getElementById("counter").innerHTML = "<img src=/images/ajax-loader.gif>";
			
		}				
		
	} //call back function			
	
	rand = Math.random();
	ajaxRequest.open("GET", "/ajaxcontent/bookscount.php?rand="+rand, true);
	ajaxRequest.send(null);
	
	
}  //BooksCounter


function evalScript(scripts) {
	try {	
	
		if(scripts != '')	{	
		
			var script = "";
			scripts = scripts.replace(/<script[^>]*>([\s\S]*?)<\/script>/gi, function(){
	       	                         if (scripts !== null) script += arguments[1] + '\n';
 	        	                        return '';});
			if(script) (window.execScript) ? window.execScript(script) : window.setTimeout(script, 0);
		}
		return false;
	}
	catch(e)
	{	alert(e)
	}
}

 function setAndExecute(divId, result)  
 {  
    var div = document.getElementById(divId);  
    div.innerHTML = result;  
    var x = div.getElementsByTagName("script");   
	evalScript(result);
	/*for(var i=0;i<x.length;i++)  
    {  
        if(x[i].text!=null) 
		eval(x[i].text);  
    } */
 }  
 
 
function GetBookMatchList() {	
	
	var ajaxRequest,innerHTML,rand;
	
	ajaxRequest=init_onup();	
	
	ajaxRequest.onreadystatechange = function(){
		
		if(ajaxRequest.readyState == 4) {
			
			
			innerHTML = ajaxRequest.responseText; 
			//alert(innerHTML);
			setAndExecute("bookmatch", innerHTML); 
			
			//document.getElementById("bookmatch").innerHTML = ajaxRequest.responseText;			
			
			
		} else {
			
				//document.getElementById("counter").innerHTML = "<img src=/images/ajax-loader.gif>";
			
		}				
		
	} //call back function			
	
	rand = Math.random();
	ajaxRequest.open("GET", "/ajaxcontent/booksmatch.php?rand="+rand, true);
	//ajaxRequest.open("GET", "/ajaxcontent/test2.php?rand="+rand, true);
	ajaxRequest.send(null);
	
	
}  //BooksCounter


function AddNeverReceivedBook() {	
	
	var ajaxRequest;
	ajaxRequest=init_onup();	
	
	ajaxRequest.onreadystatechange = function(){
		
		if(ajaxRequest.readyState == 4) {
			
			alert(ajaxRequest.responseText);		
			
		}				
		
	} //call back function			
	
	book_asin = document.getElementById("asin").value;	
	username = document.getElementById("username").value;	
	index = document.getElementById("points").selectedIndex;
	points = document.getElementById("points").options[index].value;
	
	param = "username="+username+"&asin="+book_asin+"&points="+points;
	//alert(param);

	rand = Math.random();	
	ajaxRequest.open("GET", "/controller/never_received_ctl.php?rand="+rand+"&"+param, true);
	
	ajaxRequest.send(null);
	
	
}  //AddNeverReceivedBook


