$(document).ready(function(){
	/* Login */
	//$("#login_now").click(function(){
	$("#login_frm").submit(function(){
	
	
		
	if (echeck($("#username").val())==false){
		
		$("#username").focus()
		return false
	}
	
	
	
	$.post("includes/login.inc.php", $("#login_frm").serialize(),
			   function(data){
				//alert(data.status_code);
				//alert(data.msg);
				 if(data.status_code=="1") {
					
					alert(data.msg);
					
				}else if(data.status_code=="0")
				 {
					alert(data.msg);
					location.replace('index.php');
				 }
				 else if(data.status_code=="2")
				 {
					alert(data.msg);
					location.replace('watchnow.php');
				 }
				 else if(data.status_code=="3")
				 {
					alert(data.msg);
					location.replace('forgotpassword.php');
				 }
			   }, "json");
	
	return false;

	});
	
	
	/* end of Login */

	/* Registration */
	function validate_bday()
	{
		dobd = $("#dob_day").val();
		dobm = $("#dob_month").val();
		doby = $("#dob_year").val();
		if(dobd=="" || dobm=="" || doby=="")
		{
			return false;
			
		}else
		{
			return true;
		}
	}
	
	$("#dob_day").change(function(){
		rtrn = validate_bday();
		if(rtrn==false)
		{
		$("#bday_error").show();
		}else
		{
		$("#bday_error").hide();
		}
		
	});
	
	$("#dob_month").change(function(){
		rtrn = validate_bday();
		if(rtrn==false)
		{
		$("#bday_error").show();
		}else
		{
		$("#bday_error").hide();
		}
		
	});
	
	$("#dob_year").change(function(){
		rtrn = validate_bday();
		if(rtrn==false)
		{
		$("#bday_error").show();
		}else
		{
		$("#bday_error").hide();
		}
		
	});
	
	$("#tnc").click(function(){
		if ($('#tnc:checked').val() == undefined) 
		{
			$("#tnc_text").show();
		}else
		{
			$("#tnc_text").hide();
		}
	});
	
	
	
$("#registration_btn").click(function(){
	
	err =0;
	$("#error_msg").hide();
	email = $("#email").val();
	pwd1 = $("#pwd1").val();
	pwd2 = $("#pwd2").val();
	given_name = $("#given_name").val();
	family_name = $("#family_name").val();
	display_name = $("#display_name").val();
	gender = $("#gender").val();
	dobd = $("#dob_day").val();
	dobm = $("#dob_month").val();
	doby = $("#dob_year").val();
	tnc = $("#tnc").val();
	
	if(email=="")
	{
		alert("Email Address is required");
		$("#email").focus();
		return false;
	}
	if(pwd1=="")
	{
		alert("Password is required");
		$("#pwd1").focus();
		return false;
	}else
	{
		if(pwd1.length<8)
		{
			alert("Password must be at least 8 characters long");
			$("#pwd1").focus();
			return false;
		}
	}
	
	if(pwd1!=pwd2)
	{
		alert("Please enter the same password as above");
		$("#pwd2").focus();
		return false;
	}
	
	if(given_name=="")
	{
		alert("Given name is required");
		$("#given_name").focus();
		return false;
	}
	if(family_name=="")
	{
		alert("Family name is required");
		$("#family_name").focus();
		return false;
	}
	if(display_name=="")
	{
		alert("Display name is required");
		$("#display_name").focus();
		return false;
	}
	if(gender=="")
	{
		alert("Gender is required");
		$("#gender").focus();
		return false;
	}
	
	
	
	
	if(dobd=="" || dobm=="" || doby=="")
	{
		alert("Date of Birth is required.");
		return false;
		
	}
	
	dbay = dobm+"/"+dobd+"/"+doby;
	if (isDate(dbay)==false){
		return false;
	}
	
	
	if ($('#tnc:checked').val() == undefined) 
	{
		alert("You have to agree to the Terms & Conditions.");
		return false;
	}
	
	
	
	$.post("includes/registration.inc.php", $("#registration_frm").serialize(),
				   function(data){
					 if(data.status_code=="1") {
						
						alert(data.msg);
						//$("#error_msg_text").html(data.msg);
						//$("#error_msg").show();
						
					}else if(data.status_code=="0")
					 {
						$("#registration").hide();
						$("#line-inner").html(data.msg);
						$("#reg_success").show();
						pageTracker._trackPageview('/registration/success');
						//alert(data.msg);
						//$("#error_msg_text").html(data.msg);
						//$("#error_msg").show();
						//location.replace('index.php');
					 }
					else if(data.status_code=="2")
					 {
						$("#registration").hide();
						$("#line-inner").html(data.msg);
						$("#reg_success").show();
						pageTracker._trackPageview('/registration/success');
						//alert(data.msg);
						//$("#error_msg_text").html(data.msg);
						//$("#error_msg").show();
						//location.replace('index.php');
					 } 
				   }, "json");
	
	/*
	$("#registration_frm").validate({
			rules: {
				pwd2: {
					equalTo: "#pwd1",
					minlength: 8
				},
				pwd1: {					
					minlength: 8
				}
				
			},
			messages: {
				email: {					
					required: "Email Address is required"
				},
				pwd1: {					
					minlength: "Password must be at least 8 characters long",
					required: "Password is required"
				},
				pwd2: {
					equalTo: "Please enter the same password as above",
					minlength: "Your password must be at least 8 characters long"
				},
				given_name: {
					required: "Given name is required"					
				},
				family_name: {
					required: "Family name is required"					
				},
				display_name: {
					required: "Display name is required"					
				},
				gender: {
					required: "Gender is required"					
				},
				validation_code: {
					required: "Validation code is required"					
				},
				
				tnc: {
					required: "You have to agreed to the Terms & Conditions"					
				}
				
			},
			submitHandler: function(form) {
				dbay = $("#dob_month").val()+"/"+$("#dob_day").val()+"/"+$("#dob_year").val();
				
				
				if (isDate(dbay)==false){
					
				}
				else if(err==1)
				{
				
				}
				else
				{
			
					$.post("includes/registration.inc.php", $("#registration_frm").serialize(),
				   function(data){
					 if(data.status_code=="1") {
						
						//alert(data.msg);
						$("#error_msg_text").html(data.msg);
						$("#error_msg").show();
						
					}else if(data.status_code=="0")
					 {
						alert(data.msg);
						//$("#error_msg_text").html(data.msg);
						//$("#error_msg").show();
						location.replace('index.php');
					 }
				   }, "json");
				}
			}
		});
	
	*/
	});
	
	
	/* End Of Registration */
	
/* Forgot Password */
$("#forgot_pass").click(function(){
	
	email = $("#email").val();
	dobd = $("#dob_day").val();
	dobm = $("#dob_month").val();
	doby = $("#dob_year").val();
	
	
	if(dobd=="" || dobm=="" || doby=="")
	{
		alert("Date of Birth is required");
		return false;
		
	}
	
	if(email=="")
	{
		alert("Email Address is required");
		$("#email").focus();
		return false;
	}
	
	dbay = $("#dob_month").val()+"/"+$("#dob_day").val()+"/"+$("#dob_year").val();
				if (isDate(dbay)==false){
					
				}else
				{
			
					$.post("includes/forgot_password.inc.php", $("#forgotpassword_frm").serialize(),
					 function(data){
					 if(data.status_code=="1") {
						
						
						alert(data.msg);
						//$("#error_msg_text").html(data.msg);
						//$("#error_msg").show();
						
					}else if(data.status_code=="0")
					 {
					//	alert(data.msg);
					//	location.replace('index.php');
						$("#forgetpass").hide();
						$("#action_box").show();
					 }
				   }, "json");
				}
	/*
	$("#forgotpassword_frm").validate({
			
			messages: {
				
				email: {					
					required: "Email Address is required"
				},
				
				code: {
					required: "Validation code is required"					
				}
				
			},
			submitHandler: function(form) {
				dbay = $("#dob_month").val()+"/"+$("#dob_day").val()+"/"+$("#dob_year").val();
				if (isDate(dbay)==false){
					
				}else
				{
			
					$.post("includes/forgot_password.inc.php", $("#forgotpassword_frm").serialize(),
					 function(data){
					 if(data.status_code=="1") {
						
						
						//alert(data.msg);
						$("#error_msg_text").html(data.msg);
						$("#error_msg").show();
						
					}else if(data.status_code=="0")
					 {
						//alert(data.msg);
						//location.replace('index.php');
						$("#forgotpassword_frm").html(data.msg);
						$("#forgotpassword_button").hide();
					 }
				   }, "json");
				}
			}
		});
	
	*/


});
	/* End of Forgot Password */
	
	/* Activate Worlcup */
	
$("#activate_now_btn").click(function(){
	
	if($("#nric").val()=="")
	{
		alert("NRIC/ FIN / Passport No. is required");
		$("#nric").focus();
		return false;
	}
	
	if($("#iptv_no").val()=="")
	{
		alert("Residential Telephone / mio TV No. is required");
		$("#iptv_no").focus();
		return false;
	}
	
	$.post("includes/activate_espn.inc.php", $("#activatetp_frm").serialize(),
						   function(data){
						   //alert(data.status_code);
							 if(data.status_code=="1") {
								
								//alert(data.msg);
								//$("#error_msg").html(data.msg);
								alert(data.msg);
								
								//$("#error_msg_text").html(data.msg);
								//$("#error_msg").show();
								
							}else if(data.status_code=="0")
							 {
								//$("#error_msg").html(data.msg);
								alert(data.msg);
								//$("#error_msg_text").html(data.msg);
								//$("#error_msg").show();
								location.replace("redirect.php");
							 }
							 else if(data.status_code=="2")
							 {
								randomString = randomString();
								location.replace("activate_espn.php?"+randomString);
							 }
						   }, "json");
	});
	/*
	$("#activatetp_frm").validate({
			
			messages: {
				nric: {					
					required: "NRIC/ FIN / Passport No. is required"
				},
				cmode: {					
					required: "Select either Residential Telephone or mio TV No."
				},
				iptv_no: {
					required: "Residential Telephone / mio TV No. is required"					
				}
				
			},
			errorLabelContainer: "#error_msg",
			wrapper: "li",
			submitHandler: function(form) {
				$("#error_msg").show();
				$("#error_msg_text").html("<img src='images/loader.gif'> <span style='color:#000000'> Processing ...</span>");
				$.post("includes/activate_espn.inc.php", $("#activatetp_frm").serialize(),
						   function(data){
						   //alert(data.status_code);
							 if(data.status_code=="1") {
								
								//alert(data.msg);
								//$("#error_msg").html(data.msg);
								alert(data.msg);
								
								$("#error_msg_text").html(data.msg);
								$("#error_msg").show();
								
							}else if(data.status_code=="0")
							 {
								//$("#error_msg").html(data.msg);
								alert(data.msg);
								$("#error_msg_text").html(data.msg);
								$("#error_msg").show();
								location.replace("redirect.php")
							 }
							 else if(data.status_code=="2")
							 {
								
								location.replace("activate_espn.php")
							 }
						   }, "json");
				}
				});

	
	});
	*/
	
	/* End of Activate Worldcup */
});


$("#gen_new_code").click(function(){
	$("#captcha_div").load("new_captcha_code.php");
});

/**
 * DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	
	if (strMonth.length<1 || month<1 || month>12){
		//$("#error_msg_text").html("Please enter a valid Date of Birth");
		//$("#error_msg").show();
		alert("Please enter a valid Date of Birth");
		return false;
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		//$("#error_msg_text").html("Please enter a valid Date of Birth");
		//$("#error_msg").show();
		alert("Please enter a valid Date of Birth");
		return false;
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		//alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		//$("#error_msg_text").html("Please enter a valid Date of Birth");
		//$("#error_msg").show();
		alert("Please enter a valid Date of Birth");
		return false;
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		//alert("Please enter a valid date")
		//$("#error_msg_text").html("Please enter a valid Date of Birth");
		//$("#error_msg").show();
		alert("Please enter a valid Date of Birth");
		return false;
	}
return true
}

function ValidateForm(){
	var dt=document.frmSample.txtDate
	if (isDate(dt.value)==false){
		dt.focus()
		return false
	}
    return true
 }
 
 function popUp(URL) {
		day = new Date();
		id = day.getTime();
		w = 650;
		h = 400;
		l = (screen.availWidth - w) / 2;
		t = (screen.availHeight - h) / 2;
		eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=" + w + ",height=" + h + ",left = " + l + ",top = " + t + "');");
		//alert('popUp');
		return false;
}

 function popUp2(URL,w,h) {
		day = new Date();
		id = day.getTime();
		if(w!="" && h!="")
		{
		w = w;
		h = h;
		}else
		{
		w = 650;
		h = 400;
		}
		
		l = (screen.availWidth - w) / 2;
		t = (screen.availHeight - h) / 2;
		eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=" + w + ",height=" + h + ",left = " + l + ",top = " + t + "');");
		//alert('popUp');
		return false;
}


function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("Please enter a valid E-mail address")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Please enter a valid E-mail address")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Please enter a valid E-mail address")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Please enter a valid E-mail address")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Please enter a valid E-mail address")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Please enter a valid E-mail address")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

 		 return true					
	}

function textonly(e){
var code;
if (!e) var e = window.event;
if (e.keyCode) code = e.keyCode;
else if (e.which) code = e.which;
var character = String.fromCharCode(code);
//alert('Character was ' + character);
    //alert(code);
    //if (code == 8) return true;
    var AllowRegex  = /^[\ba-zA-Z\s-]$/;
    if (AllowRegex.test(character)) return true;    
    return false;
}


function randomString() {
	var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
	var string_length = 8;
	var randomstring = '';
	for (var i=0; i<string_length; i++) {
		var rnum = Math.floor(Math.random() * chars.length);
		randomstring += chars.substring(rnum,rnum+1);
	}
	return randomstring;
}

