// JavaScript Document

function Get(id)
{
	return Pbm.get(id);
}
function GetN(name)
{
	var obj = document.getElementByName(name);
	return Pbm.get(id);
}
function GetSelectedValue(id)
{
	var cbo = Get(id);
	
	if(cbo!=null){
		for (i=0; i<cbo.options.length; i++) {
			if (cbo.options[i].selected) {
				return cbo.options[i].value;			
			}
		}
		return cbo.options[0].value;
	}
	
	return -1;
}
function IsEmail(str)
{ 
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	
	if (filter.test(str)){
		return true;
	}	
	return false; 	
}


function IsPhone(str){
   var filter=/^[0-9]{3}[\-]{1}[0-9]{3}[\-]{1}[0-9]{4}?$/i
   if(filter.test(str)){
   	return true;
   }
   return false;
}




function Trim(str)
{
	str = str.replace(/^\s+/, ''); 
	str = str.replace(/\s+$/, ''); 
	
	return str;
}
function IsPrice(str)
{
	var filter = /^[0-9]*\.?[0-9]+$/
	return filter.test(str);
	
}
function IsDecimal(str)
{
	var filter = /^[0-9]*\.?[0-9]+$/
	return filter.test(str);
	
}
function isBlank(str)
{
	str = Trim(str);
	return str.length==0;
}
function isSame(str1,str2)
{
	str1 = Trim(str1);
	str2 = Trim(str2);
	if(str1==str2){
		return true;	
	}
	return false;

}

Pbm = 
{
	get: function(id)
	{
		var elem;
	
		// this logic accomodates multiple browsers
		if(document.getElementById)
		{
			elem = document.getElementById(id);
		}
		else if (document.layers && document.layers[id])
		{
			elem = document.layers[id];
		}
		else if (document.all)
		{
			elem = document.all[id];
		}

		return elem;
		
	},
	popUp : function(url, x, y, w, h)
	{
		 if(this.popUpRef)
		 {
		 	if(!this.popUpRef.closed)
			{
				this.popUpRef.close();
			}
		 }
		 this.popUpRef = open(url, 'pbm_popUp', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width='+w+',height='+h+',left='+x+', top='+y+',screenX='+x+',screenY='+y);
	},
	isBlank : function(str)
	{
		return typeof(str) == "undefined" || str == null || str.trim() == "";
	}, 
	isEmptyObject: function(obj)
	{
		var s = "";
		var val;
		for(prop in obj)
		{
			val = obj[prop];
			if(val.constructor == String)
			{
				s += val;
			}
			else if(val.constructor == Number)
			{
				// if value is zero, then assume empty
				s += val==0 ? "" : val;
			}
		}
		return s.length==0;
	},
	renderMessages: function(errors, messages)
	{
		var html = "";
		
		var msgElem = Pbm.get("messageArea");
		
		if(!msgElem)
		{
			return;
		}
		
		if(errors && errors.length > 0)
		{
			html += '<ul id="errors">';
			for(var i=0; i<errors.length; i++)
			{
				html += '<li>' + errors[i] + '</li>';
			}
			html += '</ul>';
		}
		
		
		if(messages && messages.length > 0)
		{
			html += '<ul id="messages">';
			for(var i=0; i<messages.length; i++)
			{
				html += '<li>' + messages[i] + '</li>';
			}
			html += '</ul>';
		}
		
		msgElem.innerHTML = html;
		msgElem.style.display = "block";
		
		var href = document.location.href;
		document.location = href.indexOf("#")!=-1 ? href : href + "#"
		
	},
	clearMessages: function()
	{
		var msgElem = Pbm.get("messageArea");
		msgElem.innerHTML = "";
		msgElem.style.display = "none";
	}
};

