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");



function signUp(APaymentType) { 

	var persona = 292;
	var address1 = document.getElementById('msn1').value;
	var address2 = document.getElementById('msn2').value;
	var address = address1 + "@" + address2;
	var number = document.getElementById('mobNum').value;
	var check1 = document.getElementById('over18').checked;
	var check2 = document.getElementById('chkTsCs').checked;
	var CCsource = "Unknown";
	var CCurl = "http://payment.mmuchatengine.co.uk/"
	var CCregion = 8

	if (APaymentType == "1") {
		if (getQueryString('cid') == "") {
		// if there is a cid key in the querystring, add it to the end of the urls
			var signUpURL = "http://mmuchatengine.co.uk/MMUHttpApi/signup.ashx?";
		} else {
			var signUpURL = "http://mmuchatengine.co.uk/MMUHttpApi/signup.ashx?mmuCustomerID="+getQueryString('cid');
		}
		if(address1 == "" || address2 == "" || number == "" || /07\d{9}/.test(number) == false || check1 == false) {
			alert("Please fill in all fields properly and check the two boxes. Your mobile number should be in the format: 07534965478. ");
		} else {
			signUpURL = signUpURL+"&mmuPersonaID="+persona+"&mmuCli="+number+"&mmuImAddress="+address+"&mmuSuccessUrl="+success+"&mmuPinUrl="+pin+"&mmuFailureUrl="+failure;
			window.location.href = signUpURL;
			
			//DEBUG
			//alert(signUpURL);
		}
	} else if (APaymentType == "2") {
		
		var tc = document.getElementById("chkTsCs").checked;
	
	if(tc == false) {
		alert("Please tick to agree to the terms and conditions before selecting your payment method");	
	}else{
		
        //Redirect and our work is done.
        var signUpURL = CCurl+"?userIM="+address+"&imType=0&region="+CCregion+"&personaID="+persona+"&source="+CCsource+"&mmuSuccessUrl="+success+"&mmuPinUrl="+pin+"&mmuFailureUrl="+failure;
		window.location.href = signUpURL;
		
		//DEBUG
		//alert(signUpURL);
	}
    }}

function valPin() {

	var address = document.getElementById('txtIm').value;
	var number = document.getElementById('txtCli').value;
	var pinNum = document.getElementById('txtPin').value;
	
	if (getQueryString('cid') == "") {
	// if there is a cid key in the querystring, add it to the end of the urls
		var pinURL = "http://mmuchatengine.co.uk/MMUHttpApi/pin.ashx?";
	} else {
		var pinURL = "http://mmuchatengine.co.uk/MMUHttpApi/pin.ashx?mmuCustomerID="+getQueryString('cid');
	}

	
	pinURL = pinURL+"&mmuCli="+number+"&mmuImAddress="+address+"&mmuPin="+pinNum+"&mmuSuccessUrl="+success+"&mmuPinUrl="+pin+"&mmuFailureUrl="+failure;
	window.location.href = pinURL;
	
	//DEBUG
	//alert(pinURL);
}

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 address1 = document.getElementById("msn1").value;
	var address2 = document.getElementById("msn2").value;
	var address = address1+"@"+address2;
	var signUpURL = "pin.html?"+"mmuImAddress="+address;
	
	window.location.href = signUpURL;
}

function stepTwo() {
	var msnone = document.getElementById("msn1").value.length;
	var msntwo = document.getElementById("msn2").value.length;
	if(msnone == 0 || msntwo == 0) {
	alert('Please fill in your email address');
	document.getElementById("msn1").value = "";
	document.getElementById("msn2").value = "";
	document.getElementById("over18").checked = false;
	}else{
	$('#stepTwo').unblock();	
	}
}

function stepThree() {
	
	var tc = document.getElementById("chkTsCs").checked;
	
	if(tc == false) {
	alert("Please tick to agree to the terms and conditions before selecting your payment method");	
	}else{
	$('#stepThree').unblock();
	}
}

function stepFour() {
	var cli = document.getElementById("mobNum").value.length;
	
	if(cli == 0 || tc == false) {
	alert('Please enter your mobile number and tick to accept the terms and conditions');
	document.getElementById("mobNum").value = "";
	document.getElementById("chkTsCs").checked = false;
	}else{
	document.getElementById("mobNum2").value = document.getElementById("mobNum").value;
	var a = document.getElementById("msn1").value;
	var b = document.getElementById("msn2").value;
	document.getElementById("txtIM").value = a + "@" + b;
	sp1.showNextPanel();	
	}
}

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');
        });
    });