function post_pole(articleId,vote,divid,author)
{
    
    var status =check_block_status(author);
    
    if(status == 'blocked')
    {
        alert(author+" has engaged the cloaking device");
        return false;
    }	
    var top="top".concat(divid);
    
    document.getElementById(top).style.display='none';
    var x=document.getElementById('art_cnt').value;
    //alert(x);
    x=parseInt(x)+1;
    x=x.toString();
    document.getElementById('art_cnt').value=x;
    var id=divid;
    var d = new Date();
    var t=d.getTime();

    document.getElementById(id).innerHTML="<img src="+imagesLocation+"'layout/ac/loading.gif' style='width:50px;margin:10px 10px 5px 200px'>";
    var btndiv="btndiv".concat(divid);
    var hiddendiv="hiddendiv".concat(divid);
    //alert(portalLocation+"story/ac-insert_pole.php?art_id="+articleId+"&vote="+vote+"&hdiv="+divid+"&t="+t);
    $.get(portalLocation+"story/ac-insert_pole.php?art_id="+articleId+"&vote="+vote+"&hdiv="+divid+"&t="+t,function(data){
        //alert(portalLocation+"story/ac-insert_pole.php?art_id="+articleId+"&vote="+vote+"&hdiv="+divid+"&t="+t);
        //alert(id);
        document.getElementById(id).innerHTML=data;
        modal();

        login_modal(articleId,divid);
    });
    
    if(document.getElementById(btndiv) != undefined)
    {
        document.getElementById(btndiv).style.display="none";
    }
    if(document.getElementById(hiddendiv) != undefined)
    {
        document.getElementById(hiddendiv).style.display="block";
    }
    return true;
}

function modal(){
	
$(document).ready(function() {	
	
    //select all the a tag with name equal to modal
    $('a[name=modal]').click(function(e) {

        //alert("called");
        //Cancel the link behavior
        e.preventDefault();

        //Get the A tag
        var id = $(this).attr('href');
        var txt=$(this).attr('id');
        e.preventDefault();

        if (txt != '')
        {var url=portalLocation+"story/ac-load_argument_rooms.php?article_ref="+txt;		
            $.get(url, function(data){
                $('.argument_pop').html(data);
            //alert(txt);
            });
        }
        //Get the screen height and width
        var maskHeight = $(document).height();
        var maskWidth = $(window).width();

        //Set heigth and width to mask to fill up the whole screen
        $('#mask').css({'width':maskWidth,'height':maskHeight});

        //transition effect		
        $('#mask').fadeIn(1000);	
        $('#mask').fadeTo("slow",0.8);	

        //Get the window height and width
        var winH = $(window).height();
        var winW = $(window).width();

        //Set the popup window to center
        $(id).css('top', Math.max(winH/2-$(id).height()/2 + $(window).scrollTop(),0));
        $(id).css('left', Math.max(winW/2-$(id).width()/2 + $(window).scrollLeft(),0)); 
	
            //transition effect
            $(id).fadeIn(2000); 
	
	});

	
	
	//if close button is clicked
	$('.window .close').click(function (e) {
		//Cancel the link behavior
		e.preventDefault();
		
		$('#mask').hide();
		$('.window').hide();
	});		
	
	//if mask is clicked
	$('#mask').click(function () {
		$(this).hide();
		$('.window').hide();
	});			
	//alert("inside modal");
    });
}

function login_modal(articleId,divid,pg)
{
    $(document).ready(function () {

        $('#basic-modal input.basic, #basic-modal a.clslogin').click(function (e) {		
                var txt=$(this).attr('id');
                e.preventDefault();
                var url=portalLocation+"users/login.php?open_art="+articleId+"&pos="+divid+"&pg="+pg;

        $.get(url, function(data){
                document.getElementById('basic-modal-content').innerHTML=data;
                });
                $('#basic-modal-content').modal();

        });
    });
}

function check_block_status(blocked_by)
{
    var url = portalLocation+"users/ac-block.php?blocked_by="+blocked_by;
    var status=$.ajax({
    url: url,
    type: 'get',
    dataType: 'text',
    async: false,
    success: function(data) {

    }
}).responseText;
    return status;
}

function changeBox(divid,articleId)
{
    var standbox="standbox"+articleId
    var challengebox="challengebox"+articleId
    //alert(divid);
    document.getElementById(standbox).style.display="block";
    document.getElementById(challengebox).style.display="none";
	
}

function closeChallengeBox(divid,articleId)
{
    var standbox="standbox"+articleId
    var challengebox="challengebox"+articleId
    //alert(divid);
    hidediv(divid);
    //document.getElementById(standbox).style.display="none";
    //document.getElementById(challengebox).style.display="none";
	
}

function comment_vote(commentId,user,vote,category,touser)
{
    var status =check_block_status(touser);

    if(status == 'blocked')
    {
        alert(touser+" has engaged the cloaking device");
        return false;
    }
    var el_vote=commentId+"up";
    if(category == 3)
    {
        var evil_vote = "evil"+commentId;
    }
    var url=portalLocation+"story/ac-insert_stand_pole.php?commentId="+commentId+"&vote="+vote+"&user="+user+"&category="+category+"&touser="+touser;
    //alert(url);
    $.get(url, function(data){
        //alert(data);
        if (category == 1 || category == 2)      
        {
            document.getElementById(el_vote).innerHTML=data;
        }
        else
        {
            document.getElementById(evil_vote).innerHTML=data;
        }
    });

     return true;
}

function display_chal_box(boxid,id)
{	
    var status =check_block_status(id);
	
    if(status == 'blocked')
    {
        alert(id+" has engaged the cloaking device");

        return false;
    }
    for(var i=0; i<11; i++)
    {
        var closediv="box".concat(i);
        var hiddiv=closediv.concat("h");
        if(document.getElementById(closediv) != null)
        {
            document.getElementById(closediv).style.display="none";

        }
        if(document.getElementById(hiddiv) != null)
        {
            document.getElementById(hiddiv).style.display="block";
        }
    }
    var hdiv=boxid.concat("h");

    document.getElementById(boxid).style.display="block";
    document.getElementById(hdiv).style.display="none";
}
function validate_challenge_box(boxid)
{
    var box_content=document.getElementById(boxid).value;
    if(box_content == "")
    {
        alert("Please enter the challenge");
        return false;
    }
    spamPopUp('5 cans of spam have been added to your account');	
    return true;
}
function close_chall_box(closediv)
{
    var hiddendiv=closediv.concat("h");
    document.getElementById(closediv).style.display="none";
    document.getElementById(hiddendiv).style.display="block";
}

function post_reward(from_user,to_user,cat_id,stId,title)
{
    var status =check_block_status(to_user);

    if(status == 'blocked')
    {
        alert(to_user+" has engaged the cloaking device");

        return false;
    }	
    var id="reward"+stId;
    
    var url=portalLocation+"story/ac-reward_user.php?to_user="+to_user+"&cat_id="+cat_id+"&articleId="+stId+"&title="+title;
    //alert(url);
    $.get(url, function(data){
        //alert(data);
        if(cat_id == 1)
        {
          //document.getElementById('idd').innerHTML="<a href='#' title='You have already rewarded this user'><img src='images/rewarded.gif' width='20px' border='0' alt='rewarded user'></a>";
          spamPopUp('You rewarded '+to_user+' 25 cans of spam on this story');
        }
        else
        {

            document.getElementById(id).innerHTML="<a href='#' title='You have already rewarded this user'><img src='images/rewarded.gif' width='20px' border='0' alt='rewaded user'></a>";
            comment_vote(stId,from_user,'reward',1,to_user);
            if (cat_id == 2)
            {
                spamPopUp('You rewarded '+to_user+' 5 cans of spam on this stand');
            }
            else
            {
                 spamPopUp('You rewarded '+to_user+' 5 cans of spam on this challenge'); 
            }
        }  
    });
    
    return true;
	  
}

/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */



function getXMLHTTPRequest()
{
try
{
req=new XMLHttpRequest();
}
catch(err1)
{
try
{
req=new ActiveXObject('Msxml2.XMLHTTP');
}
catch(err2)
{
try
{
req=new ActiveXObject('Microsoft.XMLHTTP');
}
catch(err3)
{
req=false;
}
}
}
return req;
}

var http=getXMLHTTPRequest();

function ServerText(url,qstr)
{
url=url+"?"+qstr;
url=url+"&sid="+Math.random();
http.open("GET",url,true);
http.onreadystatechange=useHTTPResponse;
http.send(null);
}

function useHTTPResponse()
{
    if(http.readyState==4)
    {
    if(http.status==200)
    {
    document.getElementById('textresult').innerHTML=http.responseText;
    }
    }
    else
    {
    document.getElementById('textresult').innerHTML="";
    }
}


function edit_stand(id,mode)
{
    var sid=id+mode;
    //alert(sid);
    document.getElementById(sid).style.display="block";
    if(mode == 'cancel') hid=id+'edit'; else hid=id+'cancel';
    //alert(hid);
    document.getElementById(hid).style.display="none";

}
function update_comment(comment_id,mode)
{
    var headline = '';
    var comment = '';
    var txtid=comment_id+'txt';
    var standspan=comment_id+'cancel';
    var commentbox=comment_id+'comment';
    var comment_headline=comment_id+'hdln';
    var comment=document.getElementById(txtid).value;
    if(mode == 'update_detail')
    {
        var headline=document.getElementById(comment_headline).value;
        var mode = 'update';
    }
    //alert(headline);
    var cnf=true;
    if(mode=="delete" || comment == "" || mode=="delete_detail")
    {
        cnf=confirm("Are you sure you want to delete this??");
    }
    if(!cnf)
    {

            return false;
    }
    var url=portalLocation+"story/ac-update_comment.php?mode="+mode+"&comment_id="+comment_id+"&comment="+comment+"&headline="+headline;
    //alert(url);
    $.get(url, function(data){
        if(mode == 'delete' || comment == '')
        {
            document.getElementById(standspan).innerHTML="user has deleted the comment";
            edit_stand(comment_id,'cancel');
        }
        if(mode == 'delete_detail')
        {
            document.getElementById(commentbox).innerHTML=" ";
        }
        else
        {

            if(headline != null)
            {
                edit_stand(comment_id,'cancel');
                document.getElementById(standspan).innerHTML='<strong>'+headline+'</strong><br>'+comment;
            }
            else
            {
                edit_stand(comment_id,'cancel');
                document.getElementById(standspan).innerHTML=comment;
        }

        }
    });
	

     return true;
}

function post_story_pole(articleId,vote,divid)
{
    
    var id=divid
    var btndiv="btndiv".concat(divid);
    var hiddendiv="hiddendiv".concat(divid);
//alert(portalLocation+"story/ac-insert_pole.php?art_id="+articleId+"&vote="+vote+"&hdiv="+divid);
$('#voteType').val(vote);
    $.get(portalLocation+"story/ac-insert_pole.php?art_id="+articleId+"&vote="+vote+"&hdiv="+divid,function(data){

        //alert(data);
        document.getElementById('stand_box').innerHTML = data;
        //document.getElementById("test").innerHTML="<div style='float:right;width:550px'><i>13 people agree with you and 2 people disagree. Click Challenge to challenge them or post your own stand. View other stands <a>here</a></i><table border='0' width='550px'><tbody><tr><td id='stand1' width='50%' align='left' valign='top'><img src='images/person1.png' style='float:left;margin:5px'><b>Meryll:</b><i>(20 min ago)</i><img src='images/right.jpg' title='Next Stand' style='float:right' onclick='hide()'><br>The financial industry has been waging a war against the proposed federal laws, which would bring in unnecessary bureaucracy into the running of national banks.<br>(Challenges 4)<br><img src='images/challenges.gif' style='margin:0px 0px 0px 80px'></td><td align='right'>(You are challenging Meryll's stand)<br><textarea rows='4' cols='35'></textarea><br><a href='new1.html'><input src='images/save_button.gif' name='accomment' id='Image45' type='image'>&nbsp;&nbsp;&nbsp;<img src='images/cancel_button.gif'  border='0'></a>&nbsp;</td></tr></table></div>";
    });

    if(document.getElementById(btndiv) != undefined)
    {
        document.getElementById(btndiv).style.display="none";
    }
    if(document.getElementById(hiddendiv) != undefined)
    {
        document.getElementById(hiddendiv).style.display="block";
    }
    return true;
}
  
  
    function post_comment(divid,articleId,challenge,challenged,id)
    {	
        var status =check_block_status(id);

        if(status == 'blocked')
        {
            alert("you have been blocked");
            return false;
        }
        var placeid="place"+divid;
        id = divid;
        var d = new Date();
        var t=d.getTime();
        cmtid="comment"+articleId;
        cmntid=cmtid+challenge;
        // alert(cmntid);  

        var cmnt=document.getElementById(cmntid).value;
        if(cmnt == "")
        {
            alert("Please enter your comment");
            return false;
        }

        if(challenge != 0)
        {
            var urll = portalLocation+"story/ac-read_article.php?chal=yes&articleId="+articleId+"&comment="+cmnt+"&stId="+challenge+"&stLogn="+challenged;
        }
        else
        {
            var urll = portalLocation+"story/ac-read_article.php?commented=yes&articleId="+articleId+"&comment="+cmnt+"&stId="+challenge;
        }
        //$.get(portalLocation+"story/ac-insert_pole.php?art_id="+articleId+"&vote="+vote+"&hdiv="+divid,function(data){
        //alert(urll);
        $.get(urll,function(data){
            //alert(divid);
            if(divid == 'votes')
            {
                spamPopUp('5 cans of spam have been added to your account');
                setTimeout("window.location='"+portalLocation+"story/ac-read_article.php?articleId="+articleId+"'",1500);
            }
            
            if(challenge != 0)
            {
                document.getElementById("theheader").innerHTML="<b>| Your challenge has been posted |</b>";
            }
            else
            {
                closeChallengeBox(divid,articleId);
            }
            
            

            /*document.getElementById("thestand").innerHTML=cmnt;
            document.getElementById("thestorypage").innerHTML = "<i>Click <a href='"+portalLocation+"story/ac-read_article.php?articleId="+articleId+"&sc=yes' style='color:#17497c;text-decoration:none'>here</a> to see other stands and challenges </i>";
            document.getElementById("thefunctions").innerHTML = " <a  style='color:#17497c;text-decoration:none' href='#' onclick="+"replace_story('"+placeid+"','"+divid+"','"+articleId+"')"+"><img src='images/ac/close.gif' border='0'></a> ";
            document.getElementById(id).innerHTML=""; */
            //document.getElementById(id).innerHTML="<i>You can continue giving your opinion on other stories. Click <span   style='color:#17497c' onclick="+"replace_story('"+placeid+"','"+divid+"','"+articleId+"')"+">here</span> to change this story</i><br><br>";
            spamPopUp('5 cans of spam have been added to your account');
        });

        
        return true;
    }
    
    
    function scrollToStandBox(posId, artId, userId)
    {
        var el = $.browser.opera ? $("html") : $("html, body");  
        el.animate({ scrollTop: $('#'+artId).offset().top }, 'slow');
        post_pole_scroll(artId,'view',posId,userId);
    }
    
    
    function post_pole_scroll(articleId,vote,divid,author)
    {

        var status =check_block_status(author);

        if(status == 'blocked')
        {
            alert(author+" has engaged the cloaking device");
            return false;
        }	
        var top="top".concat(divid);

        document.getElementById(top).style.display='none';
        var x=document.getElementById('art_cnt').value;
        //alert(x);
        x=parseInt(x)+1;
        x=x.toString();
        document.getElementById('art_cnt').value=x;
        var id=divid;
        var d = new Date();
        var t=d.getTime();

        document.getElementById(id).innerHTML="<img src="+imagesLocation+"'layout/ac/loading.gif' style='width:50px;margin:10px 10px 5px 200px'>";
        var btndiv="btndiv".concat(divid);
        var hiddendiv="hiddendiv".concat(divid);
        //alert(portalLocation+"story/ac-insert_pole.php?art_id="+articleId+"&vote="+vote+"&hdiv="+divid+"&t="+t);
        $.get(portalLocation+"story/ac-insert_pole.php?art_id="+articleId+"&vote="+vote+"&hdiv="+divid+"&t="+t,function(data){
            //alert(portalLocation+"story/ac-insert_pole.php?art_id="+articleId+"&vote="+vote+"&hdiv="+divid+"&t="+t);
            //alert(id);
            document.getElementById(id).innerHTML=data;
            
            document.getElementById(id).style.display = 'block';
            
        });

        return true;
    }

function amonitor()
{
	
	 var url=portalLocation+"users/activitymonitor.php";
	
     $.get(url, function(data){
    	
             document.getElementById('amonitor').innerHTML=data;
             setTimeout('amonitor()',"30000");
             });
	}


function toggleImageSelectDiv(type)
{
    if(type == '1')
    {
        document.getElementById('imageChooseDiv').style.display = '';
    }
    else
    {
        document.getElementById('imageChooseDiv').style.display = 'none';
    }
}

function validUrl()
{
    var url = document.getElementById("externallink").value;
    if(url != 0)
    {
        grabSiteData();
    }
    else
    {
        alert("Enter Url");
        return false;
    }
}

function grabSiteData()
{
	
    document.getElementById('loading_grab').style.display='block';
    var url = document.getElementById('externallink').value;
    //alert(portalLocation+'story/grabSiteData.php?url='+url);
    $.post(portalLocation+'story/grabSiteData.php?',{'url':url},function(data){
        //alert('sdf');
        if(data != 'Error')
        {
            var val = data.split('----SPLIT----');
            //alert(val[1]);
            document.getElementById('title').value = val[0];

            tinyMCE.get('subheading').setContent(val[1]);
            document.getElementById('linkto').value = url;
            document.getElementById('loading_grab').style.display = 'none';     
        }
        else
        {
            alert('Url Error');
            document.getElementById('loading_grab').style.display = 'none';   
        }	       

    });
}


function addArticle_user(type)
{
    var title = document.getElementById("title").value;
    //var authorName = document.getElementById("authorName").value;
    var subheading = tinyMCE.get('subheading').getContent();
    var body_text = document.getElementById("body").value;
    var ext_url = document.getElementById("linkto").value;
    var txtquestion=document.getElementById("txtquestion").value;
    if(title==false)
    {
        alert("Heading field is empty");
        document.getElementById("title").focus();
        return false;
    }



    var file_name = document.getElementById("title").value;
    var iChars= /[\'\`\~\?\-\_\!\@\#\$\%\^\&\*\(\)\=\+\|\.\n\ \(\)\<\>\,\;\:\\\/\"\[\]\a-z\A-Z\0-9]/;
    
    /*  file_name = document.getElementById("subheading").value;        		
    for (var i = 0; i < file_name.length; i++) {
    if (!file_name.charAt(i).match(iChars)) {
    alert ("Junk Character("+file_name.charAt(i)+") Present on the Blurb Please remove it");
    return false;
    }
    }*/
    file_name = document.getElementById("body").value;		
    for (var i = 0; i < file_name.length; i++)
    {
        if (!file_name.charAt(i).match(iChars))
        {
            alert ("Junk Character("+file_name.charAt(i)+") Present on the Body Please remove it");
            return false;
        }
    }


    if(document.addarticle.video_image[0].checked == true){
        //var usrFile = document.getElementById("userfile1").value;

        //var image_caption = document.getElementById("image_caption").value;
        //var photo_credit = document.getElementById("photo_credit").value;
        var usrImageSrc = document.getElementById("usrImageSrc").value;
        var usrFile = document.getElementById("userfile1").value;
        if(usrFile != "")
        {
            /*if(document.getElementById("useImage").checked == false){
            alert("Please select own image");
            return false;
            }*/
        }
        else
        {
            if(usrImageSrc == false)
            {
                alert("Image upload field is empty");
                document.getElementById("userfile1").focus();
                return false;
            }
        }
    }


    var limit ="2500";
    if(subheading == "" )
    {
        alert("Blurb field is empty");
        return false;
    }
    else
    {
        var strInputCode = tinyMCE.get('subheading').getContent();
        strInputCode = strInputCode.replace(/&(lt|gt);/g, function (strMatch, p1){
            return (p1 == "lt")?"<":">";
        });
        var strTagStrippedText = strInputCode.replace(/<\/?[^>]+(>|$)/g, "");
        var len  = strTagStrippedText.length

        if(len>limit )
        {
            alert("Blurb field  maximum allowed "+limit+" characters ");
            //document.getElementById("subheading").focus();
            return false;
        }
    }
    
    if(type == 'S')
    {
        var hiddenId = document.getElementById('hiddenId').value;
        document.getElementById('searchField').value = document.getElementById('as-values-'+hiddenId).value;
        return true;
    }
    else
        return true;

}


function openArgumentHome(articleId)
{
    //alert(portalLocation+'story/getStoryArguments.php?articleId='+articleId);
    $.get(portalLocation+'story/getStoryArguments.php?articleId='+articleId,function(data){
        
        var val = data.split('----');
        var redirectUrl = '';
        var encUser = $('#encUser').val();
        var strtTime = $('#strtTime').val();
        //alert(data);
        if(val[0] > 0)
        {
            var roomDetails = val[1].split('--||--');
            var roomId = roomDetails[1];
            var hostname = roomDetails[0];
            
            redirectUrl = tomcatUrl + '/enter_arg_pop.jsp?user='+encUser+'&roomid='+roomId+'&host='+hostname;
        }
        else
        {
            var articleTitle = $('#articleTitle'+articleId).val();
//alert(articleTitle);
            articleTitle = removeSpecialChars(articleTitle);
//alert(articleTitle);
            redirectUrl = tomcatUrl + "/room_process?mode=start_arg_pop&premise="+articleTitle+"&article_id="+articleId+"&topic_id=88&user="+encUser+"&dc="+strtTime+"&argduration=30&argumentdisc="+articleTitle+"&tmpPremise="+articleTitle;
        }
        window.open(redirectUrl, "Chat Window", "width=950, height=850,menubar=yes, location=yes, resizable=yes,scrollbars=yes,status=yes");
        //alert(redirectUrl);
        //frames['loadArgumentPage'].location.href = redirectUrl;
        //openArgumentWindow(redirectUrl);
        //setTimeout('closeArgumentLoading()', 10000);
    });
    
}

function openArgumentWindow(redirectUrl)
{
//alert(redirectUrl);
    window.open(redirectUrl, "Chat Window", "width=950, height=850,menubar=yes, location=yes, resizable=yes,scrollbars=yes,status=yes");
}

function closeArgumentLoading()
{
    //frames['loadArgumentPage'].location.href = 'http://www.argumentclinic.com/new/portal/plugin/loadingPage.php';
//window.location.reload();
}


function openArgumentStory(articleId)
{
   
    //alert(portalLocation+'story/getStoryArguments.php?articleId='+articleId);
    $.get(portalLocation+'story/getStoryArguments.php?articleId='+articleId,function(data){
        
        var val = data.split('----');
        var redirectUrl = '';
        var encUser = $('#encUser').val();
        var strtTime = $('#strtTime').val();
        //alert(data);
        if(val[0] > 0)
        {
            var roomDetails = val[1].split('--||--');
            var roomId = roomDetails[1];
            var hostname = roomDetails[0];
            
            redirectUrl = tomcatUrl + '/enter_arg_pop.jsp?user='+encUser+'&roomid='+roomId+'&host='+hostname;
        }
        else
        {
            var articleTitle = $('#articleTitle').val();
articleTitle = removeSpecialChars(articleTitle);
            redirectUrl = tomcatUrl + "/room_process?mode=start_arg_pop&premise="+articleTitle+"&article_id="+articleId+"&topic_id=88&user="+encUser+"&dc="+strtTime+"&argduration=30&argumentdisc="+articleTitle+"&tmpPremise="+articleTitle;
        }
        //alert(redirectUrl);
        //frames['loadArgumentPage'].location.href = redirectUrl;
        //openArgumentWindow(redirectUrl);
        //setTimeout('closeArgumentLoading()', 10000);
        window.open(redirectUrl, "Chat Window", "width=950, height=850,menubar=yes, location=yes, resizable=yes,scrollbars=yes,status=yes");
    });
}

function removeSpecialChars(str)
{
//alert(str);
var noSpcCharRep = /[^a-zA-Z0-9 `~_=]+/g;
str = str.replace(noSpcCharRep,'');
//alert(str);
return str;
}



