/*******************************************************************************
*  mtstudios_utilities.js : 22.06.2005
* -----------------------------------------------------------------------------
*  A group of useful JavaScript utilities that can aid in the development
*  of webpages.
*******************************************************************************/

/*  Confirm Property Deletions */
function getconfirmProperty() { 
if (confirm("Are you sure you want to permanently delete the Property?")==true) 
return true; 
else 
return false; }

/*  Confirm Photo Deletions */
function getconfirmPhoto() { 
if (confirm("Are you sure you want to permanently delete this Photo?")==true) 
return true; 
else 
return false; }

/* Calendar Popup */

var popUp; 

function OpenCalendar(idname, postBack)
{
	popUp = window.open('Calendar.aspx?formname=' + document.forms[0].name + 
		'&id=' + idname + '&selected=' + document.forms[0].elements[idname].value + '&postBack=' + postBack, 
		'popupcal', 
		'width=175,height=190,left=200,top=250');
}

function SetDate(formName, id, newDate, postBack)
{
	eval('var theform = document.' + formName + ';');
	popUp.close();
	theform.elements[id].value = newDate;
	if (postBack)
		__doPostBack(id,'');
}

/* Multi-File Upload  */
	
	function mysubmit(ID)
	{

			var Flash;
			if(document.embeds && document.embeds.length>=1)
				Flash = document.getElementById("EmbedFlashFilesUpload");
			else
				Flash = document.getElementById("FlashFilesUpload");		
				/* Flash.SetVariable("uploadUrl", "http://mtserver/mfv/Uploadfiles.aspx"); */ 
				Flash.SetVariable("uploadUrl", "FlashUpload.aspx?PropID=" + ID  ); 
				Flash.SetVariable("SubmitFlash", "");
	}



/* Google Map Functions  */

 function SetMapLatLon(formName, latid, lat, lonid, lon, postBack, zoomid, zoom)
{
	eval('var theform = document.' + formName + ';');
	popUp.close();
	/* Load returned values into the saved HTML elements  */
	theform.elements[latid].value = lat;
	theform.elements[lonid].value = lon;
	theform.elements[zoomid].value = zoom;
	
}

/* Open the Map search page  */
 function OpenMapSearch(latidname, lonidname, zoomidname, postBack)
{
	popUp = window.open('MapSearch.aspx?formname=' + document.forms[0].name + 
		'&latid=' + latidname + '&lonid=' + lonidname + '&zoomid=' + zoomidname + '&curzoom=' + document.forms[0].elements[zoomidname].value + '&curlat=' + document.forms[0].elements[latidname].value + '&curlon=' + document.forms[0].elements[lonidname].value + '&postBack=' + postBack, 
		'popupGmap','width=420,height=430,left=100,top=100,scrollbars=0');
}

// returns a named value from the Querystring - equiv to FromQS

function querystring(name)  
{
   var tmp = ( location.search.substring(1) );
   var i   = tmp.toUpperCase().indexOf(name.toUpperCase()+"=");

   if ( i >= 0 )
   {
      tmp = tmp.substring( name.length+i+1 );
      i = tmp.indexOf("&");
      return unescape( tmp = tmp.substring( 0, (i>=0) ? i : tmp.length ));
   }

   return("");
}
 
/* event_attach() takes care of attaching event handlers (functions) to events. this 
 * simplifies the process of attaching multiple handlers to a single event
 */

 
function event_attach( event , func )
{
	if ( window.attachEvent )
	{
		window.attachEvent( event , func );
	}
	else
	{
		if ( ( typeof( func ) ).toLowerCase() != 'function' )
		{
			return;
		}
		if ( ( typeof( document.event_handlers ) ).toLowerCase() == 'undefined' )
		{
			document.event_handlers = new Array();
		}
		if ( ( typeof( document.event_handlers[ event ] ) ).toLowerCase() == 'undefined' )
		{
			document.event_handlers[ event ] = new Array();
		}
		if ( ( typeof( eval( 'window.' + event ) ) ).toLowerCase() != 'function' )
		{
			eval( 'window.' + event + ' = function () { if ( ( typeof( document.event_handlers[ \'' + event + '\' ] ) ).toLowerCase() != \'undefined\' ) { for ( i = 0 ; i < document.event_handlers[ \'' + event + '\' ].length ; i++ ) { document.event_handlers[ \'' + event + '\' ][ i ](); } } } ' );
		}
		document.event_handlers[ event ][ document.event_handlers[ event ].length ] = func;
	}
}

/* browser_detect() creates a simple object that can be used to determine what
 * browser the client is using. this is useful in many areas.
 */
function browser_detect() 
{
	var ua			= navigator.userAgent.toLowerCase(); 
	this.ua			= ua;
	this.isGecko		= ( ua.indexOf( 'gecko' ) != -1 );
	this.isMozilla		= ( this.isGecko && ua.indexOf( "gecko/" ) + 14 == ua.length);
	this.isIE		= ( ( ua.indexOf( "msie" ) != -1 ) && ( ua.indexOf( "opera" ) == -1) && ( ua.indexOf( "webtv" ) == -1 ) );
	this.isOpera		= ( ua.indexOf( "opera" ) != -1 );
	this.isKonqueror	= ( ua.indexOf( "konqueror" ) != -1 );
	this.isIcab		= ( ua.indexOf( "icab" ) != -1 );
	this.isAol		= ( ua.indexOf( "aol" ) != -1 );
	this.isWebtv		= ( ua.indexOf( "webtv" ) != -1 );
	this.isOmniweb		= ( ua.indexOf( "omniweb" ) != -1 );
	this.isDreamcast	= ( ua.indexOf( "dreamcast" ) != -1 );
	this.versionMinor	= parseFloat( navigator.appVersion );
	if ( this.isNS && this.isGecko ) 
	{
		this.versionMinor = parseFloat( ua.substring( ua.lastIndexOf( '/' ) + 1 ) );
	} 
	else if ( this.isIE && this.versionMinor >= 4 ) 
	{
		this.versionMinor = parseFloat( ua.substring( ua.indexOf( 'msie ' ) + 5 ) );
	} 
	else if ( this.isOpera ) 
	{
		if ( ua.indexOf( 'opera/' ) != -1 ) 
		{
			this.versionMinor = parseFloat( ua.substring( ua.indexOf( 'opera/' ) + 6 ) );
		}
		else 
		{
			this.versionMinor = parseFloat( ua.substring( ua.indexOf( 'opera ' ) + 6 ) );
		}
	} 
	else if ( this.isKonqueror ) 
	{
		this.versionMinor = parseFloat( ua.substring( ua.indexOf( 'konqueror/' ) + 10 ) );
	} 
	else if ( this.isIcab ) 
	{
		if ( ua.indexOf( 'icab/' ) != -1 ) 
		{
			this.versionMinor = parseFloat( ua.substring( ua.indexOf( 'icab/' ) + 6 ) );
		}
		else 
		{
			this.versionMinor = parseFloat( ua.substring( ua.indexOf( 'icab ' ) + 6 ) );
		}
	} 
	else if ( this.isWebtv )
	{
		this.versionMinor = parseFloat( ua.substring( ua.indexOf( 'webtv/' ) + 6 ) );
	}
	this.versionMajor	= parseInt( this.versionMinor ); 
	this.isWin		= ( ua.indexOf( 'win' ) != -1 );
	this.isWin32		= ( this.isWin && ( ua.indexOf( '95' ) != -1 || ua.indexOf( '98' ) != -1 || ua.indexOf( 'nt' ) != -1 || ua.indexOf( 'win32' ) != -1 || ua.indexOf( '32bit' ) != -1 ) );
	this.isMac		= ( ua.indexOf( 'mac' ) != -1 );
	this.isUnix		= ( ua.indexOf( 'unix' ) != -1 || ua.indexOf( 'linux' ) != -1 || ua.indexOf( 'sunos' ) != -1 || ua.indexOf( 'bsd' ) != -1 || ua.indexOf( 'x11' ) != -1 );
	this.isIE55		= ( this.isIE && this.versionMinor == 5.5 );
	this.isIE5up		= ( this.isIE && this.versionMajor >= 5 );
	this.isIE6up		= ( this.isIE && this.versionMajor >= 6 );
}

/* opacity() will enable PNG transparency for Internet Explorer
 */
function opacity( strId , strPath , intWidth , intHeight , strClass , strAlt )
{	
	if ( document.pngAlpha )
	{
		document.write( '<div style="height:'+intHeight+'px;width:'+intWidth+'px;filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\''+strPath+'.png\', sizingMethod=\'scale\')" id="'+strId+'" class="'+strClass+'"></div>' );
	}
	else if ( document.pngNormal )
	{
		document.write( '<img src="'+strPath+'.png" width="'+intWidth+'" height="'+intHeight+'" name="'+strId+'" border="0" class="'+strClass+'" alt="'+strAlt+'" />' );
	}
	else if ( document.layers )
	{
		return( '<img src="'+strPath+'.gif" width="'+intWidth+'" height="'+intHeight+'" name="'+strId+'" border="0" class="'+strClass+'" alt="'+strAlt+'" />' );
	}
	else
	{
		document.write( '<img src="'+strPath+'.gif" width="'+intWidth+'" height="'+intHeight+'" name="'+strId+'" border="0" class="'+strClass+'" alt="'+strAlt+'" />' );
	}
	return( '' );
}

/* opacity_init() handles some variable initializations required for opacity() to function
 */
function opacity_init()
{
	var browser = new browser_detect();
	document.pngAlpha = false;
	document.pngNormal = false;
	document.strExt = ".gif";

	if ( ( browser.isIE55 || browser.isIE6up ) && browser.isWin32 )
	{
		document.pngAlpha = true;
		document.strExt = ".png";
	}
	else if ( 
			( browser.isGecko ) || 
			( browser.isIE5up && browser.isMac ) || 
			( browser.isOpera && browser.isWin && browser.versionMajor >= 6 ) || 
			( browser.isOpera && browser.isUnix && browser.versionMajor >= 6 ) || 
			( browser.isOpera && browser.isMac && browser.versionMajor >= 5 ) || 
			( browser.isOmniweb && browser.versionMinor >= 3.1 ) || 
			( browser.isIcab && browser.versionMinor >= 1.9 ) || 
			( browser.isWebtv ) || 
			( browser.isDreamcast ) 
		)
	{
		document.pngNormal = true;
		document.strExt = ".png";
	}
}

/* handler for Netscape Navigator clients that screw up the display
 * of CSS pages when reloaded
 */
function NN_reloadPage( init )
{
	if ( init == true ) with ( navigator )
	{
		if ( ( appName == "Netscape" ) && ( parseInt ( appVersion ) == 4 ) )
		{
			document.NN_pgW = innerWidth;
			document.NN_pgH = innerHeight;
			event_attach ( 'onresize' , NN_reloadPage );
		}
	}
	else if ( innerWidth != document.NN_pgW || innerHeight != document.NN_pgH )
	{
		location.reload();
	}
}

/* set_min_width_rs() takes a different approach to emulating min-width functionality
 */
function set_min_width_rs( element_id , min_width )
{
	if ( document.getElementById && navigator.appVersion.indexOf( "MSIE" ) > -1 && !window.opera )
	{
		document.mw_element_id = element_id;
		document.mw_min_width = min_width;
		event_attach( 'onload' , control_min_width_rs );
		event_attach( 'onresize' , control_min_width_rs );
	}
}

/* control_min_width_rs() is the event handler used by set_min_width_rs. This method fixes a couple bugs with
 * the p7 approach that can lead to the browser crashing. This method specifically targets IE although perhaps
 * it could be modified to work with other browsers later.
 */
function control_min_width_rs()
{
	var e = document.getElementById( document.mw_element_id );
	var offset = parseInt( 0 + e.currentStyle.paddingLeft ) + parseInt( 0 + e.currentStyle.paddingRight ) + parseInt( 0 + e.currentStyle.borderLeftWidth ) + parseInt( 0 + e.currentStyle.borderRightWidth );

	if ( ( document.body.clientWidth - ( e.offsetLeft * 2 ) ) <= ( document.mw_min_width - offset ) && parseInt( 0 + e.style.width ) != document.mw_min_width )
		e.style.width = document.mw_min_width + "px";
	else if ( document.body.clientWidth > ( document.mw_min_width + offset + e.offsetLeft * 2 ) )
		e.style.width = "auto";
}

/* set_min_width() initializes min-width-like functionality
 */
function set_min_width( obj_name , min_width , ieOnly )
{

	if ( ( typeof( ieOnly ) ).toLowerCase() == 'undefined' )
	{
		ieOnly = true;
	}
	if ( ieOnly == false || ( document.getElementById && navigator.appVersion.indexOf( "MSIE" ) > -1 && !window.opera ) )
	{
		document.min_width_obj_name = obj_name;
		document.min_width_size = min_width;
		document.resizing = false;
		event_attach( 'onload' , control_min_width );
		event_attach( 'onresize' , control_min_width );
	}
}

/* control_min_width() is the event handler setup and used by set_min_width()
 * it is based on the Project Seven min-width code
 */
function control_min_width()
{

	var cw , w , pl , pr , ml , mr , br , bl , ad , theDiv = document.min_width_obj_name;
	var g = document.getElementById( theDiv );
	w = parseInt(document.min_width_size);
	if ( g && document.body && document.body.clientWidth )
	{
		gs = g.currentStyle;
		cw = parseInt( document.body.clientWidth );
		pl = parseInt( gs.paddingLeft );
		pr = parseInt( gs.paddingRight );
		ml = parseInt( gs.marginLeft );
		mr = parseInt( gs.marginRight );
		bl = parseInt( gs.borderLeftWidth );
		br = parseInt( gs.borderRightWidth );
		ml = ml ? ml : 0;
		mr = mr ? mr : 0;
		pl = pl ? pl : 0;
		pr = pr ? pr : 0;
		bl = bl ? bl : 0;
		br = br ? br : 0;
		ad = pl + pr + ml + mr + bl + br;
		if ( cw <= w )
		{
			w -= ad;
			g.style.width = w + "px";
		}
		else
		{
			g.style.width = "auto";
		}
	}
}

/* cookie functions, blatantly ripped off from some site i've since forgotten. sorry.
 */
function setCookie( name, value, expires, path, domain, secure )
{
	document.cookie= name + "=" + escape(value) +
		((expires) ? "; expires=" + expires.toGMTString() : "") +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		((secure) ? "; secure" : "");
}
function getCookie( name )
{
	var dc = document.cookie;
	var prefix = name + "=";
	var begin = dc.indexOf( "; " + prefix );
	if ( begin == -1 )
	{
		begin = dc.indexOf(prefix);
		if (begin != 0) return null;
	}
	else
	{
		begin += 2;
	}
	var end = document.cookie.indexOf( ";", begin );
	if ( end == -1 )
	{
		end = dc.length;
	}
	return unescape(dc.substring(begin + prefix.length, end));
}
function deleteCookie( name, path, domain )
{
	if ( getCookie( name ) )
	{
		document.cookie = name + "=" + 
			((path) ? "; path=" + path : "") +
			((domain) ? "; domain=" + domain : "") +
			"; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}

/* minor accessibility deal here with font sizes
 * requires the following javascript to be executed:
 *
 *	var font_sizes = new Array( 80, 100, 120 ); 
 *	var current_font_size = 0;
 *	var _old_onload = null;
 *	old_onload = window.onload;
 *	window.onload = loadFontSize;
 */
function loadFontSize ()
{
	current_font_size = parseInt( '0' + getCookie ( "font_size" ) );
	setFontSize ( current_font_size );
	if ( _old_onload != null )
	{
		_old_onload();
	}
}
function setFontSize( size )
{
	if( size >= 0 && size < font_sizes.length )
	{
		current_font_size = size;
	}
	else if( ++current_font_size >= font_sizes.length )
	{
		current_font_size = 0;
	}
	if ( document.body )
	{
		document.body.style.fontSize = font_sizes[ current_font_size ] + '%';
		setCookie( "font_size" , current_font_size );
	}
}

function trim( str )
{
   return str.replace(/^\s*|\s*$/g,"");
}

/**** ObjectSwap - Bypasses the new ActiveX Activation requirement in Internet Explorer by swapping existing ActiveX objects on the page with the same objects. Can also be used for Flash version detection by adding the param:
<param name="flashVersion" value="6" /> to the object tag.

Author: Karina Steffens, www.neo-archaic.net
Created: April 2006
Changes and bug fixes: May 2006
*/

//Check if the browser is InternetExplorer
var ie = (navigator.appName.indexOf("Microsoft") != -1);

//Hide the object to prevent it from loading twice
if (ie){
	document.write ("<style id='hideObject'> object{display:none;} </style>");
}

/*Replace all flash objects on the page with the same flash object, 
by rewriting the outerHTML values
This bypasses the new IE ActiveX object activation issue*/
objectSwap = function(){
	//An array of ids for flash detection
	var stripQueue = [];
	//Get a list of all ActiveX objects
	var objects = document.getElementsByTagName('object');
	for (var i=0; i<objects.length; i++){			
		var o = objects[i];	
		var h = o.outerHTML;
		//The outer html omits the param tags, so we must retrieve and insert these separately
		var params = "";
		var hasFlash = true;
		for (var j = 0; j<o.childNodes.length; j++) {
			var p = o.childNodes[j];
			if (p.tagName == "PARAM"){
				//Check for version first - applies to all browsers
				//For this to work, a new param needs to be included in the object with the name "flashVersion" eg:
				//<param name="flashVersion" value="7" />
				if (p.name == "flashVersion"){
					hasFlash = detectFlash(p.value);
					if (!hasFlash){
						//Add the objects id to the list (create a new id if there's isn't one already)
						o.id = (o.id == "") ? ("stripFlash"+i) : o.id;
						stripQueue.push(o.id);
						break;
					}
				} 
				params += p.outerHTML;		       
			}
		}	
		if (!hasFlash){
			continue;
		}		
		//Only target internet explorer
		if (!ie){
			continue;
		} 
		//Avoid specified objects, marked with a "noswap" classname
		if (o.className.toLowerCase().indexOf ("noswap") != -1){
			continue;
		}		
		//Get the tag and attributes part of the outer html of the object
		var tag = h.split(">")[0] + ">";			
		//Add up the various bits that comprise the object:
		//The tag with the attributes, the params and it's inner html
		var newObject = tag + params + o.innerHTML + " </OBJECT>";		
		//And rewrite the outer html of the tag 
		o.outerHTML = newObject;
	}
	//Strip flash objects
	if (stripQueue.length) {
		stripFlash(stripQueue)
	}
	//Make the objects visible again
	if (ie){
		document.getElementById("hideObject").disabled = true;
	}
}

detectFlash = function(version){
	if(navigator.plugins && navigator.plugins.length){
		//Non-IE flash detection.
		var plugin = navigator.plugins["Shockwave Flash"];
		if (plugin == undefined){
			return false;
		}
		var ver = navigator.plugins["Shockwave Flash"].description.split(" ")[2];
		return (Number(ver) >= Number(version))
	} else if (ie){
	//IE flash detection.
		try{
			var flash = new ActiveXObject("ShockwaveFlash.ShockwaveFlash." + version);
			return true;
		}
		catch(e){
			return false;
		}
	}
	//Catchall - skip detection
	return true;
}

//Loop through an array of ids to strip
//Replace the object by a div tag containing the same innerHTML.
//To display an alternative image, message for the user or a link to the flash installation page, place it inside the object tag.  
//For the usual object/embed pairs it needs to be enclosed in comments to hide from gecko based browsers.
stripFlash = function (stripQueue){
	for (var i=0; i<stripQueue.length; i++){
		var o = document.getElementById(stripQueue[i]);
		var newHTML = o.innerHTML;	
		//Strip the comments
		newHTML = newHTML.replace(/<!--\s/g, "");
		newHTML = newHTML.replace(/\s-->/g, "");
		//Neutralise the embed tag
		newHTML = newHTML.replace(/<embed/gi, "<span");		
		//Create a new div element with properties from the object
		var d = document.createElement("div");
		d.innerHTML = newHTML;
		d.className = o.className;
		d.id = o.id;
		//And swap the object with the new div
		o.parentNode.replaceChild(d, o);
	}
}

//Initiate the function without conflicting with the window.onload event of any preceding scripts
if (window.onload)
{
	window.onload = function(){
		window.onload;
		objectSwap();
	}
} else {
	window.onload = objectSwap;
}

/**** End ObjectSwap ****/


/**** StyleSwitcher ****/

function setActiveStyleSheet(title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
}

function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}

function getPreferredStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return null;
}

/* Cookie stuff for remembering which stylesheet you last used >>

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

window.onload = function(e) {
  var cookie = readCookie("style");
  var title = cookie ? cookie : getPreferredStyleSheet();
  setActiveStyleSheet(title);
}

window.onunload = function(e) {
  var title = getActiveStyleSheet();
  createCookie("style", title, 365);
}

var cookie = readCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title); */

/**** End StyleSwitcher ****/