function rand ( n )
{
  return ( Math.floor ( Math.random ( ) * n + 1 ) );
}

function viewArtist(text)
{	
	window.location=linkpage+"?text="+text+"&bio_id="+bio;
}

//set a block visible or hidden by a checkbox or a bool variable
function setVisibleIfChecked(block, condition){
	var isVisible = false;
	if(typeof(condition) == "string"){
		condition_ctrl = document.getElementById(condition);
		if(isHTMLElement(condition_ctrl, "checkbox")){
			isVisible = condition_ctrl.checked;
		}
	}else{
		isVisible = condition;
	}
	
	document.getElementById(block).className = isVisible ? 'SHOWN' : 'HIDDEN';
}

var setInnerHTML = function (el, htmlCode) {
    var ua = navigator.userAgent.toLowerCase();
    if (ua.indexOf('msie') >= 0 && ua.indexOf('opera') < 0) {
        htmlCode = '<div style="display:none">for IE</div>' + htmlCode;
        htmlCode = htmlCode.replace(/<script([^>]*)>/gi,
                                    '<script$1 defer>');
        el.innerHTML = '';
        el.innerHTML = htmlCode;
        el.removeChild(el.firstChild);
    } else {
        var el_next = el.nextSibling;
        var el_parent = el.parentNode;
        el_parent.removeChild(el);
        el.innerHTML = htmlCode;
        if (el_next) {
            el_parent.insertBefore(el, el_next)
        } else {
            el_parent.appendChild(el);
        }
    }
}

function getSelectCtrl(select_ctrl){
	if(undef(select_ctrl))
		return null;
	
	var ctrl = null;
	
	if(isHTMLElement(select_ctrl, "select")){
		ctrl = select_ctrl;
	}
	
	if(ctrl == null && typeof(select_ctrl) == "string"){
		ctrl = document.getElementById(select_ctrl);
	}	
	
	if(undef(ctrl))
		return null;
	else
		return ctrl;
}

function getSelectValue(selectctl){
	var ctrl = getSelectCtrl(selectctl);
	if(undef(ctrl))
		return;
		
	return ctrl.options[ctrl.selectedIndex].value;
}

function clearOptions(select_ctrl){
	var ctrl = getSelectCtrl(select_ctrl);
	if(undef(ctrl))
		return null;
	
	ctrl.options.length = 0;
}

function addOption(select_ctrl, option_value, option_text){
	var ctrl = getSelectCtrl(select_ctrl);
	if(undef(ctrl))
		return null;
		
	var doc = ctrl.ownerDocument;
	if (!doc)
		doc = ctrl.document;
		
	var opt = doc.createElement('OPTION');
	opt.value = option_value;
	opt.text = option_text;
	
	ctrl.options.add(opt, ctrl.options.length);
}
	
function editselected(editpage, chkName){
	//get ctrls by name
	var checkboxes=document.getElementsByName(chkName);
    
    var count = 0;
    var itemid = "";
    //check how many check box was selected
	for(var i=0;checkboxes.length != undefined && i<=checkboxes.length-1;i++){
		if(checkboxes[i].checked){
			itemid = checkboxes[i].value;
			count ++;
		}
	}
        
	//if more than 1 checkbox selected, show error
	if(count != 1){
		alert("select only one item to edit at a time");
		return;
	}
	
	//go to edit page.
	window.location=editpage +'/'+ itemid;	
}

function confirmdelete(){
	if(!confirm("Are you sure want to delete the selected items?"))
		return false;
	
	return true;
}

function listbyletter(letter) {
  	document.getElementById('search_by_letter').value=letter;
  	document.getElementById('search_artist_name').value='';
  	document.getElementById('search_page').value=1
  	document.getElementById('searchform').submit();
}

function dosearch() {
  	document.getElementById('search_by_letter').value='';
  	document.getElementById('searchform').submit();
  	return true;
}

function showpage(pageno) {
	document.getElementById('search_page').value=pageno;
	document.getElementById('searchform').submit();
}

function gotopage(url) {
	window.location=url;	
}

function openWindow(theURL,winName,features, content) {
  var newwindow = window.open(theURL,winName,features);
  if(content){
  	newwindow.document.open();
  	newwindow.document.clear();
  	newwindow.document.write(content);
  	newwindow.document.close();
  }
}

function textCounter( field, maxlimit ) {
  if ( field.value.length > maxlimit )
  {
    field.value = field.value.substring( 0, maxlimit );
    return false;
  }
}

function undef(o)
{
  return typeof(o) == 'undefined' || o === '' || o == null
}

function def(o)
{
  return !undef(o)
}

function getRadioValue(radioctl){
	if(undef(radioctl))
		return;
		
	var ctrls = document.getElementsByName(radioctl)
	for(i=0;i<ctrls.length;i++)
	{
		if(ctrls[i].checked){
			return ctrls[i].value;
		}
	}
	
	return "";
}

function isValueExists(selectctl, value){
	if(undef(selectctl))
		return;
	
	var ctrl = null;
	
	if(isHTMLElement(selectctl, "select")){
		ctrl = selectctl;
	}
	
	if(ctrl == null && typeof(selectctl) == "string"){
		ctrl = document.getElementById(selectctl);
	}	
	
	if(undef(ctrl))
		return;
	
	for(idx = 0; idx < 	ctrl.options.length; idx ++){
		if(ctrl.options[idx].value == value)
			return true;
	}
	
	return false;
}

function isArray(data)
{
	return (data && data.join) ? true : false;
}

function isHTMLElement(node, nodeName)
{
	if (node == null || typeof node != "object" || node.nodeName == null)
	{
		return false;
	}
	
	if (nodeName != null)
	{
		var test = node.nodeName.toLowerCase();
		
		if (typeof nodeName == "string")
		{
			return test == nodeName.toLowerCase();
		}
		
		if (isArray(nodeName))
		{
			var match = false;
			for (var i = 0; i < nodeName.length && !match; i++)
			{
				if (test == nodeName[i].toLowerCase())
				{
					match =  true;
				}
			}
			
			return match;
		}
		
		alert("isHTMLElement was passed test node name that is neither a string or array of strings");
		return false;
	}
	
	return true;
}

function iEquals(str1, str2) {
    return str1.toLowerCase() == str2.toLowerCase();
}

function trim(str) {
    return str.replace(/(^\s+)|(\s+$)/g, "");
}

/*
Object.prototype.equals = function (obj){
	if(this == obj)return true;
	if(typeof(obj)=="undefined"||obj==null||typeof(obj)!="object")return false;
	var length = 0; var length1=0;
	for(var ele in this) length++;for(var ele in obj) length1++;
	if(length!=length1) return false;
	if(obj.constructor==this.constructor){
		for(var ele in this){
			if(typeof(this[ele])=="object") {if(!this[ele].equals(obj[ele]))return false;}
			else if(typeof(this[ele])=="function"){if(!this[ele].toString().equals(obj[ele].toString())) return false;}
			else if(this[ele]!=obj[ele]) return false;
		}
		return true;
	}
	return false;
}

String.prototype.equals = function (str){
	if(this==str)return true;
	return false;
}

Function.prototype.equals = function (func){
	if(this.toString().equals(func.toString()))return true;
	return false;
}

Boolean.prototype.equals = function (bool){
	if(this==bool)return true;
	if (bool instanceof Boolean){
	    return this.toString().equals(bool.toString());
	} 
	return false;
}

Object.prototype.clone = function (){
	var newObj = new Object();
	for(elements in this){
		newObj[elements] = this[elements];
	}
	return newObj;
}

Object.prototype.cloneAll = function (){
	function clonePrototype(){}
	clonePrototype.prototype = this;
	var obj = new clonePrototype();
	for(var ele in obj){
		if(typeof(obj[ele])=="object") obj[ele] = obj[ele].cloneAll();
	}
	return obj;
}

Object.prototype.toString = function (){
	var str="";
	for(elements in this){
		str += elements + this[elements];
	}
	return str;
}

String.prototype.trim = function() {
    return this.replace(/(^\s+)|(\s+$)/g, "");
}

String.prototype.iEquals = function(str) {
    return this.toLowerCase() == str.toLowerCase();
}

String.prototype.compareTo = function(str) {
    if (this == str) {
        return 0;
    } else if (this < str) {
        return -1;
    } else {
        return 1;
    }
}

String.prototype.iCompareTo = function(str) {
    return this.toLowerCase().compareTo(str.toLowerCase());
}

String.prototype.startsWith = function(str) {
    return this.substr(0, str.length) == str;
}

String.prototype.iStartsWith = function(str) {
    return this.substr(0, str.length).iEquals(str);
}

String.prototype.endsWith = function(str) {
    return this.substr(this.length - str.length) == str;
}

String.prototype.iEndsWith = function(str) {
    return this.substr(this.length - str.length).iEquals(str);
}

String.prototype.style = function(style) {
    return "<span style=\"".concat(style, "\">", this, "</span>");
}
String.prototype.toInt = function(style) {
    return parseInt(this);
}
*/

