///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//	CoPilot Creative Front End CMS AJAX Call Functions 
//	Copy Right of Propel Studios, LLC.
//	Developed by Thomas Eisenbeis
//	
//	
//	File Name = ajax_framework.js
// 	This will hold all neccessary functions for calls to actions within site that will allow updates without refresh
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/* ---------------------------- */
/* XMLHTTPRequest Enable */
/* ---------------------------- */
function createObject() {
	
	var request_type;
	var browser = navigator.appName;
	
	if(browser == "Microsoft Internet Explorer"){
		
		request_type = new ActiveXObject("Microsoft.XMLHTTP");
		
	}else{
		
		request_type = new XMLHttpRequest();
		
	}
	
	return request_type;
	
}

var http = createObject();

/* -------------------------- */
/* LOGIN                      */
/* -------------------------- */
/* Required: var nocache is a random number to add to request. This value solve an Internet Explorer cache issue */

var nocache = 0;

function login() {
	
	// Optional: Show a waiting message in the layer with ID ajax_response
	document.getElementById('loginError').style.display = "block"
	document.getElementById('loginError').innerHTML = 'Loading...<br><img src="/admin/images/loader.gif" width="150" height="20" align="center" border="0" /><br>'
	
	// Required: verify that all fileds is not empty. Use encodeURI() to solve some issues about character encoding.
	var username = encodeURI(document.getElementById('username').value);
	var password = encodeURI(document.getElementById('password').value);
		
	// Set te random number to add to URL request
	nocache = Math.random();
	
	// Pass the login variables like URL variable
	http.open('GET', '/admin/process_page.php?username='+username+'&password='+password+'&nocache='+nocache+'&mode=login', true);
	http.onreadystatechange = loginReply;
	http.send(null);
	
}

/* -------------------------- */
/* LOGIN REPLY                */
/* -------------------------- */
/* This will display answer from the call to log in.  Either wrong credentials or a successfull login */

function loginReply() {
		
	if(http.readyState == 4){
		
		var response = http.responseText;
				
		if(response == 0){
			
			// FAILED
			document.getElementById('loginError').innerHTML = 'Login failed! Verify username and password';
		
		} else {
			
			// SUCCESS
			closelogin();
		
		}
	
	}
	
}

/* -------------------------- */
/* GET PASSWORD               */
/* -------------------------- */
/* Function calls a retrieve password request */

function getPassword() {
	
	// Optional: Show a waiting message in the layer with ID ajax_response
	document.getElementById('loginError').style.display = "block"
	document.getElementById('loginError').innerHTML = 'Loading...<br><img src="/admin/images/loader.gif" width="150" height="20" align="center" border="0" /><br>'
	
	// Required: verify that all fileds is not empty. Use encodeURI() to solve some issues about character encoding.
	var email = encodeURI(document.getElementById('email').value);
	
	// Set te random number to add to URL request
	nocache = Math.random();
	
	// Pass the login variables like URL variable
	http.open('GET', '/admin/process_page.php?email='+email+'&nocache='+nocache+'&mode=forgotPassword', true);
	http.onreadystatechange = passwordReply;
	http.send(null);
	
}

/* -------------------------- */
/* PASSWORD REPLY             */
/* -------------------------- */
/* Function will display answer for Password request.  Either no email address found or emaill will be sent to recipient */

function passwordReply() {
		
	if(http.readyState == 4){
		
		var response = http.responseText;
		
		
		if(response == 0){
						
			// FAILED
			document.getElementById('loginError').innerHTML = 'Email provided does not match our records.  Please try again.';
		
		} else {
			
			// SUCCESS
			document.getElementById('loginError').innerHTML = 'Email has been sent with your new password.';
		
		}
	
	}
	
}

/* -------------------------- */
/* MERCHANT LOGIN             */
/* -------------------------- */
/* Required: var nocache is a random number to add to request. This value solve an Internet Explorer cache issue */

function merchantLogin() {
	
	// Optional: Show a waiting message in the layer with ID ajax_response
	document.getElementById('loginError').style.display = "block"
	document.getElementById('loginError').innerHTML = 'Loading...<br><img src="/admin/images/loader.gif" width="150" height="20" align="center" border="0" /><br>'
	
	// Required: verify that all fileds is not empty. Use encodeURI() to solve some issues about character encoding.
	var username = encodeURI(document.getElementById('username').value);
	var password = encodeURI(document.getElementById('password').value);
	
	// Set te random number to add to URL request
	nocache = Math.random();
	
	// Pass the login variables like URL variable
	http.open('GET', '/admin/process_page.php?username='+username+'&password='+password+'&nocache='+nocache+'&mode=merchantLogin', true);
	http.onreadystatechange = merchantLoginReply;
	http.send(null);
	
}

/* -------------------------- */
/* LOGIN REPLY                */
/* -------------------------- */
/* This will display answer from the call to log in.  Either wrong credentials or a successfull login */

function merchantLoginReply() {
		
	if(http.readyState == 4){
		
		var response = http.responseText;
				
		if(response == 0){
			
			// FAILED
			document.getElementById('loginError').innerHTML = 'Login failed! Verify username and password';
		
		} else if (response == 3) {
			
			document.getElementById('loginError').innerHTML = 'Your membership has expired.<br>Please contact us to renew.';
			
		} else {
			
			// SUCCESS
			merchantCloselogin();
		
		}
	
	}
	
}

/* -------------------------- */
/* GET PASSWORD               */
/* -------------------------- */
/* Function calls a retrieve password request */

function merchantGetPassword() {
	
	// Optional: Show a waiting message in the layer with ID ajax_response
	document.getElementById('loginError').style.display = "block"
	document.getElementById('loginError').innerHTML = 'Loading...<br><img src="/admin/images/loader.gif" width="150" height="20" align="center" border="0" /><br>'
	
	// Required: verify that all fileds is not empty. Use encodeURI() to solve some issues about character encoding.
	var email = encodeURI(document.getElementById('email').value);
	
	// Set te random number to add to URL request
	nocache = Math.random();
	
	// Pass the login variables like URL variable
	http.open('GET', '/admin/process_page.php?email='+email+'&nocache='+nocache+'&mode=merchantForgotPassword', true);
	http.onreadystatechange = merchantPasswordReply;
	http.send(null);
	
}

/* -------------------------- */
/* PASSWORD REPLY             */
/* -------------------------- */
/* Function will display answer for Password request.  Either no email address found or emaill will be sent to recipient */

function merchantPasswordReply() {
		
	if(http.readyState == 4){
		
		var response = http.responseText;
		
		if(response == 3){
						
			// FAILED
			document.getElementById('loginError').innerHTML = 'Please enter an email address.';
		
		} else if(response == 0){
						
			// FAILED
			document.getElementById('loginError').innerHTML = 'Email provided does not match our records.  Please try again.';
		
		} else {
			
			// SUCCESS
			document.getElementById('loginError').innerHTML = 'Email has been sent with your new password.';
		
		}
	
	}
	
}

/* -------------------------- */
/* SHOW ARTICLE               */
/* -------------------------- */
/* Function will request the article and replace the current content with new content without refresh */

function showArticle(id) {
	
	// Optional: Show a waiting message in the layer with ID ajax_response
	document.getElementById('loading').style.display = "block";
	document.getElementById('loading').innerHTML = 'Loading...<br><img src="/admin/images/loader_light_blue.gif" width="128" height="15" align="center" border="0" /><br>'
	
	// Set te random number to add to URL request
	nocache = Math.random();
	// Pass the variables like URL variable
	http.open('get', '/admin/process_page.php?id='+id+'&nocache='+nocache+'&mode=articleCall');
	http.onreadystatechange = Article;
	http.send(null);
	
}

/* -------------------------- */
/* ARTICLE REPLY              */
/* -------------------------- */
/* Display the response from request of article call. Either there was an error or it will show the new article on page. */

function Article() {
		
	if(http.readyState == 4){
		
		var response = http.responseText;
		
		if(response == 0){
			
			// FAILED
			document.getElementById('loading').innerHTML = 'There was an error. Please try again';
		
		} else {
			
			// SUCCESS
			document.getElementById('article').innerHTML = response;
			document.getElementById('loading').style.display = "none";
		
		}
	
	}
	
}

/* -------------------------- */
/* PROCESS CONTENT            */
/* -------------------------- */
/* Function calls request for updating content for CMS Manager. This was required to eliminate nasty close and refresh from pop up window. */

function processContent() {
	
	// Optional: Show a waiting message in the layer with ID ajax_response
	document.getElementById('loading').innerHTML = 'Loading...<br><img src="/admin/images/loader.gif" width="150" height="20" align="center" border="0" /><br>'
	
	// Required: verify that all fileds is not empty. Use encodeURI() to solve some issues about character encoding.
	var title = encodeURI(document.getElementById('title').value);
	var blurb = encodeURI(document.getElementById('blurb').value);
	var id = encodeURI(document.getElementById('id').value);
	var mode = encodeURI(document.getElementById('mode').value);
	var subNav = encodeURI(document.getElementById('subNav').value);
	var navId = encodeURI(document.getElementById('navId').value);
	var subtitle = encodeURI(document.getElementById('subtitle').value);
	
	//Get updated content from WYSIWYG.  Needed for AJAX call
	//var content = encodeURI(document.getElementById('content').value);
	
	var content = tinyMCE.get('content');
	content = escape(content.getContent());
	content = content.replace("+", "%2B");
	content = content.replace("/", "%2F");

	// Set te random number to add to URL request.  This is for the string not to get cached
	nocache = Math.random();
	fakecache = Math.random();
	/*
	http.open('get', '/admin/process_page.php?content=' + escape(content) + '&id=' + id + '&mode=' + mode + '&nocache=' + nocache + '&link=' + linkPage + '&title=' + title);
	http.onreadystatechange = contentReply;
	http.send(null);
	*/
	var queryString = "?fakecache=" + fakecache + "&nocache=" + nocache + "&content=" + content + "&id=" + id + "&subtitle=" + subtitle + "&mode=" + mode + "&subNav=" + subNav + "&navId=" + navId + "&title=" + title + "&blurb=" + blurb;
	http.open('POST', '/admin/process_page.php', true);
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http.setRequestHeader("Content-length", queryString.length);
	http.setRequestHeader("Connection", "close");
	http.send(queryString);
	http.onreadystatechange = contentReply;
		
}

/* -------------------------- */
/* PROCESS CONTENT RESPONSE   */
/* -------------------------- */
/* Displays response from processing content.  Either an error is thrown or it will display a success and close window. */
function contentReply() {
		
	if(http.readyState == 4){
		
		var response = http.responseText;
		
		if(response == 0){
			
			// FAILED
			document.getElementById('error').style.display = "block"
			document.getElementById('error').innerHTML = 'There was an error. Please try again';
			document.getElementById('loading').style.display = "none";
		
		} else {
			
			// SUCCESS
			closeWin();
		
		}
	
	}
	
}

/* -------------------------- */
/* PROCESS SIGNUP             */
/* -------------------------- */
/* Function processes mailing list signups */

function processSignup() {
	
	// Optional: Show a waiting message in the layer with ID ajax_response
	
	// Required: verify that all fileds is not empty. Use encodeURI() to solve some issues about character encoding.
	var email = encodeURI(document.getElementById('email').value);
	
	// Set te random number to add to URL request
	nocache = Math.random();
	
	// Pass the login variables like URL variable
	http.open('GET', '/admin/process_page.php?email='+email+'&nocache='+nocache+'&mode=mailSignup', true);
	http.onreadystatechange = passwordReply;
	http.send(null);
	
}

/* -------------------------- */
/* PASSWORD REPLY             */
/* -------------------------- */
/* Function will display answer for Password request.  Either no email address found or emaill will be sent to recipient */

function passwordReply() {
		
	if(http.readyState == 4){
		
		var response = http.responseText;
		
		if(response == 0){
						
			// FAILED
			document.getElementById('mailingError').innerHTML = 'There was an error. Please try again.';
		
		} else if (response == 2) {
						
			// FAILED
			document.getElementById('mailingError').innerHTML = 'Email address already exists in our database.  Please try again.';
			
		} else {
			
			// SUCCESS
			document.getElementById('mailingForm').innerHTML = 'Thank you for signing up to our mailing list.';
		
		}
	
	}
	
	
}
