﻿// JScript File

var GlobalId = "";
var AlertType = "";    


    //https://twitter.com/oauth/authorize?oauth_token=zVNbm15rOkTuFg0DDWbGKe4IpYkvVZMxypF3ZIA6A    
    //consumer.initializeForm(document.request, document.etc, 'requestToken')










function unloadcomponent(id)
{      
   if($get('tbox_'+id) != null)
   {
        $get('tbox_'+id).innerHTML = "";
        $get('tbox_'+id).style.display = 'none';
   }
}


function unshow(divkey,id)
{
    $get(divkey + '_'+id).style.display = 'none';
}


function showReply(id,twitterid)
{
    //$get('reply_'+id).style.display == "inline" ? $get('reply_'+id).style.display = "none" : $get('reply_'+id).style.display = "inline" ;
    var divIndex = 0;
    while (document.getElementById('reply_'+divIndex))
    {
        var displayStyle = (divIndex==id || id===true)? $get('reply_'+divIndex).style.display  == 'inline' ? 'none': 'inline':'none';
        $get('reply_'+divIndex).style.display = displayStyle;
        if(displayStyle =='none' )
        {
            $get('reply_'+divIndex).innerHTML = "";                   
        }
        else
        {
            unshow('follow',id);
                
            //$get('reply_'+id).innerHTML = "<br /><p class=\"textlabel\">Twitter Username:<br /><input class=\"input\" type=\"text\" id=\"txtUser\" /><div id=\"divErrorUserName\" class=\"divErro\"><p class=\"Erro\">Please insert your Twitter user name</p></div></p><p class=\"textlabel\">Twitter Password:<br /><input class=\"input\" type=\"password\" id=\"txtPwd\" /><div id=\"divErrorPassword\" class=\"divErro\"><p class=\"Erro\">Please insert your Twitter password</p></div></p><p class=\"textlabel\"><textarea id=\"txtPost\" class=\"input\" cols=\"1\" rows=\"5\">@"+twitterid+": </textarea><div id=\"divErrorStatus\" class=\"divErro\"><p class=\"Erro\">Please insert a status</p></div></p><p ><input onclick=\"TweetThis("+ id + ");\"class=\"submitbutton\" type=\"button\" value=\"Tweet\" /></p>";                
            //$get('reply_'+id).innerHTML = "<br /><p class=\"textlabel\"><textarea id=\"txtPost\" class=\"input\" cols=\"1\" rows=\"5\">@"+twitterid+": </textarea><div id=\"divErrorStatus\" class=\"divErro\"><p class=\"Erro\">Please insert a status</p></div></p><p ><input onclick=\"fireClickEvent()\" class=\"submitbutton\" type=\"button\" value=\"Tweet\" /></p>";    
                        
            $get('reply_'+id).innerHTML += "<br /><div style=\"z-index:99999;width:300px\" id=\"tbox_"+id+"\"></div> ";                                                                                               
                                    
            twttr.anywhere(function (T) {

            T("#tbox_"+id).tweetBox({
            height: 80,
            width: 300,
            label:"",
            counter:false,	
            defaultContent: "RT @" + twitterid + " " + $get('txtPost_'+id).innerText
            });

            });       
                                               
                       
                                             
            $('.twitter-anywhere-tweet-box').css('width','350px');
            $('btn btn-m').css('font-size','1px');
            
            $get('tbox_'+id).style.display = 'inline';
         
            
                                                                                                             
        }
        divIndex++;
    }                                                     
}


function sleep(milliseconds) {
  var start = new Date().getTime();
  for (var i = 0; i < 1e7; i++) {
    if ((new Date().getTime() - start) > milliseconds){
      break;
    }
  }

    $('.twitter-anywhere-tweet-box').css('width','350px');

}

function showFollow(id,twitterid)
{
    var divIndex = 0;
    while (document.getElementById('follow_'+divIndex))
    {
        var displayStyle = (divIndex==id || id===true)? $get('follow_'+divIndex).style.display  == 'inline' ? 'none': 'inline':'none';
        $get('follow_'+divIndex).style.display = displayStyle;
        if(displayStyle =='none' )
            $get('follow_'+divIndex).innerHTML = "";
        else
        {
            unshow('reply',id);
            unloadcomponent(id);
            
                                    
            //$get('follow_'+id).innerHTML = "<br /><p class=\"textlabel\">Twitter Username:<br /><input class=\"input\" type=\"text\" id=\"txtUser\" /><div id=\"divErrorUserName\" class=\"divErro\"><p class=\"Erro\">Please insert your Twitter user name</p></div></p><p class=\"textlabel\">Twitter Password:<br /><input class=\"input\" type=\"password\" id=\"txtPwd\" /><div id=\"divErrorPassword\" class=\"divErro\"><p class=\"Erro\">Please insert your Twitter password</p></div></p></p><p ><input onclick=\"Follow("+ id + ",'"+twitterid+"');\"class=\"submitbutton\" type=\"button\" value=\"Follow\" /></p>";
            $get('follow_'+id).innerHTML = "<br /> <span id=\"follow-placeholder_"+id+"\"></span>";            
        
             twttr.anywhere(function (T) {
                T("#follow-placeholder_" + id).followButton(twitterid);
            });

        
        }   
          
        divIndex++;
      }
                        
   

    //alert(document.forms["etc"]);    
    //alert(document.forms["request"]);    
    //alert(document.request);
    //alert(document.etc);
    //consumer.initializeForm(document.request, document.etc, 'requestToken');   
   
    //document.request.submit();
    //document.request.onsubmit = "consumer.signForm(document.request, document.etc)"; 
    //document.request.submit();
        
    //consumer.signForm(document.request, document.etc);
    
    //alert(document.request.oauth_signature.value);
    //alert(document.request.oauth_nonce.value);
    //alert(document.request.oauth_timestamp.value);
    
    
    
   // twttr.anywhere(function (T) {
   //       T("#follow-placeholder_"+id).followButton(twitterid);
   //   });
    
    
          
         
}


        
function TweetThis(id)
{  
    GlobalId = id;
    var erro = 0;
    strUser = $get("txtUser").value;
    strPwd = $get("txtPwd").value;
    strPost = $get("txtPost").value;
    if(strUser == "")
    {   
        erro++;
        $get("divErrorUserName").style.display = 'inline';
        $get("divErrorStatus").style.display = 'inline';
    }
    if(strPwd == "")
    {   
        erro++;
        $get("divErrorPassword").style.display = 'inline';
        $get("divErrorStatus").style.display = 'inline';
    }


    if(erro == 0)
    {
    
        var txtPwd = encodeURIComponent(strPwd);
        var txtUsername = encodeURIComponent(strUser);
        var txtPost = encodeURIComponent(strPost);
        CreateXMLHTTP();
        var requestUrl = GlobalSitePath + "/postTwitterStatus.aspx?function=posTwitter&post=" + txtPost + "&pwd=" + txtPwd + "&user=" + txtUsername;
        if(XMLHTTP)
        {
            XMLHTTP.onreadystatechange = okHandler;
            XMLHTTP.open("GET", requestUrl, true);
            XMLHTTP.send(null); 
        }
       
      $get("reply_"+GlobalId).innerHTML = "<IMG SRC=\"" + GlobalSitePath + "/images/global/loader.gif\" title=\"loader\" />";
    }
}

function Follow(id, twitterid)
{  
    GlobalId = id;
    var erro = 0;
    
    //strUser = $get("txtUser").value;
    //strPwd = $get("txtPwd").value;
    
    //if(strUser == "")
    //{   
    //    erro++;
    //    $get("divErrorUserName").style.display = 'inline';
    //    $get("divErrorStatus").style.display = 'inline';
    //}
    //if(strPwd == "")
    //{   
    //    erro++;
    //    $get("divErrorPassword").style.display = 'inline';
    //    $get("divErrorStatus").style.display = 'inline';
    //}


    if(erro == 0)
    {
           
        var txtTwitterId = encodeURIComponent(twitterid);
        window.location = GlobalSitePath + "/JTweetsOauthentication.aspx?twitterid="+txtTwitterId;
    
    /*
        //var txtPwd = encodeURIComponent(strPwd);
        //var txtUsername = encodeURIComponent(strUser);
        var txtTwitterId = encodeURIComponent(twitterid);
        CreateXMLHTTP();
        //var requestUrl = GlobalSitePath + "/postTwitterStatus.aspx?function=follow&pwd=" + txtPwd + "&user=" + txtUsername + "&twitterid="+txtTwitterId;
        var requestUrl = GlobalSitePath + "/JTweetsOauthentication.aspx?twitterid="+txtTwitterId;
        if(XMLHTTP)
        {
            XMLHTTP.onreadystatechange = okHandlerFollow;
            XMLHTTP.open("GET", requestUrl+"&rand="+new Date(), true);
            XMLHTTP.send(null); 
        }
       
      //$get("follow_"+GlobalId).innerHTML = "<IMG SRC=\"" + GlobalSitePath + "/images/global/loader.gif\" title=\"loader\" />";
      
      */
    }
}




function CreateXMLHTTP()
{
    try
    {  XMLHTTP = new ActiveXObject("Msxml2.XMLHTTP");  }
    catch(e)
    {
        try
        {            XMLHTTP = new ActiveXObject("Microsoft.XMLHTTP");        } 
        catch(oc)
        {            XMLHTTP = null;        }
    }
    if(!XMLHTTP && typeof XMLHttpRequest != "undefined") 
    {        XMLHTTP = new XMLHttpRequest();    }
}

function okHandler()
{
	if(XMLHTTP.readyState == 4)
	{
		if(XMLHTTP.status == 200)
		{	$get("reply_"+GlobalId).innerHTML = "<p class=\"Erro\">Your reply was submitted.</p>";	}
		else if(XMLHTTP.status == 500)
		{	$get("reply_"+GlobalId).innerHTML = "<p class=\"Erro\">Twitter Authentication Error</p>";	}
		else
			{$get("reply_"+GlobalId).innerHTML = "<p class=\"Erro\">Twitter Communication Error: Code - "+XMLHTTP.status+"</p>";}
	}
	else
		$get("reply_"+GlobalId).innerHTML = "<p class=\"Erro\">Twitter Communication Error</p>";
}

function okHandlerFollow()
{

	if(XMLHTTP.readyState == 4)
	{
		if(XMLHTTP.status == 200)
		{	$get("follow_"+GlobalId).innerHTML = "<p class=\"Erro\">You are now following '"+XMLHTTP.responseText+"'.</p>";	}
		else if(XMLHTTP.status == 500)
		{	$get("follow_"+GlobalId).innerHTML = "<p class=\"Erro\">There is an authentication error or you already follow this person.</p>";	}
		else
			{$get("follow_"+GlobalId).innerHTML = "<p class=\"Erro\">Twitter Communication Error: Code - "+XMLHTTP.status+"</p>";}
	}
	else
		$get("follow_"+GlobalId).innerHTML = "<p class=\"Erro\">Twitter Communication Error</p>";
}

function SubmitRegistration()
{ 
    var erro = 0;
    $get("lblErrorCaptcha").style.display = "none";
    $get("lblErrorFirstName").style.display = "none";
    $get("lblErrorLastName").style.display = "none";
    $get("lblErrorEmail").style.display = "none";
    $get("lblErrorCompany").style.display = "none";
    $get("lblErrorTelephone").style.display = "none";
    $get("lblErrorKeyword").style.display = "none";
    $get("lblErrorFunction").style.display = "none";
    $get("lblErrorDepartment").style.display = "none";
    $get("lblErrorIndustry").style.display = "none";
    $get("lblErrorCountryKeyword").style.display = "none";
    
    strKeywordAlertServiceId = $get("ctl00_ContentPlaceHolder1_hdnKeywordAlertServiceId").value;
    strFirstName = MyTrim($get("ctl00_ContentPlaceHolder1_txtFirstName").value);
    strLastName = MyTrim($get("ctl00_ContentPlaceHolder1_txtLastName").value);
    strEmail = MyTrim($get("ctl00_ContentPlaceHolder1_txtEmail").value);
    strCompany = MyTrim($get("ctl00_ContentPlaceHolder1_txtCompany").value);
    strTelephone = MyTrim($get("ctl00_ContentPlaceHolder1_txtTelephone").value);
    strKeyword = MyTrim($get("ctl00_ContentPlaceHolder1_txtKeyword").value);
    strFunction = $get("ctl00_ContentPlaceHolder1_lstFunction").value;
    strDepartment = $get("ctl00_ContentPlaceHolder1_lstDepartment").value;
    strIndustry = $get("ctl00_ContentPlaceHolder1_lstIndustry").value;
    strCountryKeyword = $get("ctl00_ContentPlaceHolder1_lstCountryKeyword").value;
    strCaptcha = MyTrim($get("ctl00_ContentPlaceHolder1_txtCaptcha").value);
    strAlertType = AlertType;
    
    if(strFirstName == "")
    {   
        erro++;
        $get("lblErrorFirstName").style.display = "inline";
    }
    if(strLastName == "")
    {   
        erro++;
        $get("lblErrorLastName").style.display = "inline";
    }
    if(strEmail == "" || !EmailValidator(strEmail))
    {   
        erro++;
        $get("lblErrorEmail").style.display = "inline";
    }
    if(strKeyword == "")
    {   
        erro++;
        $get("lblErrorKeyword").style.display = "inline";
    }
    if(strCountryKeyword == "0")
    {   
        erro++;
        $get("lblErrorCountryKeyword").style.display = "inline";
    }
    if(strCaptcha == "")
    {   
        erro++;
        $get("lblErrorCaptcha").style.display = "inline";
    }

    if(erro == 0)
    {
        $get("divFormMessage").innerHTML = '';
        var txtFirstName = encodeURIComponent(strFirstName);
        var txtLastName = encodeURIComponent(strLastName);
        var txtEmail = encodeURIComponent(strEmail);
        var txtCompany = encodeURIComponent(strCompany);
        var txtTelephone = encodeURIComponent(strTelephone);
        var txtKeyword = encodeURIComponent(strKeyword);
        var txtCaptcha = encodeURIComponent(strCaptcha);
        var txtKeywordAlertServiceId = encodeURIComponent(strKeywordAlertServiceId);
        CreateXMLHTTP();
        var requestUrl = GlobalSitePath + "/registrationSubmit.aspx?FirstName=" + txtFirstName + "&LastName=" + txtLastName + "&Email=" + txtEmail+ "&Company=" + txtCompany+ "&Telephone=" + txtTelephone+ "&Keyword=" + txtKeyword+ "&Captcha=" + txtCaptcha+ "&Function=" + strFunction+ "&Department=" + strDepartment+ "&Industry=" + strIndustry+ "&CountryKeyword=" + strCountryKeyword+ "&AlertType=" + strAlertType + "&KeywordAlertServiceId=" + txtKeywordAlertServiceId;
        if(XMLHTTP)
        {
            XMLHTTP.onreadystatechange = registrationHandler;
            XMLHTTP.open("GET", requestUrl, true);
            XMLHTTP.send(null); 
        }
       
      //$get("reply_"+GlobalId).innerHTML = "<IMG SRC=\"" + GlobalSitePath + "/images/global/loader.gif\" title=\"loader\" />";
    }
}

function registrationHandler()
{
	if(XMLHTTP.readyState == 4)
	{
	   
	    $get("divFormMessage").innerHTML = "";	
	    if(XMLHTTP.responseText == "0")
	        $get("lblErrorCaptcha").style.display = "inline";
	    else if(XMLHTTP.responseText == "2")
	    {$get("lblErrorEmail").style.display = "inline";$get("divFormMessage").innerHTML = "<p class=\"Erro\">This email address is already registered. If you need help, please contact <a href='mailto:journalisttweets@cision.com'>journalisttweets@cision.com</a></p>";}	
	    else if(XMLHTTP.responseText == "3")
		    $get("lblErrorKeyword").style.display = "inline";
		else if(XMLHTTP.status == 200)
		    {
		    $get("ctl00_ContentPlaceHolder1_btnEmail").style.display = "none";
		    $get("ctl00_ContentPlaceHolder1_btnUnsubscribe").style.display = "none";
		    if(XMLHTTP.responseText == "11")
		        {
		        CleanForm();
		        $get("divFormMessage").innerHTML = "<p>Your personal data was updated.</p>";
		        }
		    else
		        {
		        CleanForm();
		        $get("divFormMessage").innerHTML = "<p>Registration complete!</p>";
		        //$get("divFormMessage").innerHTML = "<p class=\"Erro\">Instructions to activate your account will be sent to your email address in the next minutes. If you don't activate the account in the next 5 days it will be removed.</p>";
		        }
		    }
	    else
			{$get("divFormMessage").innerHTML = "<p class=\"Erro\">Error sending form: Code - "+XMLHTTP.status+"</p>";}
	}
	//else
	//	$get("divFormMessage").innerHTML = "<p class=\"Erro\">Error sending form</p>";
}

function SubmitUnsubscrive()
{ 
    var erro = 0;
    
    strKeywordAlertServiceId = $get("ctl00_ContentPlaceHolder1_hdnKeywordAlertServiceId").value;

    if(erro == 0)
    {
        $get("divFormMessage").innerHTML = '';
        var txtKeywordAlertServiceId = encodeURIComponent(strKeywordAlertServiceId);
        CreateXMLHTTP();
        var requestUrl = GlobalSitePath + "/registrationSubmit.aspx?SubmitUnsubscrive=1&KeywordAlertServiceId=" + txtKeywordAlertServiceId;
        if(XMLHTTP)
        {
            XMLHTTP.onreadystatechange = unsubscribeHandler;
            XMLHTTP.open("GET", requestUrl, true);
            XMLHTTP.send(null); 
        }
       
      //$get("reply_"+GlobalId).innerHTML = "<IMG SRC=\"" + GlobalSitePath + "/images/global/loader.gif\" title=\"loader\" />";
    }
}

function unsubscribeHandler()
{
	if(XMLHTTP.readyState == 4)
	{
	    $get("divFormMessage").innerHTML = "";	
	    if(XMLHTTP.status == 200)
	        {
	        $get("ctl00_ContentPlaceHolder1_btnEmail").style.display = "none";
	        $get("ctl00_ContentPlaceHolder1_btnUnsubscribe").style.display = "none";
    	    
            CleanForm();
            $get("divFormMessage").innerHTML = "<p class=\"Erro\">Your account was removed!</p>";
        
	        }
	    else
			{$get("divFormMessage").innerHTML = "<p class=\"Erro\">Error sending form: Code - "+XMLHTTP.status+"</p>";}
	}
	//else
	//	$get("divFormMessage").innerHTML = "<p class=\"Erro\">Error sending form</p>";
}

function CleanForm(){
    $get("divFormMessage").innerHTML = "";
    $get("ctl00_ContentPlaceHolder1_btnEmail").style.display = "inline";
    $get("lblErrorCaptcha").style.display = "none";
    $get("lblErrorFirstName").style.display = "none";
    $get("lblErrorLastName").style.display = "none";
    $get("lblErrorEmail").style.display = "none";
    $get("lblErrorCompany").style.display = "none";
    $get("lblErrorTelephone").style.display = "none";
    $get("lblErrorKeyword").style.display = "none";
    $get("lblErrorFunction").style.display = "none";
    $get("lblErrorDepartment").style.display = "none";
    $get("lblErrorIndustry").style.display = "none";
    $get("lblErrorCountryKeyword").style.display = "none";
    $get("ctl00_ContentPlaceHolder1_hdnKeywordAlertServiceId").value = "0";
    $get("ctl00_ContentPlaceHolder1_txtFirstName").value = "";
    $get("ctl00_ContentPlaceHolder1_txtLastName").value = "";
    $get("ctl00_ContentPlaceHolder1_txtEmail").value = "";
    $get("ctl00_ContentPlaceHolder1_txtCompany").value = "";
    $get("ctl00_ContentPlaceHolder1_txtTelephone").value = "";
    $get("ctl00_ContentPlaceHolder1_txtKeyword").value = "";
    $get("ctl00_ContentPlaceHolder1_lstFunction").options[0].selected = true;
    $get("ctl00_ContentPlaceHolder1_lstDepartment").options[0].selected = true;
    $get("ctl00_ContentPlaceHolder1_lstIndustry").options[0].selected = true;
    $get("ctl00_ContentPlaceHolder1_lstCountryKeyword").options[0].selected = true;
    $get("ctl00_ContentPlaceHolder1_txtCaptcha").value = "";
}
function AlertTypeClick(value)
{ AlertType = value;}

function MyTrim(str)
{return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');}

function EmailValidator(email) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   if(reg.test(email) == false) 
      return false;
    else
      return true;
}






function SetPostRank(rankingid, rank)
{    
          
    var erro = 0;  
        
    if(rankingid == "")
       erro++;
        
    if(erro == 0)
    {    
        var txtControlId = encodeURIComponent(rankingid);        
        var txtRank = encodeURIComponent(rank);
        
        CreateXMLHTTP();
        var dtnow = new Date();
        var requestUrl = GlobalSitePath + "/setPostRank.aspx?id=" + txtControlId + "&rank=" + txtRank + "&dt=" + dtnow.toString();        
        try{
        if(XMLHTTP)
        {                   
            XMLHTTP.onreadystatechange = updateRankHandler;
            XMLHTTP.open("GET", requestUrl, true);
            XMLHTTP.send(null); 
        }   
        }catch(e){alert(e);}          
    }
    
    
    function updateRankHandler()
    {
    	if(XMLHTTP.readyState == 4)
	    {
	        //if (XMLHTTP.status==404) 
	        //    alert("URL doesn't exist!")

	        //alert(XMLHTTP.getAllResponseHeaders());
	    }
    
    }
}
