﻿var mouse_X;
var mouse_Y;
var ItemDetailsShown=0;
var ie = document.all?true:false;

function ProcessInput()
{
       //if (event.keyCode==13) GoSearch();
       if (navigator.appName.indexOf('Opera') < -1) 
       {
            event.Cancel = true;
            return false; 
       }
}

// ****************************************************************************************************
// If IE, get Coordinates based on Event
// By: H. 2007-05-01
// ****************************************************************************************************
if (!ie) document.captureEvents(Event.MOUSEMOVE)
document.onmousemove = getMouseXY;

// ****************************************************************************************************
// Set control visibility based on its client Name.
// By: H. 2007-05-01
// ****************************************************************************************************
function SetVisibility (control, visible)
{
	if (visible)
	{
		document.getElementById(control).style.display = "";
		document.getElementById(control).style.visibility = "visible";
		//if (control == 'Loading') UpdatePosition(control);
	}
	else
	{
		document.getElementById(control).style.display = "none";
		document.getElementById(control).style.visibility = "hidden";
	}
}

// ****************************************************************************************************
// Validates login name and password
// By: H. 2007-05-01
// ****************************************************************************************************
function ValidateLoginAndGo()
{
    document.getElementById('imgGo').src = "../Images/Waiting.gif"
    var login     = document.getElementById('ctl00_ctrlLogin_txtLogin').value;
    var password  = document.getElementById('ctl00_ctrlLogin_txtPassword').value;
    var remember  = document.getElementById('chkRemember').checked;
    if (password != "" && validateEmail (document.getElementById('ctl00_ctrlLogin_txtLogin')) )
    {
        var _pass   = MD5(password);   
        Anthem_InvokeMasterPageMethod ( 'ValidateLogin', [login, _pass, remember], function(result){} ); 
    }
    else
    {
         alert('Please provide valid e-mail and password');
         document.getElementById('imgGo').src = "../Images/pfeil_rechts_green.gif"
    }
    
}

// ****************************************************************************************************
// Validates Signup Data
// By: H. 2007-05-01
// ****************************************************************************************************
function ValidateSignupAndGo()

{
    document.getElementById('imgSignupGo').src = "../Images/Waiting.gif"
    var login      = document.getElementById('txtLogin').value;
    var password   = document.getElementById('ctl00_ctrlSignup_txtPassword').value;
    var firstName  = document.getElementById('txtFirstName').value;
    var lastName   = document.getElementById('txtLastName').value;
    var year       = document.getElementById('ctl00_ctrlSignup_lstYear').value;
    var month      = document.getElementById('ctl00_ctrlSignup_lstMonth').value;
    var day        = document.getElementById('ctl00_ctrlSignup_lstDay').value;
    var confPwd    = document.getElementById('ctl00_ctrlSignup_txtConfPwd').value;
    //var email      = document.getElementById('txtEmail').value;
    var secCheck   = document.getElementById('txtSecurity').value;
    var agree      = document.getElementById('chkAgree').checked;
    var maleGender = document.getElementById('ctl00_ctrlSignup_optMale').checked;
    
    //var msg        = 'Please complete the following information: '
    
    if (login == '' || !validateEmail(document.getElementById('txtLogin')))
    {
        alert('Please input a valid e-mail address');
        document.getElementById('imgSignupGo').src = "../Images/pfeil_rechts_green.gif"
        return;
    }
    
    if (password == '' || password.length < 6)
    {
        alert('Please input a password with at least 6 characters');
        document.getElementById('imgSignupGo').src = "../Images/pfeil_rechts_green.gif"
        return;
    }
    
    if (firstName == '')
    {
        alert('Please input your First Name');
        document.getElementById('imgSignupGo').src = "../Images/pfeil_rechts_green.gif"
        return;
    }
    
    if (lastName == '')
    {
        alert('Please input your First Name');
        document.getElementById('imgSignupGo').src = "../Images/pfeil_rechts_green.gif"
        return;
    }
    
    if (confPwd == '')
    {
        alert('Please input your password verification');
        document.getElementById('imgSignupGo').src = "../Images/pfeil_rechts_green.gif"
        return;
    }
    
    if (confPwd != password)
    {
        alert('Please verify your password match');
        document.getElementById('imgSignupGo').src = "../Images/pfeil_rechts_green.gif"
        return;
    }
    
    if (secCheck == '')
    {
        alert('Please input the security check');
        document.getElementById('imgSignupGo').src = "../Images/pfeil_rechts_green.gif"
        return;
    }
    
    if (!agree)
    {
        alert('Please read and review the Terms of Use and Privacy Policy');
        document.getElementById('imgSignupGo').src = "../Images/pfeil_rechts_green.gif"
        return;
    }
    
    Anthem_InvokeMasterPageMethod ( 'ValidateSignup', [login, firstName, lastName, year, month, day, maleGender, MD5(password), login, secCheck], function(result){} ); 
    
}

// ****************************************************************************************************
// Updates the Item Details Position based on mouse coordinates
// By: H. 2007-05-01
// ****************************************************************************************************
function UpdatePosition(control)
{
    X=mouse_X;
    Y=mouse_Y;
   
    if (!ie)
    { 
        SX=0;
        SY=0;
        
        SX=self.screenX;
        SY=self.screenY;
    
        if (SX<0) {SX=0}
        if (SY<0) {SY=0}
        
        if (self.innerHeight)
        {
	        ix = self.innerWidth;
	        iy = self.innerHeight;
        }
        else if (document.documentElement && document.documentElement.clientHeight)
        {
	        ix = document.documentElement.clientWidth;
	        iy = document.documentElement.clientHeight;
        }
        else if (document.body)
        {
	        ix = document.body.clientWidth;
	        iy = document.body.clientHeight;
        }

        MX  =   ix-SX;
        MY  =   iy-SY;

        if (X+430>MX) {X=MX-430;Y=Y+20}

        DH = document.getElementById(control).clientHeight;
    }
    
    //if (control != 'Loading') 
    Y = Y - 80;
    X = X + 50;
    document.getElementById(control).style.left = X+'px';
    document.getElementById(control).style.top  = Y+'px';
}

// ****************************************************************************************************
// Gets the current Mouse Position
// By: H. 2007-05-01
// ****************************************************************************************************
function getMouseXY(e) 
{
    if (ie)
    {
        mouse_X = event.clientX + document.documentElement.scrollLeft;
        mouse_Y = event.clientY + document.documentElement.scrollTop;
    }
    else
    {
        mouse_X = e.pageX;
        mouse_Y = e.pageY;
    }
    if (mouse_X < 0){mouse_X = 0;}
    if (mouse_Y < 0){mouse_Y = 0;}
}

// ****************************************************************************************************
// Show details of an Item
// By: H. 2007-05-01
// ****************************************************************************************************
function ShowDetails(imgPath, linkAction)
{
	UpdatePosition('lyrPhotoDetails');
	document.getElementById('imgPhotoDetails').src = imgPath;
	//document.getElementById('imgPhotoDetails').setAttribute("onclick",linkAction);
	document.getElementById('lyrPhotoDetails').style.visibility = "visible";
	//document.getElementById('lyrPhotoDetails').setAttribute("style","cursor:hand; cursor:pointer");
}

// ****************************************************************************************************
// Hides the details of an Item
// By: H. 2007-05-01
// ****************************************************************************************************
function HideDetails()
{
	//ItemDetailsShown = 0;
	document.getElementById('lyrPhotoDetails').style.visibility = "hidden";
}

// ****************************************************************************************************
// Opens a layer with progressive height increase
// By: H. 2007-05-01
// ****************************************************************************************************
function splitOpen(id, millisec, finalHeight)
{
    var speed = Math.round(millisec / 100);
    var timer = 0;
    for(i = 0; i <= finalHeight; i++)
    {
        setTimeout("changeHeight(" + i + ",'" + id + "')",(timer * speed));
        timer++;
    }
    //document.getElementById(id).style.top = mouse_Y - 50;
}

// ****************************************************************************************************
// Changes the height of an element
// By: H. 2007-05-01
// ****************************************************************************************************
function changeHeight(height, id) 
{
    var object = document.getElementById(id);
    object.style.height = height;
    object.children[0].style.height = height;
} 

// ****************************************************************************************************
// Shows the comment box
// By: H. 2007-05-01
// ****************************************************************************************************
function showCommentBox(itemId, itemTitle, isForEdition)
{
    //opacity('lyrComment',0,90,500); 
    //splitOpen('lyrComment',300, 285); 
    
    if (document.getElementById('ValidationSummary').value == 'true')
    {
        changeOpac(90, 'lyrComment');
        if (ie)
        {
            changeHeight(320, 'lyrComment');
            document.getElementById('lyrComment').style.top = mouse_Y - 50;
        }
        else
        {
            var pos = mouse_Y - 50;
            document.getElementById('lyrComment').style.top = pos + "px";
            document.getElementById('lyrComment').height = 280;
        }
         
        document.getElementById('lyrComment').style.display = "";
        if (!isForEdition)
        {
            document.getElementById('ctl00_ctrlComment_txtComments').innerText = "";
            ResetRate();
        }
        document.getElementById('Anthem_ctl00_ctrlComment_tblSimilar__').innerText = "";
        
        if (itemTitle == undefined)
        {
            SetVisibility ('Anthem_ctl00_ctrlComment_lblName__', false);
            SetVisibility ('ctl00_ctrlComment_txtTitle', true);
            document.getElementById('ctl00_ctrlComment_txtTitle').innerText = "";
            SetVisibility ('tblSimilarContainer', true);
        }
        else
        {
            SetVisibility ('Anthem_ctl00_ctrlComment_lblName__', true);
            SetVisibility ('ctl00_ctrlComment_txtTitle', false);
            document.getElementById('Anthem_ctl00_ctrlComment_lblName__').innerHTML = itemTitle;
            document.getElementById('ctl00_ctrlComment_txtReferenceId').value = itemId;
            SetVisibility ('tblSimilarContainer', false);
        }
     }
     else
     {
        alert('Please login First');
        ShowAuthBox(true);
        
     }
}

// ****************************************************************************************************
// Affects, based in a timer, the Opacity of an item
// By: H. 2007-05-01
// ****************************************************************************************************
function opacity(id, opacStart, opacEnd, millisec)
{
    var speed = Math.round(millisec / 100);
    var timer = 0;
   
    if(opacStart > opacEnd)
    {
        for(i = opacStart; i >= opacEnd; i--) {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    } 
    else if(opacStart < opacEnd) 
    {
        for(i = opacStart; i <= opacEnd; i++)
            {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    }
	
}
// ****************************************************************************************************
// Changes the Opacity of an Item
// By: H. 2007-05-01
// ****************************************************************************************************
function changeOpac(opacity, id) 
{
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
} 

// ****************************************************************************************************
// Executes when a New Item is added.
// By: H. 2007-05-01
// ****************************************************************************************************
function SaveDone()
{

}

function HideBackground()
{
    //document.getElementById('')
    document.getElementById('modalPage').className = 'modalBackground'; 
}

function CloseCommentBox()
{
    document.getElementById('lyrComment').style.display = "none";
    document.getElementById('ctl00_ctrlComment_txtTags').innerText = "";
    
}

function ShowSuggest(textValue)
{
    if (document.getElementById('ctl00_ctrlComment_ctrlSuggest_txtData').value.length > 3)
    {
        Anthem_InvokeMasterPageMethod ('FillData',[textValue],function(result){});
    }
    else 
    {
        //document.getElementById('ctl00_ctrlComment_ctrlSuggest_lstOptions').visible = false;
    }
}

function ClearSearchBox()
{
    document.getElementById('txtSearch').value = "";
}

// ****************************************************************************************************
// Executes the Text Search
// By: H. 2007-05-01
// ****************************************************************************************************
function GoSearch()
{
    var path = location.pathname;
    if (path.indexOf("Main.aspx") > -1)
    {
        SetVisibility ('Loading', true);
        document.getElementById('lyrComment').style.display = "none";
        Anthem_InvokePageMethod('GoSearch', [document.getElementById('txtSearch').value], function(result){} );
    }
    else
    {
        var loc = "Main.aspx?Search=" + document.getElementById('txtSearch').value.replace(' ','_');
        location.href = loc;
    }
}

function SearchText(text)
{
    var path = location.pathname;
    if (path.indexOf("Main.aspx") > -1)
    {
        SetVisibility ('Loading', true);
        document.getElementById('lyrComment').style.display = "none";
        Anthem_InvokePageMethod('GoSearch', [text], function(result){} );
    }
    else
    {
        var loc = "Main.aspx?Search=" + text;
        location.href = loc;
    }
}

function GoToReview(id, text)
{
    var path = location.pathname;
    var loc = "Review.aspx?ItemId=" + id + "&ItemTitle=" + text;
    location.href = loc;
}

function SearchDefault(search)
{
    var path = location.pathname;
    if (path.indexOf("Main.aspx") > -1)
    {
        SetVisibility ('Loading', true);
        document.getElementById('lyrComment').style.display = "none";
        Anthem_InvokePageMethod('SearchDefault', [search], function(result){} );
    }
    else
    {
        var loc = "Main.aspx?SearchDefault=" + search
        location.href = loc;
    }
}

// ****************************************************************************************************
// Executes the Validation when a comments is to be saved.
// By: H. 2007-05-01
// ****************************************************************************************************
function CheckAndSaveComments()
{
    SetVisibility ('Loading', true);
    SetVisibility ('imgSave', false);
    if (document.getElementById('ctl00_ctrlComment_ctrlStarRater_txtRate').value == "" || document.getElementById('ctl00_ctrlComment_ctrlStarRater_txtRate').value == "0")
    {
        alert('Please rate this item by selecting a number of stars');
        SetVisibility ('imgSave', true);
        SetVisibility ('Loading', false);
        return false;
    }
    if (document.getElementById('ctl00_ctrlComment_txtReferenceId').value != "")
    {

    }
    else
    {
        if (document.getElementById('ctl00_ctrlComment_txtTitle').value == "")
        {
            alert('Please include a title for the Item');
            SetVisibility ('imgSave', true);
            SetVisibility ('Loading', false);
            return false;
        }
    }
    var editStatus = document.getElementById('ctl00_ctrlComment_hdnEditStatus').value;
    var editId = document.getElementById('ctl00_ctrlComment_hdnEditionId').value;
    
    Anthem_InvokeMasterPageMethod('Save', [editStatus, editId], function(result){} );
    CloseCommentBox();
}

// ****************************************************************************************************
// Cancels the Enter key
// By: H. 2007-05-01
// ****************************************************************************************************
function CancelEnter(e)
{
	var unicode=e.charCode? e.charCode : e.keyCode
	if (unicode==13)
	{ 
		return false; 
	}
}


function ShowAuthBox(isLogin)
{
    HideBackground();
    //changeOpac(90, 'lyrAuth');
    SetVisibility('lyrLogin', isLogin);
    SetVisibility('lyrSignup', !isLogin);
    
    if (ie)
    {
        document.getElementById('lyrAuth').style.top = 250;
        //move_box();
    }
    else
    {
        var pos = mouse_Y + 100;
        document.getElementById('lyrAuth').style.top = pos + "px";
    }
    //UpdatePosition('lyrAuth');
    document.getElementById('lyrAuth').style.display = "";
}

function ToogleUploaderVisibility()
{
    if (document.getElementById('lyrUploader').style.display != 'none')
    {
        ShowBackground();
        SetVisibility('lyrUploader', false);
    }
    else
    {
        HideBackground();
        SetVisibility('lyrUploader', true);
    }
}

function ToogleAuthBoxVisibility()
{
    if (document.getElementById('lyrAuth').style.display != 'none')
    {
        ShowBackground();
        SetVisibility('lyrAuth', false);
    }
    else
    {
        HideBackground();
        SetVisibility('lyrAuth', true);
    }
}

function ShowForgotPassword()
{
    alert('This function is currently disabled, please try again later');
}

function ShowBackground()
{
    document.getElementById('modalPage').className = 'modalPage'; 
}

function HideAuthBox()
{
    ShowBackground();
    document.getElementById('lyrAuth').style.display = "none";
}

function LogOut()
{
    Anthem_InvokeMasterPageMethod('LogOut', [], function(result){} );
}

function validateEmail ( emailField ) 
{
    emailpat = /^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9])+(\.[a-zA-Z0-9_-]+)+$/;
    if( !emailpat.test( emailField.value ) ) 
    {
        emailField.focus();
        emailField.select();
        return false;
    }
    return true;
}

function FillFilesCallBack(itemId)
{
    Anthem_InvokePageMethod('FillFiles', [itemId], function(result){} );
}

function ValidateCoordinates(e)
{
        var unicode=e.charCode? e.charCode : e.keyCode;
	    if (unicode!=8)
        return ( unicode>44 && unicode<58 && unicode!=47 )
}

function CheckAndAllowItemEdition(itemId, itemTitle)
{
    Anthem_InvokeMasterPageMethod('CheckAndAllowItemEdition', [itemId, itemTitle], function(result){} );
}

function CheckAndAllowCommentEdition(itemId, commentId, itemTitle)
{
    Anthem_InvokeMasterPageMethod('CheckAndAllowCommentEdition', [itemId, commentId, itemTitle], function(result){} );
}