//doesn't work with third party url's (permmision denied error)
// incase of this error the iframe is resized to body height

function iecompattest(wobj){
		if (wobj) return(wobj.document.compatMode && wobj.document.compatMode!="BackCompat")? wobj.document.documentElement : wobj.document.body;
		return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body;
    }

function collectHeight(){
    return document.all? iecompattest().scrollHeight : iecompattest().offsetHeight;
    }

function registerIframeHeight(iframe_src,iheight) {
	//register cookie with height of iframe
	//make path relative
  set_cookie(getIframeCookiename(iframe_src),iheight);
}


function resize_iframe(iframeElement) {
		if (iframeElement) {
			var iframeWindow;
			if (iframeElement.id != "" && iframeElement.name == ""){ iframeElement.name = iframeElement.id;}
			iframeWindow = window.frames[iframeElement.name];
			//determine frame height on contents
			
			try {
				  var resizeHeight = iframeWindow.document.all? iecompattest(iframeWindow).scrollHeight : iecompattest(iframeWindow).offsetHeight;
				  iframeElement.style.height = resizeHeight + 20 + 'px';
				} //try 
			catch(e) {
				//try to check whether it is a domain security error and the iframe content was rendered using popup.xml thus a cookie was set with the height.
				 
				var iframe_name = getIframeCookiename(iframeElement.src);
				if (get_cookie(iframe_name)) {
					iframeElement.style.height = get_cookie(iframe_name);
					delete_cookie(iframe_name);
				}
				else 
					{
					if(document.documentElement.clientHeight>document.body.clientHeight) {
							bodyheight = document.documentElement.clientHeight;
						} else {
							bodyheight = document.body.clientHeight;
						}
					if (document.all) {	
						iframeElement.height = bodyheight - getPosition(iframeElement).top;
					} else if (window.innerHeight) {
							iframeElement.height = window.innerHeight - getPosition(iframeElement).top;
					} else {
						iframeElement.height = bodyheight- getPosition(iframeElement).top;
					}
				}
			} //catch
		}
	}
	
	
	function set_cookie ( name, value, exp_y, exp_m, exp_d, path, domain, secure )
{
  var cookie_string = name + "=" + escape ( value );

  if ( exp_y )
  {
    var expires = new Date ( exp_y, exp_m, exp_d );
    cookie_string += "; expires=" + expires.toGMTString();
  }

  if ( path )
        cookie_string += "; path=" + escape ( path );

  if ( domain )
        cookie_string += "; domain=" + escape ( domain );
  
  if ( secure )
        cookie_string += "; secure";
  
  document.cookie = cookie_string;
}

function get_cookie ( cookie_name )
{
  var results = document.cookie.match ( cookie_name + '=(.*?)(;|$)' );
  if ( results )
    return ( unescape ( results[1] ) );
  else
    return null;
}

function delete_cookie ( cookie_name )
{
  var cookie_date = new Date ( );  // current date & time
  cookie_date.setTime ( cookie_date.getTime() - 1 );
  document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
}

function getIframeCookiename(iframeSrc) {
	//debugger;
	var iframe_name = 'iframe_'
	if (iframeSrc.substring(0,4) == 'http') {
		//absolute path
		if (iframeSrc.split('/')[3]) 
			//in case of http://www.domain.com/test.html?fjsdfkjsdlfksdjfl
			iframe_name += iframeSrc.split('/')[3].split('?')[0];
		 else 
			//in case of http://www.domain.com?fjsdfkjsdlfksdjfl
			iframe_name += iframeSrc.split('/')[2].split('?')[0];
	} else {
		//relative path
		if (iframeSrc.split('/')[1]) 
			//in case of /test.html?fjsdfkjsdlfksdjfl
			iframe_name += iframeSrc.split('/')[1].split('?')[0];
		else
			//in case of test.html?fjsdfkjsdlfksdjfl
			iframe_name += iframeSrc.split('?')[0];
	}
	return(iframe_name);
}