// Copyright 2004 Zerve, Inc.

// Note: these compatability functions dont give accurate values for safari

// Some functions from quirksmode.org
function get_view_area_width() {
	var x;
	if (self.innerHeight) // all except Explorer
	{
		x = self.innerWidth;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
	// Explorer 6 Strict Mode
	{
		x = document.documentElement.clientWidth;
	}
	else if (document.body) // other Explorers
	{
		x = document.body.clientWidth;
	}
	return x;
}

function get_view_area_height() {
	var y;
	if (self.innerHeight) // all except Explorer
	{
		y = self.innerHeight;	
	}
	else if (document.documentElement && document.documentElement.clientHeight)
	// Explorer 6 Strict Mode
	{
		y = document.documentElement.clientHeight;
	}
	else if (document.body) // other Explorers
	{
		y = document.body.clientHeight;
	}
	return y;
}

function get_page_width() {
	var x;
	var test1 = document.body.scrollHeight;
	var test2 = document.body.offsetHeight
	if (test1 > test2) // all but Explorer Mac
	{
		x = document.body.scrollWidth;
	}
	else // Explorer Mac;
	//would also work in Explorer 6 Strict, Mozilla and Safari
	{
		x = document.body.offsetWidth;
	}
	return x;
}		     								

function get_page_height() {
	var y;
	var test1 = document.body.scrollHeight;
	var test2 = document.body.offsetHeight
	if (test1 > test2) // all but Explorer Mac
	{
		y = document.body.scrollHeight;
	}
	else // Explorer Mac;
	//would also work in Explorer 6 Strict, Mozilla and Safari
	{
		y = document.body.offsetHeight;
	}
	return y;
}

// Supposed to resize a popup window to content size
// We cant rely on this function it does not work in IE right now
// It can be improved later.

function zerve_resizeme( adjustmentX, adjustmentY ) {

	if( window.sizeToContent ){ // Gecko
	
		// Due to buggy sizeToContent function in older gecko based browsers
		// we only use it if we know it will work.
		var ua = navigator.userAgent.toLowerCase();
		var isFx1_0 = ( ua.indexOf( 'firefox' ) != -1 && parseFloat( ua.substring( ua.lastIndexOf( 'firefox/' ) + 8, ua.lastIndexOf( 'firefox/' ) + 11 ) ) <= 1.0 );

		if( !isFx1_0 ) { // firefox 1.0
			// For Firefox 1.5 + 
			window.sizeToContent( );
			return true;
		}
		
	} 

	if( document.getElementById( 'content' ) ) {

		// For IE & Safari
		
		// Note: clientWidth/Height seem to work better then offsetWidth/Height in Safari and IE
		// clientHeight does not work well in Safari so we use offsetHeight for Safari
		
		var content = document.getElementById( 'content' );

		//alert( 'clientHcon:'+content.clientHeight +',clientWcon:'+content.clientWidth );
		//alert( 'clientHwin:'+document.body.clientHeight +',clientWwin:'+document.body.clientWidth );
		
		// adjust for 10px padding on body that all out popups have
		// this is because we dont want to eliminate the padding after we resize
		var adjX,adjY;
		if( adjustmentX ) {
			adjX = adjustmentX;
		} else {
			adjX = 22;
		}
		
		if( adjustmentY ) {
			adjY = adjustmentY;
		} else {
			adjY = 22;
		}
		
		var view_height = get_view_area_height( );
		var resX = content.clientWidth-document.body.clientWidth + adjX;
		var resY = content.clientHeight - view_height + adjY;

		window.resizeBy( resX, resY );

	} else {

		// old behaviour, pretty useless
		window.resizeBy( 750 - get_view_area_width() , 0 ) ;	
	}

	return true;
}

function zerve_sizeto_image( ) {
	if(! document.images[0] ) {
		return;
	}
	iWidth = document.images[0].width;
	iHeight = document.images[0].height;
	xView = get_view_area_width();
	yView = get_view_area_height();
	window.resizeBy( iWidth-xView, iHeight-yView );
}

// name: window name (dont use spaces)
// popclass: see zerve_config() for different classes
// width,height: overriede the width/height of the popclass
// Pass null for popclass for the default 
// TODO PL: refactor this function
function zerve_popup(target_url, name, popclass, width, height ) {

	config_params = zerve_popup_config( popclass, width, height );
    if(! name ) {
    	name = null;
    }
    zerve_popup_window = window.open(target_url, name, config_params);
	if (popunder) {
		zerve_popup_window.blur();
		window.focus();
	} else {
	    if (window.focus) {
	    	zerve_popup_window.focus();
	    }
	}
    return false;
}


// Wrapper around zerve_popup() 
// This function opends a popup window same way as zerve_popup except 
// form_name is submitted to the popup window. Note that HTTP POST is used here

function zerve_submit_and_popup(form_name, target_url, name, popclass, width, height ) {

	config_params = zerve_popup_config( popclass, width, height );

	// we want to open an empty window
	zerve_popup_window = window.open("", name, config_params);

	var intendedForm = eval( "document."+form_name);
	orig_action = intendedForm.action;
	orig_target = intendedForm.target;
	
	intendedForm.action = target_url;
	intendedForm.target = name;
	
	// This causes a POST to the popup
	intendedForm.submit();

	intendedForm.action = orig_action;
	intendedForm.target = orig_target;

	if (window.focus) {
		zerve_popup_window.focus();
    }
	
    return false;
}


// Another wrapper for zerve_popup() not a very good function be carefull with it
//
// target_page: the name of the page to populate in the popup. ex /sla/view_something.php
// desired_elements: a string in the form of "var_name1 = form_element1, var_name2 = form_element2, etc" where
//                           var_name is the name to pass in the query string and
//                           form_element is the element from which to grab the value
//
// This function is used when we need to populate a link with the value of one or more form elements,
// and result in a popup.
//
// NOTE!!!! NO WHITESPACE IS ALLOWED in desired_elements string 
// if you don't like this, trim out the whitespace in the function below

function zerve_popup_based_on_form_element(target_page, desired_elements, window_name, window_class, window_width, window_height) {

	myvar_pairs = desired_elements.split(',');
	query_arg_length = myvar_pairs.length;
	qs_elements = new Array(query_arg_length);
	index_count = 0;
	for (aPair in myvar_pairs) {
		desired_pair = myvar_pairs[aPair].split('=');
		query_name =  desired_pair[0];
		form_element_name = desired_pair[1];
		//if the page has more than one form, this will need to be expanded
		form_element = eval( "document.forms[0]." + form_element_name );
		form_element_value = form_element.value;
		//we need to urlencode this
		qs_elements[ index_count++ ] =  ""+query_name+"="+form_element_value;		
	}	

	querystring = qs_elements.join("&");
	target_url = target_page + "?" + querystring;	
	return zerve_popup(target_url, window_name, window_class,window_width,window_height);
}


// Helper function for other functions
function zerve_popup_config( popclass, width, height ) {

    if( popclass == 'form' ) {
        config_params = 'height=500, width=750, left=200, top=30, toolbar=no, menubar=no, scrollbars=yes, resizable=yes, location=no, directories=no, status=yes, dependent=yes';
		popunder = 0;
    } else if( popclass == 'tickets' ) {
        config_params = 'height=160, width=200, resizable=yes, dependent=yes';
 		popunder = 0;
    } else if( popclass == 'transient' ) {
        config_params = "height=1, width=1, left=0, top=0, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, directories=no, status=no, dependent=yes";
	    popunder = 1;
    } else if( popclass == 'yesno' ) {
        config_params = 'height=180, width=400, left=320, top=240, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, directories=no, status=no, dependent=yes';
		popunder = 0;
	} else if( popclass == 'asset_preview' ) {
        config_params = 'height=180, width=180, left=320, top=240, toolbar=no, menubar=no, scrollbars=no, resizable=yes, location=no, directories=no, status=no, dependent=yes';
		popunder = 0;		
	} else if( popclass == 'activity_photos' ) {
        config_params = 'height=600,width=780,left=220,top=140,scrollbars=no,resizable=yes,location=no,status=yes,dependent=yes';
		popunder = 0;
    } else {
        config_params = 'height=600,width=780,left=320,top=240,scrollbars=yes,resizable=yes,location=yes,status=yes,dependent=yes';
		popunder = 0;
    }
    if( height ) {
    	config_params = config_params.replace( /^height=([0-9]+),(.*)/ , 'height='+height+',$2' );
    }
    if( width ) {
    	config_params = config_params.replace( /^(.*)width=([0-9]+),(.*)/, '$1width='+width+',$3' );
    }
	
	return config_params;

}


// Somewhat reliable unload handler
// Call zerve_set_unload_handler_1() in the body onload attribute
// For each link on a page that you dont want an unload handler to execute
// do this: <a href="javascript:void(0)" onlick="zerve_disable_unload_hanlder();window.location.href='....'" >...
//
// If optional url is passed in as an argument
var disable_uhandler_toggle = false;
var unload_handler_url      = null;

function zerve_set_unload_handler_1( url )
{
	if( url ) {
		unload_handler_url = url;
	}

	window.onunload = function() {

		if( document.disable_uhandler_toggle ) {
			return true;
		}
		if(window.opener){
			if( unload_handler_url ) {
				window.opener.location.href=unload_handler_url;
			} else {
				window.opener.location.reload(true);
			}
		}
		return true;
	}
}

function zerve_disable_unload_handler()
{
	document.disable_uhandler_toggle = true;
}
