$(document).ready(function() {   
						   
	$("#startNowBox").hide();
	$("#signInBox").hide();
	$("#forgotPasswordForm").hide();
	$("#signUpFormSuccess").hide();
	$("#signInFormSuccess").hide();
	$("#forgotPasswordFormSuccess").hide();
	$("#addNewMonitorBalloon").hide();
	$("#newMonitorFormSuccess").hide();
	$("#editPersonalInfoFormSuccess").hide();
	$("#editEmailInfoForm").hide();
	$("#editEmailInfoFormSuccess").hide();
	$("#addMobileAlertContactFormSuccess").hide();
	$("#addEmailAlertContactFormSuccess").hide();
	$("#addTwitterAlertContactFormSuccess").hide();
	$(".errorStartStopAlertContact").hide();
	$("#addMobileAlertContactForm").hide();
	$("#addEmailAlertContactForm").hide();
	$("#addTwitterAlertContactForm").hide();
	$("#mobileAlertContactValueWrap").hide();
	$("#newMonitorAdvancedSettings").hide();
	
	$(".startNow").click( function() {
		if ($("#startNowBox").is(":hidden")) {
			$("#signInBox").fadeOut("slow");
			$("#startNowBox").fadeIn("slow");
		} else {
			$("#startNowBox").fadeOut("slow");
		}
	});  
	$(".signIn").click(function() {
		if ($("#signInBox").is(":hidden")) {
			$("#startNowBox").fadeOut("slow");
			$("#signInBox").fadeIn("slow");
		} else {
			$("#signInBox").fadeOut("slow");
		}
	}); 
	$("#forgotPasswordText").click(function() {
		$("#signInForm").fadeOut("slow", function(){
			$("#forgotPasswordForm").fadeIn("slow");									
		});
	}); 
	$("#signInText").click(function() {
		$("#forgotPasswordForm").fadeOut("slow", function(){
			$("#signInForm").fadeIn("slow");									
		});
	}); 
	
	
	$(".addNewMonitor").click(function(){
		if ($("#addNewMonitorBalloon").is(":hidden")) {
			$("#addNewMonitorBalloon").fadeIn("slow");
		} else {
			$("#addNewMonitorBalloon").fadeOut("slow");
		}		
	});
	
	$("#newMonitorAdvancedSettingsText").click(function(){
		$("#newMonitorAdvancedSettings").slideToggle("slow", function(){
			if ($("#newMonitorAdvancedSettings").is(":hidden")) {
				$("#newMonitorAdvancedSettingsText").html("show advanced settings");
			} else {
				$("#newMonitorAdvancedSettingsText").html("hide advanced settings");
			}
		});
	});
	
	$(".editMonitor").live("click", function(){
		var editMonitorIDValSplitter 	= (this.id).split("_");
		var editMonitorIDVal 			= editMonitorIDValSplitter[1];	
		if ($("#editMonitorBalloon").is(":visible")) {

		} else {
			$("#editMonitorWrap_" + editMonitorIDVal).load("includes/editMonitor.asp?monitorID=" + editMonitorIDVal).fadeIn("slow");
			$("#editMonitorFormSuccess").hide();
		}
	});
	
	$(".closeEditMonitor").live("click", function(){
		if ($("#editMonitorBalloon").is(":visible")) {
			$("#editMonitorBalloon").fadeOut("slow", function(){
				$("#editMonitorBalloon").empty()
				$("#editMonitorBalloon").remove()
			});
		}
	});

	$(".startStopMonitor").live("click", function(){
		var startStopMonitorIDValSplitter 	= (this.id).split("_");
		var startStopMonitorIDVal 			= startStopMonitorIDValSplitter[1];	
		$.ajax({
			type: "POST",
			url: "includes/dml/monitorDML.asp?action=startStopMonitor&monitorID=" + startStopMonitorIDVal,
			success: function(theResponse){
				$("#startStopMonitorLink_" + startStopMonitorIDVal).empty();
				$("#startStopMonitorLink_" + startStopMonitorIDVal).html(theResponse);
				//alert (theResponseString);
				if ((theResponse).indexOf("play") > 0) { //checking if the response includes keyword: "play"
					$("#statusCSS_" + startStopMonitorIDVal).css({'background-color' : '#000000'});
				} else {
					$("#statusCSS_" + startStopMonitorIDVal).css({'background-color' : '#9c9b9b'});
				}
			}
		});
	});
	
	
	$(".deleteMonitor").live("click", function(){
		var deleteMonitorIDValSplitter 	= (this.id).split("_");
		var deleteMonitorIDVal 			= deleteMonitorIDValSplitter[1];	
		if ($("#deleteMonitorBalloon").is(":visible")) {

		} else {
			$("#deleteMonitorWrap_" + deleteMonitorIDVal).load("includes/deleteMonitor.asp?monitorID=" + deleteMonitorIDVal).fadeIn("slow");
			$("#deleteMonitorFormSuccess").hide();
		}
	});
	
	$(".closeDeleteMonitor").live("click", function(){
		if ($("#deleteMonitorBalloon").is(":visible")) {
			$("#deleteMonitorBalloon").fadeOut("slow", function(){
				$("#deleteMonitorBalloon").empty()
				$("#deleteMonitorBalloon").remove()
			});
		}
	});
	

	$(".refreshMonitor").live("click", function(){
		var refreshMonitorIDValSplitter 	= (this.id).split("_");
		var refreshMonitorIDVal 			= refreshMonitorIDValSplitter[1];	
		if ($("#refreshMonitorBalloon").is(":visible")) {

		} else {
			$("#refreshMonitorWrap_" + refreshMonitorIDVal).load("includes/refreshMonitor.asp?monitorID=" + refreshMonitorIDVal).fadeIn("slow");
			$("#refreshMonitorFormSuccess").hide();
		}
	});
	

	
	
	$("#showHideEditEmailInfoForm").click(function(){
		$("#editEmailInfoForm").slideToggle();	
	});
	
	
	$(".activateAlertContact").live("click", function(){
		var activateAlertContactIDValSplitter 	= (this.id).split("_");
		var activateAlertContactIDVal 			= activateAlertContactIDValSplitter[1];	
		if ($("#activateAlertContactBalloon").is(":visible")) {

		} else {
			$("#alertContactWrap_" + activateAlertContactIDVal).load("includes/activateAlertContact.asp?alertContactID=" + activateAlertContactIDVal).fadeIn("slow");
			$("#activateAlertContactFormSuccess").hide();
		}
	});
	
	$(".closeActivateAlertContact").live("click", function(){
		if ($("#activateAlertContactBalloon").is(":visible")) {
			$("#activateAlertContactBalloon").fadeOut("slow", function(){
				$("#activateAlertContactBalloon").empty()
				$("#activateAlertContactBalloon").remove()
			});
		}
	});
	
	
	
	$(".deleteAlertContact").live("click", function(){
		var deleteAlertContactIDValSplitter 	= (this.id).split("_");
		var deleteAlertContactIDVal 			= deleteAlertContactIDValSplitter[1];	
		if ($("#deleteAlertContactBalloon").is(":visible")) {

		} else {
			$("#deleteAlertContactWrap_" + deleteAlertContactIDVal).load("includes/deleteAlertContact.asp?alertContactID=" + deleteAlertContactIDVal).fadeIn("slow");
			$("#deleteAlertContactFormSuccess").hide();
		}
	});
	
	$(".closeDeleteAlertContact").live("click", function(){
		if ($("#deleteAlertContactBalloon").is(":visible")) {
			$("#deleteAlertContactBalloon").fadeOut("slow", function(){
				$("#deleteAlertContactBalloon").empty()
				$("#deleteAlertContactBalloon").remove()
			});
		}
	});
	
	
	
	$(".startStopAlertContact").live("click", function(){
		var startStopAlertContactIDValSplitter 	= (this.id).split("_");
		var startStopAlertContactIDVal 			= startStopAlertContactIDValSplitter[1];	
		$.ajax({
			type: "POST",
			url: "includes/dml/userDML.asp?action=startStopAlertContact&alertContactID=" + startStopAlertContactIDVal,
			success: function(theResponse){
				if (theResponse == "false") {
					$(".errorStartStopAlertContact").fadeIn("slow", function(){
						setTimeout(function() {
							$(".errorStartStopAlertContact").fadeOut("slow");
						}, 3000);												   
					});
				} else {
					$("#startStopAlertContactLink_" + startStopAlertContactIDVal).empty();
					$("#startStopAlertContactLink_" + startStopAlertContactIDVal).html(theResponse);
				}
			}
		});
	});
	
	
	
	$(".startStopDefaultAlertContact").live("click", function(){	
		$.ajax({
			type: "POST",
			url: "includes/dml/userDML.asp?action=startStopDefaultAlertContact",
			success: function(theResponse){
				if (theResponse == "false") {
					$(".errorStartStopAlertContact").fadeIn("slow", function(){
						setTimeout(function() {
							$(".errorStartStopAlertContact").fadeOut("slow");
						}, 3000);												   
					});
				} else {
					$("#startStopDefaultAlertContactLink").empty();
					$("#startStopDefaultAlertContactLink").html(theResponse);
				}
			}
		});
	});
	

	$("#mobileProviderID").live("change", function(){
 
         var mobileProviderID = $("option:selected", this).val();
		 if (mobileProviderID == '') {
         	$("#mobileAlertContactValueWrap").hide();
		 } else {
			$.ajax({
				type: "POST",
				url: "includes/dml/userDML.asp?action=getMobileProviderDetails&mobileProviderID=" + mobileProviderID,
				success: function(theResponse){
					var mobileProviderDetails 		= (theResponse).split(";");
					if (mobileProviderDetails[0] == '-') {
						var mobileProviderPrefix		= '';
					} else {
						var mobileProviderPrefix		= '&nbsp;&nbsp;' + mobileProviderDetails[0];
					}
					if (mobileProviderDetails[1] == '-') {
						var mobileProviderSuffix		= '';
					} else {
						var mobileProviderSuffix		= mobileProviderDetails[1];
					}
						var mobileProviderNumberLimit	= mobileProviderDetails[2];
					$(".mobileProviderPrefix").html(mobileProviderPrefix);
					$(".mobileProviderSuffix").html(mobileProviderSuffix);
					$(".mobileProviderNumberLimit").html(mobileProviderNumberLimit);


				}
			});
			 
			 $("#mobileAlertContactValueWrap").show();
		 }
		
	})
	


	$("#addEmailAlertContactButton").click(function() {
		if ($("#addEmailAlertContactForm").is(":hidden")) {
			if ($("#addMobileAlertContactForm").is(":visible")) {
				$("#addMobileAlertContactForm").slideUp("slow", function() {
					$("#addEmailAlertContactForm").slideDown("slow");
				}); 
			} else if ($("#addTwitterAlertContactForm").is(":visible")) {
				$("#addTwitterAlertContactForm").slideUp("slow", function() {
					$("#addEmailAlertContactForm").slideDown("slow");
				}); 
			} else {
				$("#addEmailAlertContactForm").slideDown("slow");
			}
		} else {
			$("#addEmailAlertContactForm").slideUp("slow");
		}
	}); 
	

	$("#addMobileAlertContactButton").click(function() {
		if ($("#addMobileAlertContactForm").is(":hidden")) {
			if ($("#addEmailAlertContactForm").is(":visible")) {
				$("#addEmailAlertContactForm").slideUp("slow", function() {
					$("#addMobileAlertContactForm").slideDown("slow");
				}); 
			} else if ($("#addTwitterAlertContactForm").is(":visible")) {
				$("#addTwitterAlertContactForm").slideUp("slow", function() {
					$("#addMobileAlertContactForm").slideDown("slow");
				}); 
			} else {
				$("#addMobileAlertContactForm").slideDown("slow");
			}
		} else {
			$("#addMobileAlertContactForm").slideUp("slow");
		}
	}); 
	

	$("#addTwitterAlertContactButton").click(function() {
		if ($("#addTwitterAlertContactForm").is(":hidden")) {
			if ($("#addEmailAlertContactForm").is(":visible")) {
				$("#addEmailAlertContactForm").slideUp("slow", function() {
					$("#addTwitterAlertContactForm").slideDown("slow");
				}); 
			} else if ($("#addMobileAlertContactForm").is(":visible")) {
				$("#addMobileAlertContactForm").slideUp("slow", function() {
					$("#addTwitterAlertContactForm").slideDown("slow");
				}); 
			} else {
				$("#addTwitterAlertContactForm").slideDown("slow");
			}
		} else {
			$("#addTwitterAlertContactForm").slideUp("slow");
		}
	}); 



	var loader = jQuery('<img src="images/loader.gif" alt="loading..." />')
		.hide()
		.appendTo(".loader");
	jQuery().ajaxStart(function() {
		loader.show();
	}).ajaxStop(function() {
		loader.hide();
	}).ajaxError(function(a, b, e) {
		throw e;
	});

	
	
	
	var signUpV = jQuery("#signUpForm").validate({			  
		submitHandler: function(form) {
			jQuery(form).ajaxSubmit({
				success: function(theResponse) {
					if (theResponse == "true") {
						signUpV.resetForm();			
						$("#signUpForm").hide("slow",  function() {
							$("#signUpFormSuccess").fadeIn("slow");
						});
					} else {
						$(".errorSignUp").html("E-mail is in use. Please login using your e-mail.").show();
					}

				},
				
				error: function(label) {
					alert ("noooo");
					loader.hide();
				}		
			});					
		},

	   errorLabelContainer: ".errorSignUp",
		rules: {
			userFirstLastName: {
				required: true
			},
			userEmail: {
				required: true,
				email: true,
				remote: "includes/dml/userDML.asp?action=checkEmail"
			},
			userPassword: {
				required: true,
				minlength: 6
			},
			userPassword2: {
				required: true,
				equalTo: "#userPassword"
			}
		},
		messages: {
			userFirstLastName: {
				required: "Name is required, "
			},
			userEmail: {
				required: "E-mail is required, ",
				email: "Enter a valid e-mail, ",
				remote: jQuery.format("E-mail is in use, ")
			},
			userPassword: {
				required: "Password required, ",
				minlength: "Password minimum 6 characters, "
			},
			userPassword2: {
				required: "Re-Password required, ",
				equalTo: "Passwords must match"
			}

		}

	});
	
	
	var signInV = jQuery("#signInForm").validate({			  
		submitHandler: function(form) {
			jQuery(form).ajaxSubmit({
				success: function(theResponse) {
					if (theResponse == "true") {
						signInV.resetForm();			
						$("#signInForm").hide("slow",  function() {
							$("#signInFormSuccess").fadeIn("slow",  function() {
								top.location.href = 'myMonitors.asp';																		 
							});
						});
					} else if (theResponse == "userStatusPassive") {
						$(".errorSignIn").html("The account is not activated. Please check your e-mail").show();
					} else if (theResponse == "false") {
						$(".errorSignIn").html("Wrong e-mail and/or password. Please try again.").show();
					}
				},
				
				error: function(label) {
					loader.hide();
				}		
			});					
		},

	   errorLabelContainer: ".errorSignIn",
		rules: {
			signInEmail: {
				required: true,
				email: true
			},
			signInPassword: {
				required: true,
				minlength: 6
			}
		},
		messages: {
			signInEmail: {
				required: "E-mail is required, ",
				email: "Enter a valid e-mail, "
			},
			signInPassword: {
				required: "Password required, ",
				minlength: "Password minimum 6 characters, "
			}

		}

	});
	
	
	
	var forgotPasswordV = jQuery("#forgotPasswordForm").validate({			  
		submitHandler: function(form) {
			jQuery(form).ajaxSubmit({
				success: function(theResponse) {
					if (theResponse == "true") {
						forgotPasswordV.resetForm();			
						$("#forgotPasswordForm").hide("slow",  function() {
							$("#forgotPasswordFormSuccess").fadeIn("slow",  function() {																		 
							});
						});
					} else {
						$(".errorForgotPassword").html("E-mail doesn't exist, please make sure you mention the right address.").show();
					}
				},
				
				error: function(label) {
					loader.hide();
				}		
			});					
		},

	   errorLabelContainer: ".errorForgotPassword",
		rules: {
			forgotPasswordEmail: {
				required: true,
				email: true
			}
		},
		messages: {
			forgotPasswordEmail: {
				required: "E-mail is required.",
				email: "Enter a valid e-mail."
			}

		}

	});
	
	
	
	var newMonitorV = jQuery("#newMonitorForm").validate({			  
		submitHandler: function(form) {
			jQuery(form).ajaxSubmit({
				success: function(theResponse) {
					if (theResponse == "true") {
						newMonitorV.resetForm();
						$("#addNewMonitorHideAfterSuccess").slideUp("slow");
						$("#addNewMonitorBalloonCenter").slideUp("slow",  function() {
							$("#newMonitorFormSuccess").fadeIn("slow",  function() {
								top.location.href = 'myMonitors.asp';																		
							});
						});
					} else {
						$(".errorNewMonitor").html("This monitor is already in the list.").show();
					}
				},
				
				error: function(label) {
					loader.hide();
				}		
			});					
		},

	   errorLabelContainer: ".errorNewMonitor",
		rules: {
			newMonitorFriendlyName: {
				required: true
			},
			newMonitorURL: {
				required: true,
				url: true
			}
		},
		messages: {
			newMonitorFriendlyName: {
				required: "Friendly Name is required, "
			},
			newMonitorURL: {
				required: "URL is required, ",
				url: "Enter a valid URL"
			}
		}

	});
	
	
	
	var editPersonalInfoV = jQuery("#editPersonalInfoForm").validate({			  
		submitHandler: function(form) {
			jQuery(form).ajaxSubmit({
				success: function(theResponse) {
					if (theResponse == "true") {
						$("#userOldPassword").val('');
						$("#userPassword").val('');
						$("#userPassword2").val('');
							$("#editPersonalInfoFormSuccess").fadeIn("slow",  function() {
								setTimeout(function() {
									$("#editPersonalInfoFormSuccess").fadeOut("slow");
								}, 3000);											   
							});
					} else {
						$(".errorEditPersonalInfo").html("An error occurred. Please try again.").show();
					}

				},
				
				error: function(label) {
					loader.hide();
				}		
			});					
		},

	   errorLabelContainer: ".errorEditPersonalInfo",
		rules: {
			userFirstLastName: {
				required: true
			},
			userOldPassword: {
				required: "#userPassword:filled",
				minlength: 6
			},
			userPassword: {
				required: "#userOldPassword:filled",
				minlength: 6
			},
			userPassword2: {
				required: "#userPassword:filled",
				equalTo: "#userPassword"
			}
		},
		messages: {
			userFirstLastName: {
				required: "Name is required, "
			},
			userOldPassword: {
				required: "Current password required, ",
				minlength: "Password minimum 6 characters, "
			},
			userPassword: {
				required: "New password required, ",
				minlength: "Password minimum 6 characters, "
			},
			userPassword2: {
				required: "Re-Password required, ",
				equalTo: "Passwords must match"
			}

		}

	});




	var editEmailInfoV = jQuery("#editEmailInfoForm").validate({			  
		submitHandler: function(form) {
			jQuery(form).ajaxSubmit({
				success: function(theResponse) {
					if (theResponse == "true") {
						$("#editEmailInfoFormSuccess").fadeIn("slow",  function() {
							setTimeout(function() {
								$("#editEmailInfoFormSuccess").fadeOut("slow",  function() {
									
								});
							}, 1000);											   
						});
					} else {
						$(".errorEditEmailInfo").html("E-mail is in use. Please try another address.");
					}

				},
				
				error: function(label) {
					loader.hide();
				}		
			});					
		},

	   errorLabelContainer: ".errorEditEmailInfo",
		rules: {
			userNewEmail: {
				required: true,
				email: true,
				remote: "includes/dml/userDML.asp?action=checkEditEmail"
			}
		},
		messages: {
			userNewEmail: {
				required: "E-mail is required.",
				email: "Enter a valid e-mail.",
				remote: jQuery.format("E-mail is in use by another user.")
			}

		}

	});
	
	
	
	var addMobileAlertContactV = jQuery("#addMobileAlertContactForm").validate({			  
		submitHandler: function(form) {
			jQuery(form).ajaxSubmit({
				success: function(theResponse) {
					if (theResponse == 2) {
						addMobileAlertContactV.resetForm();			
							$("#addMobileAlertContactFormSuccess").fadeIn("slow",  function() {
								setTimeout(function() {
									top.location.href = 'mySettings.asp';
								}, 3000);		
																										 
							});
					}
					if (theResponse != 0 && theResponse != 2) {
						$(".errorAddMobileAlertContact").html(theResponse).show();
					}
					if (theResponse == 0) {
						$(".errorAddMobileAlertContact").html("The contact is already in your list.").show();
					}
				},
				
				error: function(label) {
					loader.hide();
				}		
			});					
		},

	   errorLabelContainer: ".errorAddMobileAlertContact",
		rules: {
			mobileProviderID: {
				required: true
			},
			mobileAlertContactValue: {
				required: true,
				number: true
			}
		},
		messages: {
			mobileProviderID: {
				required: "Mobile provider is required, "
			},
			mobileAlertContactValue: {
				required: "Mobile number is required, ",
				number: "Mobile number must be a number, "
			}

		}

	});
	
	
	
	var addEmailAlertContactV = jQuery("#addEmailAlertContactForm").validate({			  
		submitHandler: function(form) {
			jQuery(form).ajaxSubmit({
				success: function(theResponse) {
					if (theResponse == "true") {
						addEmailAlertContactV.resetForm();			
							$("#addEmailAlertContactFormSuccess").fadeIn("slow",  function() {
								setTimeout(function() {
									top.location.href = 'mySettings.asp';
								}, 3000);		
																										 
							});
					} else {
						$(".errorAddEmailAlertContact").html("The contact is already in your list.").show();
					}
				},
				
				error: function(label) {
					loader.hide();
				}		
			});					
		},

	   errorLabelContainer: ".errorAddEmailAlertContact",
		rules: {
			emailAlertContactValue: {
				required: true,
				email: true
			}
		},
		messages: {
			emailAlertContactValue: {
				required: "E-mail is required, ",
				email: "Please use a valid e-mail address."
			}

		}

	});
	
	
	
	var addTwitterAlertContactV = jQuery("#addTwitterAlertContactForm").validate({			  
		submitHandler: function(form) {
			jQuery(form).ajaxSubmit({
				success: function(theResponse) {
					if (theResponse == "2") {
						addTwitterAlertContactV.resetForm();			
							$("#addTwitterAlertContactFormSuccess").fadeIn("slow",  function() {
								setTimeout(function() {
									top.location.href = 'mySettings.asp';
								}, 3000);		
																										 
							});
					} else {
						if (theResponse == "0") {
							$(".errorAddTwitterAlertContact").html("The contact is already in your list.").show();
						}
						if (theResponse == "1") {
							$(".errorAddTwitterAlertContact").html('You must be following <a href="http://twitter.com/uptimerobot" target="_blank">@uptimerobot</a> Twitter user to get alerts.').show();
						}
						
					}
				},
				
				error: function(label) {
					loader.hide();
				}		
			});					
		},

	   errorLabelContainer: ".errorAddTwitterAlertContact",
		rules: {
			twitterAlertContactValue: {
				required: true
			}
		},
		messages: {
			twitterAlertContactValue: {
				required: "Twitter user is required."
			}

		}

	});
		
});