
/**
 *  Browser object constructor
 *  returns the type of browser this "Is"... :)
 */
function Is() {

    var agent   = navigator.userAgent.toLowerCase();
    this.major  = parseInt(navigator.appVersion);
    this.minor  = parseFloat(navigator.appVersion);
    this.ns     = ((agent.indexOf('mozilla')!=-1) && ((agent.indexOf('spoofer')==-1) && (agent.indexOf('compatible') == -1)));
    this.ns2    = (this.ns && (this.major == 2));
    this.ns3    = (this.ns && (this.major == 3));
    this.ns4b   = ((this.ns && (this.minor < 4.04)) || (this.ns && (agent.indexOf("os/2") != -1)));
    this.ns4    = (this.ns && (this.major >= 4) && (this.major < 5));
    this.ns6    = (this.ns && (this.major >= 5));
    this.ie     = (agent.indexOf("msie") != -1);
    this.ie3    = (this.ie && (this.major == 2));
    // ie4 means '4', '5', & '6'
    this.ie4    = (this.ie && (this.major >= 4));
    // ie5 means '5' & '6'
    this.ie5    = (this.ie && (this.major >= 5));
    // ie6 means just '6' (well until there's a '7' anyway)
    this.ie6    = (this.ie && (this.major >= 6));
    this.op3    = (agent.indexOf("opera") != -1);
    this.win    = (agent.indexOf("win")!=-1);
    this.mac    = (agent.indexOf("mac")!=-1);
    this.unix   = (agent.indexOf("x11")!=-1);
    
    return this;
}

// create the browser-type object
var is = new Is();

/**
 *  returns the stylesheet tag for Netscape version 4 browsers
 */
function getNetscapeStyleSheet () {
    
    var ns_stylesheet = '';
    
    if ( is.ns4 ) {
        ns_stylesheet = '<' + 'link rel="stylesheet" type="text/css" href="/css/nsstyle.css">';
    }
    
    return ns_stylesheet;
}

document.write( getNetscapeStyleSheet() );




/**
 * Extend the Function Object to allow for automatic inheritence configuration
 *  - see http://phrogz.net/JS/Classes/OOPinJS2.html for details
 */
Function.prototype.inheritsFrom = function( parentClassOrObject ){
	if ( parentClassOrObject.constructor == Function )
	{
		//Normal Inheritance
		this.prototype = new parentClassOrObject;
		this.prototype.constructor = this;
		this.prototype.parent = parentClassOrObject.prototype;
	}
	else
	{
		//Pure Virtual Inheritance
		this.prototype = parentClassOrObject;
		this.prototype.constructor = this;
		this.prototype.parent = parentClassOrObject;
	}
	
	// R.F. added this line to provide access to base-class's static methods
	this.parent = parentClassOrObject;
	
	return this;
}





/**
 * Builds a Location Object with properties that can be
 * consistently used throughout the site
 *
 * returns an empty initialized Object, with Properties and Methods
 */
function Location ( title, url, email ) {
    
    this.title = title;
    this.url = url;
	this.email = email;
	this.fqurl = make_fullyqualified_url( url );
    this.makeLink = makeLocationLink;
    
    return this;
}

/**
 * Location Object method
 *
 * Builds a hyperlink HREF tag using the Properties of the
 * Object that calls this method
 *
 * returns a String formated HTML hyperlink tag
 */
function makeLocationLink () {

    var href = (
          '<a href="' + this.url + '" target="_blank">'
        + this.title
        + '</a>'
    );
    
    return href;
}





function make_fullyqualified_url ( url ) {
	
	var fqurl = url;
	
	if ( url != '' && url.indexOf( 'http' ) != 0 ) {
		fqurl = location.protocol + '//' + location.host + url;
	}
	
	return fqurl;
}




/**
 *  param menu_array an array containing Objects with the following properties:
 *                       - title (the label to display in each <option> tag)
 *                       - url (the value attributed to each <option> tag)
 *
 *  returns html for the <option> tags to write inside existing <select> tags...
 */
function build_dropdown_menu( form, select_name, menu_array, selected ) {
    
    var select_object 	= eval( 'document.' + form + '.' + select_name );
    var idx 			= 0;
	var selected_idx	= -1;
    
	for ( entry in menu_array ) {
    
        var label   		= menu_array[ entry ].title;
        var value     		= menu_array[ entry ].url;
		var is_selected		= false;
        idx         		= select_object.options.length;
		
		if ( value == selected && value != '' ) {
			
			// bug in IE: defer setting selected to next loop iteration (?? DAHHH?!?)
			if ( is.ie ) {
				selected_idx = idx + 1; // store the idx that we should set "selected" for
				//alert (current_idx + "\n" + idx );
			} else {
				// if not IE, then set selected as it should be
				is_selected = true;
			}
		}
		
		
		if ( is.ie && selected_idx == idx ) {
			//alert("setting selected to : " + selected + "\n" + idx + ": " + label + " " + value + " " + is_selected + " " + selected );
			is_selected = true;
		}
		
		
        // create option in dropdown
        select_object.options[ idx ] = new Option( label, value, false, is_selected ); 
        
    }
}




/**
 * activates a link to the URL mapped to the drop-down list
 *
 * param String select_field The name of the <SELECT> dropdown which called this.
 */
function gotoLocation( select_field ) {
    
    var location_url = select_field.options[select_field.selectedIndex].value;
    
    if( location_url.indexOf('http') != -1 ) {
	location.href = location_url;  
    } else if( location_url != '#' ) {
        location.href = getSourceHost() + location_url;
    }
}




/**
 * For both development servers and master.com references (as well as any other
 * third-party external servers) - references relative url's to the correct source
 * host-server
 *
 * returns a String in the form of: 'http://www.mysourcehost.com'
 */
function getSourceHost() {
    
    // a list of hosts we use
    var ourHostsArray = new Array(
        'www.sivananda.org',
        '66.98.154.59',
	'cwh',
	'65.39.135.182'
    );
    
    var defaultHost = 'www.sivananda.org';
    var hostReferrer = document.referrer;
    var srcHost = '';
    
    var locationArr = new Array( "", "", defaultHost ); //initialize
      
    
    // if we got to this page via a GET request, 
    // then locate the host portion of referrer string
    if ( hostReferrer != "" ) {
        locationArr = hostReferrer.split( '/' );        
    }
    
    // if we were referred by an offsite host (i.e. as in a search result by master.com)
    // then set source-host to the default host;
    // else set source-host to whatever is in locationArr[2], and break
    for ( hostIdx in ourHostsArray ) {
        var ourHost = ourHostsArray[ hostIdx ];
        var isOffsiteHost = ( hostReferrer.indexOf( ourHost ) == -1 );
        
        // referred from offsite-host, use default source-host
        if ( isOffsiteHost == true ) {
            srcHost = defaultHost;
        } else {
            srcHost = locationArr[2];
            break;
        }
    }
    
    return 'http://' + srcHost;
}





/** 
 * draws a horizontal rule
 * param width the width in pixels
 * param height the height in pixels
 * param padding the space above and below the HR
 * param colour the colour of the HR
 * returns an HTML table formatted to draw a line
 */         
function hr (width, height, padding, colour) {
    var HR = new String (
                    '<table width="100%" cellpadding="0" cellspacing="0" border="0">'
                +   '<tr><td>'
                +       '<img src="/images/blank.gif" width="1" '
                +       'height="' + padding + '" border="0" />'
                +   '</td></tr>'
                +   '<tr><td bgcolor="' + colour + '" width="100%">'
                +       '<img src="/images/blank.gif" width="' + width + '" '
                +       'height="' + height + '" border="0" />'
                +   '</td></tr>'
                +   '<tr><td>'
                +       '<img src="/images/blank.gif" width="1" '
                +       'height="' + padding + '" border="0" />'
                +   '</td></tr>'
                +   '</table>');
                
     return HR;
}



function omPageLink () {
    
    var isHomePage = false;
    var baseURL = location.protocol + '//' + location.host;
    
    if ( location.href == baseURL
                        ||
         location.href == baseURL + '/'
                        ||
         location.href == baseURL + '/index.html' ) {
         
         isHomePage = true;
    }
    
    if ( isHomePage == false ) {    
        document.write( '<a href="/index.html" onMouseOut="document.ompage.src=' + "'/images/nav_ompage_off.gif'" + ';"  onMouseOver="document.ompage.src=' + "'/images/nav_ompage_on.gif'" + ';"><img src="/images/nav_ompage_off.gif" width="63" height="18" border="0" alt="OM Page" name="ompage" id="ompage"></a>' );
    }
}





/**
 * opens up a smaller resizeable window with the referenced URL
 *
 * param url the url of the page to load into the pop-up window
 *
 */
function popUp (url) {
    var myWinWidth = 570;
    var myBuffer = 40;
    var myWinLeft = screen.availWidth - (myWinWidth + myBuffer);
    var winOptions = new String (
                          "left=" + myWinLeft + ",top=50,width=" + myWinWidth 
                        + ",height=350,resizable,scrollbars,status,menubar,toolbar,location"
                     );
    var myWin = open (url, "popUpw", winOptions);
    
    myWin.focus();    
}


/**
 * date function
 * returns a formatted date
 */
function writedate () {

     var m = new Date();
     var d = m.getDate();
     var y = m.getFullYear();
     var mo = m.getMonth(); 
     
     var month = new Array (
        "January", "February", "March", "April", "May", "June",
        "July", "August", "September", "October", "November", "December"
     );
     
     return ( month[mo] + " " + d + ",  " + y );
}

