/*==================================================*
 $Id: spidyjsutils.js, v1.0 2010/02/13 01:24:00 Exp $
 Copyright 2010 Spidy Web Design
 Last Modified 3/18/2010
 http://spidy.net/
 This is a set of general JS utility functions
 *==================================================*/

/**
 * A general helper function
 * $() replaces $()
 */
if(typeof($)=='undefined'){
  function $(elementId){
    var element = document.getElementById(elementId);
    return element;
  }
}

// FIX for IE
//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;
  };
}

// Translation function to clean up output
var trtable = {'&amp;': '&', '&gt;': '>', '&lt;': '<', '&#8211;': '-', '&#8212;': '-',
     '&quot;': '"', '&#8220;': '"', '&#8221;': '"', '&#8217;': "'", '&#8216;': "'", 
     '&nbsp;': ' '}

var rtrtable = {'"':'&quot;', '>':'&gt;', '<':'&lt;', "'":'&#8217;', '-':'&#8211;'}

function clean2(phrase) {
    for (c in trtable) {
        phrase = phrase.replace(new RegExp( c, "g" ), trtable[c]);
    }
    return phrase;
}
function webify(phrase) {
    phrase = phrase.replace(/&/g, '&amp;');
    for (c in rtrtable) {
        phrase = phrase.replace(new RegExp( c, "g" ), rtrtable[c]);
    }
    return phrase;
}

// Display Amazon Ads for Books and Media
function amazon_ad(isbn, stylemod) {
    if (!stylemod) stylemod = '';
    return '<iframe src="http://rcm.amazon.com/e/cm?t=spidywebdes05-20&o=1&p=8&l=as1&asins='+isbn+
        '&fc1=000000&IS2=1&lt1=_blank&lc1=0000ff&bc1=000000&bg1=ffffff&f=ifr" style="width:120px;height:240px;'
        +stylemod+'" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"><\/iframe>';
}
function amazon_ad_sm(isbn, stylemod) {
    if (!stylemod) stylemod = '';
    return '<iframe src="http://rcm.amazon.com/e/cm?lt1=_blank&bc1=ffffff&IS2=1&npa=1&bg1=ffffff&fc1=000000&lc1=0000ff&t=spidywebdes05-20&o=1&p=8&l=as1&m=amazon&f=ifr&asins='+isbn
        + '" style="width:100px;height:120px;'+stylemod+'" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"><\/iframe>'
}

// set the radio button with the given value as being checked
// do nothing if there are no radio buttons
// if the given value does not exist, all the radio buttons
// are reset to unchecked
function setRadioValue(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		if (radioObj.value == newValue.toString()) {
            radioObj.value = newValue;
            radioObj.checked = true;
        } else {radioObj.checked = false; }
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
            radioObj.value = newValue;
		}
	}
}
