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;
	}
}


/////////////////////////////////////////////////////////functions////////////////////////////////////////////////////////

function loginaccess(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>";

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 loaddata(what,where) {
xmlHttpnew=createRequest();

what=what+"&sid="+Math.random();
//alert(what)

document.cookie = "location=" + where;
var display = get_cookie ( "location" ); 
document.getElementById(display).innerHTML ="<table align=center valign=center><tr><td align=center><img src='../images/loading.gif'></td></tr></table>";

xmlHttpnew.onreadystatechange=statechangednew;
xmlHttpnew.open("GET",what,true);
xmlHttpnew.send(null);

}

function edit(what,where,form,v,hide,IdName) {
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; 


what = what + "&formvalue=" + tag + "&hide=" + hide;
//alert (what);
what=what+"&sid="+Math.random();

document.cookie = "location=" + where;
var display = get_cookie ( "location" ); 
document.getElementById(display).innerHTML ="";

xmlHttpnew.onreadystatechange=statechangednew;
xmlHttpnew.open("GET",what,true);
xmlHttpnew.send(null);

if (hide != ''){	
var b = IdName;
b = b + hide;
showhide(b);
}

}

function remove(what,where) {
xmlHttpnew=createRequest();

if (confirm("Are you sure you want to delete")) {
what=what+"&sid="+Math.random();
//alert(what)
document.cookie = "location=" + where;
var display = get_cookie ( "location" ); 
document.getElementById(display).innerHTML ="<table align=center><tr><td width=800px height=80px align=center><b>Processing....................................</b></td></tr></table>";

xmlHttpnew.onreadystatechange=statechangednew;
xmlHttpnew.open("GET",what,true);
xmlHttpnew.send(null);
}
}

function showhide(str) {
//alert (str);
var e = document.getElementById(str);
if(e.style.display == 'none')
e.style.display = 'block';
else
e.style.display = 'none';
}

function urlDirect(pageName){
//var url = document.location.href;
//var splUrl = url.split("/");
//var splLen = splUrl.length;
//var actUrl = splUrl[splLen-1];
//var urlData = actUrl.substr(0,16);
window.location.href = pageName;
}



function mybook(bookId,url)
	{
	document.cookie = "bookId=" + bookId;
	window.location.href = url;
	}

	
function purchaseBook(bid, coverVal, totalPages, coverType)

	{

	var checkVal1 = 0;
	if (totalPages < 24 )
		checkVal1 = 1;
	else if (totalPages > 200 )
		checkVal1 = 2;
		
if (bid.length>0){
var bid = bid;
}else{
var bid = get_cookie ( "bookedit" );		
}
	if (coverVal==1 || bid.length>0) {
 
	if (checkVal1==1){
			if (coverType==1){
				Ext.MessageBox.confirm('Confirm', '<b>The minimum allowed pages is 54, You only have '+
				totalPages+' pages.<br>The rest of your book will be blank pages and that will<br>look dumb. Do you 	wish to continue?</b>', showResult);
			} else{
				Ext.MessageBox.confirm('Confirm', '<b>The minimum allowed pages is 24, You only have '+
				totalPages+' pages.<br>The rest of your book will be blank pages and that will<br>look dumb. Do you 	wish to continue?</b>', showResult);
			}

		} else if (checkVal1==2) {
			Ext.MessageBox.confirm('Confirm', '<b>The maximum pages allowed is 200, You have '+totalPages+' pages<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Pages over 200 will be removed<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Do you wish to continue?</b>', showResult);
		} else {
			var btn = 'yes';
			showResult(btn); 
		}
	} else if (bid.length=0) {
			Ext.MessageBox.alert( '! Alert ..', '<b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Please save your book to preview <br> or edit an existing to preview<br></b>');
	} else {
			Ext.MessageBox.alert( '! Alert ..', '<b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;You will need to choose a book cover first.<br>Select the Choose Cover button next to your book.</b>');
	}

		function showResult(btn){
			if (btn=='yes'){
				window.location.href = "preview.php?bookId="+bid;
				return true;
			} else if (btn=='no')
				return false;
			}
	}
function duplicate(bookid)
	{
	var dupId = 'diuplicate'+bookid;
		Ext.get(dupId).on('click', function(e){
		Ext.MessageBox.confirm('Confirm', 'Are you sure you want to make a copy of this book?', showResult);
		});

		function showResult(btn)
		{
		if (btn=='yes')
			{
			var dupbookid = bookid;
			showUser('no_of_divs=1&link1##coverdesignprocess.php?div=divDuplicate~dupbookid='+dupbookid+'~action=DupBookCreate');
			}
		else
			{
			return false;
			}
		}
	}

function deleteBook(abc)
	{
	var delId = "delete"+abc;

		Ext.get(delId).on('click', function(e){
			Ext.MessageBox.confirm('Confirm', 'Are you sure you want to delete this book?<br> Seriously- you can not get it back', showResult);
		});

		function showResult(btn)
		{
		if (btn=='yes')
			{
			var del_book = abc;
 			var divname = 'del'+del_book;
			document.getElementById(divname).style.display = 'none'
			showUser('no_of_divs=1&link1##coverdesignprocess.php?div='+divname+'~Delid='+del_book+'~action=Del_Book');
			}
		else
			{
			return false;
			}
		}
	}

	
function edituser(what,where,f) {
xmlHttpnew=createRequest();

var a = document.forms[f].firstName.value;
var b = document.forms[f].lastName.value;
var c = document.forms[f].emailID.value;
var d = document.forms[f].address1.value;
var e = document.forms[f].city.value;
var g = document.forms[f].postCode.value;
var h = document.forms[f].phoneNo.value;
var f = document.forms[f].state.value;


what=what+"&firstName="+a+"&lastName="+b+"&emailID="+c+"&address1="+d+"&city="+e+"&state="+f+"&postCode="+g+"&phoneNo="+h;
//alert (what);
what=what+"&sid="+Math.random();
document.cookie = "location=" + where;
var display = get_cookie ( "location" ); 
document.getElementById(display).innerHTML ="<table width=100% height=300px bgcolor=#6b0f0f align=center valign=center><tr><td align=center style='color:white;font-size:20px;'>Processing.......</td></tr></table>";

xmlHttpnew.onreadystatechange=statechangednew;
xmlHttpnew.open("GET",what,true);
xmlHttpnew.send(null);
return false;
}


function newuser(what,where,f) {
xmlHttpnew=createRequest();

var a = document.forms[f].firstName.value;
var b = document.forms[f].lastName.value;
var c = document.forms[f].emailID1.value;
var d = document.forms[f].emailID2.value;
var e = document.forms[f].password1.value;
var f = document.forms[f].password2.value;



what=what+"&firstName="+a+"&lastName="+b+"&emailID1="+c+"&emailID2="+d+"&password1="+e+"&password2="+f;
//alert (what);
what=what+"&sid="+Math.random();
document.cookie = "location=" + where;
var display = get_cookie ( "location" ); 



document.getElementById(display).innerHTML ="<table width=100% height=300px bgcolor=#6b0f0f align=center valign=center><tr><td align=center style='color:white;font-size:20px;'>Processing.......</td></tr></table>";
xmlHttpnew.onreadystatechange=statechangednew;
xmlHttpnew.open("GET",what,true);
xmlHttpnew.send(null);
return false;
}




function editpass(what,where,f) {
xmlHttpnew=createRequest();
//alert (where+f);
var a = document.forms[f].userPass.value;
var b = document.forms[f].newPass.value;

if(b.length>3){
	what=what+"&userPass="+a+"&newPass="+b;

	what=what+"&sid="+Math.random();
	document.cookie = "location=" + where;
	var display = get_cookie ( "location" ); 
	document.getElementById(display).innerHTML ="<table width=100% height=300px bgcolor=#6b0f0f align=center valign=center><tr><td align=center style='color:white;font-size:20px;'>Processing.......</td></tr></table>";

	xmlHttpnew.onreadystatechange=statechangednew;
	xmlHttpnew.open("GET",what,true);
	xmlHttpnew.send(null);
	return false;
}
else{
alert("please choose a password with at least 4 characters");
}

}


function edittitle(what,where,form,v,hide,IdName) {
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)
{
	
	what = what + "&formvalue=" + tag + "&hide=" + hide;
	//alert (what);
	what=what+"&sid="+Math.random();

	document.cookie = "location=" + where;
	var display = get_cookie ( "location" ); 
	document.getElementById(display).innerHTML ="";

	xmlHttpnew.onreadystatechange=statechangednew;
	xmlHttpnew.open("GET",what,true);
	xmlHttpnew.send(null);

		if (hide != ''){	
		var b = IdName;
		b = b + hide;
		showhide(b);
		}

}

else{
alert("your book title cannot exceed 32 characters");
}

}

function mybookEdit(bookId)
	{
	document.cookie = "bookedit=" + bookId;
	showUser('no_of_divs=1&link1##coverdesignprocess.php?div=divTemp~bookId='+bookId+'~action=bookEdit');
	}

	
function pname(what,where,f) {
xmlHttpnew=createRequest();

var a = document.forms[f].projectname.value;
what = what + "&bookName=" + a;
//alert(what);
what=what+"&sid="+Math.random();

document.cookie = "location=" + where;
var display = get_cookie ( "location" ); 
document.getElementById(display).innerHTML ="<table width=100% height=575px 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>";

xmlHttpnew.onreadystatechange=statechangedapp;
xmlHttpnew.open("GET",what,true);
xmlHttpnew.send(null);

}

function mybook(){
	document.cookie = "bookedit=";
	document.cookie = "bookedit=";
	document.cookie = "bookcover=";
	document.cookie = "bookpages=";

	document.cookie = "what-a=";
	document.cookie = "where-a=";
	document.cookie = "selected-a=";	

	var url="app-2.php?"+Math.random();
	window.location.href = url;	
}