﻿
var Doresize = false;

function ShowModal() {
    
    var m = document.getElementById('Modal');
    if(m)
    {
        
        m.style.display = 'block';
        var o = document.getElementById('Overlay');
        o.style.display = 'block';
        ResizeTheModal();
    }
    
}

function ResizeTheModal()
{
    var m = document.getElementById('Modal');
    var theWM = document.getElementById('wm');
        if (theWM.offsetHeight < 400)
            theWM.style.height = '400px'
        //POSITION
        var x = ((document.body.offsetWidth / 2) - (m.offsetWidth / 2));
        if (x < 0) x = 0;
        
        var y = ((document.body.parentNode.offsetHeight / 2) - (m.offsetHeight / 2));
        if (y < 0) y = 0;

        
        m.style.left = x + 'px';
        m.style.top = y + 'px';
        
        
        ResizePopUp();
        
        //Show the Overlay
        var o = document.getElementById('Overlay');
        
        o.style.top = '-20px';
        o.style.left = '0px';
        o.style.width = document.body.offsetWidth + 'px';
    //    if (document.all)
    //        o.style.height = (document.body.scrollHeight > document.body.parentNode.offsetHeight ? document.body.scrollHeight : document.body.parentNode.offsetHeight) + 20 + 'px';
    //    else
            o.style.height = getPageSizeWithScroll() + 20 + 'px';

}

function ResizePopUp()
{
          
     var wm = document.getElementById('wm');
     
     if(Doresize==true)
        {
            wm.style.height='290px';
        }
     else
     {
        wm.style.height='';
     }
     Doresize=false;
    
}

function CloseModal() {
    
    var m = document.getElementById('Modal');
    m.style.display = 'none';
    var o = document.getElementById('Overlay');
    o.style.display = 'none';
    
    var mainInsertDiv = $get('newStMain');
    if(mainInsertDiv)
    {
        var els = mainInsertDiv.getElementsByTagName('INPUT','text');
        EmptyControlValues(els);
        
        els = mainInsertDiv.getElementsByTagName('INPUT','checkbox');
        EmptyControlValues(els);
        
        
        els = mainInsertDiv.getElementsByTagName('TEXTAREA');
        EmptyControlValues(els);
        
        els = mainInsertDiv.getElementsByTagName('SELECT');
        EmptyControlValues(els);
        
    }
    Doresize=false;
   
}

function EmptyControlValues(controls,type)
{
    for (j=0; j < controls.length; j++)
    {
        if(type)
        {
            if(controls[j].type==type)
            {
                if(controls[j].type=='checkbox')
                    controls[j].checked='';
                else if(type=='radio' && controls[j].value=='0')
                    controls[j].checked='checked';
                else
                    controls[j].value='';
            }
        }
        else
        {
            controls[j].value='';
        }
    }
}

function getPageSizeWithScroll(){
	if (window.innerHeight && window.scrollMaxY) {// Firefox
		yWithScroll = window.innerHeight + window.scrollMaxY;
		xWithScroll = window.innerWidth + window.scrollMaxX;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		yWithScroll = document.body.scrollHeight;
		xWithScroll = document.body.scrollWidth;
	} else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari
		yWithScroll = document.body.offsetHeight;
		xWithScroll = document.body.offsetWidth;
  	}
	arrayPageSizeWithScroll = yWithScroll;
	//alert( 'The height is ' + yWithScroll + ' and the width is ' + xWithScroll );
	return arrayPageSizeWithScroll;
}


/*###############################################################*/
var stars;
    function checkEvent(element, evt)
	{	
		if (navigator.appName.toLowerCase().indexOf("explorer") > 1)
			return !element.contains(evt.toElement);
		else if (evt.relatedTarget)
			return !containsDOM(element,evt.relatedTarget);	
	}
	
	 function containsDOM (container, containee)
	{
		var isParent = false;
		do
		{
			if ((isParent = container == containee))
				break;
			containee = containee.parentNode;
		}
		while (containee != null);
			return isParent;
	}
    function ShowMail()
	{
      if(canSubmit()==false)
        return;
      $get('divEmail').style.display='';
      $get('divtxtEmail').style.display='';
      $get('VoteWarningTR').style.display='';
      $get('divbtnSubmitHolder').style.display='';
      $get('divVotesCountHolder').style.display='none';
      $get('divVotesLiteralHolder').style.display='none';
      $get('divStarsCountHolder').style.display='none';
      $get('divStarsCountLIteralHolder').style.display='none';
      
	}
	
	function HideEmail(starsonly)
	{
	    
       if(!$get('vtMain')) return;
       if(starsonly && starsonly==false)
       {
           $get('divEmail').style.display='none';
           $get('divtxtEmail').style.display='none';
           $get('VoteWarningTR').style.display='none';
           $get('divbtnSubmitHolder').style.display='none';
           $get('divVotesCountHolder').style.display='';
           $get('divVotesLiteralHolder').style.display='';
       }
       $get('divStarsCountHolder').style.display='none';
       $get('divStarsCountLIteralHolder').style.display='none';
	   
	}
	
function ShowStars(el)
    {
        
        if(canSubmit()==false || MailErrorRaised==true)
            return
        var currentStar = parseInt(el.id.replace('s',''));
        starsholder = $get(starsholderID);
        stars = starsholder.getElementsByTagName('IMG');
       
        for(j=0; j<stars.length;j++)
        {
            if(j<=currentStar)
                    stars[j].src = CorrectImage('images/star_ON.png');
            else
                    stars[j].src = CorrectImage('images/star.png');
        }
        
        var StarsC = $get('divStarsCountHolder');
        StarsC.innerHTML = 'Ψηφίστε με <b>'+  parseInt(currentStar + 1) + '</b>';
        var StarsL = $get('divStarsCountLIteralHolder');
        if(currentStar+1>0 && currentStar+1<2)
            StarsL.innerHTML = ' αστεράκι';
        else if(currentStar>0 && currentStar>=2)
             StarsL.innerHTML = ' αστεράκια';
        else    
            StarsL.innerHTML = ' αστεράκια';
            
       StarsC.style.display='';
       StarsL.style.display='';
       
       $get('divVotesCountHolder').style.display = 'none';
       $get('divVotesLiteralHolder').style.display = 'none';
       
       
    }
    
    function CorrectImage(src)
    {
        var arVersion = navigator.appVersion.split("MSIE");
        var version = parseFloat(arVersion[1]);
        
        if ((version >= 5.5 && version < 7) && document && document.body && (document.body.filters)) 
            src = src.replace('.png','.gif');
        
        return src;
    }
    
    function ResetStars(el,evt)
    {
        if( MailErrorRaised==true)return;
        if(el && evt && checkEvent(el,evt))
        {
            LoadStars();
            $get('divVotesCountHolder').style.display = '';
            $get('divVotesLiteralHolder').style.display = '';
           
        }
        
    }
    
    function LoadStars()
    {
            
            starsholder = $get(starsholderID);
            if (!starsholder) return
            stars = starsholder.getElementsByTagName('IMG');
            var StarsCount = parseInt( $get(txtStarsID).value);
            
            if(StarsCount>0)
            {
                for(j=0; j<stars.length;j++)
                {
                    if ((j+1)<=StarsCount)
                    {
                        
                        var star = $get('s'+j);
                        if(star)
                            star.src=CorrectImage('images/star_ON.png');
                    }
                    else
                    {
                        var star = $get('s'+j);
                        if(star)
                            star.src=CorrectImage('images/star.png');
                    }
                }
            }
             HideEmail(true);
    }
    
    function SubmitStars(el)
    {
        
        if(canSubmit()==false ||  MailErrorRaised==true)
            return
        starsholder.onmouseout = function anon(){}
        el.src='images/star_ON.png';
        el.onmouseout = function anon(){}
        el.onmouseover = function anon(){}
        for(j=0; j<stars.length;j++)
        {
          $get('s'+j).onmouseout = function anon(){}
          $get('s'+j).onmouseover = function anon(){}
        }
        $get(txtStarsID).value = el.id.replace('s','');
        
        ShowMail();
    }
    
    function canSubmit()
    {
        var res = true
        if($get(txtHasVotedID).value==-1)
            res = false;
        return res;
    }
    
     function valcanSubmit(sender,args)
    {
        args.IsValid = canSubmit();
    }
    
      function ValidateTerms(sender,args)
    {
        var ck = document.getElementById(cktermsID);
        if (ck.checked)
        {
            args.IsValid=true;
        }
        else  
        {  
            args.IsValid=false;
        }
    }

    
    function ValidateVoteEmail(sender, args){
        if (args.Value.length == 0){
            $get('spanEmailHead').style.color = 'red';
            args.IsValid = false;
        }
        else if (!new RegExp("\\w+([-+.']\w+)*@\\w+([-.]\w+)*\\.\\w+([-.]\\w+)*").test(args.Value)){
            $get('spanEmailHead').style.color = 'red';
            args.IsValid = false;
        }
        else{
            $get('spanEmailHead').style.color = 'black';
            args.IsValid = true;
        }
        
        //\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
    }
    
    
    //Story Textare functions
    function ValidateStoryLength(sender,args)
    {   
        args.IsValid=false;
        
        //GET THE TEXTAREA HERE
        var txts = document.getElementsByTagName('TEXTAREA');
        var storyTxt;
        for (i=0;i<txts.length;i++){
            if (txts[i].id.toLowerCase().indexOf('story') > -1){
                storyTxt = txts[i];
                break;
            }
        }
        
        var counter = countWords(storyTxt);
        if(counter>=50 && counter<=250)
            args.IsValid=true;
    
    }
    
    
    function countWords(obj){
        var val = obj.value;
        val = val.replace(/\s+/g, " ");
        var words = val.split(' ').length - 1 + val.split('\r').length;
        document.getElementById('spanWordCount').innerHTML = '<i>' + words + ' λέξεις</i>';
        return words;
    }
var previousMailText;
var MailErrorRaised=false;

function ShowExistingEmailError(isAuthor)
{
    var el = $get('spanEmailHead');
    if(el)
    {
        previousMailText = el.innerText;
        el.innerHTML = 'Έχετε ήδη ψηφίσει αυτή την ιστορία!';
        if(isAuthor)
            el.innerHTML = 'Δέν μπορείτε να ψηφίσετε αυτή την ιστορία!';
        el.style.color = 'red';
        
        MailErrorRaised=true;
        $get('divEmail').style.display='';
        $get('divbtnSubmitHolder').style.display='';
        $get('divtxtEmail').style.display='';
        $get('VoteWarningTR').style.display='';
        
        $get('divVotesCountHolder').style.display='none';
        $get('divVotesLiteralHolder').style.display='none';
        
        CancelVote();
        setTimeout("ResetVoteStatus()",3000);
    }
    
}

function CancelVote(){
    $get('tdThankYou').style.display='none';
    //$get('divEmail').style.display='none';
    $get('VoteWarningTR').style.display='none';
    $get('divtxtEmail').style.display='none';
    $get('divbtnSubmitHolder').style.display='none';
    $get('divVotesCountHolder').style.display='none';
    $get('divVotesLiteralHolder').style.display='none';
    $get('divStarsCountHolder').style.display='none';
    $get('divStarsCountLIteralHolder').style.display='none';
    
    setTimeout("$('#spanEmailHead').fadeTo('slow',0);",2000);
}

function ResetVoteStatus(){
    $get('divbtnSubmitHolder').style.display='none';
    $get('divVotesCountHolder').style.display='none';
    $get('divVotesLiteralHolder').style.display='none';
    $get('divEmail').style.display='none';
    $get('divVotesCountHolder').style.display = '';
    $get('divVotesLiteralHolder').style.display = '';
}

function ResetExistingEmailError()
{
    if (MailErrorRaised==true)
        var el = $get('spanEmailHead');
        if(el)
        {
            el.style.display = '';
            el.style.opacity = '1';
            if (document.all) el.filters.alpha.opacity=100;
            el.innerText = previousMailText;
            el.style.color = '#000';
        }
}


function ShowThanksForVoting(){
    var el = $get('tdThankYou');
    if(el)
    {
        el.style.display='';
        $get('divbtnSubmitHolder').style.display='none';
        $get('divVotesCountHolder').style.display='none';
        $get('divVotesLiteralHolder').style.display='none';
        setTimeout("FadeToBlack();",2000);
        $get('divEmail').style.display='none';
        MailErrorRaised=false;
      
    }
}
function FadeToBlack()
{
    $('#spanThankYouForVoting').fadeTo('slow',0);
    $get('tdThankYou').style.display='none';
    $get('divVotesCountHolder').style.display = '';
    $get('divVotesLiteralHolder').style.display = '';
}

function gotoTop()
{
    document.location.href='#top';
}
var win;
function openWin(url)
{
    win=window.open(url,'','width=500,height=500,scrollbars=1');
    return false;
}

function SwapPhotoTerms(){
    if (document.getElementById('divTerms').style.display == 'none'){
        document.getElementById('divTerms').style.display = '';
        document.getElementById('newStTerms').style.height='238px';
    }
    else{
        document.getElementById('divTerms').style.display = 'none';
        document.getElementById('newStTerms').style.height='27px';
    }
}

 var arVersion = navigator.appVersion.split("MSIE")
 var version = parseFloat(arVersion[1])
 
 if ((version > 6) || (!document.all))
    window.onresize=ResizeTheModal;