
function setClass(el,classname)
{
	el.className = classname;
}

function setAction(f,action_text)
{
	var el = f.elements['action'];
	el.value = action_text;
	f.submit;
}

   function AddOptionToSelect(sel,opt,val,selected_value)
	{
		if(opt.length>0)
		{
			var m = new Option(opt, val, false, (val==selected_value));
			m.selected = (val==selected_value);
			sel.options[sel.options.length] = m;
			if(val==selected_value)
			{
				sel.selectedIndex = sel.options.length-1;
			}
		}
	}

	function DeleteOptionFromSelect(sel,index)
	{
		sel.options[index] = null;
	}

	function DeleteAllSelectedOptions(sel)
	{
		x = 0;
		while(x<sel.options.length)
		{
			if(sel.options[x].selected)
			{
				sel.options[x] = null;
			}
			else
			{
				x++;
			}
		}
	}

    function SetSelectValue(sel,val)
	{
		for(var x=0;x<sel.options.length-1;x++)
		{
			if(m.value==val)
			{
				m.selected = true;
				sel.selectedIndex = x;
				break;
			}
		}
	}

	function inSelect(el,val)
	{
	    for (var i = 0; i< el.options.length; i++)
	    {
	        if (el.options[i].value==val)
	        {
	            return true;
	        }
	    }
	    return false;
	}
	function SelectAllOptions(el)
	{
	      for(var i=0;i<el.options.length;i++)
	          el.options[i].selected=true;
	}

	function MoveSelectedOptions(srcSelect,destSelect)
	{
	    if(srcSelect && destSelect)
	    {
	        for(var i = 0; i < srcSelect.options.length;i++)
	        {
	            if(srcSelect.options[i].selected)
	            {
	                if(!inSelect(destSelect,srcSelectoptions[i].value))
	                {
	                    x=(destSelect.options)?destSelect.options.length:0;
	                    AddOptionToSelect(srcSelect.options[i].text,srcSelect.options[i].value,0);
	                    srcSelect.options[i].selected=false;
	                }
	            }
	        }
	    }
	}
    
function getWndC(object, c)
{
    pos = 0;
    while (object != null)
    {
        pos += (c == "y") ? object.offsetTop : object.offsetLeft;
        object = object.offsetParent;
    }
    return pos;
}

function getWndX(object) {return getWndC(object, "x")}
function getWndY(object) {return getWndC(object, "y")}	
    
function showElement(el,displayMode)
{
    if(displayMode===undefined)
    {
    	displayMode ='block';
    }
	if(el)
	{
		el.style.display = displayMode; // "block";
		el.style.visibility='visible';
	}
}

function getAbsolutePos(el)
{
   var r = { x: el.offsetLeft, y: el.offsetTop };
   if (el.offsetParent) {
      var tmp = getAbsolutePos(el.offsetParent);
      r.x += tmp.x;
      r.y += tmp.y;
   }
   return r;
}

function hideElement(el)
{
	if(el)
	{
		el.style.display = "none";
		el.style.visibility='hidden';
	}
}

function showAt(el,x, y)
{
   if(el)
   {
		var s = el.style;
   		s.left = x + "px";
	    s.top = y + "px";
   		showElement(el);
   }
}

function showAtElement(el,baseEl,offsetX,offsetY)
{
   var p = getAbsolutePos(baseEl);

   var cw = 190;
   var ch = 200;

   var px = getWndX(baseEl) + baseEl.offsetWidth + 10 + offsetX;
   var py =  p.y + baseEl.offsetHeight-20 + offsetY;

   var w = document.body.offsetWidth;
//   if((px+cw) > w && w>320)
//   {
//   		px = w - 210;
//   }

   showAt(el,px, py);
}

function getElementsByClassName(oElm, strTagName, strClassName)
{
		var arrElements = (strTagName == "*" && document.all)? document.all : oElm.getElementsByTagName(strTagName);
	    var arrReturnElements = new Array();
	    strClassName = strClassName.replace(/\-/g, "\\-");
	    var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
	    var oElement;
	    for(var i=0; i<arrElements.length; i++){
	        oElement = arrElements[i];
	        if(strClassName.length>0)
	        {
//		        if(oRegExp.test(oElement.className)){
				if(oElement.className==strClassName) {
		            arrReturnElements.push(oElement);
	    	    }
	    	}
	    	else
	    	{
	    		arrReturnElements.push(oElement);
	    	}
	    }
	    return (arrReturnElements)
}

function showPopup(el,baseEl,offsetX,offsetY)
{
	if(el)
	{
		if(baseEl)
		{
			showAtElement(el,baseEl,offsetX,offsetY);
		}
		else
			showElement(el);
	}
}

function showHideSerial(id,isChecked)
{
	var txt = document.getElementById('form_serial_'+id);
	if(isChecked)
	{
	    showElement(txt,'inline');
	}
	else
	{
		hideElement(txt);
	}
}

function hideShowCovered(vis) 
{
   var tags = new Array("applet", "iframe", "select");

   for (var k = tags.length; k > 0; ) {
      var ar = document.getElementsByTagName(tags[--k]);
      var cc = null;

      for (var i = ar.length; i > 0;) {
         cc = ar[--i];

	  	cc.style.visibility = vis;
      }
   }
 }
   
function GeneratePassword(length,noPunction,randomLength) 
{
    var length=8;
    var sPassword = "";

    if (randomLength) {
        length = Math.random();

        length = parseInt(length * 100);
        length = (length % 7) + 6
    }

    for (i=0; i < length; i++) {

        numI = getRandomNum();
        if (noPunction) { while (checkPunc(numI)) { numI = getRandomNum(); } }

        sPassword = sPassword + String.fromCharCode(numI);
    }

    return sPassword;
}

function getRandomNum() {

    // between 0 - 1
    var rndNum = Math.random()

    // rndNum from 0 - 1000
    rndNum = parseInt(rndNum * 1000);

    // rndNum from 33 - 127
    rndNum = (rndNum % 94) + 33;

    return rndNum;
}

function checkPunc(num) {

    if ((num >=33) && (num <=47)) { return true; }
    if ((num >=58) && (num <=64)) { return true; }
    if ((num >=91) && (num <=96)) { return true; }
    if ((num >=123) && (num <=126)) { return true; }

    return false;
}

function showValidationErrors(str)
{
    alert("Please correct the following errors/omissions:\n\n"+str);
}

function ValidateRequiredFields(frm)
{
	   var str = '';
	   var bIsDateField = false;	
	   var bIsCheckBox = false;
	   for(i=0; i<frm.elements.length; i++)
	   {
	   		var el = frm.elements[i];
	   		var a = el.attributes.getNamedItem('Required');
	   		if(a)
	   		{
	   			var bRequired = (a.value != 'false');
	   		}
	   		else
	   			var bRequired = false;
	   			
	   		nMinLen = 1;
	   			
			var a = el.attributes.getNamedItem('MinLength');
	   		if(a)
	   		{
	   			var nMinLen = parseInt(a.value);
	   		}
	   		
	   		bIsDateField = false;
	   		bIsCheckBox = false;
	   		if(el.type=="checkbox")
	   		{
	   			bIsCheckBox = true;
	   		}
	   		var dt = el.attributes.getNamedItem('datepickerIcon');
	   		if(dt)
	   		{
	   			if(dt.value.length>0)
	   			{
	   				bIsDateField = true;
	   			}
	   		}
	   		if(bRequired || bIsDateField)
	   		{
	   				var a = el.attributes.getNamedItem('ReqPrompt');
					if(a)
					{
	   					var sReqStr = a.value;
					}
					else
						var sReqStr = '';
					
	   				if(sReqStr.length==0)
	   				{
	   					if(nMinLen<2)
	   					{
	   					  	sReqStr = el.name+' must have a value.';
	   					}
	   					else
	   					{
	   						sReqStr = el.name+' must be at least '+nMinLen+' characters long';
	   					}
	   				}
	   				if(bIsCheckBox)
	   				{
	   					if(!el.checked)
	   					{
	   						str = str+'\n'+sReqStr;
	   					}
	   				}
	   				else
	   				{
		   				if(bIsDateField)
		   				{
		   					if(!isDate(el.value))
		   					{
		   						str = str+'\n'+sReqStr;
		   					}
		   				}
		   				else
		   				{
				   			if( el.value.length < nMinLen)
				   			{
				   				str = str+'\n'+sReqStr;
				   			}
				   		}
				   	}
	   		}
	   }

	   return str;
}

var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		return false
	}
	return true
}

function hideShowCovered(vis) 
{
   var tags = new Array("applet", "iframe", "select");
   var el = this.element;

   for (var k = tags.length; k > 0; ) {
      var ar = document.getElementsByTagName(tags[--k]);
      var cc = null;

      for (var i = ar.length; i > 0;) 
      {
         cc = ar[--i];

     	 cc.style.visibility = vis;
      }
   }
}

function getW()
{
	var w;
	if(document.innerWidth)
	{ 
		w=document.innerWidth;
	} 
	else if(document.documentElement.clientWidth)
	{ 
		w=document.documentElement.clientWidth;
	} 
	else if(document.body)
	{ 
		w=document.body.clientWidth; 
	}
	return w;
}
	
function getH()
{
	var h;
	if(document.innerHeight)
	{ 
		h=document.innerHeight;
	} 
	else if(document.documentElement.clientHeight)
	{ 
		h=document.documentElement.clientHeight;
	} 
	else if(document.body)
	{ 
		h=document.body.clientHeight; 
	}
	return h;
}	