/* START show login window - available on all pages if not logged in */
var loginBoxVisible = false;
var loginBoxLoading = false;

function showLoginBox(referer){

    window.scrollTo(0,0);
	if(loginBoxLoading)
		return false;
	if(loginBoxVisible){
		new Effect.Appear('register-card');
		loginBoxLoading = true;
		new Effect.Fade('login-box',{
			duration: 1.0,
			afterFinish: function(){
				loginBoxVisible=false;
				loginBoxLoading = false;
				}
			});
		}
	else{
		new Effect.Fade('register-card');

		loginBoxLoading = true;
		new Effect.Appear('login-box',{
			duration: 2.0,
			afterFinish: function(){
				loginBoxVisible=true;
				loginBoxLoading = false;
                $('idLoginReferer').value=referer;
				}
			});
		}
	return false;
	}

/* END show logn window */

/* START validate login */
function validateLogin(){
	if($('username').value==""){
		$('username').className="inputBoxError";
		new Effect.Shake('username');
		return false;
		}
	if($('password').value==""){
		$('password').className="inputBoxError";
		new Effect.Shake('password');
		return false;
		}
	return true;
	}
/* END validate login */

/* START validate comment on movie page */
function validateComment(){
	if($('idInputTalkTurkey').value==""){
		$('idInputTalkTurkey').className="inputTalkTurkeyError";
		new Effect.Shake('idInputTalkTurkey');
		return false;
		}
	}
/* END validate comment on movie page */

/* START show login first popup if not logged in */
var loginFirstVisibile = false;
function loginFirst(id, width, height){
    var pos = Position.cumulativeOffset(id);
    $('loginPrompt').style.top = pos[1]+width+'px';
    $('loginPrompt').style.left = pos[0]-height+'px';
    $('main-wr').onclick = closeLoginFirst;
    Effect.Appear('loginPrompt', {
            duration: 0.2,
            afterFinish: function(){
                $('loginPrompt').style.display = 'block';
                loginFirstVisibile = true;
                }
            }
        );
    }
/* END show login first popup if not logged in */

var showAlreadyStarredVisibile = false;
///************************************************************
/// showAlreadyStarred - shows the popup if already starred the
/// comment for this movie
function showAlreadyStarred(id, width, height)
{                
    var pos = Position.cumulativeOffset(id);

    $('alreadyStarred').style.top = pos[1]+width+'px';
    $('alreadyStarred').style.left = pos[0]-height+'px';
    $('main-wr').onclick = closeAlreadyStarred;
                
    Effect.Appear('alreadyStarred', {
            duration: 0.2,
            afterFinish: function(){
                $('alreadyStarred').style.display = 'block';
                showAlreadyStarredVisibile = true;
                }
            }
        );      
}

var showCommentStarredVisibile = false;
///************************************************************
/// showAlreadyStarred - shows the popup if already starred the
/// comment for this movie
function showCommentStarred(id, width, height)
{                
    var pos = Position.cumulativeOffset(id);

    $('commentStarred').style.top = pos[1]+width+'px';
    $('commentStarred').style.left = pos[0]-height+'px';
    $('main-wr').onclick = closeAlreadyStarred;
                
    Effect.Appear('commentStarred', {
            duration: 0.2,
            afterFinish: function(){
                $('commentStarred').style.display = 'block';
                showCommentStarredVisibile = true;
            }
           }
    );
}



function closeAlreadyStarred(){
    $('alreadyStarred').style.display = 'none';
    }

function closeLoginFirst(){
    $('loginPrompt').style.display = 'none';
    }

/* START show Security Helper if not logged in */
var securityPopupVisible = false;
function securityPopup(id, width, height){
    var pos = Position.cumulativeOffset(id);
    $('idSecPrompt').style.top = pos[1]-width+'px';
    $('idSecPrompt').style.left = pos[0]-height+'px';
    $('main-wr').onclick = closeSecurityPopup;
    Effect.Appear('idSecPrompt', {
            duration: 0.2,
            afterFinish: function(){
                $('idSecPrompt').style.display = 'block';
                securityPopupVisible = true;
                }
            }
        );
    }
function closeSecurityPopup(){
    $('idSecPrompt').style.display = 'none';
    }
/* END show login first popup if not logged in */

/* START cast a vote when logged in */
function vote(movie_id, turkey, page, chart, rank) {
    var so = new SWFObject("flashPlayer.swf", "turkey-sounds", "12", "11", "7", "#FFFFFF");
    so.addVariable("autoPlay", "yes");
    so.addParam("wmode", "transparent");
    if(turkey==1)
        so.addVariable("soundPath", 'sounds/TURKEY-YES.mp3');
    else
        so.addVariable("soundPath", 'sounds/TURKEY-NO.mp3');
    so.write("flashPlayer");

	div_id = 'movie'+movie_id;
	new Ajax.Updater({success:div_id}, "ajax.php?main_page=vote&id="+movie_id+"&turkey="+turkey+"&page="+page+"&chart="+chart+"&rank="+rank, {
		onSuccess:function(){
            if(page == 'movie'){
                $('not-seen').style.display = 'none';
                }
			/*new Effect.Highlight(div_id);*/
			},
		on404:function(){
			if(turkey==1){
				var img_id = 'turkey'+movie_id;
				$(img_id).src = 'images/buttons/turkey.gif';
				}
			else{
				var img_id = 'not-turkey'+movie_id;
				$(img_id).src = 'images/buttons/not-turkey.gif';
				}
			alert('An error occured while processing your request. Please try again.\nIf this message re-appears, please notify the owner of the website.');
			},
		asynchronous:true,
		evalScripts:true
		});
	}
function switchImg(){
    }
function about_vote(turkey) {
    var so = new SWFObject("flashPlayer.swf", "turkey-sounds", "12", "11", "7", "#FFFFFF");
    so.addVariable("autoPlay", "yes");
    so.addParam("wmode", "transparent");
    if(turkey==1)
        so.addVariable("soundPath", 'sounds/TURKEY-YES.mp3');
    else
        so.addVariable("soundPath", 'sounds/TURKEY-NO.mp3');
    so.write("flashPlayer");

    new Ajax.Updater({success:'about-us-vote'}, "ajax.php?main_page=about_vote&turkey="+turkey, {
        onSuccess:function(){
            /*new Effect.Highlight(div_id);*/
            },
        on404:function(){
            if(turkey==1){
                var img_id = 'turkey'+movie_id;
                $(img_id).src = 'images/buttons/turkey.gif';
                }
            else{
                var img_id = 'not-turkey'+movie_id;
                $(img_id).src = 'images/buttons/not-turkey.gif';
                }
            alert('An error occured while processing your request. Please try again.\nIf this message re-appears, please notify the owner of the website.');
            },
        asynchronous:true,
        evalScripts:true
        });
    }
/* END cast a vote when logged in */

/* START Add Movie Page Validation */
function showSubmitButton(){
    if(shouldDisplaySubmitButton()){
        $('idInputSubmit').style.background = "url('images/bg-add-movie-submit.gif')";
        $('idSubmitDisabled').style.display = 'none';
        $('idSubmit').style.display = 'block';
        $('idError').style.display = 'none';
        }
    else{
        $('idInputSubmit').style.background = "";
        $('idSubmitDisabled').style.display = 'block';
        $('idSubmit').style.display = 'none';
        }
    }
function shouldDisplaySubmitButton(){
    return validateName(false) && validateYear(false) && validateTurkey(false);
    }
function validateName(display){
    if($('idName').value==''){
        if(display){
            $('idNameError').style.backgroundColor="#CC0000";
            $('idError').style.display = 'block';
            new Effect.Shake('idNameError');
            }
        return false;
        }
    else{
        if(display){
            $('idNameError').style.backgroundColor="";
            showSubmitButton();
            }
        return true;
        }
    }
function checkDuplicate(value){
    if(value!=""){
	new Ajax.Request ("ajax.php?main_page=check_movie_duplicate&movie_title="+value, { onSuccess: 
		function(t) {
			if (t.responseText != "error")  {
			        new Ajax.Updater({success:'movieNameDuplicateListings'}, "ajax.php?main_page=check_movie_duplicate&movie_title="+value, {
			            onSuccess:function(){
			                var pos = Position.cumulativeOffset($('idName'));     
			        	$('movieNameDuplicate').style.top = pos[1]+23+'px';
			         	$('movieNameDuplicate').style.left = pos[0]+0+'px';
			               	$('movieNameDuplicate').style.display = "block";      
			            },
			            on404:function(){    
			                },
			            asynchronous:true,
			            evalScripts:true
			            });



			}
		} 
	});
        }
    }
function validateYear(display){
    var error = false;
    if($('idYear').value!=''){
        dteDate=new Date($('idYear').value,0,1);
        if($('idYear').value!=dteDate.getFullYear()){
            error=true;
            }
        }
    if(error == true){
        if(display){
            $('idYearError').style.backgroundColor = "#CC0000";
            $('idError').style.display = 'block';
            new Effect.Shake('idYearError');
            }
        return false;
        }
    else{
        if(display){
            $('idYearError').style.backgroundColor = "";
            showSubmitButton();
            }
        return true;
        }
    }
function validateTurkey(display){
    if($('idTurkey').value=='' || $('idTurkey').value=='-1'){
        if(display){
            $('idError').style.display = 'block';
            new Effect.Shake('idVotes');
            }
        return false;
        }
    else{
        if(display){
            showSubmitButton();
            }
        return true;
        }
    }
function markVote(turkey){

    var so = new SWFObject("flashPlayer.swf", "turkey-sounds", "12", "11", "7", "#FFFFFF");
    so.addVariable("autoPlay", "yes");
    so.addParam("wmode", "transparent");
    if(turkey==1)
        so.addVariable("soundPath", 'sounds/TURKEY-YES.mp3');
    else
        so.addVariable("soundPath", 'sounds/TURKEY-NO.mp3');
    so.write("flashPlayer");

	$('idTurkey').value=turkey;
	if(turkey==1){
		$('imgTurkey').src="images/buttons/trans-turkey-pressed.gif";
		$('imgNotTurkey').src="images/buttons/trans-not-turkey-disabled.gif";
		}
	if(turkey==0){
		$('imgTurkey').src="images/buttons/trans-turkey-disabled.gif";
		$('imgNotTurkey').src="images/buttons/trans-not-turkey-pressed.gif";
		}
	$('imgTurkey').onclick=function(){};
	$('imgTurkey').onmousedown=function(){};
	$('imgTurkey').onmouseout=function(){};
	$('imgNotTurkey').onclick=function(){};
	$('imgNotTurkey').onmousedown=function(){};
	$('imgNotTurkey').onmouseout=function(){};
    showSubmitButton();
	}

function validateAddMovie(display){
    if(!validateName(display))
        return false;
    if(!validateYear(display))
        return false;
    if(!validateTurkey(display))
        return false;
    $('idError').style.display = 'none';
	return true;
	}
/* END Add Movie Page Validation */

/* START Register page */
function showSubmitButtonRegister(){
    if(shouldDisplaySubmitButtonRegister()){
        $('idInputSubmit').style.background = "url('images/bg-register-submit.gif')";
        $('idSubmitDisabled').style.display = 'none';
        $('idSubmit').style.display = 'block';
        }
    else{
        $('idInputSubmit').style.background = "";
        $('idSubmitDisabled').style.display = 'block';
        $('idSubmit').style.display = 'none';
        }
    }
function shouldDisplaySubmitButtonRegister(){
    return validateUsername(false) && validateEmail(false) && validatePassword(false) && validateConfirmPassword(false) && validateAcceptTerms(false) && validateSecurityCodeRegister(false);
    }
function checkUsername(username){
    new Ajax.Request("ajax.php?main_page=check_username&username="+username, {
        onSuccess:function(){
            isUsernameTaken = false;
            },
        on404:function(){
            isUsernameTaken = true;
            },
        asynchronous:false
        });
    }
function checkEmail(email){
    new Ajax.Request("ajax.php?main_page=check_email&email="+email, {
        onSuccess:function(){
            isEmailTaken = false;
            },
        on404:function(){
            isEmailTaken = true;
            },
        asynchronous:false
        });
    }
var isUsernameTaken = false;
var isEmailTaken = false;
function validateUsername(display){
    if($('idUsername').value==''){
        if(display){
            $('idUsernameBubble').innerHTML="Choose username!";
            $('idUsernameBubble').style.display="block";
            $('idUsernameFeedback').style.display="none";
            showSubmitButtonRegister();
            //$('idUsername').focus();
            }
        return false;
        }
    else{
        checkUsername($('idUsername').value);
        if(isUsernameTaken){
            if(display){
                $('idUsernameBubble').innerHTML="Already taken... try again";
                $('idUsernameBubble').style.display="block";
                $('idUsernameFeedback').style.display="none";
                showSubmitButtonRegister();
                //$('idUsername').focus();
                }
            return false;
            }
        else{
            if(display){
                $('idUsernameBubble').style.display="none";
                $('idUsernameFeedback').style.display="block";
                showSubmitButtonRegister();
                }
            return true;
            }
        }
    }
function validateEmail(display){
    var x = $('idEmail').value;
    if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(x)){
        checkEmail($('idEmail').value);
        if(isEmailTaken){
            if(display){
                $('idEmailBubble').innerHTML="Already registered";
                $('idEmailBubble').style.display="block";
                $('idEmailFeedback').style.display="none";
                showSubmitButtonRegister();
                }
            return false;
            }
        else{
            if(display){
                $('idEmailBubble').style.display="none";
                $('idEmailFeedback').style.display="block";
                showSubmitButtonRegister();
                }
            return true;
            }
        }
    else{
        if(display){
            $('idEmailBubble').innerHTML="This is not an e-mail";
            $('idEmailBubble').style.display="block";
            $('idEmailFeedback').style.display="none";
            showSubmitButtonRegister();
            //$('idEmail').focus();
            }
        return false;
        }
    }
function validatePassword(display){
    var strong = false;
    var passwd = $('idPassword').value;
    if (passwd.length>=3){
        strong = true;
        }
    if(strong){
        if(display){
            $('idPasswordBubble').style.display="none";
            $('idPasswordFeedback').style.display="block";
            showSubmitButtonRegister();
            }
        return true;
        }
    else{
        if(display){
            $('idPasswordBubble').style.display="block";
            $('idPasswordFeedback').style.display="none";
            showSubmitButtonRegister();
            //$('idPassword').focus();
            }
        return false;
        }
    }
function validateConfirmPassword(display){
    if($('idConfirmPassword').value!=$('idPassword').value){
        if(display){
            $('idConfirmPasswordBubble').style.display="block";
            showSubmitButtonRegister();
            //$('idConfirmPassword').focus();
            }
        return false;
        }
    else{
        if(display){
            $('idConfirmPasswordBubble').style.display="none";
            showSubmitButtonRegister();
            }
        return true;
        }
    }
function validateSecurityCodeRegister(display){
    if($('idSecurityCode').value=='' || $('idSecurityCode').value.length<6 ){
        if(display){
            $('idSecurityCodeError').style.backgroundColor="#CC0000";
            new Effect.Shake('idSecurityCodeError');
            }
        return false;
        }
    else{
        if(display){
            $('idSecurityCodeError').style.backgroundColor = "";
            showSubmitButtonRegister();
            }
        return true;
        }
    }
function validateAcceptTerms(display){
    if(!$('idAcceptTerms').checked){
        if(display){
            $('idAcceptTermsBubble').style.display="block";
            showSubmitButtonRegister();
            //$('idAcceptTerms').focus();
            }
        return false;
        }
    else{
        if(display){
            $('idAcceptTermsBubble').style.display="none";
            showSubmitButtonRegister();
            }
        return true;
        }
    }

function validateRegister(display){
    if(!validateUsername(display))
        return false;
    if(!validateEmail(display))
        return false;
    if(!validatePassword(display))
        return false;
    if(!validateConfirmPassword(display))
        return false;
    if(!validateAcceptTerms(display))
        return false;
    if(!validateSecurityCodeRegister(display))
        return false;
    return true;
    }
/* END Register page */

/* START Profile page validation */
function showSubmitButtonProfile(){
    if(shouldDisplaySubmitButtonProfile()){
        $('idInputSubmit').style.background = "url('images/bg-profile-submit.gif')";
        $('idSubmitDisabled').style.display = 'none';
        $('idSubmit').style.display = 'block';
        }
    else{
        $('idInputSubmit').style.background = "";
        $('idSubmitDisabled').style.display = 'block';
        $('idSubmit').style.display = 'none';
        }
    }
function shouldDisplaySubmitButtonProfile(){
    return validateEmailProfile(false) && validatePasswordProfile(false) && validateConfirmPasswordProfile(false);
///	return validateEmailProfile(false);
    }
function validateEmailProfile(display){
    var x = $('idEmail').value;
    if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(x)){
        if(display){
            $('idEmailBubble').style.display="none";
            showSubmitButtonProfile();
            }
        return true;
        }
    else{
        if(display){
            $('idEmailBubble').style.display="block";
            showSubmitButtonProfile();
            //$('idEmail').focus();
            }
        return false;
        }
    }
function validatePasswordProfile(display){
    var strong = false;
    var passwd = $('idPassword').value;
    if(passwd=="")
        return true;
    if (passwd.length>=3) {//6 && (passwd.match(/[a-z]/) || passwd.match(/[A-Z]/)) && passwd.match(/\d+/)){
        strong = true;
        }
    if(strong){
        if(display){
            $('idPasswordBubble').style.display="none";
            $('idPasswordFeedback').style.display="block";
            showSubmitButtonProfile();
            }
        return true;
        }
    else{
        if(display){
            $('idPasswordBubble').style.display="block";
            $('idPasswordFeedback').style.display="none";
            showSubmitButtonProfile();
            //$('idPassword').focus();
            }
        return false;
        }
    }
function validateConfirmPasswordProfile(display){
    if($('idConfirmPassword').value!=$('idPassword').value){
        if(display){
            $('idConfirmPasswordBubble').style.display="block";
            showSubmitButtonProfile();
            //$('idConfirmPassword').focus();
            }
        return false;
        }
    else{
        if(display){
            $('idConfirmPasswordBubble').style.display="none";
            showSubmitButtonProfile();
            }
        return true;
        }
    }
function validateProfile(display){
    if(!validateEmailProfile(display))
        return false;
    if(!validatePasswordProfile(display))
        return false;
    if(!validateConfirmPasswordProfile(display))
        return false;
    return true;
    }

function validatePassProfile(display){
    if(!validatePasswordProfile(display))
        return false;
    if(!validateConfirmPasswordProfile(display))
        return false;

    return true;
    }
/* END Profile page validation */

/* START Contact Us page validation */
function validateContactName(display){
    if($('idName').value=='' ){
        if(display){
            $('idNameBubble').style.display="block";
            //$('idName').focus();
            }
        return false;
        }
    else{
        if(display){
            $('idNameBubble').style.display="none";
            }
        return true;
        }
    }
function validateContactEmail(display){
    var x = $('idEmail').value;
    if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(x)){
        if(display){
            $('idEmailBubble').style.display="none";
            }
        return true;
        }
    else{
        if(display){
            $('idEmailBubble').style.display="block";
            //$('idEmail').focus();
            }
        return false;
        }
    }
function validateContactComments(display){
    if($('idComments').value=='' ){
        if(display){
            $('idCommentsBubble').style.display="block";
            //$('idComments').focus();
            }
        return false;
        }
    else{
        if(display){
            $('idCommentsBubble').style.display="none";
            }
        return true;
        }
    }
function validateContact(display){
    if(!validateContactName(display))
        return false;
    if(!validateContactEmail(display))
        return false;
    if(!validateContactComments(display))
        return false;
    return true;
    }
/* END Profile page validation */

/* START comment report */
function report_comment_abuse(comment_id, user_id) {
    div_id = 'abuse-comment-' + comment_id;
    new Ajax.Updater({success:div_id}, "ajax.php?main_page=comment_abuse&comment_id="+comment_id+"&user_id="+user_id, {
        onSuccess:function(){
            /*new Effect.Highlight(div_id);*/
            },
        on404:function(){
            alert('An error occured while processing your request. Please try again.\nIf this message re-appears, please notify the owner of the website.');
            },
        asynchronous:true
        });
    }
/* END comment report */
function show_abuse(id){
    var pos = Position.cumulativeOffset($(id));
    $('movie-abuse').style.top = pos[1]-320+'px';
    $('movie-abuse').style.left = pos[0]-200+'px';
    Effect.Appear('movie-abuse', {
            duration: 0.2,
            afterFinish: function(){
                $('movie-abuse').style.display = 'block';
                }
            }
        );
    }
function report_movie_abuse(movie_id){
    div_id = "abuse-movie-" + movie_id;
    $('movie-abuse').style.display='none';
    new Ajax.Updater ( {success:div_id}, "ajax.php?main_page=movie_abuse", {
        onSuccess:function(){
            /*new Effect.Highlight(div_id);*/
            },
        on404:function(){
            alert('An error occured while processing your request. Please try again.\nIf this message re-appears, please notify the owner of the website.');
            },
        postBody: Form.serialize( 'idFrmMovieAbuse'),
        asynchronous:true
        } );
    }
function show_forgotten_form(){
    new Ajax.Updater ( {success:'login-box'}, "ajax.php?main_page=forgotten_form", {
        onSuccess:function(){
            },
        on404:function(){
            alert('An error occured while processing your request. Please try again.\nIf this message re-appears, please notify the owner of the website.');
            },
        asynchronous:true
        } );
    }
function submit_forgotten_form(){
    new Ajax.Updater ( {success:'login-box'}, "ajax.php?main_page=forgotten_form", {
        onSuccess:function(){
            },
        on404:function(){
            alert('An error occured while processing your request. Please try again.\nIf this message re-appears, please notify the owner of the website.');
            },
        postBody: Form.serialize( 'idFrmForgottenForm'),
        asynchronous:true
        } );
    }
function show_login_box(){
    new Ajax.Updater ( {success:'login-box'}, "ajax.php?main_page=login_box", {
        onSuccess:function(){
            },
        on404:function(){
            alert('An error occured while processing your request. Please try again.\nIf this message re-appears, please notify the owner of the website.');
            },
        asynchronous:true
        } );
    }
function add_to_want2see(movie_id, user_id){
    div_id = 'want2see-' + movie_id;
    new Ajax.Updater({success:div_id}, "ajax.php?main_page=add_to_want2see&movie_id="+movie_id+"&user_id="+user_id, {
            onSuccess:function(){
                $('want2see').style.display = 'none';
                },
            on404:function(){
                alert('An error occured while processing your request. Please try again.\nIf this message re-appears, please notify the owner of the website.');
                },
            asynchronous:true
            });
    }
function remove_from_want2see(user_id, movie_id){
    new Ajax.Request("ajax.php?main_page=remove&user_id="+user_id+"&movie_id="+movie_id, {
        onSuccess:function(){
            document.location = 'index.php?main_page=history&order=want2see';
            },
        on404:function(){
            alert('An error occured while processing your request. Please try again.\nIf this message re-appears, please notify the owner of the website.');
            },
        asynchronous:false,
        evalScripts:true
        });
    }

