/**
 * Test the string is empty or not
 */
function isEmpty(strText) {
	var re = /^\s{1,}$/g; //match any white space including space, tab, form-feed, etc. 
	if ((strText.length==0) || (strText=="") || 
		((strText.search(re)) > -1)) {
		return true;
	}
	else {
		return false;
	}
}

/**
 * Test whether the string all numeric. No negatvie number
 */
function isAllNumeric(sTestText)
{
	var regexp = /^(\d+)$/;	
	return regexp.test(sTestText);
}

/**
 * Test whether the string is numeric. Negative is allowed here
 */
function isNumeric(sTestText)
{
	var regexp = /^([-]?\d+)$/;	
	return regexp.test(sTestText);
}

/**
 * Test whether the string is numeric and greater than zero
 */
function isGreaterThanZero(sTestText){
	if(isEmpty(sTestText))
		return false;
	if(isNumeric(sTestText) && parseInt(sTestText) > 0)
		return true;
	return false;
}

/**
 * Test whether the string matches these numeric format:
 * 123.12 or -123.12
 */
function isFloat(sTestText)
{
	var regexp = /^([-]?\d+)(\.\d+)?$/;	
	return regexp.test(sTestText);
}

function isFloatRange(sTestText){
	var regexp = /^([-]?\d+)(\.\d+)?\/([-]?\d+)(\.\d+)?$/;	
	return regexp.test(sTestText);
}

/**
 * Test whether the string is the date format
 */
function isDate(sTestText)
{
	/*
	 * RULES:
	 *		MONTHS: 
	 *			Case 1: 0 followed by 1 to 9					0[1-9]
	 *			Case 2: 1 followed by 0,1,2					1[012]
	 *			Case 3: 1 to 9										[1-9]
	 *		DAYS: 
	 *			Case 1: 1 to 9										[1-9]
	 *			Case 1: 0 followed by 1 to 9					0[1-9]
	 *			Case 2: 1 or 2 followed by any digits		[12]\d
	 *			Case 3: 3 followed by 0 or 1					3[01]
	 *		YEAR:		Contain 4 digits							\d{4}
	 */	
	 
	 //mm/dd/yyyy or mm/dd/yy		mm-dd-yyyy or mm-dd-yy	
	 var dateReg1 = /^([1-9]|0[1-9]|1[012])(\/|-)([1-9]|0[1-9]|[12]\d|3[01])(\/|-)(\d{4}|\d{2})$/;
	 
	 //dd/mm/yyyy or dd/mm/yy		dd-mm-yyyy or dd-mm-yy
	 //var dateReg2 = /^([1-9]|0[1-9]|[12]\d|3[01])(\/|-)([1-9]|0[1-9]|1[012])(\/|-)(\d{4}|\d{2})$/;
	 
	 if(dateReg1.test(sTestText))
	 	return true;
	 //else if(dateReg2.test(sTestText))
	 //	return true;
	 else
	 	return false;
}

/**
 * Test whether the string is in the data format range
 */ 
function isDateRange(sTestText)
{
	/*
	 * RULES:
	 *		MONTHS: 
	 *			Case 1: 0 followed by 1 to 9					0[1-9]
	 *			Case 2: 1 followed by 0,1,2					1[012]
	 *			Case 3: 1 to 9										[1-9]
	 *		DAYS: 
	 *			Case 1: 1 to 9										[1-9]
	 *			Case 1: 0 followed by 1 to 9					0[1-9]
	 *			Case 2: 1 or 2 followed by any digits		[12]\d
	 *			Case 3: 3 followed by 0 or 1					3[01]
	 *		YEAR:		Contain 4 digits							\d{4}
	 */	
	 
	 //mm/dd/yyyy or mm/dd/yy		mm-dd-yyyy or mm-dd-yy	
	 var dateReg1 = /^([1-9]|0[1-9]|1[012])(\/|-)([1-9]|0[1-9]|[12]\d|3[01])(\/|-)(\d{4}|\d{2})@([1-9]|0[1-9]|1[012])(\/|-)([1-9]|0[1-9]|[12]\d|3[01])(\/|-)(\d{4}|\d{2})$/;
	 
	 //dd/mm/yyyy or dd/mm/yy		dd-mm-yyyy or dd-mm-yy
	 //var dateReg2 = /^([1-9]|0[1-9]|[12]\d|3[01])(\/|-)([1-9]|0[1-9]|1[012])(\/|-)(\d{4}|\d{2})$/;
	 
	 if(dateReg1.test(sTestText))
	 	return true;
	 //else if(dateReg2.test(sTestText))
	 //	return true;
	 else
	 	return false;
}

/**
 * Test whether the string is the numeric range
 * 123-456 or -123-456 or -123--456
 */
function isNumericRange(sTestText)
{
	var regexp = /^([-]?\d+)-([-]?\d+)$/;	
	return regexp.test(sTestText);
}

/**
 * Test whether the string is an email format
 */
function isEmail(sTestText)
{
	/* RULE:
    * One or more character(s) before @ sign   
    * There may be either a . or a -, but not together before the @ sign
    * Have @ and one or more character(s) followed
    * There may be either a . or a -, but not together in the address
    * The address must end with a . followed by at least 2 characters
    */
   
   var regExp = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,})+$/;
	return regExp.test(sTestText);        	
}

/**
 * Test whether the string is an SSN format
 */
function isSSN(sTestText)
{
	/*
	 *	Define a RegExp object which checks for either a 9 digit input or an input in the form
	 * xxx-xx-xxxx or 123456789
	 */
	var regexp = "/^(\d{9}|\d{3}[-./\|]?\d{2}[-./\|]?\d{4})$/";
	return regexp.test(sTestText);	
}

/**
 * Show the object
 */
function doExpand(sObjName)
{
	var oObject = document.getElementById(sObjName);
	oObject.style.display='inline'; 
}

/**
 * Hide the object
 */
function doUnExpand(sObjName)
{
	var oObject = document.getElementById(sObjName);
	oObject.style.display='none';					
}

/**
 * Auto hide and show the object. If the current stage of the
 * object is show, then hide it; otherwise, show it.
 */
function autoExpand(sObjName){
	var oObject = document.getElementById(sObjName);
	if(oObject.style.display=='none')
		oObject.style.display='inline';
	else
		oObject.style.display='none';
}

/**
 * Display the Progress message
 */
function displayProgressMsg(sObjName, sText)
{
	var oObject = document.getElementById(sObjName);
	oObject.innerHTML = sText;
	//oObject.style.display='inline' 	
}

/**
 * Show the object with option text
 */
function displayObject(sObjName, sText)
{
	var oObject = document.getElementById(sObjName);
	oObject.innerHTML = sText;
}

/**
 * Focus the object
 */
function onFocusMenu(oObject)
{
	oObject.style.backgroundColor='#F5F5FF';
	oObject.style.color='#2D5986';
	oObject.style.cursor='hand';
}
function onBlurMenu(oObject)
{
	oObject.style.backgroundColor='#93B0C6';
	oObject.style.color='white';
	oObject.style.cursor='auto';
}

/**
 * Focus the object
 */
function onFocusColor(oObject)
{
	//oObject.style.backgroundColor='lemonchiffon';
	oObject.style.backgroundColor='#FFFFF9';
}
	
/**
 * Blur the object
 */
function onBlurColor(oObject)
{
	//oObject.style.backgroundColor='white';
	oObject.style.backgroundColor='#F3F3FF';
}

/**
 * Focus/hi-lite the cell
 */
function onFocusCell(oObject)
{
	oObject.style.backgroundColor='lemonchiffon';
}
	
/**
 * Blur/unhi-lite the cell
 */
function onBlurCell(oObject)
{
	oObject.style.backgroundColor='white';
}

/**
 * Set the cell to gray color
 */
function onBlurCell_grey(oObject)
{
	oObject.style.backgroundColor='#F5F5F5';
}

function changeColorIn3(oObject)
{
	oObject.style.color='#CC3300'
	
}

function changeColorOut3(oObject)
{
	oObject.style.color='white'
}

//pop up with everything
function openNewWin(newURL, width, height) {
  document.open(newURL, "NewName", "location=no, menubar=no, dependent=yes, directories=no, status=no, titlebar=yes, toolbar=no, scrollbars=no, width=" + width + ", height=" + height + ", resizable=no");
}

//pop up with resize+scroll bar
function openResizeScroll(newURL, width, height, newName, orgName) {
  var remote = open(newURL, newName, "location=no, menubar=no, dependent=no, directories=no, status=no, titlebar=yes, toolbar=no, scrollbars=yes, width=" + width + ", height=" + height + ", resizable=yes" );
  if (remote.opener == null)
    remote.opener = window;
  remote.opener.name = orgName;  
}

//pop up with everything
function openAnotherWin(newURL, width, height, newName, orgName) {
  var remote = open(newURL, newName, "location=yes, menubar=yes, dependent=yes, directories=no, status=yes, titlebar=yes, toolbar=yes, scrollbars=yes, width=" + width + ", height=" + height + ", resizable=yes" );
  if (remote.opener == null)
    remote.opener = window;
  remote.opener.name = orgName;  
  //return remote;
}

//pop up with everything
function openWinMenu(newURL, width, height, newName, orgName) {
  var remote = open(newURL, newName, "location=no, menubar=yes, dependent=yes, directories=no, status=no, titlebar=yes, toolbar=no, scrollbars=yes, width=" + width + ", height=" + height + ", resizable=yes" );
  if (remote.opener == null)
    remote.opener = window;
  remote.opener.name = orgName;  
  //return remote;
}

//pop up with everything
function openDepWin(newURL, width, height) {  
	 window.showModalDialog(newURL, self ,"dialogWidth:" + width + "px; dialogHeight:" + height + "px; dialogTop=10px; dialogLeft=10px; status=no; titlebar=no; scroll=yes");
}

function openSimpleDialog(newURL, width, height) {
	 window.showModalDialog(newURL, "" ,"dialogWidth:" + width + "px; dialogHeight:" + height + "px; dialogTop:10px; dialogLeft:10px; status:no; scroll:no");
}

function createPopup(newURL, width, height, newName, orgName) {
  var remote = open(newURL, newName, "location=no, menubar=no, dependent=yes, modal=yes, directories=no, status=no, titlebar=yes, toolbar=no, scrollbars=no, width=" + width + ", height=" + height + ", resizable=no");
  if (remote.opener == null)
    remote.opener = window;
  remote.opener.name = orgName;
  
  //return remote;
}

function createPopupScroll(newURL, width, height, newName, orgName) {
  var remote = open(newURL, newName, "location=no, menubar=no, dependent=yes, directories=no, status=no, titlebar=Yes, toolbar=no, scrollbars=yes, width=" + width + ", height=" + height + ", resizable=no");
  if (remote.opener == null)
    remote.opener = window;
  remote.opener.name = orgName;
  
  //return remote;
}

function showCalendarDialog(pContextPath, pFormName, pFieldName) {
	var sLink = pContextPath + '/CalendarPopup.do?SCREEN=CALENDAR&FORMNAME=' + pFormName + '&FIELDNAME=' + pFieldName;
	window.showModalDialog(sLink, self ,"dialogWidth:150px; dialogHeight:195px; top:10px; left:10px; status:no; titlebar:no; scroll:no; help:no");
	//alert(sLink);
	//createPopupScroll(sLink, 500, 400, '1', 'b');
}

/*
 * Function get Radio Value
 * Parameter: radioComponent
 */
function getRadioValue(oRadioComp)
{	
    for (x = 0; x < oRadioComp.length; x++)
    {    
    	//alert("dd: "+oRadioComp[x].checked);
       if (oRadioComp[x].checked == true) return oRadioComp[x].value;                      
    } 
    // if it didn't find anything, return the .value  (behaviour of single radio btn)
    return "false";
}

function getSelectValue(oSelectComp){
	//alert("oSelectComp.selectedIndex="+oSelectComp.selectedIndex);
	return oSelectComp.options[oSelectComp.selectedIndex].value;
}

function getSelectText(oSelectComp){
	//alert("oSelectComp.selectedIndex="+oSelectComp.selectedIndex);
	return oSelectComp.options[oSelectComp.selectedIndex].text;
}

function getIsCheckboxChecked(oSelectComp){	
	if(oSelectComp.checked){ 
		return true; //checkbox has only one option
	}else{
		for (counter = 0; counter < oSelectComp.length; counter++)
		{	
			if(oSelectComp[counter].checked)
				return true;
		}	
	}	
	return false;
}

function getCheckboxValueByComma(oSelectComp){	
	var idVal = "";
	if(oSelectComp.checked){ 
		return oSelectComp.value; //checkbox has only one option
	}else{
		
		for (counter = 0; counter < oSelectComp.length; counter++)
		{	
			if(oSelectComp[counter].checked){				
				if(idVal != "")
					idVal += ",";
				idVal += oSelectComp[counter].value;
			}
		}	
		return idVal;
	}	
}

function checkAllBox(pFormName, pObjName){
	var formObj = eval("document."+pFormName+"."+pObjName);
	checkBoxCheckAll(formObj, true);
}

function unCheckAllBox(pFormName, pObjName){
	var formObj = eval("document."+pFormName+"."+pObjName);
	checkBoxCheckAll(formObj, false);
}

function checkBoxCheckAll(oSelectComp, pChecked){
	if(oSelectComp.length==undefined){		
		oSelectComp.checked = pChecked; //checkox has only one option
	}
	else{
		for (counter = 0; counter < oSelectComp.length; counter++)
		{	
			oSelectComp[counter].checked = pChecked;
		}	
	}
}

function msgLimit(sFormName, sFieldName) {
	var obj = eval("document."+sFormName+"."+sFieldName);
	//var obj=form1.Comments;
	if (obj.value.length==obj.maxLength*1) 
	{
		alert ("Only " + obj.maxLength + " characters are allowed!");
		obj.value = obj.value.substr(0, 250);
		return false;
	}
}

function msgCount(sFormName, sFieldName, msgCounterTxt) { 
	var oMsgCounter = document.getElementById(msgCounterTxt);
	
	if(oMsgCounter != null || oMsgCounter!="undefined")
	{
		var obj = eval("document."+sFormName+"."+sFieldName);		
		if (obj.value.length>obj.maxLength*1) obj.value=obj.value.substring(0,obj.maxLength*1);	
		oMsgCounter.innerText=obj.maxLength-obj.value.length;		
	}
	
}

function autoExpand(sObjName)
{
	var oObject = document.getElementById(sObjName);
	if(oObject.style.display == 'inline')	
		oObject.style.display='none'; 
	else
		oObject.style.display='inline' 
}

function loadLink(strLink){
	self.location = strLink;
}

function swapImage(imageObjName, newSrc){
	var oObject = document.getElementById(imageObjName);
	oObject.src = newSrc;
}

function goLastMonth(month,year,form,field) { 
	// If the month is January, decrement the year. 
  if(month == 1) { 
  	--year;   
  	month = 13; 
	}        
  document.location.href = 'schedule.php?month='+(month-1)+'&year='+year; 
} 
  
function goNextMonth(month,year,form,field) { 
	// If the month is December, increment the year. 
  if(month == 12) { 
		++year; 
  	month = 0; 
	}    
  document.location.href = 'schedule.php?month='+(month+1)+'&year='+year; 
} 

function changeScheduleTime(paramLink, pTimeVal){		
		var sLink = paramLink + pTimeVal;
		//openAnotherWin(sLink, '400', '350', 'a', 'b');
		window.showModalDialog(sLink, self ,"dialogWidth:350px; dialogHeight:200px; top:10px; left:10px; status:no; titlebar:no; scroll:no; help:no");
}

function jsValidateAppForm(oFormObj){
	
	if(isEmpty(oFormObj.name.value)){
		alert("Name is a required field");
		oFormObj.name.focus();
		return false;
	}
	
	if(isEmpty(oFormObj.nameLast.value)){
		alert("Last name is a required field");
		oFormObj.nameLast.focus();
		return false;
	}
	
	if(isEmpty(oFormObj.phone.value)){
		alert("Phone # is a required field");
		oFormObj.phone.focus();
		return false;
	}
	
	if(isEmpty(oFormObj.email.value)){
		alert("Email is a required field");
		oFormObj.email.focus();
		return false;
	}
	
	if(!isEmail(oFormObj.email.value)){
		alert("Invalid email address");
		oFormObj.email.focus();
		return false;
	}
	
	if(isEmpty(oFormObj.occupation.value)){
		alert("Occupation is a required field");
		oFormObj.occupation.focus();
		return false;
	}
	
	if(getSelectValue(oFormObj.apptLocation) == ""){
		alert("Appointment location is a required field");		
		return false;
	}
	
	return confirm('Are you sure you want to submit?');
}
