// Basic Functions - getElementsByTagName, getElementsByName

// cant remember where i got this
function e(id){ return document.getElementById(id); }

// taken from ismail code
function obj(e) { return document.getElementById(e); }
function selectedOption(e){e=obj(e)||e;return e.options[e.selectedIndex].value;}
function show(e){e=obj(e)||e;e.style.display='inline';}
function hide(e){e=obj(e)||e;e.style.display='none';}
function isShown(e){e=obj(e)||e;return(e.style.display=='none')?false:true;}

function obj_value ( e ) {
   html_element = obj(e);
   if ( html_element.type == "text" || html_element.type == "textarea" || html_element.type == "hidden" ) {
      return html_element.value;
   } else if ( html_element.type == "select-one" ) {
      return html_element.options[html_element.selectedIndex].value;
   } else if ( html_element.type == "select-multiple" ) {
      // http://www.digitalamit.com/blog/blog/23.html
      selected = new Array();
      for (var i = 0; i < html_element.options.length; i++)
        if (html_element.options[i].selected)
          selected.push(html_element.options[i].value);
      return selected;
   }
}

// http://blog.stevenlevithan.com/archives/faster-trim-javascript
String.prototype.trim = function() {
   return this.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}

// -------------------------------------------------------- //

// Alternative - http://www.dynamicsitesolutions.com/download_file/javascript/add_bookmark.js
// Browser Detect - http://www.quirksmode.org/js/detect.html
function Bookmark( title, url ) {
   var b_version = parseFloat( navigator.appVersion );
//   if ( window.opera && window.print ) {
//      var operalink = document.createElement( 'a' );
//      operalink.setAttribute( 'href', url );
//      operalink.setAttribute( 'title', title );
//      operalink.setAttribute( 'rel', 'sidebar' );
//      operalink.click();
////   } else if( window.sidebar ) {
////      // Firefox
////      window.sidebar.addPanel( title, url, "" );
//   } else 
   if ( navigator.appName.indexOf("Microsoft") != -1 && parseInt(navigator.appVersion) >= 4 && navigator.userAgent.indexOf("Opera") == -1 ) {
      // IE
      window.external.AddFavorite( url, title );
   } else {
      if ( navigator.userAgent.indexOf("Opera") != -1 && b_version < 9 ) {
         alert("Press Ctrl + T to bookmark this Webpage");
      } else {
         alert("Press Ctrl + D to bookmark this Webpage");
      }
   }
   // adding rel="sidebar" to a link will bookmark it in opera 7+
}

// http://www.quirksmode.org/js/popup.html
function MM_openBrWindow(theURL,winName,features) { // Philipz Inc.
  // features = toolbar,location,directories,status,scrollbars,menubar,resizable
  // Fix - issue with blank 'features' and also if 'features' had values for others than width and height
  // http://www.w3schools.com/HTMLDOM/met_win_open.asp
  var FeatureValue;
  var newFeatures = "";
  var width = 0;
  var height = 0;
  if ( features ) {
     var popupFeatures = features.split(",");
     for ( var x = 0; x < popupFeatures.length; x++ ) {
        FeatureValue = popupFeatures[x].split("=");
        if ( FeatureValue[0] == "width" ) {
           width = parseInt( FeatureValue[1] );
        } else if ( FeatureValue[0] == "height" ) {
           height = parseInt( FeatureValue[1] );
        } else {
           newFeatures = newFeatures + popupFeatures[x] + ",";
        }
     }
     features = newFeatures.substring(0,newFeatures.length-1);
  } else {
     var features = "";
  }

  if ( width != 0 && height != 0 ) {
     var LowerCaseURL = theURL.toLowerCase();
     var OperaAdjustment = 0;
     if ( theURL.lastIndexOf('.jpg') != -1 || theURL.lastIndexOf('.gif') != -1 || theURL.lastIndexOf('.png') != -1 ) {
        // Centers the new window in the middle of the screen
        if (navigator.userAgent.indexOf('Opera') != -1) {
           var OperaVersionLoc = navigator.userAgent.indexOf('Opera') + 6;
           var OperaVersion = parseInt( navigator.userAgent.substring(OperaVersionLoc,OperaVersionLoc+4) );
           if ( OperaVersion >= 7 && OperaVersion < 9 ) {
              width = width + 26;
              height = height + 6;
           } else if ( OperaVersion >= 9 ) {
              width = width + 6;
              height = height + 6;
           }
           OperaAdjustment = 70;
        } else if (navigator.appName.indexOf('Netscape') != -1) {
           width = width + 16;
           height = height + 16;
        } else if (navigator.appName.indexOf('Microsoft') != -1) {
           width = width + 20;
           height = height + 30;
        }
     } else {
        if (navigator.userAgent.indexOf('Opera') != -1) {
           OperaAdjustment = 70;
        }
     }
     features = features + "width=" + width.toString() + ",height=" + height.toString();
     //alert( features );
     if ( navigator.appName.indexOf("Microsoft") != -1 && navigator.userAgent.indexOf("Opera") == -1 ) {
        // IE screw up
        winName = "_blank";
     }
     resultsWindow = window.open(theURL,winName,features);

     var TopRightX = 20;
     var TopRightY = 20;

     if ( self.screen ) {
        TopRightX = ( screen.availWidth - width - 10 ) / 2;
        TopRightY = ( screen.availHeight - height - OperaAdjustment - 29 ) / 2;
        if ( TopRightY < 0 ) { TopRightY = 0; }
        if ( TopRightX < 0 ) { TopRightX = 0; }
     }

     resultsWindow.moveTo( TopRightX, TopRightY );
  } else {
     resultsWindow = window.open(theURL,winName,features);
  }
}

function clearit ( fieldtoclear, textvaluetoclear ) {
	var object1 = document.getElementById(fieldtoclear);
	if ( object1.value == textvaluetoclear ) {
		object1.value = '';
	}
}

// right-&#9658; left-&#9668; up-&#9650; down-&#9660;
// right-arrow to down-arrow - 'More &#9658;','Less &#9660;'
// down-arrow to up-arrow - 'More &#9660;','Less &#9650;'
function hide_show ( object_name, hide_html, show_html ) {
   object1 = document.getElementById( object_name );
   hide_show_icon = document.getElementById( object_name + "_icon" );
   hide_show_html = document.getElementById( object_name + "_html" );
   if ( !hide_show_html && hide_show_icon ) hide_show_html = hide_show_icon;
   if ( object1.style.display == "none" ) {
      object1.style.display = "block";
      if ( hide_show_html ) {
//         if ( hide_show_html.innerHTML == hide_html ) {
//            hide_show_html.innerHTML = show_html;
//         } else 
         htmlentities_code = htmlentities( hide_show_html.innerHTML );
         if ( htmlentities_code == "&#9660;" || htmlentities_code == "&#9650;" ) {
            hide_show_html.innerHTML = "&#9650;";
         } else {
            hide_show_html.innerHTML = show_html;
         }
//      } else if ( hide_show_icon ) {
//         if ( htmlentities(hide_show_icon.innerHTML) == "Show Details &#9660;" ) {
//            hide_show_icon.innerHTML = "Hide Details &#9650;";
//         }
      }
   } else {
      object1.style.display = "none";
      if ( hide_show_html ) {
//         if ( hide_show_html.innerHTML == show_html ) {
//            hide_show_html.innerHTML = hide_html;
//         } else 
         htmlentities_code = htmlentities( hide_show_html.innerHTML );
         if ( htmlentities_code == "&#9650;" || htmlentities_code == "&#9660;" ) {
            hide_show_html.innerHTML = "&#9660;";
         } else {
            hide_show_html.innerHTML = hide_html;
         }
//      } else if ( hide_show_icon ) {
//         if ( htmlentities(hide_show_icon.innerHTML) == "Hide Details &#9650;" ) {
//            hide_show_icon.innerHTML = "Show Details &#9660;";
//         }
      }
   }
}

function hide_show_swap ( id_name, id_name2 ) {
   object1 = document.getElementById( id_name );
   if ( !id_name2 ) {
      object2 = document.getElementById( id_name+"_full" );
   } else {
      object2 = document.getElementById( id_name2 );
   }
   if ( object1.style.display == "none" ) {
      object1.style.display = "block";
      object2.style.display = "none";
   } else {
      object1.style.display = "none";
      object2.style.display = "block";
   }
}

function disable_field_on_value_array(field_id,disable_value_array) {
   var main_object = document.getElementById(field_id);
   // the array has to be ( disable_value, disable_field_id, ... )
   for ( i=0; i < disable_value_array.length; i=i+2 ) {
      var disable_value = disable_value_array[i];
      var disable_object = document.getElementById(disable_value_array[i+1]);
      if ( main_object.value == disable_value ) {
         disable_object.disabled = true;
      } else {
         disable_object.disabled = false;
      }
   }
}

function disable_field_on_value(disable_field_id,field_id,disable_value) {
   var object1 = document.getElementById(disable_field_id);
   var object2 = document.getElementById(field_id);
   if ( object2.value == disable_value) {
      object1.disabled = true;
   } else {
      object1.disabled = false;
   }
}

function swap_image( id, image_filename ) {
   var Object = document.getElementById(id);
   Object.src = image_filename;
}

// Adopted from code by Ronnie T. Moore [http://javascript.internet.com/forms/limit-textarea.html]
// Code by Adam Matthews (showChar) was interesting [http://www.codingforums.com]
function textCounter ( field, countfield, maxlimit ) {
   var textfield = document.getElementById( field );
   if ( maxlimit == null ) {
      // if null it looks for the textarea attribute maxlength
      var maxlimit = textfield.attributes.getNamedItem('maxlength').value;
   }
   if ( textfield.value.length > maxlimit ) {
      textfield.value = textfield.value.substring(0, maxlimit);
   }
   if ( countfield != null ) {
      var textcountfield = document.getElementById( countfield );
      if ( textcountfield.type ) {
         // most likely a text box
         textcountfield.value = maxlimit - textfield.value.length;
      } else {
         // likely <span>
         textcountfield.innerHTML = maxlimit - textfield.value.length;
      }
   }
}

function goto_url( theURL, message ) {
   if ( message == "" ) {
      message = "Are you sure you wish continue with this action?";
   }
   confirmation = confirm( message );
   if ( confirmation == 1 ) {
      top.location.href = theURL;
   }
}

function disable_field_on_value_array(field_id,disable_value_array) {
   var main_object = document.getElementById(field_id);
   // the array has to be ( disable_value, disable_field_id, ... )
   for ( i=0; i < disable_value_array.length; i=i+2 ) {
      var disable_value = disable_value_array[i];
      var disable_object = document.getElementById(disable_value_array[i+1]);
      if ( main_object.value == disable_value ) {
         disable_object.disabled = true;
      } else {
         disable_object.disabled = false;
      }
   }
}

function disable_field_on_value(disable_field_id,field_id,disable_value) {
   var object1 = document.getElementById(disable_field_id);
   var object2 = document.getElementById(field_id);
   if ( object2.value == disable_value) {
      object1.disabled = true;
   } else {
      object1.disabled = false;
   }
}

// http://www.dynamicdrive.com/emailriddler/
// http://www.ssmedia.com/Utilities/Unicode/
// http://joemaller.com/js-mailer.shtml
function decrypt_email ( chr_sequence, anchor_id ) {
   var anchor_obj = document.getElementById( anchor_id );
   var chr_array = chr_sequence.split( "," );
   var mailto_link = "mailto:";
   for ( var x = 0; x < chr_array.length; x++ ) {
      mailto_link += String.fromCharCode( chr_array[x] );
   }
   anchor_obj.href = mailto_link;
}






// Aging Code
function BreakFrame( ) {
   if (window != top) {
      top.location.href = location.href;
   }
}

function OpenBrowserWindow(theURL,winName,features,width,height) {
   if ( width ) features = features + ",width=" + width;
   if ( height ) features = features + ",height=" + height;
   MM_openBrWindow(theURL,winName,features);
}

function OpenWindow(theURL) {
  window.open(theURL,theURL,"toolbar=yes,location=no,directories=yes,status=yes,scrollbars=yes,menubar=yes,resizable=yes");
}

// Other Peoples Code

// http://snipplr.com/view/1369/set-select-list-to-value/
//  modified it a bit
function setSelectListToValue( value, selectId, value_location ){
	var i, si, option_value, obj;
	var value_location = ( value_location == null ) ? "" : value_location;
	var value_length = value.length;
	if ((obj=document.getElementById(selectId)) != null){
		for(i=0; i<obj.length; i++){
		   option_value = obj.options[i].value;
			if( option_value == value ){
				si = i;
				break;
			} else if( value_location == "left" && option_value.substr(0,value_length) == value ){
				si = i;
				break;
			}
		}
		obj.selectedIndex = si;
	}
}

// http://www.sean.co.uk/a/webdesign/javascriptdelay.shtm
// alternatively - http://www.ozzu.com/programming-forum/javascript-sleep-function-t66049.html
// another - http://ourworld.cs.com/tcmits1/javascript/papers/pause/pause.html
function pausecomp(millis) 
{
var date = new Date();
var curDate = null;

do { curDate = new Date(); } 
while(curDate-date < millis);
}

// taken from http://www.arkinex.com/javascript/js-functions/26/javascript-function-htmlentities/
//  but i modified it a bit and made it work in IE
function htmlentities(str) {
    var i,output='',len,char=0;
    len = str.length;
    for(i=0;i< len;i++){
        // char = str[i].charCodeAt(0); - Doesnt work in IE
        char = str.charCodeAt(i);
        if ( (char >47 && char< 58)||(char >62 && char< 127)|| char==32 || char==38 || char==59 ){
            //output += str[i];
            output += str.charAt(i);
        }else{
            //output += "&#" + str.charCodeAt(0) + ";";
            output += "&#" + char + ";";
        }
    }
    return output;
}

// http://snippets.dzone.com/posts/show/1575
function showObj(obj,output_area) {
  document.write( "<style type=\"text/css\">" );
  document.write( "div    {display: inline;}" );
  document.write( "o_prop_name.a,o_prop_name.a:link,o_prop_name.a:hover  { text-decoration: none; color:blue; }" );
  document.write( ".o_prop_name  { font-family: Verdana, Tahoma, Arial; font-weight: bold; color: blue; font-size: 14px; }" );
  document.write( ".o_prop_value  { font-family: Verdana, Tahoma, Arial; font-weight: bold; color: green; font-size: 12px; }" );
  document.write( "</style>" );

  var obj= eval(obj);
  if ( obj.id == "" ) {
   obj.id = obj.name;
  }
  var str='<div class="formated">';
  for (prop in obj) {
    str +='<div class="o_prop_name"><a href="#" title="next" onClick="showObj1('+obj.id+'['+prop+']'+',\''+obj.id+'_'+prop+'\')">' +prop+ '</a></div>=<div class="o_prop_value" id="'+obj.id+'_'+prop+'">' +obj[prop]+ '</div><br />' + "\r\n";
  }
  str += '</div>';
  if ( output_area ) {
     var div1 = document.getElementById( output_area );
  } else {
     var div1 = document.createElement("div");
  }
  document.write( str );
  //div1.innerHTML = str;
  //document.all.txt3.innerHTML=str;
}
function showObj1(obj,output_area) {
  var obj = document.getElementById( obj );
  var str='<div class="formated">';
  for (prop in obj) {
    str +='<div class="o_prop_name"><a href="#" title="next" onClick="showObj('+obj.id+'['+prop+']'+',\''+obj.id+'_'+prop+'\')">' +prop+ '</a></div>=<div class="o_prop_value" id="'+obj.id+'_'+prop+'">' +obj[prop]+ '</div><br />' + "\r\n";
  }
  str += '</div>';
  if ( output_area ) {
     var div1 = document.getElementById( output_area );
  } else {
     var div1 = document.createElement("div");
  }
  document.write( str );
}

function showObj_org(obj) {
var obj= eval(obj);
var str='<div class="formated">';
for (prop in obj) {
str +='<div class="o_prop_name"><a href="#" title="next" onClick="changeObj(this.innerText)">' +prop+ '</a></div>=<div class="o_prop_value">' +obj[prop]+ '</div><br />';
}
str += '</div>';
//document.write( str );
a = document.createElement('div');
a.innerHTML = str;
document.body.appendChild(a);
}

// so i dont have to remember the name
function print_r(obj) { showObj_org(obj); }


function eraser() {
  var tmpstr = document.all.txtObj.value;
  tmpstr=tmpstr.replace(/\.\w*$/ig,"");
  document.all.txtObj.value=tmpstr;
  showObj(tmpstr);
}

// http://www.breakingpar.com/bkp/home.nsf/0/87256B280015193F87256BF8004D72D6
function dumpProps(obj, parent) {
   // Go through all the properties of the passed-in object 
   for (var i in obj) {
      // if a parent (2nd parameter) was passed in, then use that to 
      // build the message. Message includes i (the object's property name) 
      // then the object's property value on a new line 
      if (parent) { var msg = parent + "." + i + "\n" + obj[i]; } else { var msg = i + "\n" + obj[i]; }
      // Display the message. If the user clicks "OK", then continue. If they 
      // click "CANCEL" then quit this level of recursion 
      if (!confirm(msg)) { return; }
      // If this property (i) is an object, then recursively process the object 
      if (typeof obj[i] == "object") { 
         if (parent) { dumpProps(obj[i], parent + "." + i); } else { dumpProps(obj[i], i); }
      }
   }
}

// http://www.shawnolson.net/a/639/select-all-checkboxes-in-a-form-with-javascript.html
function checkUncheckAll(theElement) {
   var theForm = theElement.form, z = 0;
   for(z=0; z<theForm.length;z++){
      if(theForm[z].type == 'checkbox' && theForm[z].name != 'checkall'){
         theForm[z].checked = theElement.checked;
      }
   }
}

// Dreamweaver Codes

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_popupMsg(msg) { //v1.0
   if ( navigator.userAgent.indexOf('Opera') == -1 && navigator.appName.indexOf('Netscape') != -1 ) {
      // fix for firefox as \r doesnt work, and if \n was used, then it is shown as text in IE and Opera
      msg = msg.replace( /\r/g, "\n" );
   }
  alert(msg);
}
