function stripCharsNotInBag(s, bag) { 
	var i;
    var returnString = "";

    // Search through string's characters one by one.
    // If character is in bag, append to returnString.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) != -1) returnString += c;
    }

    return returnString;
}

function reformat (s) {   
	var arg;
    var sPos = 0;
    var resultString = "";

    for (var i = 1; i < reformat.arguments.length; i++) {
       arg = reformat.arguments[i];
       if (i % 2 == 1) resultString += arg;
       else {
           resultString += s.substring(sPos, sPos + arg);
           sPos += arg;
       }
    }
    return resultString;
}

function reformatUSPhone(USPhone) {   
	if (USPhone.length < 10) { return USPhone; }
	USPhone = stripCharsNotInBag (USPhone, "1234567890");
	return (reformat (USPhone, "(", 3, ") ", 3, "-", 4));
}

function checkUSPhone(USPhone) {
	USPhone = stripCharsNotInBag (USPhone, "1234567890");
	if (USPhone.length < 10) { return false; }
	if (USPhone.length > 10) { return false; }
	return true;
}


function checkText(field) {
	if(field.value == "" || field.value.charAt(0) == " "){
		return false;
	}
	return true;
}

function checkSelect(field) {
	selected = field.selectedIndex;
	if(field[selected].value == "" || field[selected].value == "Select") {
		return false;
	}
	return true;
}

function checkRadioXbox(field) {
	var checked = false
	for(r=0;r < field.length;r++) {
		if(field[r].checked) {
			checked = true
		}
	}
	if(!checked) {
		return false;
	}
	return true;
}

function check_message(form)
{
	var check = false;
		
	if(error_message != "")
	{
		alert("There are errors with the following fields:\n" + "_______________________________\n\n" + error_message);
		error_message = "";
		if(window.focus)
		{
			if(firstfield != "")
			{
			    focusfield = firstfield;
				firstfield = "";
				focusfield.focus();		
			}  
		}
		return false;
	}
return true;
}

/**
 * Sets/unsets the pointer and marker in browse mode
 *
 * @param   object   the table row
 * @param   string   the action calling this script (over, out or click)
 * @param   string   the default background color
 * @param   string   the color to use for mouseover
 * @param   string   the color to use for marking a row
 *
 * @return  boolean  whether pointer is set or not
 * 
 * usage ->
 * onmouseover="setPointer(this, 'over', '#DDDDDD', '#CCFFCC', '#FFCC99')" onmouseout="setPointer(this, 'out', '#DDDDDD', '#CCFFCC', '#FFCC99')" onmousedown="setPointer(this, 'click', '#DDDDDD', '#CCFFCC', '#FFCC99')"
 */
function setPointer(theRow, theAction, theDefaultColor, thePointerColor, theMarkColor)
{
    var theCells = null;

    // 1. Pointer and mark feature are disabled or the browser can't get the
    //    row -> exits
    if ((thePointerColor == '' && theMarkColor == '')
        || typeof(theRow.style) == 'undefined') {
        return false;
    }

    // 2. Gets the current row and exits if the browser can't get it
    if (typeof(document.getElementsByTagName) != 'undefined') {
        theCells = theRow.getElementsByTagName('td');
    }
    else if (typeof(theRow.cells) != 'undefined') {
        theCells = theRow.cells;
    }
    else {
        return false;
    }

    // 3. Gets the current color...
    var rowCellsCnt  = theCells.length;
    var domDetect    = null;
    var currentColor = null;
    var newColor     = null;
    // 3.1 ... with DOM compatible browsers except Opera that does not return
    //         valid values with "getAttribute"
    if (typeof(window.opera) == 'undefined'
        && typeof(theCells[0].getAttribute) != 'undefined') {
        currentColor = theCells[0].getAttribute('bgcolor');
        domDetect    = true;
    }
    // 3.2 ... with other browsers
    else {
        currentColor = theCells[0].style.backgroundColor;
        domDetect    = false;
    } // end 3

    // 4. Defines the new color
    // 4.1 Current color is the default one
    if (currentColor == ''
        || currentColor.toLowerCase() == theDefaultColor.toLowerCase()) {
        if (theAction == 'over' && thePointerColor != '') {
            newColor = thePointerColor;
        }
        else if (theAction == 'click' && theMarkColor != '') {
            newColor = theMarkColor;
        }
    }
    // 4.1.2 Current color is the pointer one
    else if (currentColor.toLowerCase() == thePointerColor.toLowerCase()) {
        if (theAction == 'out') {
            newColor = theDefaultColor;
        }
        else if (theAction == 'click' && theMarkColor != '') {
            newColor = theMarkColor;
        }
    }
    // 4.1.3 Current color is the marker one
    else if (currentColor.toLowerCase() == theMarkColor.toLowerCase()) {
        if (theAction == 'click') {
            newColor = (thePointerColor != '')
                     ? thePointerColor
                     : theDefaultColor;
        }
    } // end 4

    // 5. Sets the new color...
    if (newColor) {
        var c = null;
        // 5.1 ... with DOM compatible browsers except Opera
        if (domDetect) {
            for (c = 0; c < rowCellsCnt; c++) {
                theCells[c].setAttribute('bgcolor', newColor, 0);
            } // end for
        }
        // 5.2 ... with other browsers
        else {
            for (c = 0; c < rowCellsCnt; c++) {
                theCells[c].style.backgroundColor = newColor;
            }
        }
    } // end 5

    return true;
} // end of the 'setPointer()' function

function deleteConfirm()
{
	var returnValue;
	returnValue = confirm('Do you really want to delete this event?');
	return returnValue;
}

function deleteFileConfirm()
{
	var returnValue;
	returnValue = confirm('Do you really want to delete all files for this event?');
	return returnValue;
}

function deleteAllFileConfirm()
{
	var returnValue;
	returnValue = confirm('Do you really want to delete all files in the calendar?');
	return returnValue;
}

var winHandle = null;

function openPrint(v,m,d,y)
{
	
	var awidth = 700;
	var aheight = 600;
	var xPo = (screen.availWidth - awidth)/2;
	var yPo = (screen.availHeight - aheight)/2;
	var args = "width="+awidth+",height="+aheight+",scrollbars=yes,toolbar=yes,resizable=yes";
	var url = "print_calendar.php?view_mode="+v+"&m="+m+"&d="+d+"&y="+y;
	winHandle=window.open(url,"PrintableCalenar",args);
	winHandle.moveTo(xPo,yPo);

}

function openEventPrint(v,c)
{
	
	var awidth = 680;
	var aheight = 600;
	var xPo = (screen.availWidth - awidth)/2;
	var yPo = (screen.availHeight - aheight)/2;
	var args = "width="+awidth+",height="+aheight+",scrollbars=yes,toolbar=yes,resizable=yes";
	var url = "print_calendar.php?view_mode="+v+"&c="+c;
	winHandle=window.open(url,"PrintableCalenar",args);
	winHandle.moveTo(xPo,yPo);
}

function testPass(fm)
{

	if( ( fm.pass1.value == "" || fm.pass2.value == "" ) || 
		( fm.pass1.value != fm.pass2.value ) )
	{
		alert('Please enter valid password');
		return false;
	}
	return true;
}



function xbGetElementById(aID){ 
	var rv = (document.getElementById) ? document.getElementById(aID) : document.all[aID]; 
	return rv; 
} 

function showlogin(md) { 
	var myDiv = xbGetElementById(md); 
	myDiv.style.display = myDiv.style.display=='none'?'':'none'; 
} 
