/***	Javascript for Webview -- Common Utilities
**		Started: Feb 11 2006
**		Creator: Sara Sioux
****/

      function eblast_insert_image() {
      	var txt = [],
      	    image = document.getElementById( 'eblast_images' ),
      	    url = document.getElementById( 'eblast_image_url' ),
      	    body = document.getElementById( 'body' );
      	txt.push( '<html>' );
      	txt.push( '<head></head>' );
      	txt.push( '<body>' );
      	if( url.value.length > 0 ) {
      	    txt.push( '<a href="' + url.value + '">' );
      	}
      	txt.push( '<img src="' + image.options[image.selectedIndex].value + '" alt="Image" />' );
      	if( url.value.length > 0 ) {
      	    txt.push( '</a>' );
      	}
      	txt.push( '</body></html>' );
      	body.value = txt.join( '' );
      }

Events.addEvent(window, "load", function(oEvent){
  wv_resize_iframe();                  // Resize if inside an iframe
  window.wvNavMenu.registerResizeFunc(wv_resize_iframe);
});

/**
 * wv_get_doc_height
 * Return the height of the document in pixels
 */
function wv_get_doc_height(){
  var height = 0;
  var body = htmlDOM.getBody();
  try{
    if(window.getComputedStyle){         // w3c
      height = document.defaultView.getComputedStyle(body,
                                               null).getPropertyValue("height");
    }else if(body.currentStyle){         // IE
      height = document.body.clientHeight;
    }
  }catch(e){
    height = 0;
  }
  return parseInt(height);
}

/**
 * wv_resize_iframe
 * If Webview is hosted inside of an iframe, then we will need to occasionally
 * need to resize the iframe to fit the content of the page.
 */
function wv_resize_iframe(){
  var height = wv_get_doc_height();
  if(window == top ||                            // Not in iframe
     !window.wvIFrameResizeURI.length ||         // No resize script
     !height){ return; }                         // Height is zero, null, undef.

  // We will now create a new iframe pointing to the resize script (hosted by
  // our client).  We pass in the height of the document as well as a random
  // number (to prevent caching).
  var date = new Date();
  var iframe = document.createElement("iframe");
  var body = htmlDOM.getBody();
  if(!date || !iframe || !body){ return; }       // Early return condition
  iframe.style.display = "none";                 // Invisible
  body.appendChild(iframe);                      // Add to body
  iframe.src = window.wvIFrameResizeURI + "?height="
             + height + "&update=" + date.getMilliseconds()
             + Math.random();
  iframe.onload = function(){                    // Remove iframe onload
    iframe.parentNode.removeChild(iframe);
  }

  // Must also hide the header and footer elements, if they're visible
  var hdr = document.getElementById("PageHeader");
  if(hdr){
    hdr.style.display = "none";
  }
  var ftr = document.getElementById("PageFooter");
  if(ftr){
    ftr.style.display = "none";
  }
}

/**
 * Select all opens in a select control
 */
function select_selectAll(ctrl){
  try{
    ctrl = document.getElementById(ctrl);
    var len = ctrl.options.length;
    for(var i = 0; i < len; i++){
      ctrl.options[i].selected = true;
    }
  }catch(e){
    ;
  }
  return false;
}

// hideById
// ids - array of ids to hide
// Find each of the elements in $ids and hide it (set display property to none)
function hideById(ids){
  for(var i = 0; i < ids.length; i++){
    var el = document.getElementById(ids[i]);
    if(el && el.style){
      el.style.display = "none";
    }
  }
  return;
}


// ******* CSS *********

// cssGetStyle
// RETURN: effective stylesheet property
// courtesy: http://www.quirksmode.org/dom/getstyles.html
function cssGetStyle(el,styleProp)
{
  var x = el;
  if(typeof(el) != "object"){
    var x = document.getElementById(el);
  }
	if (x.currentStyle)
		var y = x.currentStyle[styleProp];
	else if (window.getComputedStyle)
		var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(styleProp);
	return y;
}

// ******* EXTEND JAVA OBJECTS **********
String.prototype.ltrim = function(){
  var regexp = /^\s*/;
  return this.replace(regexp, "");
}

String.prototype.rtrim = function(){
  var regexp = /\s*$/;
  return this.replace(regexp, "");
}

String.prototype.trim = function(){
  var regexp = /(^(\n|\s)*)|((\n|\s)*$)/g;
  return this.replace(regexp, "");
}

// www.sean.co.uk
function wv_PauseComp(millis){
  date = new Date();
  var curDate = null;
  do { var curDate = new Date(); }
  while(curDate-date < millis);
}

function wv_OpenImageWindow(imgURL) {
	w = window.open(imgURL, 'imageWindow', 'height=600, width=650, scrollbars=yes, statusbar=no, location=no, toolbar=no');
}

function wv_write2id(id,txt) {
	var elem = document.getElementById(id);
	if(elem) {
		elem.innerHTML = txt;
	} else {
		alert('could not find elem '+id);
	}
}

function wv_change_font_color(id,color) {
	var elem = document.getElementById(id);
	if(elem) {
		elem.color = color;
	}
}

function wv_hide_element(id) {
	var elem = document.getElementById(id);
	if(elem) {
		elem.style.display = 'none';
	}
}

function wv_show_element(id) {
	var elem = document.getElementById(id);
	if(elem) {
		elem.style.display = 'block';
	}
}

/********		Form manipulation			**********/
function wv_enable(fieldid) {
	var elem = document.getElementById(fieldid);
	if(elem) {
		elem.disabled = false;
	} else {
		alert('Could not find element with ID '+fieldid);
	}
}

function wv_disable(fieldid) {
	var elem = document.getElementById(fieldid);
	if(elem) {
		elem.disabled = true;
	} else {
		alert('Could not find element with ID '+fieldid);
	}
}

/********		Debug			**********/
function wv_dump_props(dumparr) {
	var txt = '';
	for(var i in dumparr) {
		if(i != 'domConfig') {
			txt += i + ' = ' + dumparr[i] + "\n\r<br>";
		}
	}
	wv_write2id('debug-dump',txt);
}

/**
 * wv_dump_obj
 * Prepares a string of the properties and their values within an object.
 * object                    The object to display properties of
 * [object]                  Additional parameters to supply to the function
 *                           bool      html - true to render as HTML
 *                           bool      funcs - true to display functions
 *                           bool      body - true to append output to body
 *                                     Implies html set to true
 */
function wv_dump_obj(obj, params){
  // Configure our additional parameters
  if(!params){
    params = { html : false, funcs : false, body : false };
  }
  params.funcs = params.funcs ? true : false;
  params.body = params.body ? true : false;
  params.html = params.html || params.body ? true : false;

  var nl = params.html ? "<br />" : "\n";
  var out = "wv_dump_obj: obj is invalid object" + nl; // Init output
  if(!obj){
    if(params.body){
      htmlDOM.appendBody(out);         // Append to body
    }
    return out;                        // Early return
  }

  out = "";                            // Init output
  if(params.html){
    out += "<div><hr/>";
  }
  for(var x in obj){
    try{
      if(typeof obj[x] == "function" && !params.funcs){
        continue;
      }
      out += x + ": " + obj[x] + nl;
    }catch(e){
      out += "[" + x + "] caused a Javascript error" + nl;
    }
  }
  if(params.html){
    out += "<hr/></div>";
  }
  if(params.body){
    htmlDOM.appendBody(out);
  }
  return out;
}
