﻿var pFix = "ctl00_cphMain_"
var isIE = document.all ? true : false;
var isNS = document.layers ? true : false;

function $Get( objId ) { return document.getElementById( objId ) }
function $Set( objId, value ) { if($Get( objId )) $Get( objId ).value = value }

var docX, docY; // mouse position after DetectMousePosition
function DetectMousePosition( e )
{
   if( e )
   {
      if( typeof( e.pageX ) == 'number' )
      {
         docX = e.pageX;
         docY = e.pageY;
      }
      else
      {
         docX = e.clientX;
         docY = e.clientY;
      }
   }
   else
   {
      e = window.event;
      docX = e.clientX;
      docY = e.clientY;
      if( document.documentElement
        && ( document.documentElement.scrollTop
            || document.documentElement.scrollLeft ) )
      {
         docX += document.documentElement.scrollLeft;
         docY += document.documentElement.scrollTop;
      } 
      else if( document.body
         && ( document.body.scrollTop
             || document.body.scrollLeft ) )
      {
         docX += document.body.scrollLeft;
         docY += document.body.scrollTop;
      }
   }
}

function RadioListItemChecked( rbl )
{
    for ( i = 0; i < rbl.children.length; i++ )
        if ( rbl.children[ i ].checked == true )
            return i;
    return -1;
}

function HideWarn( obj ) { if ( $Get ( pFix + obj ) ) $Get ( pFix + obj ).style.display = 'none'; }
function SetWarn( obj, Flag ) { $Get ( pFix + obj ).style.display = ( Flag ) ? 'none' : ''; return Flag; }

function HideRbWarn( obj ) { $Get ( obj ).style.display = 'none'; }

function CheckEmail()
{
    if ( $Get( pFix + "fmtWarnEmail" ).style.display != 'none' )
    { 
        if ( CheckEmailValidity( $Get( pFix + "vEmail" ).value ) )
            $Get( pFix + "fmtWarnEmail" ).style.display = 'none';
    }
}

function onlyDigits( e ) 
{
    var key = ( isIE ) ? e.keyCode : e.which
    if ( ! ( ( ( key > 47 && key < 58 ) || key == 8 ) && e.ctrlKey != true ) )
    {
        if ( isIE )
            e.keyCode = 0
        return false
    }
    else
        return true
}

function onlyDecimal( e, obj )
{
    var key = ( isIE ) ? e.keyCode : e.which;
    if ( ( ( key < 48 || key > 57 ) && key != 8 && key != 46 && key != 110 && key != 190 ) || e.ctrlKey == true )
	{
	    if ( isIE )
	        e.keyCode = 0
		if ( obj != null )
			$Get( pFix + obj ).style.display = ''
		return false
	}
	else if ( obj != null )
		$Get( pFix + obj ).style.display = 'none';
	return true
}

function CheckPasswordIdentical( pass, conf, rfWarn )
{
    var p1 = Trim ( $Get( pass ).value );
    var p2 = Trim ( $Get( conf ).value );
    $Get( rfWarn ).style.display = ( ( p1 != p2 ) ? 'inline' : 'none' )
    return ( p1 == p2 )
}

function CheckEmailValidity( obj )
{
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if ( filter.test( obj ) )
		return true;
	else
		return false;
}

function Check2EmailValidity( email, rfWarn )
{
    var vEmail = Trim ( $Get( email ).value )
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	var res = filter.test( vEmail )
    $Get( rfWarn ).style.display = ( (!res) ? 'inline' : 'none' )
    return res
}

function Trim( STRING )
{
    STRING = LTrim( STRING );
    return RTrim( STRING );
}

function RTrim( STRING )
{
    while( STRING.charAt( ( STRING.length -1 ) ) == " " )
    {
        STRING = STRING.substring( 0, STRING.length - 1 );
    }
    return STRING;
}

function LTrim( STRING )
{
    while( STRING.charAt == " " )
    {
        STRING = STRING.replace( STRING.charAt, "" );
    }
    return STRING;
}

function TextCounter( maxlimit, txtRemainsLength, vText ) 
{
    var txtRemain = $Get( txtRemainsLength );
    var txt = $Get( vText );
    
    if ( txt.value.length > maxlimit )
	    txt.value = txt.value.substring( 0, maxlimit );
    else
	    txtRemain.value = maxlimit - txt.value.length;
}

function printf()
{
	document.forms[0].focus();
	window.print();
}

function ShowMyDiv( divId, e )
{
    DetectMousePosition( e )
    var mydiv = $Get( divId );
    var divS = $Get( 'DivShim' + divId );
    if ( ! window.event )
    {
        var objMydiv = mydiv.cloneNode(true);
        var objDivS = divS.cloneNode(true);
        mydiv.parentNode.removeChild( mydiv );
        divS.parentNode.removeChild( divS );
        document.body.appendChild( objMydiv );
        document.body.appendChild( objDivS );
        mydiv = $Get( divId );
        divS = $Get( 'DivShim' + divId )
    }
    tempX = docX
    tempY = docY
    mydiv.style.left = '350px'
    mydiv.style.top = ((tempY > 300) ? tempY - 100 : tempY + 10) + 'px';
    divS.style.width = mydiv.offsetWidth;
    divS.style.height = mydiv.offsetHeight;
    divS.style.top = mydiv.style.top;
    divS.style.left = mydiv.style.left;
    divS.style.zIndex = mydiv.style.zIndex - 1;
    divS.style.display = '';
    mydiv.style.display = '';
    Effect.Appear( divId, {duration:.3} );
}

function ShowMyDivAt( divId, Xpos, Ypos )
{
    var mydiv = $Get( divId )
    var divS = $Get( 'DivShim' + divId )
    mydiv.style.position = 'absolute'
    mydiv.style.display = ''
    mydiv.style.left = Xpos + 'px'
    mydiv.style.top = Ypos + 'px'
    divS.style.width = mydiv.offsetWidth
    divS.style.height = mydiv.offsetHeight
    divS.style.top = mydiv.style.top
    divS.style.left = mydiv.style.left
    divS.style.zIndex = mydiv.style.zIndex - 1
    divS.style.display = ''
    Effect.Appear( divId, {duration:.3} )
}

function HideMyDiv( divId )
{
    $Get( 'DivShim' + divId ).style.display = 'none';
    var mydiv = $Get( divId );
    Effect.Fade( divId, {duration:.3} );
    mydiv.style.display = 'none';
}

function ShowStatement( Id, StatementIndex, CharIndex, CharDelay, StatementDelay )
{
    if ( $Get( Id ) == null )
        return;
    $Get( Id ).innerHTML = MyStatements[ StatementIndex ].substr( 0, CharIndex )
    if ( CharIndex < MyStatements[ StatementIndex ].length  )
        setTimeout( "ShowStatement('" + Id + "'," + StatementIndex + "," + ( CharIndex + 1 ) + "," + CharDelay + "," + StatementDelay + ")", CharDelay )
    else if ( MyStatements.length > ( StatementIndex + 1 ) )
        setTimeout( "ShowStatement('" + Id + "'," + ( StatementIndex + 1 ) + ", 1," + CharDelay + "," + StatementDelay + ")", StatementDelay )
    else
        setTimeout( "ShowStatement('" + Id + "', 0, 1," + CharDelay + "," + StatementDelay + ")", StatementDelay )
}

function ValidateField( field, rfWarn )
{
    var Cond
    var obj = $Get( field )
    if ( null != obj )
        Cond = ( obj.value == '' && Trim( obj.value ) == '' )
    
    var obj = $Get( rfWarn )
    if ( null != obj )
        obj.style.display = ( Cond ? 'inline' : 'none' )
    return ( ! Cond )
}

function ValidateListField( field, rfWarn )
{
    var Cond
    var obj = $Get( field )
    if ( null != obj )
        Cond = ( obj.value == '0' || obj.value == '' )
    
    var obj = $Get( rfWarn )
    if ( null != obj )
        obj.style.display = ( Cond ? 'inline' : 'none' )
    return ( ! Cond )
}

function SetSelectedValue( List, Value )
{
    if ( Value && List.options.length > 0 )
    {
        for ( i = 0; i < List.options.length; i++ )
        {    
            if ( List.options[ i ].value == Value )
                List.options[ i ].selected = true
            else
                List.options[ i ].selected = false
        }
    }
}

var destChildDD
var valueToSelect
var SelFirst = false
var SelChild = 0

function FillList( MyDocumentElement )
{
    var x = MyDocumentElement.getElementsByTagName('root');
    if ( null == x )
        return
    destChildDD.disabled = false
    for ( j = 0; j < x[ 0 ].childNodes.length; j++ )
    {
        var oNode = x[ 0 ].childNodes[ j ];
        if ( oNode != null )
        {
            destChildDD[ j ] = new Option( oNode.getAttribute( 'n' ), oNode.getAttribute( 'v' ) );
            if ( SelChild > 0 && SelFirst == false && oNode.getAttribute( 'v' ) == SelChild )
                destChildDD[ j ].selected = true
        }
    }
    SetSelectedValue( destChildDD, valueToSelect );
    if ( SelFirst && destChildDD.options.length > 0 )
    {
        destChildDD.options[1].selected = true
        SelFirst = false
    }    
}

function LoadChildList( g_server, TargetId, SrcId )
{
	var Target  = $Get( TargetId )
	var Src = $Get( SrcId )
	var ParId = Src.options[ Src.selectedIndex ].value
	Target.options.length = 0
	if ( ParId != '0' )
	{
		destChildDD = Target
		//valueToSelect = valToSel;
	    importXML( g_server + ParId , 'FillList' )
	}
	else
	    Target.disabled = true
}

function FloorFilter( from, ofFloorsObj, MaxFloors, choose )
{
    var ddlOf = $Get( ofFloorsObj )
    ddlOf.options.length = 0
    if ( from < 50 || from == 253 || from == 252 || from == 254 ) // Not a Special Floor
    {
        if ( from == 253 || from == 252 || from == 254 )
            from = 1
        i = from
        var option = new Option( choose , 0 )
        ddlOf.disabled = ''
        ddlOf.options[ 0 ] = option
        var k = 1
        for ( ; i <= MaxFloors; i++ )
        {
            var option = new Option( i, i )
            ddlOf.options[ k++ ] = option
        }
    }
    else
    {
        ddlOf.options.length = 0
        ddlOf.disabled = 'disabled'
    }
}

function CreateControl(DivID, ObjectID, CLSID, WIDTH, HEIGHT, URL, AUTOSTART, WinMode)
{
	var sParams = '';
	if(CLSID == '') 
	{
		CLSID = '"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codeBase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" '
		sParams = 
			'<PARAM NAME="BGColor" VALUE="FFFFFF">\
			<PARAM NAME="Play" VALUE="-1">\
			<PARAM NAME="Loop" VALUE="-1">\
			<PARAM NAME="SeamlessTabbing" VALUE="1">\
			<PARAM NAME="Quality" VALUE="High">\
			<PARAM NAME="FlashVars" VALUE="">\
			<PARAM NAME="SAlign" VALUE="">\
			<PARAM NAME="Menu" VALUE="0">\
			<PARAM NAME="Base" VALUE="">\
			<PARAM NAME="AllowScriptAccess" VALUE="always">\
			<PARAM NAME="Scale" VALUE="ExactFit">\
			<PARAM NAME="DeviceFont" VALUE="0">\
			<PARAM NAME="EmbedMovie" VALUE="0">\
			<PARAM NAME="SWRemote" VALUE="">\
			<PARAM NAME="MovieData" VALUE="">\
			<EMBED src="' + URL + '" width="' + WIDTH + '" height="' + HEIGHT + '"\
			 wmode="transparent" quality="high" name="' + ObjectID + '"></EMBED>\
			</OBJECT>';
	}
	var d = document.getElementById(DivID);
	if (null == WinMode)
		WinMode = "Transparent";
	d.innerHTML = '<OBJECT swliveconnect="true" classid='
		+ CLSID + ' id=' + ObjectID + ' width=' 
		+ WIDTH + ' height=' + HEIGHT + '>' 
		+ '<PARAM NAME="Movie" value=' + URL + '> ' 
		+ '<PARAM NAME="autoStart" value=' + AUTOSTART + '> ' 
		+ '<PARAM NAME="WMode" VALUE=' + WinMode 
		+ '> ' + sParams;
}

function ChangSmallPic( tdId, ImgId, imgTitleId, imgSrc, imgTitle, effect )
{
    var vTdImg = $Get( tdId )
    var objImg = $Get( ImgId )
    var ImgTitle = $Get( imgTitleId )
    ImgTitle.innerHTML = imgTitle
    if ( vTdImg.filters )
        vTdImg.filters[ effect % 2 ].apply()
    objImg.src = imgSrc
    if ( vTdImg.filters )
        vTdImg.filters[ effect % 2 ].play()
    objImg.onclick.call()
}


// Add Selected List Item Inside the target List.
function MoveIn( lbSrcId, lbTgtId, HdId )
{
    var lbSrc = document.getElementById( lbSrcId )
    var lbTgt = document.getElementById( lbTgtId )
    if ( lbSrc.options.length > 0 )
    {
        for ( i = 0; i < lbSrc.options.length; i++ )
        {
            if ( lbSrc.options[ i ].selected == true )
            {
                if ( FindByValue( lbTgt, lbSrc.options[ i ].value ) >= 0 )
                    continue
                var opt = new Option( lbSrc.options[ i ].innerHTML, lbSrc.options[ i ].value )
                lbTgt.options[ lbTgt.options.length ] = opt
            }
        }
    }
    SaveAll( lbTgtId, HdId )
}

// Move List Item Out.
function MoveOut( lbTgtId, HdId )
{
    var lbTgt = document.getElementById( lbTgtId )
    if ( lbTgt.options.length > 0 )
    {
        for ( k = 0; k < lbTgt.options.length; k++ )
        {
            if ( lbTgt.options[ k ].selected == true )
                lbTgt.remove( k )
        }
    }
    SaveAll( lbTgtId, HdId )
}

// Find Item in List By Value and return its index.
function FindByValue( lb, val )
{
    for ( j = 0; j < lb.options.length; j++ )
    {
        if ( lb.options[ j ].value == val )
            return j;
    }
    return -1;   
}

/// Set All items in List Box to Selected
function SaveAll( lbId, HdId )
{
    var hd = document.getElementById( HdId )
    var lb = document.getElementById( lbId )
    hd.value = ''
    if ( lb.options.length > 0 )
    {
        for ( n = 0; n < lb.options.length; n++ )
        {    
            hd.value = hd.value + lb.options[ n ].value + ','
        }
        hd.value = hd.value.substring( 0, hd.value.length - 1 )
    }
}