/*Admin confirmation */
function confirmation_alert(editURL,displayText){	
if(confirm(displayText)==true){
window.location=editURL;
}else{return false;}}


//instance switcher
$(document).ready(function(){
	$(".but-but").click(function(){

		//buttons = on || off
		//div     = active || hidden
		
		//find the content div that is on and fade it out, reset its class
		$('div.panel').find('div').filter('[class=active]').hide().removeClass("active").addClass("hidden");
		
		//fade in the new content and toggle class from hidden to on	
		$("div#panel_"+$(this).attr('id')).show().removeClass("hidden").addClass("active");

		//find whatever the on tab was and swap it to off
		$('ul.tabs').find('a').filter('[class=but-but on ]').removeClass("on").addClass("off");					
		
		//make the clicked button on
		$(this).removeClass("off").addClass("on");
		return false;
	});
});


//right side sub nav sliding
$(document).ready(function(){
	$(".parent").click(function(){
		$("div#"+$(this).attr('id')+"Child").slideToggle('fast');
		if ($(this).hasClass('off')){
			$(this).removeClass("off").addClass("on");	
		} else if ($(this).hasClass('on')){
			$(this).removeClass("on").addClass("off");	
		}		
	});								
});


//contact form validation
$(document).ready(function(){
	$(".submit-contact").click(function(){		
		var errors = 0;
		var cssObj = {"border-color" : "#ff0000","border-style" : "solid","border-width" : "1px"};
			
		var n = $("#n").val();
		var e = $("#e").val();
		var m = $("#m").val();
		var s = $("#s").val();		
		
		if (n == "" || n == " " || n == null){
			$("div#nError").show();
			$("#n").css(cssObj);
			errors++;
		} 
		if (e == "" || e == " " || e == null){
			$("div#eError").show();
			$("#e").css(cssObj);
			errors++;
		}		
		if (m == "" || m == " " || m == null){
			$("div#mError").show();
			$("#m").css(cssObj);
			errors++;
		}
		if (s != "1979"){
			$("div#sError").show();
			$("#s").css(cssObj);
			errors++;
		}
		if (errors == 0){			
			jQuery.ajax({
				type: "POST",
				url: "lib/emailer.php",
				data: "n="+n+"&e="+e+"&m="+m+";",
				beforeSend: function(){jQuery.facebox('<h3><img alt="bin" style="margin-bottom:-20px;" src="lib/facebox/icon_working-button.gif" />Sending...</h3>');},
				error:function(msg){ 
					jQuery.facebox('<h3><img alt="err" class="generic-icon-shift" src="css/images/icon-error-2.png" />Communication error</h3>Oops, our email system has fallen on it\s face!<br/>Please use <a href="mailto:chris@elearning.ac.nz">chris@elearning.ac.nz</a> instead.');
				},
				success: function(result){
					if (result == 200){
						jQuery.facebox('Your email has been sent!<br/>We will get back to you shortly');
						$("#n").val('');
						$("#e").val('');
						$("#m").val('');
						$("#s").val('');
					} else {
						jQuery.facebox('<h3><img alt="err" class="generic-icon-shift" src="css/images/icon-error-2.png" />Email error</h3>Oops, your email was undeliverable');
					}
				}
			});
		}
		return false;
	});
}); 


//reset contact form
$(document).ready(function(){	
	var cssObj = {"border-color" : "#dbdbdb","border-style" : "solid","border-width" : "1px"};
	
	$("#n").focus(function(){
		$("div#nError").hide();
		$(this).css(cssObj);
	});
	
	$("#e").focus(function(){
		$("div#eError").hide();
		$(this).css(cssObj);
	});
	
	$("#m").focus(function(){
		$("div#mError").hide();
		$(this).css(cssObj);
	});	
	
	$("#s").focus(function(){
		$("div#sError").hide();
		$(this).css(cssObj);
	});		
});


//spam link - contact page
$(document).ready(function(){
	$("#spam").click(function(){
		jQuery.facebox('<h3><img alt="bin" class="generic-icon-shift" src="css/images/icon-bin-full.png" />Stop the spam!</h3>Please type the number you see above this box to help us to prevent spam');
	});
});


//login form
$(document).ready(function(){
	$(".login-submit").click(function(){

		var errors = 0;
		var cssObj = {"border-color" : "#ff0000","border-style" : "solid","border-width" : "1px"};
			
		var un = $("#un").val();
		var pa = $("#pa").val();
		
		if (un == "" || un == " " || un == null){
			$("div#uError").show();
			$("#un").css(cssObj);
			errors++;
		} 
		if (pa == "" || pa == " " || pa == null){
			$("div#pError").show();
			$("#pa").css(cssObj);
			errors++;
		}
		if (errors == 0){
			jQuery.ajax({
				type: "POST",
				url: "lib/xca.php",
				data: "un="+un+"&pa="+pa,
				beforeSend: function(){jQuery.facebox('<h3><img alt="bin" style="margin-bottom:-20px;" src="lib/facebox/icon_working-button.gif" />Sending...</h3>');},
				error:function(msg){
					jQuery.facebox('<h3><img alt="err" class="generic-icon-shift" src="css/images/icon-error-2.png" />Communication error</h3>The system was unable to locate the login processor<br/>.');
				},
				success: function(result){
					if (!result || result == 1){
						jQuery.facebox('<h3><img alt="err" class="generic-icon-shift" src="css/images/icon-error-2.png" />Login error</h3>Unable to log in. Please check your User name and Password are correct');
					} else if (result == 200) {
						jQuery.facebox('<h3><img alt="bung" class="generic-icon-shift" src="css/images/lock.png" />Login successful</h3>Welcome back Administrator.<br/><br/>Remember that whatever changes you make and commit to whilst logged in are irreversible so take care. <br/><br/>As always have fun editing <img alt=":)" style="margin-bottom:-10px; padding-right:10px;" src="css/images/smiley.png" />');
						$("#un").val('');
						$("#pa").val('');
					}
				}
			});
		}
		return false;
	});
});


//reset login form
$(document).ready(function(){	
	var cssObj = {"border-color" : "#dbdbdb","border-style" : "solid","border-width" : "1px"};
	
	$("#un").focus(function(){
		$(this).css(cssObj);
	});
	
	$("#pa").focus(function(){
		$(this).css(cssObj);
	});
});


//Hide the billing address - join form
$(document).ready(function(){
	$("#same").click(function(){
		$("#orgBilling").slideToggle('slow');
	});
});


//join form validation
$(document).ready(function(){
	$(".submit-join").click(function(){
		if ($("#tc").attr('checked') == false){
			jQuery.facebox('<h3><img class="generic-icon-shift" src="css/images/icon-error-2.png" />Form Error</h3>Please read the terms and conditions and click the checkbox');	
		} else {
			var errors = 0;
			var dArr = '';
			var cssObj = {"border-color" : "#ff0000","border-style" : "solid","border-width" : "1px"};		
			
			var orgname   = $("#orgname").val();
			var orgstreet = $("#orgstreet").val();
			var orgtown   = $("#orgtown").val();		
			var managername     = $("#managername").val();
			var managerlastname = $("#managerlastname").val();
			var managerphone    = $("#managerphone").val();
			var manageremail    = $("#manageremail").val();
			if (is_empty(orgname)){
				$("div#orgnameError").show();
				$("#orgname").css(cssObj);
				errors++;
			} 
			if (is_empty(orgstreet)){
				$("div#orgstreetError").show();
				$("#orgstreet").css(cssObj);
				errors++;
			} 
			if (is_empty(orgtown)){
				$("div#orgtownError").show();
				$("#orgtown").css(cssObj);
				errors++;
			} 
			if (is_empty(managername)){
				$("div#managernameError").show();
				$("#managername").css(cssObj);
				errors++;
			}
			if (is_empty(managerlastname)){
				$("div#managerlastnameError").show();
				$("#managerlastname").css(cssObj);
				errors++;
			}
			if (is_empty(managerphone)){
				$("div#managerphoneError").show();
				$("#managerphone").css(cssObj);
				errors++;
			}
			if (is_empty(manageremail)){
				$("div#manageremailError").show();
				$("#manageremail").css(cssObj);
				errors++;
			}
			if (errors == 0){
				dArr = "&orgname="+orgname+"&orgstreet="+orgstreet+"&orgtown="+orgtown+"&managername="+managername+
					   "&managerlastname="+managerlastname+"&managerphone="+managerphone+"&manageremail="+manageremail+
					   "&misc="+$("#misc").val()+"&orgroad="+$("#orgroad").val()+"&orgpostcode="+$("#orgpostcode").val()+
					   "&managermobile="+$("#managermobile").val()+"&nominatedname="+$("#nominatedname").val()+
					   "&nominatedlastname="+$("#nominatedlastname").val()+"&nominatedphone="+$("#nominatedphone").val()+
					   "&nominatedmobile="+$("#nominatedmobile").val()+
					   "&nominatedemail="+$("#nominatedemail").val()+"&hosting="+$("input[name=plan]:checked").val()+
					   "&theme="+$("input[name=theme]:checked").val()+"&trainingadmin="+$("input[id=admin]:checked").val()+
					   "&trainingintro="+$("input[id=intro]:checked").val()+"&trainingdesigner="+$("input[id=designer]:checked").val()+
					   "&choiceone="+$("#choiceone").val()+"&choicetwo="+$("#choicetwo").val()+
					   "&payment="+$("input[name=payment]:checked").val()+"&comments="+$("#comments").val();
	
				if ($("#same").is(":checked")){
					dArr += "&billstreet="+$("#billstreet").val()+"&billpobox="+$("#billpobox").val()+"&billroad="+$("#billroad").val()+
							"&billtown="+$("#billtown").val()+"&billpostcode="+$("#billpostcode").val();
				}
				
				jQuery.ajax({
					type: "POST",
					url: "lib/emailer-join.php",
					data: dArr+";",
					beforeSend: function(){jQuery.facebox('<h3><img alt="bin" style="margin-bottom:-20px;" src="lib/facebox/icon_working-button.gif" />Sending...</h3>');},
					error:function(msg){
						jQuery.facebox('<h3><img class="generic-icon-shift" src="css/images/icon-error-2.png" />Communication Error</h3>Oops, our email system has fallen on it\s face!<br/>Please use <a href="mailto:chris@elearning.ac.nz">chris@elearning.ac.nz</a> instead.');
					},
					success: function(result){
						if (!result){
							jQuery.facebox('<h3><img class="generic-icon-shift" src="css/images/icon-error-2.png" />Email error</h3>Oops, your email was undeliverable');
						} else {
							jQuery.facebox('<h3><img class="generic-icon-shift" src="css/images/smiley.png" />Your email has been sent!</h3><br/>We will get back to you shortly');
						}
					}
				});
			} else {
				jQuery.facebox('<h3><img class="generic-icon-shift" src="css/images/icon-error-2.png" />Form Error</h3>Oops, some required fields have not been completed.<br/> Please complete the marked fields.');
			}
		}
	});
});


//reset join form
$(document).ready(function(){	
	var cssObj = {"border-color" : "#ddd","border-style" : "solid","border-width" : "1px"};
	
	$("#orgname").focus(function(){
		$("div#orgnameError").hide();
		$(this).css(cssObj);
	});
	
	$("#orgstreet").focus(function(){
		$("div#orgstreetError").hide();
		$(this).css(cssObj);
	});
	
	$("#orgtown").focus(function(){
		$("div#orgtownError").hide();
		$(this).css(cssObj);
	});	
	
	$("#managername").focus(function(){
		$("div#managernameError").hide();
		$(this).css(cssObj);
	});
	
	$("#managerlastname").focus(function(){
		$("div#managerlastnameError").hide();
		$(this).css(cssObj);
	});	
	
	$("#managerphone").focus(function(){
		$("div#managerphoneError").hide();
		$(this).css(cssObj);
	});	
	
	$("#manageremail").focus(function(){
		$("div#manageremailError").hide();
		$(this).css(cssObj);
	});		
});


//is a value empty
function is_empty(el){	
	if (jQuery.trim(el).length <= 0){
		return true;	
	} else {
		return false;
	}		
}


//saving the checkbox value to show switcher tabs or not
$(document).ready(function(){
	$("#showtabs").click(function(){		
		jQuery.ajax({
			type: "POST",
			url: "lib/saveswitcher.php",
			data: 'showtabs='+$("input[id=showtabs]:checked").val(),
			  
			beforeSend: function(){jQuery.facebox('<h3><img alt="bin" style="margin-bottom:-20px;" src="lib/facebox/icon_working-button.gif" />Sending...</h3>');},
			error:function(msg){
				jQuery.facebox('<h3><img class="generic-icon-shift" src="css/images/icon-error-2.png" />Communication Error</h3>Oops, there was a connection error with the server.');
			},
			success: function(result){
				if (!result){
					jQuery.facebox('<h3><img class="generic-icon-shift" src="css/images/icon-error-2.png" />Save Error</h3>Oops, unable to save the checkbox value');
				} else if(result==200) {				
					jQuery.facebox('<h3><img class="generic-icon-shift" src="css/images/smiley.png" />Setting Saved</h3>Switcher settings were saved');
				} else {				
					jQuery.facebox('<h3><img class="generic-icon-shift" src="css/images/icon-error-2.png" />Save Error</h3>Oops, unable to save the checkbox value');
				}
			}
		});				
	});
});


//Fades in and the out the large images on the instance page
$(document).ready(function(){
	$(".i-image").click(function(){				
		var id = $(this).attr('id');		
		$('#mainImage').fadeOut('slow',function(){$(this).attr('src', id).fadeIn('slow');});
	});
});


//slides up and down the 'click me' text ont eh sub images - instances page
$(document).ready(function(){
	$(".i-image").hover(function(){$("#hover"+$(this).attr('rel')).slideDown('fast');},function(){$("#hover"+$(this).attr('rel')).slideUp('fast');})
});

//show 'click here' message on main image - instances page
$(document).ready(function(){
	$("a#mainImageLink").hover(function(){$(".main-click-me").slideDown('fast');},function(){$(".main-click-me").slideUp('fast');});
});