var xmlHttpnew;

function get_cookie ( cookie_name ){
  var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );
  if ( results )
    return ( unescape ( results[2] ) );
  else
    return null;
}

function statechangednew(){
if (xmlHttpnew.readyState==4){
	test=xmlHttpnew.responseText;
var display = get_cookie ( "location" );
document.getElementById(display).innerHTML=xmlHttpnew.responseText;

	var loadnext = get_cookie ( "loadnext" );
//	alert (loadnext);
		if (loadnext=="loginform"){
		loadapp('./displaydetail-new.php?page=2','application','start');
		document.cookie = "loadnext=";
		}
		if (loadnext=="previewform"){
		loadapp3('./displaydetail-new.php?page=2','application','start');
//		url="./preview.php";
//		window.location.href = url;
		document.cookie = "loadnext=";
		}
}
}

function createRequest(){ 
var xmlHttpnew=null;
  if (document.cookie == "")
  {
    alert("Please enable cookies to Login and enjoy the site!");
  } else {
	try{
	  // Firefox, Opera 8.0+, Safari
	  xmlHttpnew=new XMLHttpRequest();
	  }
	catch (e){
	  // Internet Explorer
	  try{
		xmlHttpnew=new ActiveXObject("Msxml2.xmlHttp");
		}
	  catch (e){
		xmlHttpnew=new ActiveXObject("Microsoft.xmlHttp");
		}
	  }
	return xmlHttpnew;
	}
}



/////////////////////////////////////builder///////////////////////////////////////////////////////////////

function showwiz(what,where,tab1,tab2,tab3,tab4,tab5,tab6) {
xmlHttpnew=createRequest();
//alert(where);
document.cookie = "b-what=" + what;
document.cookie = "tab1=" + tab1;
document.cookie = "tab2=" + tab2;
document.cookie = "tab3=" + tab3;
document.cookie = "tab4=" + tab4;
document.cookie = "tab5=" + tab5;
document.cookie = "tab6=" + tab6;

var ws = document.forms["info"].wsize.value;
document.getElementById("selected").style.width = ws;


var pcount = document.forms["info"].pc.value;
document.getElementById("pagecount").innerHTML ="<p style='color:red; padding:0 0 0 10px'><b>"+pcount+"</b></p>";

if (tab1=="ser") {
//alert(tab1);
document.getElementById(tab1).className = "wiz-ser";
}else{
document.getElementById(tab1).className = "selected2";
}	
document.getElementById(tab2).className = "detail2";
document.getElementById(tab3).className = "detail2";
document.getElementById(tab4).className = "detail2";
document.getElementById(tab5).className = "detail2";
//document.getElementById(tab6).className = "detail2";


var key = document.forms["search"].keyword.value;
what=what+"&keyword="+key;

document.cookie = "location=" + where;

document.getElementById(where).innerHTML ="<table height=260px; width=935px; bgcolor=#ffffff align=center valign=center><tr><td align=center style='color:#6b0f0f;font-size:20px;'><img src=./images/loading-1.gif></td></tr></table>";
//alert("hello");
xmlHttpnew.onreadystatechange=statechangedwiz;
xmlHttpnew.open("GET",what,true);
xmlHttpnew.send(null);
}

function statechangedwiz(){
if (xmlHttpnew.readyState==4){
	test=xmlHttpnew.responseText;
//alert(test);
		var display = get_cookie ( "location" );
		
document.getElementById(display).innerHTML=xmlHttpnew.responseText;
}
}

function wizselect(where,image,el,hide) {
	xmlHttpnew=createRequest();

	document.getElementById('light').style.display='none';document.getElementById('fade').style.display='none';
	
	var w = document.forms["info"].wsize.value;
	var ws = parseInt(w)
	ws = ws + 75;
	document.getElementById("selected")
	//alert( document.getElementById("selected") );

	document.getElementById("selected").style.width = ws;

	document.forms["info"].wsize.value=ws;
	
	
	document.getElementById(hide).style.display = 'none';
	
	var pcount = document.forms["info"].pc.value;
	var pagecount = parseInt(pcount)
	pagecount =pagecount + 1;
	document.cookie = "bookpages=" + pagecount;
	document.getElementById("pagecount").innerHTML ="<p style='color:red; padding:0 0 0 10px'><b>"+pagecount+"</b></p>";
	document.forms["info"].pc.value=pagecount;
	
	var sesId = document.getElementById('sesId').value;
	
	what= "process_builder.php?action=builder-addpage&pageId="+el+"&sesId="+sesId;
	what=what+"&sid="+Math.random();
	
		document.cookie = "location=" + where;

//		alert(where);
//		alert(what);
		
//		document.getElementById("selected").innerHTML ="<div style='color:#ffffff;'><b>Processing&nbsp;&nbsp;&nbsp;<img src=./images/loading-3.gif></b></div>";

		document.getElementById("selected").innerHTML ="<div style='color:#000000; padding:30px 0 0 20px; '><b>Creating Page....&nbsp;&nbsp;&nbsp;<img src=./images/loading-2.gif></b></div>";


xmlHttpnew.onreadystatechange=statechangedwiz;
xmlHttpnew.open("GET",what,true);
xmlHttpnew.send(null);
}

function removeExisting(where,el) {
if (confirm("Are you sure you want to delete this page")) {

	document.getElementById('light').style.display='none';document.getElementById('fade').style.display='none';
	
	var w = document.forms["info"].wsize.value;
	var ws = parseInt(w)
	ws = ws - 75;
	document.forms["info"].wsize.value=ws;
	document.getElementById("selected").style.width = ws;		

	
	var pcount = document.forms["info"].pc.value;
	var pagecount = parseInt(pcount)
	pagecount = pagecount - 1;
	document.cookie = "bookpages=" + pagecount;
	
	document.getElementById("pagecount").innerHTML ="<p style='color:red; padding:0 0 0 10px'><b>"+pagecount+"</b></p>";
	document.forms["info"].pc.value=pagecount;
	
	var sesId = document.getElementById('sesId').value;
	what= "process_builder.php?action=deletePageExisting&pageId="+el+"&sesId="+sesId;
	what=what+"&sid="+Math.random();
	
	document.cookie = "location=" + where;
	
	xmlHttpnew.onreadystatechange=statechangedwiz;
	xmlHttpnew.open("GET",what,true);
	xmlHttpnew.send(null);
	
}
}
function loadnextpage(what,where,selected,bookId,pages,cover){
		document.cookie = "bookedit=" + bookId;
		document.cookie = "bookpages=" + pages;
		document.cookie = "bookcover=" + cover;
		
//alert(bookId);

document.getElementById("light").innerHTML ="<div style='padding:40px 0 0 0;'><b>Loading...</b></div>";
document.getElementById('light').style.display='block';document.getElementById('fade').style.display='block'

		var url=what;
//		setTimeout('window.location.href = url;',3000);
		window.location.href = url;	
}	

function coverselect(where,t,id,p,title) {
xmlHttpnew=createRequest();

//alert(where);
document.cookie = "bookcover=" + t;
//document.cookie = "bookpages=" + pages;

document.cookie = "location=" + where;
var display = get_cookie ( "location" ); 
document.getElementById(display).innerHTML ="<img src='../images/loading.gif' width=237px>";

var title=document.coverform.bookname.value;
var sub=document.coverform.subname.value;
var bookid=document.coverform.bookid.value;

document.coverform.covertype.value = t;
document.coverform.coverId.value = id;

if (p==0 || p==null){
document.coverform.price.value = '39.95';
}else{
document.coverform.price.value = p;
}
//alert (p);

if (id=='16' || id=='24' || id=='32' || id=='34' || id=='36' || id=='38' || id=='40' || id=='44' || id=='46'){ //hard cover top
var what = "./cover/imagem-process.php?coverImage="+id+".jpg&coverText="+title+"&subText="+sub+"&bookId="+bookid+"&locations=90&coverSize=preview&fontsize=29&coverPrice="+p;
var cover_print = "./cover/imagem.php?coverImage="+id+".jpg&coverText="+title+"&subText="+sub+"&bookId="+bookid+"&location=650&coverSize=print&fontsize=160&coverPrice="+p;
}
else if (id=='20' || id=='26' || id=='33' || id=='35' || id=='37' || id=='39' || id=='41' || id=='45' || id=='47'){ //soft cover top
var what = "./cover/imagem-process.php?coverImage="+id+".jpg&coverText="+title+"&subText="+sub+"&bookId="+bookid+"&locations=90&coverSize=preview&fontsize=29&coverPrice="+p;
var cover_print = "./cover/imagem.php?coverImage="+id+".jpg&coverText="+title+"&subText="+sub+"&bookId="+bookid+"&location=525&coverSize=print&fontsize=160&coverPrice="+p;
}
else if (id=='17' || id=='18' || id=='19' || id=='28' || id=='30' || id=='42'){ //hard cover bottom
var what = "./cover/imagem-process.php?coverImage="+id+".jpg&coverText="+title+"&subText="+sub+"&bookId="+bookid+"&locations=340&coverSize=preview&fontsize=29&coverPrice="+p;
var cover_print = "./cover/imagem.php?coverImage="+id+".jpg&coverText="+title+"&subText="+sub+"&bookId="+bookid+"&location=2000&coverSize=print&fontsize=160&coverPrice="+p;
}
else if (id=='21' || id=='22' || id=='23' || id=='29' || id=='31' || id=='43'){ //soft cover bottom
var what = "./cover/imagem-process.php?coverImage="+id+".jpg&coverText="+title+"&subText="+sub+"&bookId="+bookid+"&locations=340&coverSize=preview&fontsize=29&coverPrice="+p;
var cover_print = "./cover/imagem.php?coverImage="+id+".jpg&coverText="+title+"&subText="+sub+"&bookId="+bookid+"&location=1775&coverSize=print&fontsize=160&coverPrice="+p;
}
//alert(cover_print);
//document.getElementById('print').innerHTML = cover_print;
document.cookie = "cover=" + cover_print;

//alert(what)
xmlHttpnew.onreadystatechange=statechangednew;
xmlHttpnew.open("GET",what,true);
xmlHttpnew.send(null);
}

function changetitle(what,where,form,v,hide,IdName) {
//alert(form+v+where);
xmlHttpnew=createRequest();
//Get the form reference:
var form = document.forms[form];
//Get the exact form element:
var formTag = form.elements[v];
//Get the value of the input element:
var tag = formTag.value; 

if(tag.length<32){
	if (v=="bookTitle"){
	document.coverform.bookname.value = tag;
	}
	else {
	document.coverform.subname.value = tag;
	}
	
	var str=tag;
	var replace=str.replace("'", "");
	//alert(replace);		
	
	what = what + "&formvalue=" + replace + "&hide=" + hide;
	//alert (what);
	what=what+"&sid="+Math.random();

	document.cookie = "location=" + where;
	var display = get_cookie ( "location" ); 
	document.getElementById(display).innerHTML ="<div style='color:red;'>Processing.....</div>";

	//alert ("page udated");
	var t=document.coverform.covertype.value;
	var id=document.coverform.coverId.value;
	
	var p=document.coverform.price.value;

	xmlHttpnew.onreadystatechange=statechangedcover;
	xmlHttpnew.open("GET",what,true);
	xmlHttpnew.send(null);
	
		if (v=="bookTitle"){
		var title=document.coverform.bookname.value;
		}
		else {
		var title=document.coverform.subname.value;
		}
}
else{
alert("your book title cannot exceed 32 characters");
}
}

function statechangedcover(){
if (xmlHttpnew.readyState==4){
	test=xmlHttpnew.responseText;
var display = get_cookie ( "location" );
document.getElementById(display).innerHTML=xmlHttpnew.responseText;

var t=document.coverform.covertype.value;
var id=document.coverform.coverId.value;
var p=document.coverform.price.value;
//alert(p);

var title=document.coverform.bookname.value;
///fix this area
coverselect('preview',t,id,p,title);

return null;

}
}

function loginaccessv2(what,where,f) {
xmlHttpnew=createRequest();
var l = document.forms[f].login.value;
var p = document.forms[f].pword.value;

if (document.forms[f].rememberMe.checked){
	var r = "true";
}
//alert (r)
if (l=="")
  {
  alert ("Please enter Username");
  return;
  } 
if (p=="")
  {
  alert ("Please enter password");
  return;
  }   
what=what+"&login="+l+"&pword="+p+"&r="+r;
what=what+"&sid="+Math.random();
document.cookie = "location=" + where;
//alert (l);
document.cookie = "loadnext=" + f;
var display = get_cookie ( "location" ); 
document.getElementById(display).innerHTML ="<div style='color:#6b0f0f;'><b>Processing&nbsp;&nbsp;<img src=./images/loading-2.gif></b></div>";
//alert(what);
xmlHttpnew.onreadystatechange=statechangednew;
xmlHttpnew.open("GET",what,true);
xmlHttpnew.send(null);
return false;
}


function logout(what,where,pageName) {
xmlHttpnew=createRequest();

what=what+"&sid="+Math.random();
document.cookie = "location=" + where;
//alert (what);
var display = get_cookie ( "location" ); 
document.getElementById(display).innerHTML ="<div style='color:#6b0f0f;'><b>Processing....................................</b></div>";

xmlHttpnew.onreadystatechange=statechangednew;
xmlHttpnew.open("GET",what,true);
xmlHttpnew.send(null);

}

function gotopreviewv2(what,where,selected){
		var b = get_cookie ( "bookedit" ); 
		var p = get_cookie ( "bookpages" ); 
		var c = get_cookie ( "bookcover" ); 
//		alert(what);
		loadpreviewv2(what,where,selected,b,p,c);
} 

function gotopreviewstart(what,where,selected,b,p,c){
		loadpreviewv2(what,where,selected,b,p,c);
} 

function loadpreviewv2(what,where,selected,bookId,pages,cover){

if (cover==2){
var tp=20;
var c ="Soft Cover";
}else{
var tp=20;
var c="Hard Cover";
}
//alert (pages+"-"+tp + "-" + cover);
if(pages<tp){
	if (confirm("There are only "+pages+" pages in your book.\nA "+c+" book requires a minimum of "+tp+" pages.\nBlank pages will be added at the end of your book.\n\nDo you wish to continue?")) {
		document.cookie = "what-a=" + what;
		document.cookie = "where-a=" + where;
		document.cookie = "selected-a=" + selected;
		document.cookie = "bookedit=" + bookId;
		document.cookie = "bookcover=" + cover;
		document.cookie = "bookpages=" + pages;
		var bid = get_cookie ( "bookedit" );
		var url="preview-book.php?bookId="+bid;
		window.location.href = url;		
		}
}else{
		document.cookie = "what-a=" + what;
		document.cookie = "where-a=" + where;
		document.cookie = "selected-a=" + selected;
		document.cookie = "bookedit=" + bookId;
		document.cookie = "bookcover=" + cover;
		document.cookie = "bookpages=" + pages;
		var bid = get_cookie ( "bookedit" );
		

		var url="preview-book.php?bookId="+bid;
		window.location.href = url;	
}
}