/*
 *  Project:    AD747
 *  Author(s):  Frank Oh
 *  Website:    www.it747.com
 *  Copyright:  This is the property of IT747.COM
 *              You are not allowed to change anything in this file
 *              without the permission of the owner.
 */


function
open_box( name )
{
    var w = open( '', name );
    w.focus();
}


function
win( name, msg )
{
    var w = window.open( '', name, '' );
    var d = w.document;

    d.open();
    d.write( msg );
    d.close();
}

function
dump_obj( obj )
{
    var msg = '';

    obj = get_object( obj );

    for ( var p in obj ) {
        msg += "<b>" + p + "</b>" + ": " + typeof( obj[ p ]) + ":" + obj[ p ] + "<br>" + "\n";
    }
    // alert( msg );
    win( 'dump', msg );

}

function
not_object( obj )
{
    var ret = false;

    if ( typeof( obj ) === typeof( 'string' ) ) {
        ret = true;
    }

    return ( ret );
}

function
get_object( obj )
{
    var o = obj;

    if ( not_object( o ) ) {
        var o = document.getElementById( o );
    }

    return ( o );
}

function
hide_object( obj )
{
    var o = get_object( obj );
    var style = o.style;

    style.visibility = 'hidden';
}

function
show_object( obj )
{
    var o = get_object( obj );
    var style = o.style;

    style.visibility = 'visible';

}

function
delete_object( obj )
{
    var o = get_object( obj );
    if ( o ) {
        o.parentNode.removeChild( o );

    }
}


function
get_absolute_left( obj_id )
{
    var obj = get_object( obj_id );
    var left = obj.offsetLeft;

    while ( obj.offsetParent ) {
        var parent = obj.offsetParent;
        left += parent.offsetLeft;
        obj = parent;
    }

    return ( left );

}


function
get_absolute_top( obj_id )
{
    var obj = get_object( obj_id );
    var top = obj.offsetTop;

    while ( obj.offsetParent ) {
        var parent = obj.offsetParent;
        top += parent.offsetTop;
        obj = parent;
    }

    return ( top );
}


function
create_element( element )
{
    var body = document.getElementsByTagName( "body" ).item(0);

    body.appendChild( element );

    return;
}

function
delete_element( element )
{
    var body = document.getElementsByTagName( "body" ).item(0);

    body.removeChild( element );

    return;
}

function
create_div( name, top, left, width, height )
{
    // <div class="" id="" name="" style=""></div>
    var id = name;
    var element;
    var ele = document.getElementsByName( name ).item( 0 );

    if ( ele ) {
        element = ele;
    } else {
        element = document.createElement( "div" );
        element.setAttribute( "id", id );
        element.setAttribute( "name", id );
        element.setAttribute( "class", id );
    }


    var css = "";
    css  = " " + "position: absolute; z-index: 10; visibility: visible;";
    css += " " + "top: " + top + "px; left: " + left + "px;";
    css += " " + "width: " + width + "px; height: " + height + "px;";
    css += " " + "background-color: white;";
    element.style.cssText = css;

    if ( !ele ) create_element( element );

    return ( element );
}

function
delete_div( name )
{
    var elements = document.getElementsByName( name );
    var n = elements.length;

    for ( var i=0; i<n; i++ ) {
        delete_element( elements.item( i ) );
    }
    return;
}


function
show_it( left, top, target_obj_id )
{
    var obj = document.getElementById( target_obj_id );
    var style = obj.style;

    style.position   = 'absolute';
    style.left       = left + 'px';
    style.top        = top  + 'px'
    style.zIndex     = 255;
    style.visibility = 'visible';

}

function
hide_it( target_obj_id )
{
    hide_object( target_obj_id );
}

function
hide_block( obj )
{
    var o = get_object( obj );

    if ( o ) {
        o.style.display = 'none';
    }
}

function
show_block( obj )
{
    var o = get_object( obj );

    if ( o ) {
        o.style.display = 'block';
    }
}



// resource: http://javascript.internet.com/forms/check-email.html
function
is_valid_email ( emailStr )
{
    var emailPat=/^(.+)@(.+)$/;
    var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
    var validChars="\[^\\s" + specialChars + "\]";
    var quotedUser="(\"[^\"]*\")";
    var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
    var atom=validChars + '+';
    var word="(" + atom + "|" + quotedUser + ")";
    var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
    var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");

    var matchArray=emailStr.match(emailPat);
    if (matchArray==null) {
        return false;
    }
    var user=matchArray[1];
    var domain=matchArray[2];

    if (user.match(userPat)==null) {
        return false;
    }

    var IPArray=domain.match(ipDomainPat);
    if (IPArray!=null) {
        return false;
    }

    var domainArray=domain.match(domainPat)
    if (domainArray==null) {
        return false;
    }

    var atomPat=new RegExp(atom,"g");
    var domArr=domain.match(atomPat);
    var len=domArr.length;
    if (domArr[domArr.length-1].length<2 ||
        domArr[domArr.length-1].length>3) {
       return false;
    }

    if (len<2) {
       return false;
    }

    return true;
}

function
is_valid_number( param )
{
  var numbers = '0123456789';

  if ( param == "") return false;

  for (i=0; i<param.length; i++) {
    if ( numbers.indexOf( param.charAt(i), 0 ) == -1 ) return false;
  }
  return true;

}

function
is_radio_button_checked( form, button_name )
{
    var checked = false;

    for (var n=0; n<form[ button_name ].length; n++)
    {
        if ( form[ button_name][n].checked) {
            checked = true;
        }
    }

    return ( checked );
}

function
get_radio_button_value( form, button_name )
{
    var value = false;

    for (var n=0; n<form[ button_name ].length; n++)
    {
        if ( form[ button_name][n].checked ) {
            value = form[ button_name][n].value;
        }
    }

    return ( value );
}

function
pdf_view( href )
{
    var w = open( href, '', 'toolbar=no,scrollbars=yes,resizable=yes,width=800' );
    w.focus();
}


function
tour( href )
{
    var w = open( href, '', 'toolbar=no,scrollbars=yes,resizable=yes,width=750' );
    w.focus();
}


function
doc( href )
{
    var w = open( href, '', 'toolbar=no,scrollbars=yes,resizable=yes,width=750' );
    w.focus();
}

function
map( href )
{
    var w = open( href, '', 'toolbar=no,scrollbars=yes,resizable=yes,width=800' );
    w.focus();
}

function
result_window( window_name )
{
    var w = open( '', window_name, 'toolbar=no,scrollbars=yes,resizable=yes,width=800' );
    w.focus();
}



function
big_image( href, name )
{
    var w = open( href, 'big_image', 'toolbar=no,scrollbars=yes,resizable=yes,width=650' );

    if ( !name ) name='';

    var s = '';
    s += '<html><head><title>' + name + '</title></head><body>';
    s += '<h2>' + name + '</h2>' + '<hr>';
    s += '<img src="' + href + '" />';
    s += '</body></html>';

    w.document.write( s );
    w.document.close();
    w.focus();

}


function
big_image_window( href, name )
{
    var w = open( href, 'big_image_window', 'toolbar=no,scrollbars=yes,resizable=yes,width=650' );

    if ( !name ) name='';

    var s = '';
    s += '<html><head><title>' + name + '</title></head><body>';
    s += '<h2>' + name + '</h2>' + '<hr>';
    s += '<img src="' + href + '" />';
    s += '</body></html>';

    w.document.write( s );
    w.document.close();
    w.focus();

}


/*
 * ! Maps AJAX API
 *
 * http://developer.yahoo.com/maps/ajax/index.html
 * Version 3 - Beta
 *
 */

function
Listing( address, marker_image, auto_expand_html )
{
    this.address            = address;
    this.marker_image       = marker_image;
    this.auto_expand_html   = auto_expand_html;
}


function
yahoo_map( map_container_id, zoom_level, center_address, listing_list )
{
    var map = new YMap( document.getElementById( map_container_id ));
    map.addZoomLong();
    map.setMapType( YAHOO_MAP_REG );
    map.addTypeControl();
    map.drawZoomAndCenter( center_address, zoom_level );

        // for each listing
    var n = listing_list.length;
    for ( var i=0; i<n; i++ ) {
        var l = listing_list[ i ];

        var image = new YImage();
        image.src = l.marker_image;
        image.size = new YSize( 20, 20 );
        image.offsetSmartWindow = new YCoordPoint( 0, 0 );

        var marker = new YMarker( l.address, image );
        marker.addAutoExpand( l.auto_expand_html );
        map.addOverlay(marker);
    }
}


/*
 * Google Maps API Version 2
 *
 * http://www.google.com/apis/maps/documentation/
 *
 */

function js_unescape( str )
{
    var r = str;

    r = r.replace( /%22%/g, "\"" );
    r = r.replace( /%27%/g, "'" );

    return ( r );
}

function
Map_object( lat, lon, image, info )
{
    this.lat    = lat;
    this.lon    = lon;
    this.image  = image;
    this.info   = info;
}

function
Map_config( map_area_id, zoom )
{
    this.map_area_id = "map_area";
    this.zoom = 13;

    if ( map_area_id )  this.map_area_id = map_area_id;
    if ( zoom )         this.zoom = zoom;
}


function
google_map( map_container_id, zoom_level, center_object, object_list, map_type )
{

        // optional: object_list, map_type
        // map_type: G_NORMAL_MAP G_SATELLITE_MAP G_HYBRID_MAP
    var map = new GMap2( document.getElementById( map_container_id ) );
    map.addControl( new GLargeMapControl() );
    map.addControl( new GMapTypeControl() );

        // Create our marker icon - base
    var base_icon    = new GIcon();
    base_icon.image      = "images/map_icon_for_sale.gif";
    base_icon.shadow     = "images/map_icon_shadow.gif";
    base_icon.iconSize   = new GSize( 40, 44 );
    base_icon.shadowSize = new GSize( 60, 44 );
    base_icon.iconAnchor = new GPoint( 10, 40 );
    base_icon.infoWindowAnchor = new GPoint( 10, 25 );

        // center object
    var point = new GLatLng( center_object.lat, center_object.lon );
    map.setCenter( point, zoom_level );
    if ( map_type ) {
        map.setMapType( map_type );
    }

    var icon    = new GIcon( base_icon );
    icon.image  = center_object.image;
    var marker  = new GMarker( point, icon );
    if ( center_object.info ) {
        marker.info = js_unescape( center_object.info );
        GEvent.addListener( marker, "mouseover", function() { this.openInfoWindowHtml( this.info ); } );
        GEvent.addListener( marker, "click", function() { this.openInfoWindowHtml( this.info ); } );
    }
    map.addOverlay( marker );
    if ( center_object.info ) {
        marker.openInfoWindowHtml( marker.info );
    }

        // object list
    if ( object_list ) {
        for (var i=0; i<object_list.length; i++ ) {
            var o = object_list[ i ];
            var point = new GLatLng( o.lat, o.lon );
            var icon  = new GIcon( base_icon );
            icon.image = o.image;
            var marker = new GMarker( point, icon );
            if ( o.info ) {
                marker.info = js_unescape( o.info );
                GEvent.addListener( marker, "mouseover", function() { this.openInfoWindowHtml( this.info ); } );
                GEvent.addListener( marker, "click", function() { this.openInfoWindowHtml( this.info ); } );
            }
            map.addOverlay( marker );
        }
    }
}

function
Map_object_v2( id, lat, lon, image, info )
{
    this.id     = id;
    this.lat    = lat;
    this.lon    = lon;
    this.image  = image;
    this.info   = info;
}

function
google_map_config_v2()
{
        // set defaults
        // Use:
        //  var config = google_map_config_v2()
        //  config.zoom_level = 5;
        //  google_map_v2( center_object, object_list, config );

    this.map_area_id    = "map_area";
    this.zoom_level     = 13;
    this.map_control    = 1;    // 0: no control 1: GLargeMapControl() 2: GSmallMapControl()
    this.map_type       = 1;    // 0: no control 1: G_NORMAL_MAP 2: G_SATELLITE_MAP 3: G_HYBRID_MAP
}

function
google_map_base_icon()
{
        // Create our marker icon - base
    var base_icon = new GIcon();
    base_icon.image      = "images/map_icon_for_sale.gif";
    base_icon.shadow     = "images/map_icon_shadow.gif";
    base_icon.iconSize   = new GSize( 40, 44 );
    base_icon.shadowSize = new GSize( 60, 44 );
    base_icon.iconAnchor = new GPoint( 35, 40 );
    base_icon.infoWindowAnchor = new GPoint( 10, 25 );

    return ( base_icon );
}

function
google_map_v2( center_object, object_list, map_config )
{
    var config = map_config? map_config : new google_map_config_v2();

        // optional: map_config

    var map = new GMap2( document.getElementById( config.map_area_id ) );

    var map_control;
    switch ( config.map_control ) {
        case    0:
            map_control = false;
            break;
        case    1:
            map_control = new GLargeMapControl();
            break;
        case    2:
            map_control = new GSmallMapControl();
            break;
        default:
            map_control = new GLargeMapControl();
            break;
    }
    if ( map_control ) map.addControl( map_control );

    var map_type;
    switch ( config.map_type ) {
        case    0:
            map_type = false;
            break;
        case    1:
            map_type = G_NORMAL_MAP;
            break;
        case    2:
            map_type = G_SATELLITE_MAP;
            break;
        case    3:
            map_type = G_HYBRID_MAP;
            break;
        default:
            map_type = G_NORMAL_MAP;
            break;
    }
    if ( map_type ) map.addControl( new GMapTypeControl() );

        // center object
    var point = new GLatLng( center_object.lat, center_object.lon );
    map.setCenter( point, config.zoom_level );
    if ( map_type ) map.setMapType( map_type );

    if ( center_object.id != 0 ) {
        var base_icon = google_map_base_icon();
        var icon    = new GIcon( base_icon );
        icon.image  = center_object.image;
        var marker  = new GMarker( point, icon );
        if ( center_object.info ) {
            marker.info = js_unescape( center_object.info );
            GEvent.addListener( marker, "mouseover", function() { this.openInfoWindowHtml( this.info ); } );
            GEvent.addListener( marker, "click", function() { this.openInfoWindowHtml( this.info ); } );
        }
        map.addOverlay( marker );
        if ( center_object.info ) {
            marker.openInfoWindowHtml( marker.info );
        }
    }
        // object list
    if ( object_list ) {
        for (var i=0; i<object_list.length; i++ ) {
            var o = object_list[ i ];
            var point = new GLatLng( o.lat, o.lon );
            var icon  = new GIcon( base_icon );
            icon.image = o.image;
            var marker = new GMarker( point, icon );
            if ( o.info ) {
                marker.info = js_unescape( o.info );
                GEvent.addListener( marker, "mouseover", function() { this.openInfoWindowHtml( this.info ); } );
                GEvent.addListener( marker, "click", function() { this.openInfoWindowHtml( this.info ); } );
            }
            map.addOverlay( marker );
        }
    }

    return ( map );
}

function
google_map_center( map, map_object, zoom_level )
{
    var zoom = zoom_level? zoom_level : 10;

    if ( !map ) return;
    if ( !map_object ) return;

    var point = new GLatLng( map_object.lat, map_object.lon );
    map.setCenter( point, zoom_level );

    var base_icon = google_map_base_icon();
    var icon    = new GIcon( base_icon );
    icon.image  = map_object.image;
    var marker  = new GMarker( point, icon );
    if ( map_object.info ) {
        marker.info = js_unescape( map_object.info );
        GEvent.addListener( marker, "mouseover", function() { this.openInfoWindowHtml( this.info ); } );
        GEvent.addListener( marker, "click", function() { this.openInfoWindowHtml( this.info ); } );
    }
    map.addOverlay( marker );
    if ( map_object.info ) {
        marker.openInfoWindowHtml( marker.info );
    }

}

/* vim: set expandtab sw=4 ts=4 sts=4: */

