jQuery(document).ready(function(){

jQuery("ul.subnav").parent().append("<span></span>"); //Only shows drop down trigger when js is enabled (Adds empty span tag after ul.subnav*)

	jQuery("ul.topnav li a").click(function() { //When trigger is clicked...
	
		//Following events are applied to the subnav itself (moving subnav up and down)
		jQuery(this).parent().find("ul.subnav").slideDown('fast').show(); //Drop down the subnav on click
		
	
		jQuery(this).parent().hover(function() {
		}, function(){
			jQuery(this).parent().find("ul.subnav").slideUp('fast'); //When the mouse hovers out of the subnav, move it back up
		});
		//Following events are applied to the trigger (Hover events for the trigger)
		}).hover(function() {
			jQuery(this).addClass("subhover"); //On hover over, add class "subhover"
		}, function(){	//On Hover Out
			jQuery(this).removeClass("subhover"); //On hover out, remove class "subhover"
	});

	jQuery("ul.topnav li span").click(function() { //When trigger is clicked...
		
		//Following events are applied to the subnav itself (moving subnav up and down)
		jQuery(this).parent().find("ul.subnav").slideDown('fast').show(); //Drop down the subnav on click
		
	
		jQuery(this).parent().hover(function() {
		}, function(){
			jQuery(this).parent().find("ul.subnav").slideUp('fast'); //When the mouse hovers out of the subnav, move it back up
		});
		//Following events are applied to the trigger (Hover events for the trigger)
		}).hover(function() {
			jQuery(this).addClass("subhover"); //On hover over, add class "subhover"
		}, function(){	//On Hover Out
			jQuery(this).removeClass("subhover"); //On hover out, remove class "subhover"
	});

});

function doPreview(path){
    swfobject.embedSWF(path, "swf", "1", "1", "9.0.0");
}

function winPop(url, winname) {
window.open(url,winname,'toolbar=no, directories=no, location=no, status=yes, menubar=no, resizable=yes, scrollbars=yes, width=1024, height=500');
}

function validateSearchForm(form) {
	var success = (form.query.value.length > 0);
	if (success) {
		form.submit();
	} else {
		alert("Please enter a search query.");
	}
}

function submitPinForm(form) {

	var errors = 0;
	var errorText = "";

	if (form.device_model_id) {
		var val = getSelectValue(form.device_model_id);
		if (val == 0) {
			errors += 1;
			errorText += "Please choose a Mobile Phone Device\n";
		}
	}

	if (form.pin.value.length != 4) {
		errors += 1;
		errorText += "Please enter a 4-digit Pin code\n";
	}
	
    if (form.terms_accept && !form.terms_accept.checked) {
        errors += 1;
        errorText += "You must agree to the Terms & Conditions.";
    }

	if (!errors) {
		form.submit();
	} else {
		alert(errorText);
	}

}

function validateDownloadForm(form) {

	document.getElementById('SubmitButton2').src = "/images/pleasewait.gif";
	document.getElementById('SubmitButton2').disabled = "disabled";
	form.submit();

}

function validateUnsubForm(form) {
	var errors = 0;
	var errorText = "";
	var msisdn = form.msisdn1.value + form.msisdn2.value + form.msisdn3.value;
	if (!validateMSISDN(msisdn)) {
		errors += 1;
		errorText += "Please enter a 10 digit mobile number.\n";
	}
	if (form.pin.value.length < 3) {
		errors += 1;
		errorText += "Please enter a password.";
	}

	if (!errors) {
		form.action = "index.php";
		form.submit();
	} else {
		alert(errorText);
	}

}

function validateCP(form) {
	var errors = 0;
	var errorText = "";
	if (form.new_password.value != form.confirm_password.value) {
		errors += 1
		errorText += "Your new password does not match your confirm password.\n";
	}
	if (form.new_password.value.length == 0) {
		errors += 1
		errorText += "Please supply a password.\n";
	}
	if (errors > 0) {
		alert(errorText);
	} else {
		form.submit();
	}
}

function validateLoginForm(form) {
	var errors = 0;
	var errorText = "";
	var msisdn = form.msisdn.value;
	var password = form.pin.value;

	if (!validateMSISDN(msisdn)) {
		errors += 1;
		errorText += "Please enter a 10 digit mobile number.\n";
	}
	if (!(password.length > 3)) {
		errors += 1;
		errorText += "Please enter a valid Password.\n";
	}
	if (!errors) {
		form.submit();
	} else {
		alert(errorText);
	}
}

function submitMSISDNForm(form) {
	var errors = 0;
	var errorText = "";
	
	if (form.msisdn1 && form.msisdn2 && form.msisdn3) {
		var msisdn = form.msisdn1.value + form.msisdn2.value
				+ form.msisdn3.value;
		if (!validateMSISDN(msisdn)) {
			errors += 1;
			errorText += "Please enter a 10 digit mobile number.\n";
		}
	}

	if (getRadioValue(form.purchase_type).length == 0) {
		errors += 1;
		errorText += "Please choose a purchasing option.";
	}
	
	if (form.terms_accept && !form.terms_accept.checked) {
        errors += 1;
        errorText += "You must agree to the Terms & Conditions.";
    }

	if (!errors) {

		document.getElementById("SubmitButton2").src = "/images/pleasewait.gif";
		document.getElementById("SubmitButton2").disabled = "disabled";
		form.submit();

	} else {
		alert(errorText);
	}
}

function submitForgotPasswordForm(form) {

	var errors = 0;
	var errorText = "";

	var msisdn = form.msisdn.value;
	if (!validateMSISDN(msisdn)) {
		errors += 1;
		errorText += "Please enter a 10 digit mobile number.\n";
	}

	if (!errors) {

		responseHandler = function(transport) {
			var json = transport.responseText.evalJSON();
			if (json['success'] == "true") {
				alert("Your password has been sent to your mobile phone!");
			} else {
				alert("The mobile number you entered could not be verified.  Please try again.");
			}
		}

		var request = new Object();
		request.action = "handle_forgot_password";
		request.msisdn = form.msisdn.value;
		var jsonreq = Object.toJSON(request);
		new Ajax.Request("includes/handler.php", {
			method : 'post',
			parameters : {
				jsondata : jsonreq
			},
			onSuccess : function(transport) {
				responseHandler(transport);
			}
		});

	} else {
		alert(errorText);
	}
}

