var success = escape("http://www.psychictv.messagemeup.com/success.html");
var pin = escape("http://www.psychictv.messagemeup.com/pin.html");
var failure = escape("http://www.psychictv.messagemeup.com/failure.html");
var SMSregion = 2;
var CCregion = 8;	
var SMSpricingpoint = 500;
var CCpricingpoint = 500;
var persona = 292;
var CCsource = "PsychicTV";
var Msource = "PsychicTV";
var CCurl = "http://payment.mmuchatengine.co.uk/"
var Murl = "http://mmuchatengine.co.uk/MMUHttpApi/signup.ashx?";
var pinURL = "http://mmuchatengine.co.uk/MMUHttpApi/pin.ashx?";

function signUp(APaymentType) { 	
	var address = $('#msn1').val();
	var number = $('#mobNum').val();
	var validAddress = $('#msn1').valid();
	var validNumber = $('#mobNum').valid();
	var check1 = ($('#over18:checked').val() !== undefined);
	var check2 = ($('#chkTsCs:checked').val() !== undefined);
	
	if (APaymentType == "1") {
		// Mobile
		if (validAddress && validNumber && check1 && check2) {
			var signUpURL = Murl + (getQueryString('cid') == "" ? "mmuCustomerID=" + getQueryString('cid') : "");
			signUpURL = signUpURL + "&mmuPersonaID=" + persona + "&mmuCli=" + number + "&mmuImAddress=" + address + "&mmuRegion=" + SMSregion + "&amount=" + SMSpricingpoint + "&mmuSource=" + Msource + "&mmuIMType=0&mmuSuccessUrl=" + success + "&mmuPinUrl=" + pin + "&mmuFailureUrl=" + failure;
			window.location.href = signUpURL;
		}
		else {
			alert("Please fill in all fields properly and check the two boxes. Your mobile number should be in the format: 07534965478. ");
		}
	} 
	else if (APaymentType == "2") {
		// Credit Card
		if (validAddress && check1 && check2) {				
			var signUpURL = CCurl + "?userIM=" + address + "&imType=0&region=" + CCregion + "&amount=" + CCpricingpoint + "&personaID=" + persona + "&source=" + CCsource + "&mmuSuccessUrl=" + success + "&mmuPinUrl=" + pin + "&mmuFailureUrl=" + failure;
			window.location.href = signUpURL;
		}
		else {
			alert('You need to confirm you have read and accept the Ts & Cs');
		}		
    }
}

function valPin() {
	var address = $('#txtIm').val();
	var number = $('#txtCli').val();
	var pinNum = $('#txtPin').val();
	
	if ($('#txtPin').valid()) {	
		pinURL += (getQueryString('cid') == "" ? "mmuCustomerID=" + getQueryString('cid') : "");		
		pinURL += "&mmuCli=" + number + "&mmuImAddress=" + address + "&mmuPin=" + pinNum + "&mmuRegion=" + SMSregion + "&mmuSource=" + Msource + "&mmuIMType=0&mmuSuccessUrl=" + success + "&mmuPinUrl=" + pin + "&mmuFailureUrl=" + failure;
		window.location.href = pinURL;
	}
	else {
		
	}
}

function getQueryString(key, default_)
{
  if (default_==null) default_="";
  
  key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  
  var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
  var qs = regex.exec(window.location.href);
  
  if(qs == null)
    return default_;
  else
    return qs[1];
} 

function pinPage() {
	var address = $('#msn1').val();	
	var signUpURL = "pin.html?mmuImAddress=" + address;	
	window.location.href = signUpURL;
}

function stepTwo() {
	var validAddress = $('#msn1').valid();
	var validNumber = $('#mobNum').valid();
	var check1 = ($('#over18:checked').val() !== undefined);
	var check2 = ($('#chkTsCs:checked').val() !== undefined);
	
	if (validAddress) {
		$('#stepTwo').unblock();
	}
	else {
		$('#over18').attr('checked', false);
	}
}

function stepThree() {
	var validAddress = $('#msn1').valid();
	var validNumber = $('#mobNum').valid();
	var check1 = ($('#over18:checked').val() !== undefined);
	var check2 = ($('#chkTsCs:checked').val() !== undefined);
	
	if (validAddress && check1 && check2) {
		$('#stepThree').unblock();
	}
	else {
		alert('You need to confirm you have read and accept the Ts & Cs');
	}
}

function stepFour() {
	var validAddress = $('#msn1').valid();
	var validNumber = $('#mobNum').valid();
	var check1 = ($('#over18:checked').val() !== undefined);
	var check2 = ($('#chkTsCs:checked').val() !== undefined);
	
	if (validNumber && check2) {
		$("#mobNum2").val($("#mobNum").val());
		$("#txtIM").val($("#msn1").val());
		sp1.showNextPanel();	
	}
	else {
		alert('Please enter your mobile number and tick to accept the terms and conditions');
	}
}

function showTerms() {
	$('#terms').fadeIn("slow");	
	$('#stepOne').block({ message: null });
	$('#stepTwo').block({ message: null });
	$('#stepThree').block({ message: null });
}
function hideTerms() {
	$('#terms').fadeOut("slow");
	$('#stepOne').unblock();
	$('#stepTwo').unblock();
	$('#stepThree').unblock();
}

$(document).ready(function() {
	$('#stepTwo').block({ message: null });
	$('#stepThree').block({ message: null });
	$("#backBtn").mouseover(function() {
		$("#backBtn").attr('src', 'images/back-button-over.png');
	});
	$("#backBtn").mouseout(function() {
		$("#backBtn").attr('src', 'images/back-button.png');
	});
	$("#smsBtn").mouseover(function() {
		$("#smsBtn").attr('src', 'images/smsBtnOver.jpg');
	});
	$("#smsBtn").mouseout(function() {
		$("#smsBtn").attr('src', 'images/smsBtn.jpg');
	});
	$("#sendPinBtn").mouseover(function() {
		$("#sendPinBtn").attr('src', 'images/sendPinBtnOver.jpg');
	});
	$("#sendPinBtn").mouseout(function() {
		$("#sendPinBtn").attr('src', 'images/sendPinBtn.jpg');
	});
	$("#ccBtn").mouseover(function() {
		$("#ccBtn").attr('src', 'images/ccBtnOver.jpg');
	});
	$("#ccBtn").mouseout(function() {
		$("#ccBtn").attr('src', 'images/ccBtn.jpg');
	});
	$("#talktomeBtn").mouseover(function() {
		$("#talktomeBtn").attr('src', 'images/talktomeBtnOver.jpg');
	});
	$("#talktomeBtn").mouseout(function() {
		$("#talktomeBtn").attr('src', 'images/talktomeBtn.jpg');
	});
	
	$("input[type=text]").focus(function() {
        this.select();
    });
    
	$('#form1').validate({
        meta: 'validate',
        highlight: function(element, errorClass) {
            $(element).addClass('invalidField');
        },
        unhighlight: function(element, errorClass) {
            $(element).removeClass('invalidField');
        }
    });
});
