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

var weburl = "https://ebookcvm.contentoro.net/ebookc5/";

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 co_TOC(xbookid, xchapterid, xtheme, xcustomerid, targetdiv)
{
	co_Core(xbookid, -2, xtheme, xcustomerid, targetdiv);
}

function co_(xbookid, xtheme, xcustomerid, targetdiv)
{
    chapterid = getParameterByName('chapterid');
    if(chapterid == "")
    {
        chapterid = getParameterByName('ChapterTitle');
        if(chapterid == "")
            chapterid = -2;                        
    }

    co_Core(xbookid, chapterid, xtheme, xcustomerid, targetdiv);
}

function co_Core(xbookid, xchapterid, xtheme, xcustomerid, targetdiv)
{    

	bookid = getParameterByName('bookid');
	catbookid = getParameterByName('catbookid');
	
	if(bookid == "")
	{
	    bookid = xbookid;
        //initial
  	    catbookid = bookid;	    	    
	}   

	chapterid = xchapterid;
        if(chapterid == "" || chapterid == -2)
		catbookid = bookid;	    	                
			
 
    callbackurl = window.location.href;   //getParameterByName('callbackurl');    
    callbackurl = window.location.pathname;

	customerid = getParameterByName('customerid');
	catid = getParameterByName('catid');
    callbacksuffix = "themeid=" + xtheme;   

	myurl = weburl + xtheme + "_Body.asp?catid=" + catid + "&catbookid=" + catbookid + "&bookid=" + bookid + "&chapterid=" + chapterid + "&customerid=" + xcustomerid + "&callbackurl=" + callbackurl + "&callbacksuffix=" + callbacksuffix;
    
    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('GET', myurl);
        xhr.onerror = function() 
        {
            alert('Sorry! Contentoro book is not available.');
        };
        xhr.onload = function() 
        {                      
            photosDiv.innerHTML = xhr.responseText;
        };
        xhr.send();
    }


if(chapterid == "" || chapterid==-2)    
	myurlhead = weburl + "BookTitle.asp?bookid=" + bookid + "&customerid=" + xcustomerid;               
else
	myurlhead = weburl + "ChapterTitle.asp?bookid=" + bookid + "&chapterid=" + chapterid + "&customerid=" + xcustomerid;               



    var xhrhead = new XMLHttpRequest();      
    if (!('withCredentials' in xhrhead)) 
    {
        alert('Browser does not support CORS.');
        return;
    }
    xhrhead.open('GET', myurlhead);
    xhrhead.onerror = function() 
    {
        alert('Sorry! Contentoro book is not available.');
    };
    xhrhead.onload = function() 
    {                      
        document.title = xhrhead.responseText;
    };
    xhrhead.send();





myurlhead2 = weburl + "BookTitle.asp?bookid=" + bookid + "&customerid=" + xcustomerid;               


    var xhrhead2 = new XMLHttpRequest();      
    if (!('withCredentials' in xhrhead2)) 
    {
        alert('Browser does not support CORS.');
        return;
    }
    xhrhead2.open('GET', myurlhead2);
    xhrhead2.onerror = function() 
    {
        alert('Sorry! Contentoro book is not available.');
    };
    xhrhead2.onload = function() 
    { 
        //$('meta[name="description"]').attr("content", newDescription);                     
        document.querySelector('head').innerHTML += '<meta name="decription" content="' + xhrhead2.responseText + '" />';
    };
    xhrhead2.send();

}



/************************************************/

function ListBooks(mybookid, listtarget)
{
	myurl = weburl + "BookListEx.asp?bookid=" + mybookid;
        
    var photosDiv = document.getElementById(listtarget);
            
    if(false)
        photosDiv.innerHTML = myurl;
    else  
    {
	    var xhr = new XMLHttpRequest();
      
        if (!('withCredentials' in xhr)) 
        {
            alert('Browser does not support CORS.');
            return;
        }
        xhr.open('GET', myurl);
        xhr.onerror = function() 
        {
            alert('Sorry! Contentoro book is not available.');
        };
        xhr.onload = function() 
        {                      
            photosDiv.innerHTML = xhr.responseText;
        };
        xhr.send();
    }
}


function ListThemes(mybookid, mythemeid, listtarget)
{
	myurl = weburl + "ThemeList.asp?bookid=" + mybookid + "&ThemeName=" + mythemeid;
    
    var photosDiv = document.getElementById(listtarget);
            
    if(false)
        photosDiv.innerHTML = myurl;
    else  
    {
	    var xhr = new XMLHttpRequest();
      
        if (!('withCredentials' in xhr)) 
        {
            alert('Browser does not support CORS.');
            return;
        }
        xhr.open('GET', myurl);
        xhr.onerror = function() 
        {
            alert('Sorry! Contentoro book is not available.');
        };
        xhr.onload = function() 
        {                      
            photosDiv.innerHTML = xhr.responseText;
        };
        xhr.send();
    }
}

function AddThemes(mybookid, mythemeid, myaction)
{
	myurl = weburl + "ThemeForBook.asp?bookid=" + mybookid + "&ThemeName=" + mythemeid;
    if(myaction == "add")
		myurl = myurl + "&action=add";
		
    var photosDiv = document.getElementById(listtarget);
            
    if(false)
        photosDiv.innerHTML = myurl;
    else  
    {
	    var xhr = new XMLHttpRequest();
      
        if (!('withCredentials' in xhr)) 
        {
            alert('Browser does not support CORS.');
            return;
        }
        xhr.open('GET', myurl);
        xhr.onerror = function() 
        {
            alert('Sorry! Contentoro book is not available.');
        };
        xhr.onload = function() 
        {                      
            photosDiv.innerHTML = xhr.responseText;
        };
        xhr.send();
    }
}

function DisplayTheme(mythemeid, listtarget)
{
	myurl = weburl + "themedetail.asp?ThemeName=" + mythemeid;
		
    var photosDiv = document.getElementById(listtarget);
            
    if(false)
        photosDiv.innerHTML = myurl;
    else  
    {
	    var xhr = new XMLHttpRequest();
      
        if (!('withCredentials' in xhr)) 
        {
            alert('Browser does not support CORS.');
            return;
        }
        xhr.open('GET', myurl);
        xhr.onerror = function() 
        {
            alert('Sorry! Contentoro book is not available.');
        };
        xhr.onload = function() 
        {                      
            photosDiv.innerHTML = xhr.responseText;
        };
        xhr.send();
    }
}



//*************added
function co_2BookLayout(bookid1, bookid2, div1, div2, div3, theme1, theme2, theme3, customerid)
{
    bookid = getParameterByName('bookid');
    ChapterTitle = getParameterByName('ChapterTitle');

    chapterid = getParameterByName('chapterid');
    if(chapterid == "")
    {
        chapterid = getParameterByName('ChapterTitle');
        if(chapterid == "")
            chapterid = -2;                        
    }


    if(ChapterTitle == "")
    
    //if(chapterid == -2)
    {
    	
	co_TOC(bookid1, -2, theme1, customerid, div1);
	    
	co_TOC(bookid2, -2, theme2, customerid, div2);
    
    }
    
    else
	    
	co_Core(bookid, ChapterTitle, theme3, customerid, div3);
	
}

