//function for subscribe
var checkedCnt = 0;
var alreadyReturned = 0;
var hCA=null;
var tCA=null;
var iCA=null;
// subscribe
function Subscribe(hidCAControl, txtCAControl,imgCAControl)
{    
	//for captcha
    hCA = document.getElementById(hidCAControl);
    tCA = document.getElementById(txtCAControl);            
    iCA = document.getElementById(imgCAControl);   
	var hidCA = "";
	var txtCA = "";
	if(hCA!=null && tCA!=null && hCA.value!=null && tCA.value!=null)
	{
		hidCA=hCA.value;
		txtCA=tCA.value;
	}
    var checkedCnt = 0;
    var list = document.getElementsByName("mailerTopic");
    var email = document.getElementById("SubscribeEmail1");                
    if(email.value.length>0)
    {
        if(checkemail(email.value))
        {
            
            //we make this loop for getting checked count, we have to know it before we call
            // webservice, so we know when turn off animation
            alreadyReturned = 0;
            for(i=0; i< list.length; i++)
            {
                //calling webservice to add recipient
                if(list[i].checked){
					checkedCnt++;
                    showLoadingImage(true);
                    www.rferl.org.Services.MailerService.Subscribe(list[i].value,email.value,hidCA,txtCA, CallBackRecipient,ErrorCallbackCaptcha);  
                }
            }
            if(checkedCnt==0){
                alert('Please select at least one topic to subscribe!');
            }
        }        
    }
    else
        alert('Enter email.');        
}
function ErrorCallbackCaptcha(error)
{
}
function CallBackCaptcha(result)
{   
    if(result!=null && result!="" &&  hCA!=null &&  tCA!=null && iCA!=null)
	{
		tCA.value="";
		hCA.value = result;
		iCA.src="/jpegimage.aspx?id="+Server.UrlEncode(result);
    }
}
// function for getting result back
function CallBackRecipient(result)
{   
    
    var rec = document.getElementById('resultDiv');
    if(rec)
    {
        if(alreadyReturned==0)
                {
                    rec.innerHTML = result;                  
                }              
                else
                {                            
                    rec.innerHTML = rec.innerHTML + result;
                }
                
        alreadyReturned=alreadyReturned+1;
           
    }
	www.rferl.org.Services.MailerService.GenerateCaptchaCode(CallBackCaptcha,ErrorCallbackCaptcha);
    showLoadingImage(false);
}
//function for checking email validity
function checkemail(emailValue)
{        
    var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
    if (filter.test(emailValue))
        return true;
    else
    {
        alert("Please input a valid email address!")
        return false;
    }    
}
// turn on / off animation of loading
function showLoadingImage(flag)
{
    var rec = document.getElementById('animationDiv');
    var rec2 = document.getElementById('captchaDiv');
    
    var btn = document.getElementById('btnDiv');
    
    if(flag)
    {
        rec.style.display = 'block';
        btn.style.display = 'none';
        if(rec2) {rec2.style.display = 'none';}
    }
    else
    {
        rec.style.display = 'none';
        btn.style.display = 'block';
        if(rec2) {rec2.style.display = 'block';}
    }
}

// notifying ms AJAX script is loaded
if (typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();