$(document).ready(function()
{
	$("#txtQueryEmail").bind("keypress", function(e)
	{
		$("#txtQueryEmail").removeClass("errorStyle");    
		$(".querymessage").text("").show();
		
		if(e.keyCode==13 || e.which==13)
        {
            $('#btnQuery').click();            
        }
	});
	
	$('#btnQuery').bind('click', function()
	{
		var email = $("#txtQueryEmail").val();
		var lang = $("#hidLang").val();
	
		//CHECK
		var error = 0;
		if (email=="") {
			$("#txtQueryEmail").addClass("errorStyle");
			$(".querymessage").text("Please enter your E-mail!").show();
			error = 1;
		}

		if ( !isValidEmail(email) )
		{
			$("#txtQueryEmail").addClass("errorStyle");
			$(".querymessage").text("Please enter a valid E-mail address!").show();
			error = 1;
		}	

		if ( error == 0 )
		{
			$(this).ajaxStart(function()
			{
				$(".querymessage").text("Querying...").show();
			});
			
			//SUBMIT
			$.post('/memberex/memberservice.asmx/RLNewsletter_GetSubscription', {'sEmail':email , 'sLang':lang} , function(querydata)
			{
				var querystatus = querydata.getElementsByTagName("Status")[0].childNodes[0].nodeValue;
				
				if ( querystatus == "0" )
				{
					var PaperList = "";
					if( querydata.getElementsByTagName("PaperList")[0].hasChildNodes() )
					{
						PaperList = querydata.getElementsByTagName("PaperList")[0].childNodes[0].nodeValue;
					}
					
					if( $.trim(PaperList) != "" && $.trim(PaperList) != "0" )
					{
						showSubscription(PaperList);
						subscription = GetPaperText(PaperList);
						
						$(".querymessage").html("<br>Your current subscription : <br>"+subscription);
					}
					else
					{
						$(".querymessage").text("You have no subscription.").show();
					}
				}
				else
				{  
					$(".querymessage").text("You have no subscription.").show();
				}
			});
		}
	});
});

function GetPaperText(PaperList)
{
	var arrEpaper_Name = new Array("", "Realitycheck Newsletter", "Contentstore Newsletter", "CCD Newsletter", "Education Newsletter"); //Display Newsletter Text
	
	var arrPaperList = PaperList.split(",");
	var sSubscriptionList = "";
	var nPaperValue = 0;
	var i=0;
	
	for( i=0 ; i<arrPaperList.length ; i++ )
	{
		nPaperValue = Number(arrPaperList[i]);
		if( arrEpaper_Name[nPaperValue] != "" )
		{
			sSubscriptionList += "<br>- " + arrEpaper_Name[nPaperValue];
		}
	}
	return sSubscriptionList;
}

function showSubscription(PaperList)
{
	var i; //count index
	
	//make all as unsubscribe
	$(":radio:input[id$='_0']").attr("checked","checked");
	
	//display subscription
	if( $.trim(PaperList) != "" )
	{
		var arrPaperList = PaperList.split(",");
		for(i=0 ; i<arrPaperList.length ; i++)
		{
			if( $("#paper_" + arrPaperList[i] + "_1") )
			{
				$("#paper_" + arrPaperList[i] + "_1").attr("checked","checked");
			}
		}
	}
}

function checkSubscribe()
{
	var sEmail = Trim(document.getElementById("txtQueryEmail").value);
	
	if( ! isValidEmail(sEmail) )
	{
		alert("Please check subscription email and enter a valid E-mail address!");
		return false;
	}
	return ture;
}

function checkChange()
{
	var sOldEmail = Trim(document.getElementById("txtOldEmail").value);
	var sNewEmail = Trim(document.getElementById("txtNewEmail").value);
	
	if( ! isValidEmail(sOldEmail) )
	{
		alert("Please check the old email and enter a valid E-mail address!");
		return false;
	}
	
	if( ! isValidEmail(sNewEmail) )
	{
		alert("Please check the new email and enter a valid E-mail address!");
		return false;
	}
	
	if( sOldEmail == sNewEmail )
	{
		alert("The old Email is same as the new one, please check.");
		return false;
	}
	
	return true;
}

//Update (AndrewShaw, 20090422) : allow + exist in email (before @)
function isValidEmail(emailstr)
{
	var account = emailstr.substring(0,emailstr.indexOf("@"));	
	var domain = emailstr.substring(emailstr.indexOf("@"));
	
	if ( emailstr.length <= 0 || account.length <= 0 || domain.length <= 0 )
	{
		return false;
	}
	else
	{
		var chkemailstr = ReplaceAll(account,"+","") + domain;	    	    
		return (chkemailstr.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1);	
	}	
}

function ReplaceAll(strOrg,strFind,strReplace){
	var index = 0;
	while(strOrg.indexOf(strFind,index) != -1){
	strOrg = strOrg.replace(strFind,strReplace);
	index = strOrg.indexOf(strFind,index);
	}
	return strOrg
}

function Trim(VALUE)
{
	return VALUE.replace(/^\s+|\s+$/g,"");
}