///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//	CoPilot Creative Front End CMS Admin 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
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


/* -------------------------- */
/* REFRESH WINDOW             */
/* -------------------------- */
/* Function called to refresh window on current page */
function refreshWin() {

  	window.location.href = window.location.href;
	
}

/* -------------------------- */
/* DELETE ARTICLE             */
/* -------------------------- */
/* Function to call confirmation of deletion and call request to processing page */
function deleteArticle (id) {

	var answer = confirm('Are you sure you would like to delete this entry?  This action cannot be reversed.');
	
	if (answer) {

	  window.location = '/admin/process_page.php?mode=delete&id='+id+'';
	  
	}

}

/* -------------------------- */
/* DELETE ARTICLE             */
/* -------------------------- */
/* Function to call confirmation of deletion and call request to processing page */
function deleteMerchantArticle (id) {

	var answer = confirm('Are you sure you would like to delete this entry?  This action cannot be reversed.');
	
	if (answer) {

	  window.location = '/admin/process_page.php?mode=merchantDelete&id='+id+'';
	  
	}

}

/* -------------------------- */
/* SHOW WINDOW FOR USERS      */
/* -------------------------- */
/* Function calls window to open to manage users */
function showWinUsers () {

	myLightWindow.activateWindow({
		href: '/admin/manageAccount.php',
		width: 675,
		height: 700
	});

}

/* -------------------------- */
/* SHOW WINDOW                */
/* -------------------------- */
/* Function calls window to open to manage content */
function showWinAdd (id,type) {

	myLightWindow.activateWindow({
		href: '/admin/modify.php?subNav='+id+'&type='+type+'',
		width: 675,
		height: 700
	});

}

/* -------------------------- */
/* SHOW WINDOW                */
/* -------------------------- */
/* Function calls window to open to manage content */
function showWinMerchantAdd (id,type) {

	myLightWindow.activateWindow({
		href: '/admin/modifyMerchant.php?subNav='+id+'&type='+type+'',
		width: 675,
		height: 700
	});

}

/* -------------------------- */
/* SHOW WINDOW                */
/* -------------------------- */
/* Function calls window to open to manage content */
function showWin (id,type) {

	myLightWindow.activateWindow({
		href: '/admin/modify.php?id='+id+'&type='+type+'',
		width: 675,
		height: 700
	});

}

/* -------------------------- */
/* SHOW WINDOW                */
/* -------------------------- */
/* Function calls window to open to manage content */
function showWinMerchant (id,type) {

	myLightWindow.activateWindow({
		href: '/admin/modifyMerchant.php?id='+id+'&type='+type+'',
		width: 675,
		height: 700
	});

}

/* -------------------------- */
/* SHOW WINDOW                */
/* -------------------------- */
/* Function calls window to open to manage content */
function showWinMerch (id,type) {

	myLightWindow.activateWindow({
		href: '/admin/storeListings.php?id='+id+'&type='+type+'',
		width: 675,
		height: 700
	});

}

/* -------------------------- */
/* SHOW WINDOW                */
/* -------------------------- */
/* Function calls window to open to manage content */
function showWinEdit (page,id,type) {

	myLightWindow.activateWindow({
		href: '/admin/'+page+'.php?id='+id+'&type='+type+'',
		width: 675,
		height: 700
	});

}

/* -------------------------- */
/* SHOW WINDOW  STORE         */
/* -------------------------- */
/* Function calls window to open to manage content */
function showWinStore (id) {

	myLightWindow.activateWindow({
		href: '/admin/storeListings.php',
		width: 675,
		height: 700
	});

}

/* -------------------------- */
/* SHOW WINDOW                */
/* -------------------------- */
/* Function calls window to open to manage content */
function showWinCoupon (id) {

	myLightWindow.activateWindow({
		href: '/admin/couponList.php',
		width: 675,
		height: 700
	});

}

/* -------------------------- */
/* DELETE ARTICLE             */
/* -------------------------- */
/* Function to call confirmation of deletion and call request to processing page */
function deleteStore (id) {

	var answer = confirm('Are you sure you would like to delete this entry?  This action cannot be reversed.');
	
	if (answer) {

	  window.location = '/admin/process_page.php?mode=deleteStore&id='+id+'';
	  
	}

}

/* -------------------------- */
/* SHOW WINDOW FOR IMAGE      */
/* -------------------------- */
/* Function calls window to open to manage images */
function showWinImage (id) {

	myLightWindow.activateWindow({
		href: '/admin/manageImages.php?id='+id+'',
		width: 675,
		height: 800
	});

}

/* ------------------------------ */
/* SHOW WINDOW FOR GALLERY IMAGES */
/* ------------------------------ */
/* Function calls window to open to manage gallery images */
function showWinGalleryImage () {

	myLightWindow.activateWindow({
		href: '/admin/manageGalleryImages.php',
		width: 675,
		height: 700
	});

}

/* ------------------------------ */
/* CLOSE WINDOW                   */
/* ------------------------------ */
/* Function calls to close pop up window after update */
function closeWin() {
	
	parent.refreshWin();
	parent.myLightWindow.deactivate();
	
}

/* ------------------------------ */
/* VALIDATION OF USER ADD         */
/* ------------------------------ */
/* Function calls validation for users being added */	
function validateUser(form){
	
	
	if(document.forms['edituser'].elements.password2.value != document.forms['edituser'].elements.password.value){
		
		document.getElementById('passError').style.display = "block";
		document.getElementById('passError').innerHTML = 'Your passwords do not match';
		return false;
		
	}
	
	
	if(document.forms['edituser'].elements.username.value == ''){
		
		document.getElementById('userError').style.display = "block";
		document.getElementById('userError').innerHTML = 'Please enter a username';
		return false;
		
	}
	
	
	if(document.forms['edituser'].elements.email.value == ''){
		
		document.getElementById('emailError').style.display = "block";
		document.getElementById('emailError').innerHTML = 'Please enter an email';
		return false;
		
	}
	
	return true;
	
}

/* ------------------------------ */
/* VALIDATION MAILING SIGNUP      */
/* ------------------------------ */
/* Function calls validation for mailing signups */	
function validateSignup(mailingList){
	
	var error = "";

	if(document.forms['mailingList'].elements.email.value == 'email' || document.forms['mailingList'].elements.email.value == ''){
		
		document.getElementById('mailingError').style.display = "block";
		document.getElementById('mailingError').innerHTML = 'Please enter an email address.';
		error += "true";
		
	} else if (!emailCheck(document.forms['mailingList'].elements.email.value)) {
		
		document.getElementById('mailingError').style.display = "block";
		document.getElementById('mailingError').innerHTML = 'Email is invalid.';
		error += "true";
		
	}
	
	if (error){
		
		return false;
		
	}else{
		
		return true;
		
	}
	
}

/* ------------------------------ */
/* CLOSE LOGIN                    */
/* ------------------------------ */
/* Function calls to close pop up window after login success */
function closelogin() {

	parent.refreshWin();
	parent.myLightWindow.deactivate();
	
}

/* ------------------------------ */
/* CLOSE LOGIN                    */
/* ------------------------------ */
/* Function calls to close pop up window after login success */
function merchantCloselogin() {

	parent.window.location = 'http://www.shopoldcoloradocity.com/merchants/';
	parent.myLightWindow.deactivate();
	
}



/* ------------------------------ */
/* EMAIL CHECK                    */
/* ------------------------------ */
/* Function validates email */

function emailCheck (emailStr) {

	var checkTLD=1;
	
	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
	
	var emailPat=/^(.+)@(.+)$/;
	
	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
	
	var validChars="\[^\\s" + specialChars + "\]";
	
	var quotedUser="(\"[^\"]*\")";
	
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	
	var atom=validChars + '+';
	
	var word="(" + atom + "|" + quotedUser + ")";
	
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
	
	var matchArray=emailStr.match(emailPat);
	
	if (matchArray==null) {
	return false;
	}
	var user=matchArray[1];
	var domain=matchArray[2];
	
	for (i=0; i<user.length; i++) {
		if (user.charCodeAt(i)>127) {
			return false;
		}
	}
	for (i=0; i<domain.length; i++) {
		if (domain.charCodeAt(i)>127) {
			return false;
		}
	}
	if (user.match(userPat)==null) {
		return false;
	}
	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null) {
		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
		
			return false;
		   }
		}
		return true;
	}
	var atomPat=new RegExp("^" + atom + "$");
	var domArr=domain.split(".");
	var len=domArr.length;
	for (i=0;i<len;i++) {
		if (domArr[i].search(atomPat)==-1) {
			return false;
	   }
	}
	if (checkTLD && domArr[domArr.length-1].length!=2 && domArr[domArr.length-1].search(knownDomsPat)==-1) {
		return false;
	}
	if (len<2) {
		return false;
	}
return true;
}
