//using 'dhtmlHistory.js';
var expectedHash = "";
var g_unsubscribe = 0;
function showElement(element)
{
	document.getElementById(element).style.display='inline';
}
function hideElement(element)
{
	document.getElementById(element).style.display='none';
}

function submitText( element, heading, showDiv, hideelement )
{
	makeRequest();
	//document.getElementById(element).value;
	//document.getElementById('div_better_description').innerHTML = "Your Data is submitted";
	if ( hideelement != null )
	{
		document.getElementById(hideelement).style.display='none';
	}
	showElement( showDiv );
	url = "send_mail.php?mail_body=" + document.getElementById(element).value;
	
	parameters = "mail_body=" + document.getElementById(element).value+"&mail_heading="+heading;
	
	http_request.open('POST', url, true);
	http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http_request.setRequestHeader("Content-length", parameters.length);
	http_request.setRequestHeader("Connection", "close");
	http_request.send(parameters);
	
}
function clearContents( element )
{
	document.getElementById(element).value = null;
}
	
	///////////
function makeHistory(newHash)
{
	var hh = 'message:'+newHash;
	//alert(hh);
	dhtmlHistory.add( newHash, hh);
//alert( expectedHash );
  return true;
}


function handleHistory()
{
	

if(window.location.hash != "#div_listing")
  if ( window.location.hash != expectedHash )
  {
    expectedHash = window.location.hash;
    var newoption = expectedHash.substring(2);
    sndReqArg('div_listing', newoption);
  }

  pollHash();
  return true;
}

function pollHash() {
 /*var links = document.getElementsByTagName("a");
for(var i=0; i<links.length; i++) {
	var href = links[i].getAttribute("href");
	var hash = href.substr(href.indexOf("hash=")+5);
	links[i].setAttribute("href",href+"#"+i);
}*/
  window.setTimeout("handleHistory()", 500);
  return true;
}
/////////////////////////




function createRequestObject() {
    var ro;
    var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer")
	{
        ro = new ActiveXObject("Microsoft.XMLHTTP");
		
    }
	else
	{
        ro = new XMLHttpRequest();
    }

    return ro;
}

var http = createRequestObject();

function makeRequest() 
{
	http_request = false;

	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
		}
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
			http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}

	if (!http_request) {
		alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}
	
}//makeRequest()
function CheckSubmitEmail()
{

/*var email_str = trim(document.getElementById('club_email_id').value);
	if (email_str == '')
	{
		return false;
	}
	else*/
	{
		var email_str = document.getElementById('email').value;
		
		if ( email_str == '' )
			return false;
		//alert ('str: ' + str);
		var format =  email_str.match(/^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/);
		//alert ('format: ' + format);
		if(!format)
		{
			//alert ('E-mail address format invalid.');
			//document.getElementById("club_email_id").focus();
			return false;		
		}
		return true;
	}
}//CheckSubmitEmail

function removeEmail()
{
	g_unsubscribe = 1;
	hideElement( 'div_email_submit_feedback' );
	document.getElementById('div_email_submit_feedback').innerHTML = '<B><I>You are unsubscribed from the list</B></I>';
	
	document.getElementById('subscribe_btn').value = 'Unsubscribe';
	
	document.getElementById('newsletter_heading_id').innerHTML = 'TR Newsletter - Unsubscribe';
}

function submitEmail( id )
{
	hideElement( 'div_email_submit_feedback' );
	
	if ( !CheckSubmitEmail())
	{
		alert('Mail address formatting error');
		return;
	}
	if ( makeRequest() == false )
		alert("make request error");
	
	http_request.onreadystatechange = emailResponse;
	
	email = document.getElementById(id).value;
	emailconfirm = email;
	htmlemail = '1';
	//var list;
	//var listname;
	//list = 'signup';
	//listname = 'employerlawyer.net';
	//subscribe = 'Subscribe';
	if ( g_unsubscribe == 0 )
	{
		document.getElementById('div_email_submit_feedback').innerHTML = '<B><I>Thank you for subscribing. Please check you email for confirmation</B></I>';

		parameters = "email=" + encodeURIComponent( email );
		
		parameters += ( "&emailconfirm=" + encodeURIComponent( emailconfirm ));
		parameters += ( "&htmlemail=" + encodeURIComponent( '1' ));
		parameters += ( "&list[1]=" + encodeURIComponent( 'signup' ));
		parameters += ( "&listname[1]=" + encodeURIComponent( 'tomeraider.com' ));
		parameters += ( "&subscribe=" + encodeURIComponent( 'Subscribe' ));
	
	
		url = 'phplist/?p=subscribe';
		http_request.open('POST', url, true);
		http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		http_request.setRequestHeader("Content-length", parameters.length);
		http_request.setRequestHeader("Connection", "close");
		http_request.send(parameters);
	}
	else
	{
		parameters = "unsubscribeemail=" + encodeURIComponent( email );
		
		parameters += ( "&unsubscribereason=Automated" );
		parameters += ( "&unsubscribe=" + encodeURIComponent( 'Unubscribe' ));
	
	
		url = 'phplist/?p=unsubscribe';
		http_request.open('POST', url, true);
		http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		http_request.setRequestHeader("Content-length", parameters.length);
		http_request.setRequestHeader("Connection", "close");
		http_request.send(parameters);		
	}
}

function emailResponse()
{
		if (http_request.readyState == 4) 
	{
		//alert( http_request.status );
		if (http_request.status == 200) 
		{
		
			if ( g_unsubscribe == 1 )
			{
				g_unsubscribe = 0;
				document.getElementById('subscribe_btn').value = 'Subscribe';
				document.getElementById('newsletter_heading_id').innerHTML = 'TR Newsletter';
			}
			else
			{
				document.getElementById('div_email_submit_feedback').innerHTML = '<B><I>Thank you for subscribing. Please check your email for confirmation</B></I>';
			}
			showElement( 'div_email_submit_feedback' );
		}
	}
}


function downloadBook( id )
{
	url = "book_download.php?id=" + id;
	if ( makeRequest() == false )
		alert("make request error");
	http_request.onreadystatechange = setBookDownload;
	http_request.open('get',url);
	http_request.send(null);
}
function setTextToDIV( div, text )
{
	document.getElementById(div).innerHTML = text;
}
function setBookDownload() 
{ //getFileList onreadystatechange
	if (http_request.readyState == 4) 
	{
		//alert( http_request.status );
		if (http_request.status == 200) 
		{
			document.getElementById('adsense').style.display = 'none';

			//alert( http_request.getAllResponseHeaders() );
			//alert( http_request.responseText );
			//document.getElementById('div_download_result').innerHTML = http_request.responseText;
		}
	}
}

function getBookDetails( id )
{

	url = "book_details.php?id=" + id;
	if ( makeRequest() == false )
		alert("make request error");
	http_request.onreadystatechange = setBookDetails;
	http_request.open('get',url);
	http_request.send(null);
	makeHistory('getBookDetails:'+id);
	//alert( url );
}

function setBookDetails() 
{ //getFileList onreadystatechange
	if (http_request.readyState == 4) 
	{
		if (http_request.status == 200) 
		{
			document.getElementById('div_cover_images').style.display = 'none';
			document.getElementById('adsense').style.display = 'block';
			document.getElementById('div_listing').innerHTML = http_request.responseText;
			window.scrollTo(0,0);
		}
	}
}

function getTopBooks( page_no )
{

	url = "top_rated.php?page_no=" + page_no;
	if ( makeRequest() == false )
		alert("make request error");
	http_request.onreadystatechange = setTopBooks;
	http_request.open('get',url);
	http_request.send(null);
	makeHistory('getTopBooks:'+page_no);
	document.getElementById('loading').style.display = 'inline';
	window.scrollTo(0,0);
	
	//alert( url );
}

function setTopBooks() 
{ //getFileList onreadystatechange
	if (http_request.readyState == 4) 
	{
		if (http_request.status == 200) 
		{
			//document.getElementById('div_cover_images').style.display = 'none';
			document.getElementById('adsense').style.display = 'none';
			document.getElementById('loading').style.display = 'none';
			document.getElementById('div_listing').innerHTML = http_request.responseText;
			window.scrollTo(0,0);
		}
	}
}

function doSearch( msg, pageNo )
{
	//alert(q_str);
	makeHistory("doSearch:"+msg+":"+pageNo);
	var url = "";
	var query = document.getElementById("search_text").value;
	//alert(category);

	makeRequest();
	http_request.onreadystatechange = setSearchResults;
	
	// Only go on if there is a values 
	//if ((text1 == null) || (text1 == "")) return;
	//if ((category == null) || (category == "")) return;
	
	// Build the URL to connect to
	if ( msg == 'firsttime' )
	{
		url = "process_search.php?search_text=" + encodeURIComponent( query );
		
		// Open a connection to the server
		parameters = "search_text=" + encodeURIComponent( query );
	}
	else
	{
		url = "process_search.php?page_no=" + pageNo;
		
		parameters = "page_no=" + pageNo;
	}
	//alert(url);
	// Open a connection to the server
	http_request.open('POST', url, true);
	http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http_request.setRequestHeader("Content-length", parameters.length);
	http_request.setRequestHeader("Connection", "close");
	document.getElementById('loading').style.clientTop = window.screenTop;
	document.getElementById('loading').style.display = 'inline';
	window.scrollTo(0,0);
	http_request.send(parameters);

}//getFileList

function setSearchResults() { //getFileList onreadystatechange
	if (http_request.readyState == 4) {
	//alert(http_request.status);
		if (http_request.status == 200) {
			//alert(http_request.responseText);
			document.getElementById('adsense').style.display = 'none';

			var response = http_request.responseText;
			document.getElementById('div_cover_images').style.display = 'inline';
			document.getElementById('div_listing').innerHTML = response;
					document.getElementById('loading').style.display = 'none';

//			drawBoxes();
			//var xmldoc = http_request.responseXML;
			//var root_node = xmldoc.getElementsByTagName('root').item(0);
			//alert(root_node.firstChild.data);

		} else {
			alert('There was a problem with the request.');
		}
	}

}//setFileList()


function callInProgress(xmlhttp) 
{
	switch ( xmlhttp.readyState ) 
	{
	case 1, 2, 3:
	return 1;
	break;
	
	// Case 4 and 0
	default:
	return 0;
	break;
	}
	return 0;
}
var selected_index = 0;
function changeStaticPage(id)
{
	//alert(document.getElementById(id).value);
	selected_index = document.getElementById(id).selectedIndex;
	showStaticPage(document.getElementById(id).value);
	
}

function showStaticPage(page, s_index )
{
	window.scrollTo(0,0);
	document.getElementById('loading').style.clientTop = window.screenTop;
	document.getElementById('loading').style.display = 'inline';
	//alert('dfs');
	if ( s_index != null )
	selected_index = s_index;
	http.open('get', 'static_pages.php?static_file_name='+page);
    makeHistory('showStaticPage:'+page);
	http.onreadystatechange = handleStaticPage;
    http.send(null);
	
}

function handleStaticPage() {
	
    if(http.readyState == 4){
		
		var response = http.responseText;
		document.getElementById('adsense').style.display = 'none';
		document.getElementById('loading').style.display = 'none';

		document.getElementById('div_cover_images').style.display = 'none';
	    
		document.getElementById('div_listing').innerHTML = response;
		if ( null != document.getElementById('device') )
		{
			document.getElementById('device').selectedIndex = selected_index;
		}
	}
}

function sndReq(action) {
	http.open('get', 'test.php?action='+action);
    http.onreadystatechange = handleResponse;
    http.send(null);
}

  function sndReqArg(action,arg, sortID) {
   //window.location = "www.google.com";
  	//alert(this.getHref());
	//
//alert(window.screenTop);

document.getElementById('loading').style.clientTop = window.screenTop;
	document.getElementById('loading').style.display = 'inline';
	//alert(callInProgress(http));
 
 	if ( callInProgress(http) == 1 )
	{
		//http.Abort();
	}

 	//if ( callInProgress(http) == 0 )
	{
		makeHistory('sndReqArg:'+arg+':'+sortID);
		http.open('get', 'test.php?action='+action+'&arg='+arg+'&sort='+sortID);
		http.onreadystatechange = handleResponse;


		http.send(null);
	}


	
	
  }

  function sndBReqArg(action, arg, id ) {
   //window.location = "www.google.com";
  	//alert(this.getHref());
	//

	http.open('get', 'test.php?action='+action+'&arg='+arg+'&erotic_book='+id );
   // alert( "xxx" );
	http.onreadystatechange = handleResponse;
    http.send(null);
	
	makeHistory(arg);
  }

  function sndReqArgHeader(action,arg,page, sortID, headerID ) 
  {
	document.getElementById('loading').style.display = 'block';
	makeHistory('sndReqArgHeader:'+arg+':'+page+':'+sortID+':'+headerID );  
    http.open('get', 'test.php?action='+action+'&arg='+arg+'&page_no='+page+'&sort='+sortID+'&header='+headerID );
    http.onreadystatechange = handleResponse;
	//if ( callInProgress(http) == 1 )
	{
		//http.Abort();
	}
    http.send(null);
  }

  function sndReqArgPage(action,arg,page, sortID) 
  {
	document.getElementById('loading').style.display = 'block';
	makeHistory('sndReqArgPage:'+arg+':'+page+':'+sortID );  
    http.open('get', 'test.php?action='+action+'&arg='+arg+'&page_no='+page+'&sort='+sortID );
    http.onreadystatechange = handleResponse;
	//if ( callInProgress(http) == 1 )
	{
		//http.Abort();
	}
    http.send(null);
  }


function handleResponse() {
	
	
    if(http.readyState == 4){
  		var response = http.responseText;


		document.getElementById('adsense').style.display = 'none';

		document.getElementById('div_cover_images').style.display = 'none';
	    document.getElementById('div_listing').innerHTML = response;

		document.getElementById('loading').style.display = 'none';

//		window.location.hash = "div_listing";
	//	window.location.hash = "div_top";
//(document.getElementById('loading').style.top = document.body.scrollHeight);

(window.scrollTo(0,0));
    }
}

function handleEroticResponse() {
	
	
    if(http_request.readyState == 4){
        
		document.getElementById('loading').style.display = 'none';
		document.getElementById('adsense').style.display = 'none';

		var response = http_request.responseText;
		//alert(response);
		var arr = response.split('|');

		if ( response == "error!!!!" )
		{
			document.getElementById('erotic_submit_result').style.display = 'inline';
		}
		/*else if ( arr[0] == "error!!!!" )
		{
			document.getElementById('erotic_book').value = arr[1];
			alert("split");
		}*/
		else
		{
			//document.getElementById('erotic_submit_result').style.display = 'hidden';
			document.getElementById('div_listing').innerHTML = response;
		}
    }
}


function validateEmail()
{
	email = document.getElementById('erotic_email').value;
	if ( email == null || email == ' ' || email == '')
	{
		alert("Please Enter the Email Address");
		return;
	}
	
	document.getElementById('loading').style.display = 'inline';

	makeRequest();
	id = document.getElementById('erotic_category').value;
	erbook = document.getElementById('erotic_book').value;
	http_request.open('get', 'erotic_validate.php?id='+id+'&email='+email+'&erotic_book='+ erbook );
	http_request.onreadystatechange = handleEroticResponse;
	http_request.send(null);
}



window.onload = initialize;


function handleHistoryChange(newLocation,
                             historyData) {
  // use the history data to update our UI
  updateUI(newLocation, historyData);                           
}

function initialize() {
  // initialize RSH
  //ZebraTable.stripe('booktable');
  //initRotator();
  dhtmlHistory.initialize();
  
  // add ourselves as a listener for history
  // change events
  dhtmlHistory.addListener(handleHistoryChange);
  
  // determine our current location so we can
  // initialize ourselves at startup
  var initialLocation = 
                dhtmlHistory.getCurrentLocation();
  
  // if no location specified, use the default
  if (initialLocation == null)
    initialLocation = "location1";
  
  // now initialize our starting UI
  //-updateUI(initialLocation, null);
}

function updateUI(newLocation,
                  historyData) {
	//alert(historyData);
	
	if ( historyData != null )
	{
		var data = historyData.split(':');
		
		if ( data[0] == 'message' )
		{
			
			switch( data[1] )
			{
				
				case 'sndReqArg':
				{
					sndReqArg('div_listing', parseInt(data[2],10), parseInt(data[3],10));
					//SwitchMenu(parseInt(data[2],10));
					
				}
				break;
				
				case 'showStaticPage':
					showStaticPage(data[2]);
					break;
				
				case 'getBookDetails':
					getBookDetails(parseInt(data[2],10));
					break;

				case 'getTopBooks':
					getTopBooks(parseInt(data[2],10));
					break;

				case 'sndReqArgHeader':
					sndReqArgHeader('div_listing', parseInt(data[2],10), parseInt(data[3],10), parseInt(data[4],10), parseInt(data[5],10));
					break;
				
				case 'sndReqArgPage':
					sndReqArgPage('div_listing', parseInt(data[2],10), parseInt(data[3],10), parseInt(data[4],10));
					break;
					
				case 'doSearch':
					doSearch( data[2], parseInt(data[3],10));
					break;
				
			}
		}
	}
	else
	{
		window.location.href = 'index.php';
	}
 /* var output = document.getElementById("output");
  
  // simply display the location and the
  // data
  var historyMessage;
  if (historyData != null)
    historyMessage = historyData.message;
    
  var message = "New location: "
                + newLocation 
                + ", history data="
                + historyMessage;
                
  output.innerHTML = message;*/
  
}

function adsense()
{

google_ad_client = "pub-5745562053510104";
google_ad_width = 336;
google_ad_height = 280;
google_ad_format = "336x280_as";
google_ad_type = "text_image";
google_ad_channel ="";
google_color_border = "#f1f1f1";
google_color_bg = "E6E6E6";
google_color_link = "80FF00";
google_color_text = "FFFFFF";
google_color_url = "99C9FF";
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"
}

function searchEnter(e){
	if(window.event) // IE
	{
	keynum = e.keyCode
	}
	else if(e.which) // Netscape/Firefox/Opera
	{
	keynum = e.which
	}
	//alert(keynum);
	if ( keynum == 13 )
	doSearch('firsttime',1);
	return false;
}