﻿// JScript File
var topPosition,validMainDestination;

//Save scrolling position on postback
function SaveScrollPos()
{
    document.getElementById("ScrollPos").value=document.body.scrollTop;
}

function CheckScrollPos()
{
    document.body.scrollTop=document.getElementById("ScrollPos").value;
}

//Repositions Information Strength whenever the page is scrolled
function ScrollIS()
{
    //Removing header height
    if(document.body.scrollTop > 75)
        topPosition=document.body.scrollTop-75;        
    else
        topPosition=0;
    
    if(parseInt(topPosition) < parseInt(document.getElementById("DivInformationStrength").style.top))
    {
        MoveUp();
    }        
    else
    {
       MoveDown();
    } 
}

function MoveDown()
{    
    pos=document.getElementById("DivInformationStrength").style.top;
    if(parseInt(pos) <= topPosition && parseInt(pos) <= (document.body.scrollHeight - 1000))
    {		
		pos=parseInt(pos)+1;
		document.getElementById("DivInformationStrength").style.top=pos;
		setTimeout("MoveDown()",1);
	}
	else
	    document.getElementById("DivInformationStrength").style.top=pos;		
}

function MoveUp()
{    
    pos=document.getElementById("DivInformationStrength").style.top;
    if(parseInt(pos) >= topPosition)
    {		
		pos=parseInt(pos)-1;
		document.getElementById("DivInformationStrength").style.top=pos;
		setTimeout("MoveUp()",1);
	}
	else
	    document.getElementById("DivInformationStrength").style.top=pos;	
}

//Function to set the position of AutoCompleteExtender
function ResetPosition(object,args)
{
    var tb = object._element;	
    var tbposition = findPositionWithScrolling(tb);
	var xposition = tbposition[0];	// - 40
	var yposition = tbposition[1] + 25;	// 25 = textbox height + a few pixels spacing

	var ex = object._completionListElement;	if (ex)
	{		$common.setLocation(ex, new Sys.UI.Point(xposition, yposition));

	}
}

function findPositionWithScrolling( oElement )
{	if( typeof( oElement.offsetParent ) != 'undefined' )
	{		
	    var originalElement = oElement;
		for( var posX = 0, posY = 0; oElement; oElement = oElement.offsetParent )
		{			
		    posX += oElement.offsetLeft;
		    posY += oElement.offsetTop;
		    if( oElement != originalElement && oElement != document.body && oElement != document.documentElement )
			{
				posX -= oElement.scrollLeft;
				posY -= oElement.scrollTop;
			}
		}
		return [ posX, posY ];
	} 
	else 
	{
	return [ oElement.x, oElement.y ];
    }
}

//Function to calculate Destination Strength
function CheckDestinationStrength()
{
    if(document.getElementById("ExistingDestinations"))
    allDestinations=document.getElementById("ExistingDestinations").value.toLowerCase();
    if(document.getElementById("MainDestination"))
    {
        destinationEntered=document.getElementById("MainDestination").value.toLowerCase();
        if(destinationEntered != "")
        {
            //calculate the number of time it has appeard before
            count = 0;
            pos = allDestinations.indexOf("$"+destinationEntered+"$");
            while ( pos != -1 ) 
            {
                count++;
                pos = allDestinations.indexOf("$"+destinationEntered+"$",pos+1);        
            }    
            if(count > 4)
            {
                FillColor1(0);      
                validMainDestination=false;        
                document.getElementById("CustomValidator2").style.visibility="visible";  
                document.getElementById("CustomValidator2").style.display="block";  
            }
            else
            {
                validMainDestination=true;
                document.getElementById("CustomValidator2").style.visibility="hidden";        
                document.getElementById("CustomValidator2").style.display="none";  
                FillColor1(5-count);
                //Store DS
                document.getElementById("HiddenDS").value=5-count;
            }
        }
    }
}

function FillColor1(x)
{
    var boxColors=new Array("#fecfb0","#feb787","#fea061","#fd8434","#ff6600");
    //clear all cells
    for(i=0;i<5;i++)
    {
        varTD="document.getElementById('tdDestinationStrength"+i+"')";
        eval(varTD).style.backgroundColor="#ffffff";
    }   
    //Fill in colors
    for(i=0;i<x;i++)
    {
        varTD="document.getElementById('tdDestinationStrength"+i+"')";
        eval(varTD).style.backgroundColor=boxColors[i];
        eval(varTD).title=x;
    }   
}

function CheckMainDestination(source,args)
{
    if(!validMainDestination)
        args.IsValid=false;
}

//Clear Textareas
function ClearTextArea(x)
{
    varTextArea="document.getElementById('GetForm1_textarea"+x+"')";
    if(eval(varTextArea).value == "Text Area: Click to Begin typing - You can choose to Ignore / not fill it too")
        eval(varTextArea).value = '';
}

//Word Count
function CalWordCount(x)
{
    DisplayWordCount();
}

function DisplayWordCount()
{
    totalCount=0;
    for(i=1;i<=17;i++)
    {
        varTextArea="document.getElementById('GetForm1_textarea"+i+"')";
        if(eval(varTextArea).value != "Text Area: Click to Begin typing - You can choose to Ignore / not fill it too")
        {
            matter=eval(varTextArea).value;
            matter=matter.split(" ");
            thisCount=matter.length-1;    
            totalCount=totalCount+thisCount;
        }
    }
    document.getElementById("ShowWordCount").innerHTML=totalCount;
    document.getElementById("HiddenWordCount").value=totalCount;
    CheckInformationStrength1(totalCount);
    CheckInformationStrength2();  
    CheckInformationStrength3();      
}

function ShowSavedWordCount()
{
    document.getElementById("ShowWordCount").innerHTML=document.getElementById("HiddenWordCount").value;
    FillColor2(document.getElementById("HiddenIS").value);    
}

var InformationStrength=0;
var AddFactfile1=false;
var AddFactfile2=false;
var AddWordCount1=false;
var AddWordCount2=false;
var AddWordCount3=false;
var AddPictures1=false;
var AddPictures2=false;


function CheckInformationStrength1(totalCount)
{
    //check if the strength has increased/decreased
    if(totalCount > 1500 && AddWordCount1 == false)
    {
        MoreInformationStrength(1);
        AddWordCount1=true;
    }
    else if(totalCount < 1500 && AddWordCount1 == true)
    {
        LessInformationStrength(1);
        AddWordCount1=false;
    }
    if(totalCount > 2000 && AddWordCount2 == false)
    {
        MoreInformationStrength(1);
        AddWordCount2=true;
    }
    else if(totalCount < 2000 && AddWordCount2 == true)
    {
        LessInformationStrength(1);
        AddWordCount2=false;
    }
    if(totalCount > 2400 && AddWordCount3 == false)
    {
        MoreInformationStrength(1);
        AddWordCount3=true;
    }
    else if(totalCount < 2400 && AddWordCount3 == true)
    {
        LessInformationStrength(1);
        AddWordCount3=false;
    }        
}

function CheckInformationStrength2()
{
    if(document.getElementById("GetForm1_textarea4").value != "" && document.getElementById("GetForm1_textarea4").value != "Text Area: Click to Begin typing - You can choose to Ignore / not fill it too" && AddFactfile1 == false)
    {
        MoreInformationStrength(2);
        AddFactfile1=true;
    }
    else if(document.getElementById("GetForm1_textarea4").value == "" && document.getElementById("GetForm1_textarea4").value != "Text Area: Click to Begin typing - You can choose to Ignore / not fill it too" && AddFactfile1 == true)
    {
        LessInformationStrength(2);
        AddFactfile1=false;
    }
    if(document.getElementById("GetForm1_textarea9").value != "" && document.getElementById("GetForm1_textarea9").value != "Text Area: Click to Begin typing - You can choose to Ignore / not fill it too" && AddFactfile2 == false)
    {        
        MoreInformationStrength(2);
        AddFactfile2=true;
    }
    else if(document.getElementById("GetForm1_textarea9").value == "" && document.getElementById("GetForm1_textarea9").value != "Text Area: Click to Begin typing - You can choose to Ignore / not fill it too" && AddFactfile2 == true)
    {
        LessInformationStrength(2);
        AddFactfile2=false;
    }
}

function CheckInformationStrength3()
{
    countNumberOfPhotos=0;
    for(i=1;i<=10;i++)
    {
        var pictureTextBox="document.getElementById('GetForm1_PhotoTitle"+i+"')";
        if(eval(pictureTextBox))
        {            
            if(eval(pictureTextBox).disabled == true)
                countNumberOfPhotos++;
        }        
    }
    if(countNumberOfPhotos > 4 && AddPictures1 == false)
    {
        MoreInformationStrength(2);
        AddPictures1=true;
    }
    else if(countNumberOfPhotos < 4 && AddPictures1 == true)
    {
        LessInformationStrength(2);
        AddPictures1=false;
    }
    if(countNumberOfPhotos > 6 && AddPictures2 == false)
    {
        MoreInformationStrength(1);
        AddPictures2=true;
    }
    else if(countNumberOfPhotos < 4 && AddPictures2 == true)
    {
        LessInformationStrength(1);
        AddPictures2=false;
    }
}


function MoreInformationStrength(x)
{
    InformationStrength+=x;    
    FillColor2(InformationStrength);
    document.getElementById("HiddenIS").value=InformationStrength;
}

function LessInformationStrength(x)
{
    InformationStrength-=x;    
    FillColor2(InformationStrength);
    document.getElementById("HiddenIS").value=InformationStrength;
}

function FillColor2(x)
{
    var boxColors=new Array("#d1e2e9","#c8e1eb","#b4dbea","#9cd5eb","#7dcae8","#65c3e6","#47b7e1","#24b6ed","#13b1ed","#00adef");
    //clear all cells
    for(i=0;i<10;i++)
    {
        varTD="document.getElementById('tdInformationStrength"+i+"')";
        eval(varTD).style.backgroundColor="#ffffff";
    }   
    
    //Fill in colors
    for(i=0;i<x;i++)
    {
        varTD="document.getElementById('tdInformationStrength"+i+"')";
        eval(varTD).style.backgroundColor=boxColors[i];
        eval(varTD).title=x;
    }   
}

//check if terms checkbox is checked in the contribute page
function checkterms()
{
	if(document.getElementById("terms").checked == false)
	{
		document.getElementById("termsMsg").innerHTML="You need to agree with the Terms & Conditions of the site to continue";
		return false;
	}
}

//In the Separate images form show appropriate Photo Upload Comp
function ShowPhotoUploadTable(x)
{
    for(i=1;i<=10;i++)
    {
        varPhotoUploadTable="document.getElementById('TablePhotoUpload"+i+"')";
        eval(varPhotoUploadTable).style.visibility="hidden";
        eval(varPhotoUploadTable).style.display="none";
    }
    
    varPhotoUploadTable="document.getElementById('TablePhotoUpload"+x+"')";
    eval(varPhotoUploadTable).style.visibility="visible";
    eval(varPhotoUploadTable).style.display="block";
}

//Add a comment on the detail page
function ShowPostComment()
{
    document.getElementById("DivPostComment").style.visibility="visible";
    document.getElementById("DivPostComment").style.display="block";
    document.body.scrollTop=document.getElementById("DivPostComment").offsetParent.offsetTop;
}

//Show Comment Count in detail page
function ShowCount()
{
    if(document.getElementById("count1"))
        document.getElementById("count1").value=1000-(document.getElementById("CommentBox").value.length);
    else if(document.getElementById("AskQuestion1_count1"))
        document.getElementById("AskQuestion1_count1").value=1000-(document.getElementById("AskQuestion1_CommentBox").value.length);
}