<!--
/**
 * $Id: common.js,v 1.4 2007/08/31 22:48:51 sudaraka Exp $
 * Created: 03/13/2007
 * $Log: common.js,v $
 * Revision 1.4  2007/08/31 22:48:51  sudaraka
 * adjustments before rc1
 *
 * Revision 1.3  2007/08/31 07:24:41  sudaraka
 * complete public area artist and category pages
 *
 * Revision 1.2  2007/08/30 23:03:02  sudaraka
 * - applied new layout and modified homepage accordingly
 *
 * Revision 1.1  2007/08/13 08:05:08  sudaraka
 * Adding to CVS
 *
 *
 */


var isNN=(navigator.appName.indexOf("Netscape")!=-1);
var isIE=(navigator.appName.indexOf("Internet Explorer")!=-1);

Number.prototype.NaN0=function(){return isNaN(this)?0:this;}

function validateField(field,message,regexp){
	var bEmpty=false;
	var strAction='enter';
	var field_value='';
	var xField=(field.length>0 && !field.type)?field[0]:field;
	var selectedIndex=-1;

	switch(xField.type){
		case 'textarea':
		case 'hidden':
		case 'password':
		case 'file':
		case 'text':{
			field_value=field.value;
			bEmpty=(field_value.length<1);
			break;
		}
		case 'select-one':{
			if(field.selectedIndex>=0) field_value=field.options[field.selectedIndex].value;
			bEmpty=(field.selectedIndex<0 || field_value.length<1);
			strAction='select';
			break;
		}
		case 'checkbox':{
			strAction='select';
			bEmpty=!xField.checked;
			break;
		}
		case 'radio':{
			strAction='select';
			bEmpty=true;
			if(xField!=field){
				for(var intIdx=0;intIdx<field.length;intIdx++){
					if(field[intIdx].checked){
						bEmpty=false;
						selectedIndex=intIdx;
						break;
					}
				}
			}
			else{
				bEmpty=!xField.checked;
				if(!bEmpty) selectedIndex=0;
			}
			break;
		}
		default:{
			bEmpty=true;
			alert('form element \''+xField.name+'\' ['+xField.type+'] is not supported by checkEmpty() function');
			break;
		}
	}

	if(regexp && !bEmpty){
		bEmpty=!regexp.test(field_value);
	}

	if(bEmpty){
		if(message) alert(message);
		else alert('Please '+strAction+' a value for field \''+xField.name+'\'');
		if(xField.type!='hidden') xField.focus();
	}

	return (xField.type=='radio')?selectedIndex:!bEmpty;
}

function gotoPage(url){
	top.location.href=url;
}

function trim(value){
   var temp=value;
   var obj=/^(\s*)([\W\w]*)(\b\s*$)/;
   if (obj.test(temp)) { temp=temp.replace(obj, '$2'); }
   var obj=/ +/g;
   temp=temp.replace(obj, " ");
   if (temp == " ") { temp=""; }
   return temp;
}

function removeChar(Target){
	var re=/[^0-9]/g;
	Target=Target.replace(re,'');
	return Target;}

function removeNonNum(Target){
	var re=/[^0-9|\^.|\^-]/g;
	Target=Target.replace(re,'');
	return Target;}

function removeDigit(Target){
	var re=/[0-9]/g;
	Target=Target.replace(re,'');
	return Target;
}

function containsElement(arr, ele){
	var found=false, index=0;
	while(!found && index < arr.length){
		if(arr[index] == ele)
			found=true;
		else
			index++;
	}
	return found;
}
function getIndex(input){
	var index=-1, i=0, found=false;
	while (i < input.form.length && index == -1){
		if (input.form[i] == input)index=i;
		else i++;
	}
	return index;
}
function autoTab(input, e){
	var keyCode=(isNN) ? e.which : e.keyCode; 
	var filter=(isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
	if(input.value.length >= input.maxLength && !containsElement(filter,keyCode)){
		input.value=input.value.slice(0, input.maxLength);
		input.form[(getIndex(input)+1) % input.form.length].focus();
	}
return true;
}

function popup(url, width, height, scroll, name){
	if(isNaN(width) || width<1) width=400;
	if(isNaN(height) || height<1) height=250;
	
	var top=(screen.height-height)/2;
	var left=(screen.width-width)/2;
	var strParams='toolbar=0,statusbar=0, scrollbars='+(scroll?1:0)+' ,width='+width+',height='+height+',top='+top+',left='+left;
	
	var win=window.open(url, 'win_'+name, strParams);
	return win;
}

function shortPhara(val, len){
	if(isNaN(len) || len<1) return val;
	
	val=trim(val);
	if(val.length>len){
		val=trim(val.substr(0, len))+'...';
	}
	
	return val;
}

function formatNumber(number, decPoints){
	var tmpRT=number.toString(10);
	if(tmpRT.indexOf('.')<0)
	tmpRT+='.';
	var tmp=tmpRT.substr(tmpRT.indexOf('.')+1,tmpRT.length);
	if(tmp.length>2)
	tmpRT=tmpRT.substr(0,tmpRT.indexOf('.')+3);
	for(var j=tmp.length;j<2;j++)tmpRT+='0';
	return tmpRT;
}

function clearFields(defval){
	var fields=document.getElementsByTagName('input');
	
	if(!defval) defval='';
	
	if(fields.length){
		for(var i=0;i<fields.length;i++){
			var val=fields[i].getAttribute('autocomplete');
			if(val) val=val.toLowerCase();
			if(val=='off') fields[i].value=defval;
		}
	}
	else{
		if(fields.getAttribute('autocomplete').toLowerCase()=='off') fields.value=defval;
	}
}

function browseCategory(elm){
	gotoPage('category.php?catid='+elm.options[elm.selectedIndex].value);
}

function browseArtist(elm){
	gotoPage('artist.php?artistid='+elm.options[elm.selectedIndex].value);
}

function getOffset(elm,ofName){
	var totOFS=0;
	do{
		totOFS+=eval('elm.'+ofName);
		elm=eval('elm.offsetParent');
	}
	while(elm!=null)
	
	return totOFS;
}

function slimboxClose()
{
}
function slimboxOpen()
{
}

//-->
