var bookid;
var chapterid;
var paragraphid;
var customerid;
var catid;
var selclass;

var weburl = "http://www.proximica.com/";

function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(location.search);
    return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}

function InitContentStream()
{
	bookid = getParameterByName('bookid');
	chapterid = getParameterByName('chapterid');
    if(chapterid == "")
    {
        chapterid = getParameterByName('ChapterTitle');
        if(chapterid == "")
            chapterid = -2;
    }
    
	paragraphid = getParameterByName('paragraphid');
	customerid = getParameterByName('customerid');
	catid = getParameterByName('catid');
	selclass = getParameterByName('class');
}


function StreamCoCOntentTA1(xbookid, targetdiv, xcallbackurl)
{    
    var method = 'GET';
	
	bookid = getParameterByName('bookid');
	if(bookid == "")
	    bookid = xbookid;
	    
	chapterid = getParameterByName('chapterid');
    if(chapterid == "")
    {
        chapterid = getParameterByName('ChapterTitle');
        if(chapterid == "")
            chapterid = -2;
    }
 
        callbackurl = './' + xcallbackurl;   //getParameterByName('callbackurl');
	customerid = getParameterByName('customerid');
	catid = getParameterByName('catid');

	if(catid == "")
	{
		if(chapterid == -2)
			myurl = "http://www.proximica.com/ebookc5/TOCButtonPretty.asp?catid=";		
		else
			myurl = "http://www.proximica.com/ebookc5/TOCButtonPretty.asp?catid=" + chapterid;						
    }
	else
	{	
		if(catid != chapterid)
			myurl = "http://www.proximica.com/ebookc5/Chapter.asp?catid=" + catid;
		else			
			alert("Content Not Available !");
    }

	myurl = myurl + "&bookid=" + bookid + "&chapterid=" + chapterid + "&customerid=1322&callbackurl=" + callbackurl;

    //alert(myurl);
    
    var photosDiv = document.getElementById(targetdiv);
    
    if(false)
        photosDiv.innerHTML = myurl;
    else  
    {
	    var xhr = new XMLHttpRequest();
      
        if (!('withCredentials' in xhr)) 
        {
            alert('Browser does not support CORS.');
            return;
        }
        xhr.open(method, myurl);
        xhr.onerror = function() 
        {
            alert('Sorry! Contentoro book is not available.');
        };
        xhr.onload = function() 
        {          
            photosDiv.innerHTML = xhr.responseText;
        };
        xhr.send();
    }
}


/*   *******************************************   */
function ContentoroNuggetDiv(bookid, chapterid, paragraphid, customerid, callbackurl, apifunction, targetdiv)
{    	
	ContentoroNuggetDiv2Ex(bookid, chapterid, paragraphid, customerid, callbackurl, '', apifunction, targetdiv, true, true, true, true, '');
}    

function ContentoroNuggetDiv2(bookid, chapterid, paragraphid, customerid, callbackurl, callbacksuffix, apifunction, targetdiv, showImage, showbookImage, showText)
{
	ContentoroNuggetDiv2Ex(bookid, chapterid, paragraphid, customerid, callbackurl, callbacksuffix, apifunction, targetdiv, showImage, showbookImage, showText, true, '');
}

function ContentoroNuggetDiv2Ex(bookid, chapterid, paragraphid, customerid, callbackurl, callbacksuffix, apifunction, targetdiv, showImage, showbookImage, showText, showChapterImage, selclass)
{    
	var method = 'GET';
  
    var url = weburl + 'ebookc5/' + apifunction + '.asp?bookid=' + bookid + '&chapterid=' + chapterid + '&paragraphid=' + paragraphid + '&customerid=' + customerid + '&callbackurl=' + callbackurl;
 
    if(selclass != '')
        url += "&class=" + selclass;
  
    
    if(callbacksuffix)
        url += "&callbacksuffix=" + callbacksuffix;

    if(!showImage)
        url += "&showImage=0";

    if(!showbookImage)
        url += "&showbookImage=0";

    if(!showText)
        url += "&showText=0";

    if(!showChapterImage)
        url += "&showChapterImage=0";
     
   
	var xhr = new XMLHttpRequest();
  
if (!('withCredentials' in xhr)) {
    alert('Browser does not support CORS.');
    return;
  }
  xhr.open(method, url);
  xhr.onerror = function() {
    alert('Sorry! Contentoro book is not available.');
  };
  xhr.onload = function() {  
    var photosDiv = document.getElementById(targetdiv);
    photosDiv.innerHTML = xhr.responseText;
  };
  xhr.send();
}    

function ContentoroNuggetDiv3Ex(bookid, chapterid, paragraphid, customerid, callbackurl, callbacksuffix, apifunction, targetdiv, showChapterImage, showChapterTitle)
{    
	var method = 'GET';
  
    var url = weburl + 'ebookc5/' + apifunction + '.asp?bookid=' + bookid + '&chapterid=' + chapterid + '&paragraphid=' + paragraphid + '&customerid=' + customerid + '&callbackurl=' + callbackurl;
  
    
   if(callbacksuffix)
        url += "&callbacksuffix=" + callbacksuffix;


    if(showChapterImage)
        url += "&showChapterImage=1";
    else
        url += "&showChapterImage=0";


    if (showChapterTitle)
        url += "&showChapterTitle=1";
    else
        url += "&showChapterTitle=0";



	 var xhr = new XMLHttpRequest();
  
if (!('withCredentials' in xhr)) {
    alert('Browser does not support CORS.');
    return;
  }
  xhr.open(method, url);
  xhr.onerror = function() {
    alert('Sorry! Contentoro book is not available.');
  };
  xhr.onload = function() {  
    var photosDiv = document.getElementById(targetdiv);
    photosDiv.innerHTML = xhr.responseText;
  };
  xhr.send();
	
}    


function ContentoroNuggetImg(bookid, chapterid, paragraphid, customerid, callbackurl, apifunction, targetdiv) 
{  
  var method = 'GET';
  
	var url = weburl + 'ebookc5/' + apifunction + '.asp?bookid=' + bookid + '&chapterid=' + chapterid + '&paragraphid=' + paragraphid + '&customerid=' + customerid;
  var xhr = new XMLHttpRequest();
  if (!('withCredentials' in xhr)) {
    alert('Browser does not support CORS.');
    return;
  }
  xhr.open(method, url);
  xhr.onerror = function() {
    alert('Sorry! Contentoro book is not available.');
  };
  xhr.onload = function() {  
    var photosDiv = document.getElementById(targetdiv);
    photosDiv.src = xhr.responseText;
  };
  xhr.send();
}  
  
function ContentoroNuggetDivImg(bookid, chapterid, paragraphid, customerid, callbackurl, apifunction, targetdiv)
{  
  var method = 'GET';
  
var url = weburl + 'ebookc5/' + apifunction + '.asp?bookid=' + bookid + '&chapterid=' + chapterid + '&paragraphid=' + paragraphid + '&customerid=' + customerid;
  var xhr = new XMLHttpRequest();
  if (!('withCredentials' in xhr)) {
    alert('Browser does not support CORS.');
    return;
  }
  xhr.open(method, url);
  xhr.onerror = function() {
    alert('Sorry! Contentoro book is not available.');
  };
  xhr.onload = function() {  
    var photosDiv = document.getElementById(targetdiv);
    photosDiv.style.backgroundImage = "url('"+xhr.responseText+"')";
  };
  xhr.send();
} 
