﻿//////////////////////////////////////////////////////////////////////////
//CLASS CRATINGBAR
//////////////////////////////////////////////////////////////////////////
function OnOverRatingBar(FormId, Value , ImgSrcLeft, ImgSrcRight, ImgSrcLeftFull, ImgSrcRightFull ) 
{
    var _Index = 1;
    for (; _Index <= Value; _Index++) 
    {
        if (_Index % 2 == 1)
        {
            document.getElementById("RATING_BAR_IMAGE_" + FormId + "_" + _Index).src = ImgSrcLeftFull;
        }else
        {
            document.getElementById("RATING_BAR_IMAGE_" + FormId + "_" + _Index).src = ImgSrcRightFull;
        }
    }
    for (; _Index < 11; _Index++)
    {
        if (_Index % 2 == 1) 
        {
            document.getElementById("RATING_BAR_IMAGE_" + FormId + "_" + _Index).src = ImgSrcLeft;
        } else 
        {
            document.getElementById("RATING_BAR_IMAGE_" + FormId + "_" + _Index).src = ImgSrcRight;
        }
    }
    document.getElementById("RATING_BAR_DATA_" + FormId).innerHTML = Value;
    
}
//////////////////////////////////////////////////////////////////////////
//SAFE ELEMENT FUNCTION
//////////////////////////////////////////////////////////////////////////
function SafeHideObjectById(ObjectId)
{
    if(document.getElementById(ObjectId)!=null)
        document.getElementById(ObjectId).style.display = 'none';
}
//////////////////////////////////////////////////////////////////////////
function SafeShowObjectById(ObjectId)
{
    if(document.getElementById(ObjectId)!=null)
        document.getElementById(ObjectId).style.display = 'block';
}
//////////////////////////////////////////////////////////////////////////
//DEBUG
//////////////////////////////////////////////////////////////////////////
function Debug_CloseConsol()
{
    document.getElementById("DebugConsol").style.visibility = "hidden";
}
//////////////////////////////////////////////////////////////////////////
function Debug_ShowConsol()
{
    document.getElementById("DebugConsol").style.visibility = "visible";
}
//////////////////////////////////////////////////////////////////////////
function Debug_Consol_Select(Selected)
{
    document.getElementById("DebugConsol_Content_All_Messages").style.display = "none";
    document.getElementById("DebugConsol_Content_Warning_Messages").style.display = "none";
    document.getElementById("DebugConsol_Content_Error_Messages").style.display = "none";
    document.getElementById("DebugConsol_Content_Session_Messages").style.display = "none";
    document.getElementById("DebugConsol_Content_DataBase_Messages").style.display = "none";
    document.getElementById("DebugConsol_Content_Media_Messages").style.display = "none";
    document.getElementById("DebugConsol_Content_Mouse_Messages").style.display = "none";
    document.getElementById("DebugConsol_Content_Time_Messages").style.display = "none";
    document.getElementById("DebugConsol_Content_" + Selected + "_Messages").style.display = "block";
    
    document.getElementById("DebugConsol_Menu_All").style.background = "#DDE";
    document.getElementById("DebugConsol_Menu_Warning").style.background = "#DDE";
    document.getElementById("DebugConsol_Menu_Error").style.background = "#DDE";
    document.getElementById("DebugConsol_Menu_Session").style.background = "#DDE";
    document.getElementById("DebugConsol_Menu_DataBase").style.background = "#DDE";
    document.getElementById("DebugConsol_Menu_Media").style.background = "#DDE";
    document.getElementById("DebugConsol_Menu_Mouse").style.background = "#DDE";
    document.getElementById("DebugConsol_Menu_Time").style.background = "#DDE";
    
    document.getElementById("DebugConsol_Menu_All").style.borderBottom = "none";
    document.getElementById("DebugConsol_Menu_Warning").style.borderBottom = "none";
    document.getElementById("DebugConsol_Menu_Error").style.borderBottom = "none";
    document.getElementById("DebugConsol_Menu_Session").style.borderBottom = "none";
    document.getElementById("DebugConsol_Menu_DataBase").style.borderBottom = "none";
    document.getElementById("DebugConsol_Menu_Media").style.borderBottom = "none";
    document.getElementById("DebugConsol_Menu_Mouse").style.borderBottom = "none";
    document.getElementById("DebugConsol_Menu_Time").style.borderBottom = "none";
    
    document.getElementById("DebugConsol_Menu_"+Selected).style.background = "white";
    document.getElementById("DebugConsol_Menu_"+Selected).style.borderBottom = "1px solid white";
}
//////////////////////////////////////////////////////////////////////////
//VAR 
//////////////////////////////////////////////////////////////////////////
var g_ZIndex = 1000;
var g_DragMouseClickPositionX=0;
var g_DragMouseClickPositionY=0;

var g_IsDragObject=false;
var g_IdDragObject="";

var g_MouseMoveFunctionArray = new Array;
var g_MouseUpFunctionArray = new Array;
var g_MouseDownFunctionArray = new Array;
var resise_win;
var cur_id_win;

var g_MouseInformation = new CMouseInformation();
//////////////////////////////////////////////////////////////////////////
//FUNCTION AFFECTATION 
//////////////////////////////////////////////////////////////////////////
document.onload = InitJavascriptHandler;
document.onmousemove = OnMouseMoveHandler;
document.onmousedown = OnMouseDownHandler;
document.onmouseup = OnMouseUpHandler;
//////////////////////////////////////////////////////////////////////////
//HANDLER
//////////////////////////////////////////////////////////////////////////
function InitJavascriptHandler()
{
    //alert('Init javascript');
    InitTime();
    AddMouseMoveFunction( TraceStatusBar );
    g_DragMouseClickPositionX = 0;
    g_DragMouseClickPositionY = 0;
}
//////////////////////////////////////////////////////////////////////////
function OnMouseDownHandler( e )
{
    g_MouseInformation.MouseDown( e );
    g_MouseInformation.UpdateDebugInfo();
}
//////////////////////////////////////////////////////////////////////////
function OnMouseUpHandler( e )
{
    g_MouseInformation.MouseUp( e );
    g_MouseInformation.UpdateDebugInfo();
    g_IsDragObject = false;
    resise_win =false;
}
//////////////////////////////////////////////////////////////////////////
function OnMouseMoveHandler( e )
{
    g_MouseInformation.UpdatePosition( e );
    //g_MouseInformation.UpdateDebugInfo();
    for( index = 0 ; index < g_MouseMoveFunctionArray.length ; index++ )
    {
        g_MouseMoveFunctionArray[ index ]();
    }
    
    if( g_IsDragObject )
    {
        MoveObjectTo( g_IdDragObject ,
         g_MouseInformation.GetMousePosition().GetX() - g_DragMouseClickPositionX ,
         g_MouseInformation.GetMousePosition().GetY() - g_DragMouseClickPositionY);
    }
    
    
    if( resise_win )
    {
        ResizeWindow ( cur_id_win );
    }

}

//////////////////////////////////////////////////////////////////////////
//COMMON
//////////////////////////////////////////////////////////////////////////
function CheckMail(MailAdresse)
{
    if(MailAdresse=="")
        return false;

    var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
    if (filter.test(MailAdresse))
    {
        testresults=true;
    }
    else
    {
        testresults=false;
    }
    return (testresults)
  //var _RegularExpression = /([a-z0-9]+([-_]?[a-z0-9]+)*.)*[a-z0-9]+([-_]?[a-z0-9]+)*@([a-z0-9]+([-_]?[a-z0-9]+)*.)+[a-z]{2,4}/gi;
  //return (MailAdresse.match(_RegularExpression) == MailAdresse);
}
//////////////////////////////////////////////////////////////////////////
function ShowElement(ID_Element)
{
    document.getElementById(ID_Element).style.display = 'block';
}
//////////////////////////////////////////////////////////////////////////
function HideElement(ID_Element)
{
    document.getElementById(ID_Element).style.display = 'none';
}
//////////////////////////////////////////////////////////////////////////
function GetSafeString(SafeString)
{
    var reg=new RegExp("(')", "g");
    SafeString = SafeString.replace(reg,"");
    reg=new RegExp("(´)", "g");
    SafeString = SafeString.replace(reg,"");
    reg=new RegExp("(&)", "g");
    SafeString = SafeString.replace(reg,"");
    return SafeString;
}
//////////////////////////////////////////////////////////////////////////
function EncodeUTF8(s)
{
    for(var c, i = -1, l = (s = s.split("")).length, o = String.fromCharCode; ++i < l;
      s[i] = (c = s[i].charCodeAt(0)) >= 127 ? o(0xc0 | (c >>> 6)) + o(0x80 | (c & 0x3f)) : s[i]);
    
    
    return s.join("");
}

function DecodeUTF8(s)
{
    for(var a, b, i = -1, l = (s = s.split("")).length, o = String.fromCharCode, c = "charCodeAt"; ++i < l;
    ((a = s[i][c](0)) & 0x80) &&
    (s[i] = (a & 0xfc) == 0xc0 && ((b = s[i + 1][c](0)) & 0xc0) == 0x80 ?
    o(((a & 0x03) << 6) + (b & 0x3f)) : o(128), s[++i] = "")
    );
    
    return s.join("");
}




function ConvertString( StringToConvert )
{
/*
    *  é : \351
    * è : \350
    * ê : \352
    * à : \340
*/
    var chrsIso=new Array('é','è','ê','à');
    var chrsUni=new Array('&eacute;','&egrave;','&ecirc;','&agrave;');

    var chrsCnt=chrsIso.length;
    for(var k=0;k<chrsCnt;k++)
    {
        StringToConvert=StringToConvert.replace(chrsIso[k],chrsUni[k]);
    }
 //   StringToConvert = encodeURI ( StringToConvert );
    alert(StringToConvert);
    return StringToConvert;
}
//////////////////////////////////////////////////////////////////////////
function RemovePxFromStyle( string_px)
{
    var no_px = String( string_px );
    no_px = no_px.replace(/px,*\)*/g,"");
    return no_px; 
}
//////////////////////////////////////////////////////////////////////////
function SetTextStatusBar( Text )
{
    window.status = Text;
}
//////////////////////////////////////////////////////////////////////////
function GetTextStatusBar( )
{
    return window.status;
}
//////////////////////////////////////////////////////////////////////////
function AddMouseMoveFunction( Function )
{
    g_MouseMoveFunctionArray.push( Function );
}
//////////////////////////////////////////////////////////////////////////
function StopDragObject( IdObject )
{
    g_IdDragObject = "";
    g_IsDragObject = false;
    g_DragMouseClickPositionX = 0;
    g_DragMouseClickPositionY = 0;
    
}
//////////////////////////////////////////////////////////////////////////
function StartDragObject( IdObject )
{
    g_IdDragObject = IdObject;
    g_IsDragObject = true;
    
    var object_left = String(IdObject.style.left);
    object_left = object_left.replace(/px,*\)*/g,"");
   
    var object_top = String(IdObject.style.top);
    object_top = object_top.replace(/px,*\)*/g,"");
    
    g_DragMouseClickPositionX = g_MouseInformation.GetMousePosition().GetX() - object_left;
    g_DragMouseClickPositionY = g_MouseInformation.GetMousePosition().GetY() - object_top;

}
//////////////////////////////////////////////////////////////////////////
function MoveObjectTo( IdObject , PositionX , PositionY )
{
    if(PositionY<0)
    {
        PositionY = 0;
        StopDragObject( IdObject );
    }
        
    IdObject.style.top = PositionY+'px';
    IdObject.style.left = PositionX+'px';
    IdObject.style.position="absolute";
    SetTextStatusBar( "Move Object to X:"+PositionX + "Y:" + PositionY+" ->>"+g_DragMouseClickPositionX+":"+g_DragMouseClickPositionY );
}
//////////////////////////////////////////////////////////////////////////
function TraceStatusBar()
{
    SetTextStatusBar( g_MouseInformation.GetMousePosition().GetX() + ":" + g_MouseInformation.GetMousePosition().GetY()+" ->>"+g_MouseMoveFunctionArray.length );
}
//////////////////////////////////////////////////////////////////////////
function findPositionWithScrolling( oElement ) 
{
    if( typeof( oElement.offsetParent ) != 'undefined' ) //check if parent exist
    {
        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;
            }
        }
        alert( "Position X:"+ posX + " Y:" + posY 
        +"\n Size X:" + originalElement.offsetWidth+"Y:" + originalElement.offsetHeight);
        return [ posX, posY ];
    } 
    else 
    {
        return [ oElement.x, oElement.y ];
    }
}
//<script type="text/javascript">findPositionWithScrolling (document.getElementById('Test_img'));</script>

//////////////////////////////////////////////////////////////////////////
function ZoomPage()
{
    Components.classes["@mozilla.org/appshell/window-mediator;1"].getService(Components.interfaces.nsIWindowMediator).getMostRecentWindow("navigator:browser").getBrowser().mCurrentBrowser.markupDocumentViewer.fullZoom = 3;
}
//////////////////////////////////////////////////////////////////////////
//CLASS CVector2D
//////////////////////////////////////////////////////////////////////////
function CVector2D()
{
    this.m_X=0;
    this.m_Y=0;
}
//////////////////////////////////////////////////////////////////////////
CVector2D.prototype.GetX = function() 
{
    return this.m_X;
}
//////////////////////////////////////////////////////////////////////////
CVector2D.prototype.GetY = function()
{
    return this.m_Y;
}
//////////////////////////////////////////////////////////////////////////
function AddVector( VectorA , VectorB )
{
    var return_vector = new CVector2D();
    return_vector.m_X = VectorA.m_X + VectorB.m_X;
    return_vector.m_Y = VectorA.m_Y + VectorB.m_Y;
    return return_vector;
}
//////////////////////////////////////////////////////////////////////////
//CLASS CMouseInformation
//////////////////////////////////////////////////////////////////////////
function CMouseInformation()
{
    this.m_Position = new CVector2D();
    this.m_LeftClickPosition = new CVector2D();
    this.m_RightClickPosition = new CVector2D();
    this.m_MiddleClickPosition = new CVector2D();
    this.m_LeftButtonState=false;
    this.m_RighButtonState=false;
    this.m_MiddleButtonState=false;
    
}
//////////////////////////////////////////////////////////////////////////
CMouseInformation.prototype.UpdateDebugInfo = function ( )
{
    if( ! document.getElementById('DEBUG_MOUSE_INFO') ) return;
    document.getElementById('DEBUG_MOUSE_INFO').innerHTML="";
    document.getElementById('DEBUG_MOUSE_INFO').innerHTML+="Position X:"+this.m_Position.m_X+" Y:"+this.m_Position.m_Y+"<br>";
    document.getElementById('DEBUG_MOUSE_INFO').innerHTML+="Left click position X:"+this.m_LeftClickPosition.m_X+" Y:"+this.m_LeftClickPosition.m_Y+"<br>";
    document.getElementById('DEBUG_MOUSE_INFO').innerHTML+="Right click position X:"+this.m_RightClickPosition.m_X+" Y:"+this.m_RightClickPosition.m_Y+"<br>";
    document.getElementById('DEBUG_MOUSE_INFO').innerHTML+="Middle click position X:"+this.m_MiddleClickPosition.m_X+" Y:"+this.m_MiddleClickPosition.m_Y+"<br>";
    
    if( this.m_LeftButtonState && document.getElementById("DEBUG_MOUSE_INFO_BUTTON_LEFT")!= null)
    {
        document.getElementById("DEBUG_MOUSE_INFO_BUTTON_LEFT").style.visibility = "visible";
    }else
    {
        document.getElementById("DEBUG_MOUSE_INFO_BUTTON_LEFT").style.visibility = "hidden";
    }
    
    if( this.m_MiddleButtonState )
    {
        document.getElementById("DEBUG_MOUSE_INFO_BUTTON_MIDDLE").style.visibility = "visible";
    }else
    {
        document.getElementById("DEBUG_MOUSE_INFO_BUTTON_MIDDLE").style.visibility = "hidden";
    }
    
    if( this.m_RightButtonState )
    {
        document.getElementById("DEBUG_MOUSE_INFO_BUTTON_RIGHT").style.visibility = "visible";
    }else
    {
        document.getElementById("DEBUG_MOUSE_INFO_BUTTON_RIGHT").style.visibility = "hidden";
    }
}


//////////////////////////////////////////////////////////////////////////
CMouseInformation.prototype.GetMousePosition = function ( )
{
    return this.m_Position;
}
//////////////////////////////////////////////////////////////////////////
CMouseInformation.prototype.SetMousePosition = function ( PositionX , PositionY )
{
    this.m_Position.m_X = PositionX;
    this.m_Position.m_Y = PositionY;
}
//////////////////////////////////////////////////////////////////////////
CMouseInformation.prototype.UpdatePosition = function ( e )
{
    this.m_Position.m_X = (navigator.appName.substring(0,3) == "Net") ? e.pageX : event.x+document.body.scrollLeft;
    this.m_Position.m_Y = (navigator.appName.substring(0,3) == "Net") ? e.pageY : event.y+document.body.scrollTop;
}
//////////////////////////////////////////////////////////////////////////
CMouseInformation.prototype.MouseUp = function ( e )
{
    this.UpdatePosition( e );
    if (!e)
    { 
        var e = window.event;
    }

    if (e.which == null)/* IE case */
    {
       
       button_pressed = (e.button < 2) ? "LEFT" :
                 ((e.button == 4) ? "MIDDLE" : "RIGHT");
    }else/* All others */
    {
       
       button_pressed = (e.which < 2) ? "LEFT" :
                 ((e.which == 2) ? "MIDDLE" : "RIGHT");
     }
     
    //alert(button_pressed ); 
    switch( button_pressed )
    {
        case "LEFT":
            this.m_LeftButtonState = false;
            this.m_LeftClickPosition.m_X = this.m_Position.m_X;
            this.m_LeftClickPosition.m_Y = this.m_Position.m_Y;
            break;
        case "MIDDLE":
            this.m_MiddleButtonState = false;
            this.m_MiddleClickPosition.m_X = this.m_Position.m_X;
            this.m_MiddleClickPosition.m_Y = this.m_Position.m_Y;
            break;
        case "RIGHT":
            this.m_RightButtonState = false;
            this.m_RightClickPosition.m_X = this.m_Position.m_X;
            this.m_RightClickPosition.m_Y = this.m_Position.m_Y;
            break;
    }
}
//////////////////////////////////////////////////////////////////////////
CMouseInformation.prototype.MouseDown = function ( e )
{
    this.UpdatePosition( e );
    if (!e)
    { 
        var e = window.event;
    }

    if (e.which == null)/* IE case */
    {
       
       button_pressed = (e.button < 2) ? "LEFT" :
                 ((e.button == 4) ? "MIDDLE" : "RIGHT");
    }else/* All others */
    {
       
       button_pressed = (e.which < 2) ? "LEFT" :
                 ((e.which == 2) ? "MIDDLE" : "RIGHT");
     }
     
    //alert(button_pressed ); 
    switch( button_pressed )
    {
        case "LEFT":
            this.m_LeftButtonState = true;
            this.m_LeftClickPosition.m_X = this.m_Position.m_X;
            this.m_LeftClickPosition.m_Y = this.m_Position.m_Y;
            break;
        case "MIDDLE":
            this.m_MiddleButtonState = true;
            this.m_MiddleClickPosition.m_X = this.m_Position.m_X;
            this.m_MiddleClickPosition.m_Y = this.m_Position.m_Y;
            break;
        case "RIGHT":
            this.m_RightButtonState = true;
            this.m_RightClickPosition.m_X = this.m_Position.m_X;
            this.m_RightClickPosition.m_Y = this.m_Position.m_Y;
            break;
    }
}


//////////////////////////////////////////////////////////////////////////
//TRASH
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////


function agent(v) { return(Math.max(navigator.userAgent.toLowerCase().indexOf(v),0)); }
function xy(g_e,v) 
{ 
    return(v?(agent('msie')?event.clientY+document.body.scrollTop:g_e.pageY):(agent('msie')?event.clientX+document.body.scrollTop:g_e.pageX)); 
}

/////////////////////////////////////////////////////////////////////////
function opacity(id, opacStart, opacEnd, millisec) {
	//speed for each frame
	var speed = Math.round(millisec / 100);
	var timer = 0;

	//determine the direction for the blending, if start and end are the same nothing happens
	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++;
		}
	}
}

//change the opacity for different browsers
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 + ")";
}

function shiftOpacity(id, millisec) {
	//if an element is invisible, make it visible, else make it ivisible
	if(document.getElementById(id).style.opacity == 0) {
		opacity(id, 0, 100, millisec);
	} else {
		opacity(id, 100, 0, millisec);
	}
}

function blendimage(divid, imageid, imagefile, millisec) {
	var speed = Math.round(millisec / 100);
	var timer = 0;
	
	//set the current image as background
	document.getElementById(divid).style.backgroundImage = "url(" + document.getElementById(imageid).src + ")";
	
	//make image transparent
	changeOpac(0, imageid);
	
	//make new image
	document.getElementById(imageid).src = imagefile;

	//fade in image
	for(i = 0; i <= 100; i++) {
		setTimeout("changeOpac(" + i + ",'" + imageid + "')",(timer * speed));
		timer++;
	}
}

function currentOpac(id, opacEnd, millisec) {
	//standard opacity is 100
	var currentOpac = 100;
	
	//if the element has an opacity set, get it
	if(document.getElementById(id).style.opacity < 100) {
		currentOpac = document.getElementById(id).style.opacity * 100;
	}

	//call for the function that changes the opacity
	opacity(id, currentOpac, opacEnd, millisec)
}

function fixPNG() {
	if(navigator.appName == 'Microsoft Internet Explorer') {
		var png = /\.png$/i;
		var imgs = document.getElementsByTagName('img');
		for(var i = 0, l = imgs.length; i < l; i++) {
			if(png.test(imgs.item(i).src)) {
				imgs.item(i).style.width = imgs.item(i).offsetWidth;
				imgs.item(i).style.height = imgs.item(i).offsetHeight;
				imgs.item(i).style.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'' + imgs.item(i).src + '\',sizingMethod=\'image\')';
				imgs.item(i).src = 'empty.gif';
			}
		}
	}
}
