
function getObj(n,d) {
  var p,i,x;
  if(!d)
      d=document;
   if(n != undefined)
   {  if((p=n.indexOf("?"))>0&&parent.frames.length) {
		   d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);
	   }
   }

  if(!(x=d[n])&&d.all)
      x=d.all[n];

  for(i=0;!x&&i<d.forms.length;i++)
      x=d.forms[i][n];

  for(i=0;!x&&d.layers&&i<d.layers.length;i++)
      x=getObj(n,d.layers[i].document);

  if(!x && d.getElementById)
      x=d.getElementById(n);

  return x;

}

function trim(s)
{
	while (s.substring(0,1) == " " || s.substring(0,1) == "\n")
	{
		s = s.substring(1, s.length);
	}
	while (s.substring(s.length-1, s.length) == " " || s.substring(s.length-1,s.length) == "\n") {
		s = s.substring(0,s.length-1);
	}
	return s;
}

function emptyCheck(fldName, fldType, errMsg) {
	var currObj=getObj(fldName)

	if (fldType=="text" || fldType=='hidden') {
		if (currObj.value.replace(/^\s+/g, '').replace(/\s+$/g, '').length==0) {
       			alert(errMsg)
           if (fldType!='hidden')
			currObj.focus()
        return false
		}
   	else
		return true
	} else {
		if (trim(currObj.value) == '') {
                alert(errMsg)
       	        return false
       } else return true
	}
}

function errAlert(fld, fldType, errMsg)
{
	switch(fldType)
    {   case 'text':
            alert(errMsg);
            getObj(fld).focus();
            break;
        default:
            alert(errMsg);
    }
}

function charsLeft(_this, maxChars, countBox)
 {
     currCharLen = _this.value.length;
     if (currCharLen <= maxChars)
     {   remChars = maxChars - currCharLen;
         getObj(countBox).innerHTML = remChars;
      }
     else
         _this.value = _this.value.substring(0,maxChars);
}

function emailValidate(val)
{   var re=new RegExp(/^[a-zA-Z0-9]([a-zA-Z0-9_\-\.]*)@([a-zA-Z0-9_\-\.]*)(\.[a-zA-Z]{2,3}(\.[a-zA-Z]{2}){0,2})$/)

    if (!re.test(val))
        return false;
    else
        return true
}

function popupWin(url, width, height, params)
{
  _width = (width) ? width : 300;
  _height = (height) ? height : 300;

  availWidth =  window.screen.availWidth;
  availHeight = window.screen.availHeight;

  _left = (availWidth / 2) - (_width/2);
  _top = (availHeight / 2) - (_height/2);

  document.open(url, 'popWin', 'status=no,menubar=no,left='+_left+',top='+_top+',width='+_width+',height='+_height+','+params);
}

function resizeToScale(width, height, toWidth, toHeight)
{
   xscale = width/ toWidth;
   yscale = height/ toHeight;

   // Recalculate new size with default ratio
   if (yscale > xscale){
        new_width = Math.round(width * (1/yscale));
        new_height = Math.round(height * (1/yscale));
   }
   else {
        new_width = Math.round(width * (1/xscale));
        new_height = Math.round(height * (1/xscale));
   }

  newDim = new Array();
  newDim['width'] = new_width;
  newDim['height'] = new_height;

  return newDim;

}

function resizeImageToScale(imgObj, toWidth, toHeight)
{
   if (!imgObj) return false;

   width = imgObj.width;
   height = imgObj.height;

   newDim = resizeToScale(width, height, toWidth, toHeight);

   imgObj.width = newDim['width'];
   imgObj.height = newDim['height'];
}

function AddToFavorites(obj, title, url){

  if(window.sidebar){
    // Mozilla Firefox Bookmark
    window.sidebar.addPanel(title, url,"");
    return false;
  } else if(window.external){
    // IE Favorite
    window.external.AddFavorite( url, title);
    return false;
  } else if(window.opera && window.print){
    //Opera Hotlist
    obj.setAttribute('href',url);
    obj.setAttribute('title',title);
    obj.setAttribute('rel','sidebar');
    obj.click();
    return false;
  }
}

function findPosX(obj) {
	var curleft = 0;
	if (document.getElementById || document.all) {
		while (obj.offsetParent) {
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	} else if (document.layers) {
		curleft += obj.x;
    }
	return curleft;
}

function findPosY(obj) {
	var curtop = 0;
	if (document.getElementById || document.all) {
		while (obj.offsetParent) {
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	} else if (document.layers) {
		curtop += obj.y;
	}
	return curtop;
}

function fetchKnowledgeTip()
{
   $.get(baseUrl+'/knowledge_tip.php', function(data){ $('#featured_ktip').html(data); $('#featured_ktip').hide(); $('#featured_ktip').slideDown('slow')})
}