// <!-- #	#	#	#	#	#	#	#	#	#
//       File:  SwsSysCmn.js
//   Language:  JavaScript
//     Client:  Ganeral.
//     Author:  www.sarand.com
//    Purpose:  Sarand WebStore common functionality - to be loaded by all windows.
//	#	#	#	#	#	#	#	#	#	#
var doc=window.document
//	#	#	#	#	#	#	#	#	#	#
// Basic, raw functionality:
function di(){if(!doc)doc=window.document;return doc}// Doc init.
function dg(obj){return obj&&obj.document?obj.document:obj&&obj.nodeName=='#document'?obj:di()}// Doc get.
function dw(s){di();doc.write(s);}// Doc write.
function getElement(id,obj)
{
	obj=dg(obj)
	if(undef(obj))obj=di()
	if(obj.getElementById)return obj.getElementById(id);
	if(obj.all)return obj.all[id];
	if(obj.layers)return obj.layers[id];
	return obj[id]
}
function getStyle(obj){return(undef(obj)?obj:obj.style)?obj.style:obj;}
function getInnerDoc(obj){return(undef(obj)?obj:obj.contentDocument)?obj.contentDocument:obj.contentWindow.document;}
function rnd(iTop){return Math.round(Math.random()*(iTop-1))}
function pi10(x){return parseInt(x,10)}
function undef(o){return typeof o=='undefined'}
function debug(s){if(!bDebug)return;var div=getElement('DebugConsole');if(div)div.innerHTML=s;}
function alertElements(obj,match)
{
	// Display object element names:
	var msg='Object elements:\n'
	for(el in obj){if(!match||el.indexOf(match)>=0)msg+=el+'\n'}
	alert(msg)
}
// Cookies:
function setCookie(id,value,expires,path,domain,secure)
{
	var cookie=id+"="+escape(value)
	if(expires)cookie+="; expires="+expires.toGMTString();
	if(path)cookie+="; path="+escape(path);
	if(domain)cookie+="; domain="+escape(domain);
	if(secure)cookie+="; secure";
	document.cookie=cookie;
}
function getCookie(id)
{
	var data=document.cookie.match(id+'=(.*?)(;|$)');
	if (data)return(unescape(data[1]));
	return null
}
function clearCookie(id)
{
	//alert(window.document.cookie)
	//alert(id+' = '+getCookie(id));
	var expires=new Date()
	expires.setTime(0);//(expires.getTime()-60000)
	setCookie(id,undef,expires);
}
// Form handling:
function initFocus(ctrl)
{
	// Set focus to control:
	if(ctrl&&ctrl.focus){ctrl.focus();if(ctrl.type=='text')ctrl.select();}
}
function initFocusById(id)
{
	initFocus(getElement(id))
}
// Return x:y coordinates of the object, relative to the page.
function getPosById(id){return getPos(getElement(id))}
function getPos(obj)
{
	var x=0,y=0,h=0,w=0;
	if(!obj)return;
	// Find object position:
	if(doc.getElementById||doc.all)
	{
		x=getPageOffsetLeft(obj)
		y=getPageOffsetTop(obj)
	}
	else if(doc.layers)
	{
		x=obj.x;
		y=obj.y;
	}
	h=(obj.offsetHeight?obj.offsetHeight:obj.clientHeight)
	w=(obj.offsetWidth?obj.offsetWidth:obj.clientwidth)
	return {x:pi10(x),y:pi10(y),height:pi10(h),width:pi10(w)};
}
// Return x:y coordinates of the object, relative to the screen.
function getPosScreenById(id){return getPosScreen(getElement(id))}
function getPosScreen(obj)
{
	var pos=getPos(obj);
	if (isNaN(window.screenX))
	{
		pos.x=pos.x-document.body.scrollLeft+window.screenLeft;
		pos.y=pos.y-document.body.scrollTop+window.screenTop;
	}
	else
	{
		pos.x=pos.x+window.screenX+(window.outerWidth-window.innerWidth)-window.pageXOffset;
		pos.y=pos.y+window.screenY+(window.outerHeight-24-window.innerHeight)-window.pageYOffset;
	}
	return pos;
}
// IE functions to get object's position.
function getPageOffsetLeft(o){if(!o||undef(o.offsetLeft))return 0;var ol=0;do{ol+=o.offsetLeft;o=o.offsetParent;}while(o!=null);return ol;}
function getWindowOffsetLeft(o){return getPageOffsetLeft(o)-doc.body.scrollLeft;}
function getPageOffsetTop(o){var ot=o.offsetTop;while((o=o.offsetParent)!=null){ot+=o.offsetTop;}return ot;}
function getWindowOffsetTop(o){return getPageOffsetTop(o)-doc.body.scrollTop;}
// Trap Javascript exceptions:
if(navigator.userAgent.indexOf('MSIE')==0)onerror=function(sErr,sUrl,iLine)
{
	di()
	var msg="A script error has been identified on this page.\n"+doc.location+"\n\n"
	msg+="Click OK to continue.\n\nError: "+sErr+"\n"
	msg+="URL: "+sUrl+"\nLine: "+iLine
	alert(msg);return true
}
//	#	#	#	#	#	#	#	#	#	#
// Event handling:
// Swap image and display caption on status bar - called on mouseOver/Out:
function omOver(sCaption,imgTgt,imgSrc){if(imgTgt&&imgSrc)imgTgt.src=imgSrc.src;status=sCaption;return true;}
function omOut(imgTgt,imgSrc){if(imgTgt&&imgSrc)imgTgt.src=imgSrc.src;status='';return true;}
// Convert control value to upper case - called on input.onChange:
function upperCtrl(ctrl){if(ctrl&&ctrl.value&&ctrl.value.length)ctrl.value=ctrl.value.toUpperCase();return true;}
//	#	#	#	#	#	#	#	#	#	#
// Additional string methods:
String.prototype.left=function(n){return(n<1?'':n>this.length?this:this.substring(0,n))}
String.prototype.right=function(n){return(n<1?'':n>this.length?this:this.substring(this.length,this.length-n))}
String.prototype.find=function(s){return(this.indexOf(s)>=0?true:false);}
// Trim leading, trailing and multiple internal spaces:
String.prototype.trim=function(){return this.replace(/^\s*/,"").replace(/\s*$/,"").replace(/ +/g," ");}
String.prototype.strip=function(sStrip)
{
	if(sStrip==undefined||!sStrip.length)return this;
	var re=new RegExp (sStrip,'g') ;
	return this.replace(re,"");
}
//	#	#	#	#	#	#	#	#	#	# -->
