// This is the main repository of JS functions for Clubhouse-Ace.com

// This function set is used when reserving a block of tee times in the admin area
function reserveBlock(){
   if(checkTTblock()) {
	document.submitTTblock.submit();
	}
}
function checkTTblock() {    
	if(checkSelection('A Reservation Name')) { 
	return true;
	}
}

// this is used to register a person for an event
function registerUser(formName) {
	if(checkEventRegister()) {
		var buildString = Form.serialize(formName);
		new Ajax.Updater('calLightbox', 'membersEventDetails.php?'+buildString);
	}
}

// This function set is used when a member enters a Member Number to try and register
function eventRegistration(){
   if(checkEventRegister()) {
	document.submitForm.submit();
	}
}
function checkEventRegister() {    
	if(checkSelection('Number Attending') && checkNumericOnly('Number Attending')) { 
	return true;
	}
}

// Check the Tee Times settings when submitted
function submitTeeTimesSettings(){
   if(checkTTsettings()) {
	document.submitForm.submit();
	}
}

// This function makes sure all of the Tee Times Settings are selected before the form is submitted
function checkTTsettings() {
	if (checkSelection('First Tee Time: HOUR') && checkSelection('First Tee Time: MINUTES') && checkSelection('First Tee Time: AM or PM') && 
		checkSelection('Last Tee Time: HOUR') && checkSelection('Last Tee Time: MINUTES') && checkSelection('Last Tee Time: AM or PM') &&
		checkSelection('Tee Time Frequency') && checkNumericOnly('Tee Time Frequency') && checkSelection('Advanced Booking Time') && checkNumericOnly('Advanced Booking Time')) {
	return true;
	}
}

function uploadFile(){
    pdiv = document.getElementById('processingDiv');
	pdiv.style.display = 'block'; 
	document.uploadForm.submit();
	
	//new Ajax.updater('processingDiv','customers.php?action=getcustomerlist&test=value');
	//$action = $_GET('action');
	//$$action($_GET);
}

// This is a generic function set used to make sure a valid selection is made in a drop down
function validateSelection(){
   if(checkSelectionMade()) {
	document.submitForm.submit();
	}
}
function checkSelectionMade() {
	if(checkSelection('Selection')) {
	return true;
	}
}



// This function set is used when a member enters a Member Number to try and register
function registerNewAccount(){
   if(checkMemberCanRegister()) {
	document.submitForm.submit();
	}
}
function checkMemberCanRegister() {    
	if(checkSelection('Member Number') && checkNumericOnly('Member Number')) { 
	return true;
	}
}



// This function set is used when posting Tournament Scores if the tournament IS NOT flighted
function registerPostTourneyScores(){
   if(checkPostTourneyScore()) {
	document.submitForm.submit();
	}
}
function checkPostTourneyScore() { 
	if(checkSelection('Player Name') || checkSelection('Team Name')) {
			return true;
	}
}





// This function set is used when posting Tournament Scores if the tournament IS flighted
function registerPostTourneyScoresFlighted(){
   if(checkPostTourneyScoreFlighted()) {
	document.submitForm.submit();
	}
}
function checkPostTourneyScoreFlighted() {    
	if(checkSelection('Flight Name') && (checkSelection('Player Name') || checkSelection('Team Name'))) { 
	return true;
	}
}




// This function set is used when a new member is trying to register a username and password
function register(){
   if(checkRegistrationForm()) {
	document.registrationForm.submit();
	}
}
function checkRegistrationForm() {    
	if(checkRequired('Username') && checkPasswordMatch('Password','Verify Password') && checkEmailMatch('Email','Verify Email')) { 
	return true;
	}
}




// This function is used when you just need to make sure a field has a value
function checkRequired(field){

    fieldObj = document.getElementById(field);

    if(fieldObj.value == '') {
          	alert (fieldObj.id+ " is required, please fill it in.");
          return false;
    } else {
           return true;
    }
}


//This function is used to validate passwords. Its check both if the password exists and also that both pw match
function checkPasswordMatch(field1,field2)
{
        if(document.getElementById(field1).value == '') {
            alert ("At least 1 Password field was left blank.");
            return false;
        } else if (document.getElementById(field1).value.length < 6) {
			alert ("Please specify a password with at least 6 characters.");
			return false;
		} else if (document.getElementById(field1).value != document.getElementById(field2).value) {
            alert ("Passwords did not match. Please retype your password.");
            return false;
        } else {
            return true;
        }
}


// This function validates an email naming convetion and also makes sure both email addresses match
function checkEmailMatch(field1,field2)
{
     email1= document.getElementById(field1).value;
     email2 = document.getElementById(field2).value;
     isEmail =email1.indexOf('@');

        if( email1== '') {
            alert ("Email was empty. Please specify your real email address.");
            return false;
        } else if (email1 != email2) {
            alert ("Email did not match. Please retype your email");
            return false;
        } else if (isEmail <1){
           alert ("This is not a valid email address. Please retype your email");
            return false;
        } else {
            return true;
        }
}


// This function makes sure a selection is made for a valid option
function checkSelection(field){
	
	fieldObj= document.getElementById(field);
	
	if(fieldObj.value == ""){
		alert(fieldObj.id+ ' is required before you can continue!');
		//elem.focus();
		return false;
	}else{
		return true;
	}
}


// This function checks to make sure the field only consists of numeric characters.
function checkNumericOnly(field){
	
	fieldObj= document.getElementById(field);
	var numericExpression = /^[0-9]+$/;

	if (fieldObj.value == "") {	
		return true;
	} else if (fieldObj.value.match(numericExpression)) {
		return true;
	} else {
		alert(fieldObj.id+ ' should only include numbers.\nPlease remove all other characters and try again.');
		fieldObj.focus();
		return false;
	}
}

//these functions will show and hide the menu picture
function showMenuPic(menu,course,pic) {
	var vCenter = -250;
	var yScroll = document.body.scrollTop;
	var newHeight = result = yScroll + vCenter;
	var buildString = "?menuID=" +menu +"&menuCourseID=" +course +"&imageName=" +pic;
	new Ajax.Updater('menuPic', 'menuImage.php' +buildString);
		document.getElementById('menuPic').style.display = "block";
		document.getElementById('menuPic').style.marginTop = newHeight;
	return true;
}


function hideMenuPic() {
	document.getElementById('menuPic').style.display = "none";
}

//these functions will show and hide the uploaded ad images
function showUploadPreview(imgName,adPlace) {
	var buildString = "?imgName=" +imgName +"&adPlace=" +adPlace;
	new Ajax.Updater('previewUpload', 'settingsImageAssignPreview.php' +buildString);
}

function shouldIdelete() {
	var approve = confirm("Are you sure you want to delete this?");
	if (approve){
		return true;
	} else {
		return false;
	}
}

function openCalendarLightbox(cal_id) {
	if (window.innerHeight && window.scrollMaxY) {
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else if (document.documentElement && document.documentElement.scrollHeight > document.documentElement.offsetHeight){ // Explorer 6 strict mode
		xScroll = document.documentElement.scrollWidth;
		yScroll = document.documentElement.scrollHeight;
	} else { // Explorer Mac...would also work in Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) { // all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	} 
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else {
		pageHeight = yScroll;
	}
	var realHeight = pageHeight;
	var queryString = "?cal_id=" +cal_id;
	var vCenter = -275;
	var yScroll = document.body.scrollTop;
	var newHeight = result = yScroll + vCenter;
	new Ajax.Updater('calLightbox', 'membersEventDetails.php'+queryString, {
		onComplete:function(){ 
			document.getElementById('lightboxBG').style.height = realHeight;
			document.getElementById('calLightbox').style.marginTop = newHeight;
			document.getElementById('lightboxBG').style.display = "block";
			document.getElementById('calLightbox').style.display = "block"; } });
}

// this closes the lightbox
function closeCalLightbox() {
	document.getElementById('calLightbox').style.display = "none";
	document.getElementById('lightboxBG').style.display = "none";
	var queryString = "?cal_id=";
	new Ajax.Updater('calLightbox', 'membersEventDetails.php'+queryString );
}

function openLeagueLightbox(cli_id) {
	if (window.innerHeight && window.scrollMaxY) {
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else if (document.documentElement && document.documentElement.scrollHeight > document.documentElement.offsetHeight){ // Explorer 6 strict mode
		xScroll = document.documentElement.scrollWidth;
		yScroll = document.documentElement.scrollHeight;
	} else { // Explorer Mac...would also work in Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) { // all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	} 
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else {
		pageHeight = yScroll;
	}
	var realHeight = pageHeight;
	var queryString = "?cli_id=" +cli_id;
	var vCenter = -275;
	var yScroll = document.body.scrollTop;
	var newHeight = result = yScroll + vCenter;
	new Ajax.Updater('calLightbox', 'membersEventDetailsLeague.php'+queryString, {
		onComplete:function(){ 
			document.getElementById('lightboxBG').style.height = realHeight;
			document.getElementById('calLightbox').style.marginTop = newHeight;
			document.getElementById('lightboxBG').style.display = "block";
			document.getElementById('calLightbox').style.display = "block"; } });
}


// this closes the lightbox
function closeLeagueCalLightbox() {
	document.getElementById('calLightbox').style.display = "none";
	document.getElementById('lightboxBG').style.display = "none";
	var queryString = "?cli_id=";
	new Ajax.Updater('calLightbox', 'membersEventDetailsLeague.php'+queryString );
}

function shouldIdeleteTeam(id,phpPage,div,name,eventID,teamType) {
	var url = phpPage
	var pars = "removeItem=yes&teamID=" + id +"&tName=" + name +"&eventID=" +eventID +"&teamType=" +teamType;
	var approve = confirm("Are you sure you want to delete this?");
	if (approve){
		var myAjax = new Ajax.Updater(div,url, {method:'post',parameters:pars});
		return true;
	} else {
		return false;
	}
}

function shouldIdeleteResult(id,phpPage,div,name,eventID,style,flighted,rounds) {
	var url = phpPage
	var pars = "removeItem=yes&resultID=" + id +"&rName=" + name +"&eventID=" +eventID +"&tStyle=" +style +"&e_flight=" +flighted +"&e_rounds=" +rounds;
	var approve = confirm("Are you sure you want to delete this?");
	if (approve){
		var myAjax = new Ajax.Updater(div,url, {method:'post',parameters:pars});
		return true;
	} else {
		return false;
	}
}

function shouldIdeleteReservation(id,phpPage,div,eventID,member) {
	var url = phpPage
	var pars = "cancelRes=yes&resID=" + id +"&eventID=" +eventID +"&memID=" +member;
	var approve = confirm("Are you sure you want to cancel this reservation?");
	if (approve){
		var myAjax = new Ajax.Updater(div,url, {method:'post',parameters:pars});
		return true;
	} else {
		return false;
	}
}
