﻿var IE = false;
if (document.all) IE = true;

var radian = 57.2957;
var semi = (180/radian);
var scrollTimer = null;
var scrollDirection = 0;
var scrollSteps = 100;
var scrollStep = semi / scrollSteps;
var scrollStepCurrent = 0;
var scrollValue = 0;
var scrollDelay = 5;
var scrollPos = 0;
var scrollWidth = 640;
var scrollContainer = "exhib_image_browse";
var fadeRate = 0.025;

function StringBuilder()
{
    this.strings = new Array("");
}
StringBuilder.prototype.Append = function (value)
{
    if (value)
    {
        this.strings.push(value);
    }
    return this; 
}
StringBuilder.prototype.Clear = function ()
{
    this.strings.length = 1;
}
StringBuilder.prototype.ToString = function ()
{
    return this.strings.join("");
}    

var pageForm=null;

// Get the page form
function GetForm()
{
     if( pageForm != null )
    {
        return pageForm;
    }
    if( window.navigator.appName.toLowerCase().indexOf("microsoft") > -1 )
    {
	    pageForm = document.Form1;
	    if( pageForm == null )
	    {
            pageForm = document.form1;
	    }
    }
    else 
    {
	    pageForm = document.forms["Form1"];
	    if( pageForm == null )
	    {
            pageForm = document.forms["form1"];
	    }
	    
    }
    if( pageForm == null )
    {
        alert("No form");
    }
    return pageForm;
}

// Get a Form Field value
function GetFormField(field)
{
    return GetForm().elements[field].value;
}

// Set a form field value
function SetFormField(field,value)
{
    return GetForm().elements[field].value = value;
}
function EncodeHtml(id)
{
    var element = document.getElementById(id);
    //if(!element) return "";
    var value = element.value;
    if( value == null )
    {
        return "";
    }
    if( value.length == 0 )
    {
        return value;
    }
    value=value.replace(/&/g,"&amp;");
    value=value.replace(/"/g,"&quot;");
    value=value.replace(/</g,"&lt;");
    value=value.replace(/>/g,"&gt;");
    return value;
}
function EncodeInnerHtml(id)
{
    var value = document.getElementById(id).innerHTML;
    if( value == null )
    {
        return "";
    }
    if( value.length == 0 )
    {
        return value;
    }
    value=value.replace(/&/g,"&amp;");
    value=value.replace(/"/g,"&quot;");
    value=value.replace(/</g,"&lt;");
    value=value.replace(/>/g,"&gt;");
    return value;
}
function DecodeHtml(html)
{
    if( html == null )
    {
        return "";
    }
    if( html.length == 0 )
    {
        return html;
    }
    html=html.replace(/&quot;/g,"\"");
    html=html.replace(/&amp;/g,"&");
    html=html.replace(/&lt;/g,"<");
    html=html.replace(/&gt;/g,">");
    return html;
}
function DecodeHtmlAndInsertLineBreaks(html)
{
    if( html == null )
    {
        return "";
    }
    if( html.length == 0 )
    {
        return html;
    }
    html=html.replace(/&quot;/g,"\"");
    html=html.replace(/&amp;/g,"&");
    html=html.replace(/&lt;/g,"<");
    html=html.replace(/&gt;/g,">");
    html=html.replace(/\n/g,"<br />");
    return html;
}

function EnableTextSelection(root)
{
    var length = root.childNodes.length;
    for( var i=0; i<length; i++ )
    {
        DisableTextSelection(root.childNodes[i]);
    }
    
    if( root.style && !root.wasUnselectable )
    {
        root.onselectstart = null;
        root.unselectable = null;
        root.style.MozUserSelect = null;
        root.style.cursor = null;
    }
}

function DisableTextSelection(root)
{
    var length = root.childNodes.length;
    for( var i=0; i<length; i++ )
    {
        DisableTextSelection(root.childNodes[i]);
    }
    
    if( root.style )
    {
        root.wasUnselectable = (root.unselectable == "on");
        root.onselectstart = function() 
        {
            return false;
        };
        root.unselectable = "on";
        root.style.MozUserSelect = "none";
        root.style.cursor = "default";
    }
}

var editor;

function SetFontName(control,font)
{
    editor.execCommand("FontName",false,font);
}
function SetFontSize(control,size)
{
    editor.execCommand("FontSize",false,size);
}
function FormatSelection(tag)
{
    editor.execCommand("FormatBlock",false,"<"+tag+">");
}
function ExecCommand(cmd)
{
    editor.execCommand(cmd);
}
function Undo()
{
    editor.execCommand("Undo");
}
function TextRange()
{
    return editor.selection.createRange();
}
function AddLink()
{
    //identify selected text
    var sText = TextRange();
    if (sText.text != "")
    {
      //create link
      editor.execCommand("CreateLink");
      //change the color to indicate success
      if (sText.parentElement().tagName == "A")
      {
        sText.execCommand("ForeColor",false,"#FF0033");
      }
    }
    else
    {
        alert("Please select some text!");
    }   
}
function ExtractDateFromString(str,msg)
{
    try
    {
        var parts = str.split('/');
        return new Date(Number(parts[2]),Number(parts[1])-1,Number(parts[0]));
    }
    catch(e)
    {
        alert(msg);
        return null;
    }
}
function ToShortDateString(date)
{
    var dateOnly = date.substring(0,10);
    var parts = dateOnly.split('-');
    return parts[2]+"/"+parts[1]+"/"+parts[0];
}


function ScrollInit(container,width)
{
    if( scrollTimer )
    {
        clearInterval(scrollTimer);
    }
    scrollTimer = null;
    scrollDirection = 0;
    scrollStepCurrent = 0;
    scrollValue = 0;
    scrollPos = 0;
    scrollContainer = container;
    scrollWidth = document.getElementById(container).clientWidth;
}

function ScrollLeft()
{
    if( currentSection == 0 || scrollTimer )
    {
        return;
    }
    ScrollInit(scrollContainer);
    
    scrollDirection = -1;
    scrollPos = document.getElementById(scrollContainer).scrollLeft;
    scrollTimer = setInterval("Scroll()",scrollDelay);
    currentSection--;
}

function ScrollRight()
{
    if( (currentSection+1) == sections || scrollTimer )
    {
        return;
    }
    ScrollInit(scrollContainer);
    
    scrollDirection = 1;
    scrollPos = document.getElementById(scrollContainer).scrollLeft;
    scrollTimer = setInterval("Scroll()",scrollDelay);
	
    currentSection++;
}

function Scroll()

{
    scrollValue += scrollStep;
    if( scrollStepCurrent>=scrollSteps )
    {
        clearInterval(scrollTimer);
        scrollTimer = null;
        SetScroll(semi);
        return;
    }
    SetScroll(scrollValue);
}

function SetScroll(value)
{
    var d1 = Math.cos(value);
    var d2 = Math.cos(value+scrollStep);
    
    var pos = scrollPos + (Math.abs(d2-d1) * scrollDirection * scrollWidth * 0.5);
    var cntr = document.getElementById(scrollContainer);
    if( cntr ){
     cntr.scrollLeft = pos;
     }
    scrollPos = pos;
    scrollStepCurrent++;
}

function GetMouseInfo(e) 
{
    var pos = new Object();
    pos.X = 0;
    pos.Y = 0;
    if (!e) var e = window.event;
    if (e.pageX || e.pageY) 	
    {
	    pos.X = e.pageX;
	    pos.Y = e.pageY;
    }
    else if (e.clientX || e.clientY) 	
    {
        var scroll = GetScrollBarPos();
	    pos.X = e.clientX + scroll.X;
	    pos.Y = e.clientY + scroll.Y;
    }
    return pos;
}

function GetScrollBarPos()
{
    var x,y;
    if (self.pageYOffset) // all except Explorer
    {
	    x = self.pageXOffset;
	    y = self.pageYOffset;
    }
    else if (document.documentElement && document.documentElement.scrollTop)
	    // Explorer 6 Strict
    {
	    x = document.documentElement.scrollLeft;
	    y = document.documentElement.scrollTop;
    }
    else if (document.body) // all other Explorers
    {
	    x = document.body.scrollLeft;
	    y = document.body.scrollTop;
    }
    return {X:x,Y:y};
}

function GetLeft(e)
{
    if( IE )
    {
        return e.style.posLeft;
    }
    
    var v = e.style.left.split('p');
    return Number(v[0]);
}
function SetLeft(e,v)
{
    if( IE )
    {
        e.style.posLeft=v;
        return;
    }
    e.style.left=v+"px";
}
function PushHistory(url)
{
    window.history.go(url);
}