// Basic Megalodon Scripts
/*
function GetsetIDtoClass(Objid, className){//changes the class of whatever object has that id, returns the old class
function stripOutComments(str){
function makeQuoteFormQueryString(obj,packagingStr){// Attaches as an onclock action to links to GetAQuote.html - puts the proper query string onto the existing href url (no need to duplicate code that way). Also, works if no javascript.
function filenameRootFromUrl(){
function prependLoadEvent(func) {// adds a function to the onload forthe window
function addLoadEvent(func) {
function addUnLoadEvent(func) {
function popUp(w,h) {
function closeWinIfOpen(){
function goTo (page) {   
function setUpFormValsArray(){// set up formVals array if not already set up
function SaveFormValues(formId){
function LoadFormValues(formId){// formVals array is either newly created and empty or is carried over from page self-load after writeQS
function getCookie(c_name){
function setCookie(c_name,value,expiredays)
function checkCookie(){//put this wherever you need it to happen     <body onLoad="checkCookie()">
function fisherYatesShuffle ( myArray ) {//shuffles an array
function findStyleSheet(filename){// returns the stylesheet object whose href contains the substring filename
function getStyle(filenameOrObj, aSelector){// returns the style object from specified stylesheet - fed either a css filename or a CSS object
function replaceStyleSheet(targetStrOrObj, newStyleSheetUrl){//this isn't working - I can't set the href and get it to stick
function getTargetFromEvent(e){// returns the originating element object associated with an event object
function setOpacity(obj, opacity) {
function TextAreaResizer(elt)

*/

//This prototype is provided by the Mozilla foundation and
//is distributed under the MIT license.
//http://www.ibiblio.org/pub/Linux/LICENSES/mit.license

if (!Array.prototype.indexOf)
{
  Array.prototype.indexOf = function(elt /*, from*/)
  {
    var len = this.length;

    var from = Number(arguments[1]) || 0;
    from = (from < 0)
         ? Math.ceil(from)
         : Math.floor(from);
    if (from < 0)
      from += len;

    for (; from < len; from++)
    {
      if (from in this &&
          this[from] === elt)
        return from;
    }
    return -1;
  };
}


function GetsetIDtoClass(Objid, className){//changes the class of whatever object has that id, returns the old class
	var obj = document.getElementById(Objid);
	var oldClass = obj.className;
	obj.className = className;
	return oldClass;
}


function stripOutComments(str){
	rExp = /(<!--)[^>]+(-->)/gi;
	results = str.replace(rExp, "");
	return results;
}


function makeQuoteFormQueryString(obj,packagingStr){// Attaches as an onclock action to links to GetAQuote.html - puts the proper query string onto the existing href url (no need to duplicate code that way). Also, works if no javascript.
	locKey = "LastPageVisited";//querystring key for current location
	pkgKey = "pkgType";//querystring key for packaging type

currURL = new String(window.location.href);	
	obj.href = obj.href + "?"+pkgKey+"="+packagingStr+"&"+locKey+"="+currURL;
	return true;
	}
	

function filenameRootFromUrl(){
	url = new String(window.location);
	foo=url.slice(url.lastIndexOf('/')+1);
	foo=foo.slice(0,foo.lastIndexOf('.'));
	return(foo);
}


function prependLoadEvent(func) {// adds a function to the onload forthe window
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            func();
            oldonload();//order is reversed
        }
    }
}

function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            oldonload();
            func();
        }
    }
}

function addUnLoadEvent(func) {
    var oldonload = window.onunload;
    if (typeof window.onunload != 'function') {
        window.onunload = func;
    } else {
        window.onunload = function() {
            oldonunload();
            func();
        }
    }
}

// functions having to do with the product popup window
var gPopWin;

function popUp(w,h) {
	if(arguments.length<2){
	w=225;
	h=300;}
	
	try{
	//if(gPopWin){gPopWin.window.close();}
   gPopWin=window.open
   ("","product_win","toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,width="+w+",height="+h);
   gPopWin.focus();
	}
   catch(er){}
   //nothing
  
 
   }
     
function closeWinIfOpen(){
	if(gPopWin){gPopWin.window.close();}}

function goTo (page) {   
	if (page != "" ) {
			document.location.href = page;
			}
	return false;
}

// Functions to get and save form values

	
function setUpFormValsArray(){// set up formVals array if not already set up
	try{
		foo = formVals.count;//cheapo test of existence
	}
	catch(er){
		//first time run will do this
		formVals = new Array();
	}
	}

function SaveFormValues(formId){
		var formSize = document.getElementById(formId).elements.count;
		
		for (var i = 0; i<FormSize; i++){
			formVals[i] = document.getElementById(formId).elements[i];
		}
	}
	
function LoadFormValues(formId){// formVals array is either newly created and empty or is carried over from page self-load after writeQS
		
		var formSize = 0;
		if(typeof (formVals.count) != "undefined"){
			
			formSize = formVals.count;
			
			for (var i = 0; i<FormSize; i++){
				document.getElementById(formId).elements[i] = formVals[i];
			}
			
		}
		

	}

// Functions for use of cookies
function getCookie(c_name){
		if (document.cookie.length>0)
		{ 
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1)
			{ 
			c_start=c_start + c_name.length+1 ;
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
				return unescape(document.cookie.substring(c_start,c_end));
			} 
		}
		return "";
	}

function setCookie(c_name,value,expiredays)
	{
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : "; expires="+exdate.toGMTString());
	}
	
function checkCookie(){//put this wherever you need it to happen     <body onLoad="checkCookie()">
	
	username=getCookie('username');
	if (username!=null && username!="")
	  {alert('Welcome again '+username+'!');}
	else 
	  {
	  username=prompt('Please enter your name:',"")
	  if (username!=null && username!="")
		{
		setCookie('username',username,365);
		}
	  }
	}



function fisherYatesShuffle ( myArray ) {//shuffles an array
  var i = myArray.length;
  if ( i == 0 ) return false;
  while ( --i ) {
     var j = Math.floor( Math.random() * ( i + 1 ) );
     var tempi = myArray[i];
     var tempj = myArray[j];
     myArray[i] = tempj;
     myArray[j] = tempi;
   }
}



function findStyleSheet(filename){// returns the stylesheet object whose href contains the substring filename
		
		rexp = new RegExp(filename, "gi");
 	 	var nStyleSheets = document.styleSheets.length;
		
		for(i=0; i<nStyleSheets; i++){
	 		url = (document.styleSheets[i]).href;

			if(url.search(rexp)>=0){
				return(document.styleSheets[i]);
			}
		}
		return false;
	}
	

function getStyle(filenameOrObj, aSelector){// returns the style object from specified stylesheet - fed either a css filename or a CSS object
	
		if(typeof(filenameOrObj)=='string')
			{aStyleSheetObj = findStyleSheet(filenameOrObj);}
		else
			{aStyleSheetObj = filenameOrObj;}
			
		if (!aStyleSheetObj) {
			return false;}
		rexp = new RegExp(aSelector, "gi");

		var browser=navigator.appName;
		var b_version=navigator.appVersion;
		var version=parseFloat(b_version);
		if (browser!="Microsoft Internet Explorer")
			{var rules = aStyleSheetObj.cssRules;}
		else
			{var rules = aStyleSheetObj.rules;}
			
		var nRules = rules.length;
		
		for(i=0; i<nRules; i++){
	 		sel = rules[i].selectorText;
			

			if(sel.search(rexp)>=0){
				return(rules[i].style);
			}
		}
		return false;
	}


function replaceStyleSheet(targetStrOrObj, newStyleSheetUrl){//this isn't working - I can't set the href and get it to stick
		if(typeof(targetStrOrObj)=='string')
			{origStyleSheetObj = findStyleSheet(targetStrOrObj);}
		else
			{origStyleSheetObj = targetStrOrObj;}
			
		if (!origStyleSheetObj) {
			return false;}
		str = "changing from: "+origStyleSheetObj.href;

		
		origStyleSheetObj.href = newStyleSheetUrl;
}
	
function getTargetFromEvent(e){// returns the originating element object associated with an event object
	//source: http://www.w3schools.com/js/tryit.asp?filename=try_dom_event_srcelement
	var targ;
if (!e)
  {
  var e=window.event;
  }
if (e.target)
  {
  targ=e.target;
  }
else if (e.srcElement)
  {
  targ=e.srcElement;
  }
if (targ.nodeType==3) // defeat Safari bug
  {
  targ = targ.parentNode;
  }
return targ;
}


function setOpacity(obj, opacity) {
  opacity = (opacity == 100)?99.999:opacity;
  
  // IE/Win
  obj.style.filter = "alpha(opacity:"+opacity+")";
  
  // Safari<1.2, Konqueror
  obj.style.KHTMLOpacity = opacity/100;
  
  // Older Mozilla and Firefox
  obj.style.MozOpacity = opacity/100;
  
  // Safari 1.2, newer Firefox and Mozilla, CSS3
  obj.style.opacity = opacity/100;
}

/*
**  TextAreaResizer script by Jason Johnston (jj@lojjic.net)
**  Created August 2003.  Use freely, but give me credit.
**
**  This script adds a handle below textareas that the user
**  can drag with the mouse to resize the textarea.
*/

function TextAreaResizer(elt) {
	this.element = elt;
	this.create();
}

TextAreaResizer.prototype = {
	create : function() {
		var elt = this.element;
		var thisRef = this;
		
		var h = this.handle = document.createElement("div");
		h.className = "textarea-resizer";
		h.title = "Drag to resize text box";
		h.addEventListener("mousedown", function(evt){thisRef.dragStart(evt);}, false);
		elt.parentNode.insertBefore(h, elt.nextSibling);
	},
	
	dragStart : function(evt) {
		var thisRef = this;
		this.dragStartY = evt.clientY;
		this.dragStartH = parseFloat(document.defaultView.getComputedStyle(this.element, null).getPropertyValue("height"));
		document.addEventListener("mousemove", this.dragMoveHdlr=function(evt){thisRef.dragMove(evt);}, false);
		document.addEventListener("mouseup", this.dragStopHdlr=function(evt){thisRef.dragStop(evt);}, false);
	},
	
	dragMove : function(evt) {
		this.element.style.height = this.dragStartH + evt.clientY - this.dragStartY + "px";
	},
	
	dragStop : function(evt) {
		document.removeEventListener("mousemove", this.dragMoveHdlr, false);
		document.removeEventListener("mouseup", this.dragStopHdlr, false);
	},
	
	destroy : function() {
		var elt = this.element;
		elt.parentNode.removeChild(this.handle);
		elt.style.height = "";
	}
};
TextAreaResizer.scriptSheetSelector = "textarea";

