//
// Copyright ByTheJob Inc. All Rights Reserved
// Unauthorized use or duplication of this content or program code is a
// violation of applicable laws and subject to prosecution thereunder.
//
//------------------------------------------------------------------------------------------
//	Library functions and subroutines.
//------------------------------------------------------------------------------------------

function isEmail(s) {
	if (s.value.indexOf("@") < 0 || s.value.indexOf(".",s.value.indexOf("@")+2) < 0 || s.value.indexOf(".")+1 == s.value.length) { 
		return false; 
	}
	return true;
}

function isConfirmation(s) {
	var numValue = new Number(s);
	if (numValue < 1101001 || numValue > 9909009) {
		return false;
	}
	return true;
}

//------------------------------------------------------------------------------------------
// Function Desc: set window status to null
//------------------------------------------------------------------------------------------

function winStatus() {
	self.defaultStatus = '';
}

//------------------------------------------------------------------------------------------
// Function Desc: change value to upper case
//------------------------------------------------------------------------------------------

function toUpper(field) {
	field.value = field.value.toUpperCase();
}

//------------------------------------------------------------------------------------------
// Function Desc: is string value blank
//------------------------------------------------------------------------------------------
function isBlank(s) {
 	for (var i = 0; i < s.value.length; i++) {
         	var c = s.value.charAt(i);
         	if ( (c != ' ') && (c != '\n') && (c != '\t') ) {
	          	return false;
         	}
      	}
	return true;
}  


//------------------------------------------------------------------------------------------
// Function Desc: is string value numeric
//------------------------------------------------------------------------------------------
function isNumeric(s) {
	for (var i = 0; i < s.value.length; i++) {
         	var c = s.value.charCodeAt(i);
	     	if ( c < 48  || c > 57 ) {
			return false;
	    	}
      	}
	return true;
}  

//------------------------------------------------------------------------------------------
// Function Desc: is string value a valid currency amount
//------------------------------------------------------------------------------------------
function isCurrency(s) {
	var checkStr  = s.value;				//input string
	var decPos    = checkStr.indexOf(".",decPos+1); 	//decimal position

	if (isNaN(checkStr)) {
		alert('Not a valid Cost. Enter a single decimal point only.');
		s.select();
		s.focus();
		return false; 
	}

	// check number of digits after decimal
	// only one digit for this value
	if (decPos == -1) {
	}
	else {
		var decPlaces = (checkStr.length - (decPos + 1));
		if (decPlaces > 2) {
			alert('Enter only two digits after decimal point.');
			s.select();
			s.focus();
			return false; 
		}
	}
	return true;
}

//------------------------------------------------------------------------------------------
// Function Desc: left trim string value
//------------------------------------------------------------------------------------------
function StringLTrim(s) {
	var i, l = s.value.length;
	for (i=0; (s.value.charAt(i) == ' ') && (i < l); ++i) {
		if (i == l) {
			return '';
		}
	}
	return s.value.substring(i, l);
}

//------------------------------------------------------------------------------------------
// Function Desc: right trim string value
//------------------------------------------------------------------------------------------
function StringRTrim(s) {
	var i;
	for (i=s.value.length-1; (s.value.charAt(i) == ' ') && (i > -1); --i) {
		if (i == -1) {
			return '';
		}
	}
	return s.value.substring(0, i+1);
}

//------------------------------------------------------------------------------------------
// Function Desc: replace string value
//------------------------------------------------------------------------------------------
function StringReplace(src, marker, embed) {
	var i, mark;
	var len = src.length;
	var out = '';
	mark = 0;
	while (mark < len) {
		i = src.indexOf(marker, mark);
		if (i == -1) {
			break;
		}
		out = out + src.substring(mark, i) + embed;
		mark = i + marker.length;
	}
	if (mark < len) {
		out = out + src.substring(mark, len);
	}
	return(out);
}

function LocalizeReplace(src, idx, embed) {
	return StringReplace(src, '%' + idx + '%', embed);
}

//------------------------------------------------------------------------------------------
// Function Desc: add leading zeros to string value
//------------------------------------------------------------------------------------------
function ZeroPadL(str, spaces) {
	var s = str + '', l;
	s = s.value.substring(0, spaces);
	l = s.value.length;
	if (spaces > l) {
		for (var i = 0; i < spaces - l; i++) {
			s = '0' + s;
		}
	}
	return s;
}

//------------------------------------------------------------------------------------------
// Function Desc: validate date format
//------------------------------------------------------------------------------------------
// global message variable for proper date formatting

var dateErrMsg = 'Invalid Date Format\n\nEnter date as: mm/dd/yyyy.';

function isDate(s) {
	// check characters
	var checkOK = "/0123456789";		//valid values
	var allValid = true;

	for (i = 0;  i < s.value.length;  i++) {
		ch = s.value.charAt(i);
		for (j = 0;  j < checkOK.length;  j++) {
			if (ch == checkOK.charAt(j)) {
				break;
			}
		}
		if (j == checkOK.length) {
			allValid = false;
       			break;
		}
	}

	if (!allValid)	{
		alert(dateErrMsg);
		s.select();
		s.focus();
		return (false);
	}

	// check input of "/"
	if (s.value.indexOf("/") != 2 || s.value.indexOf("/", 3) != 5
	  || s.value.indexOf("/", 6) == 6 || s.value.indexOf("/", 7) == 7
	  || s.value.indexOf("/", 8) == 8 || s.value.indexOf("/", 9) == 9
	  || s.value.length != 10) {
		alert(dateErrMsg);
		s.select();
		s.focus();
		return (false);
	}
	
	// check month, day, and leap year
	var ckmonth = s.value.substr(0, 2);
	ckmonth = parseInt(ckmonth,10);
	var ckday = s.value.substr(3, 2);
	var ckyear = s.value.substr(6, 4);

	if (ckmonth < 1 || ckmonth > 12) {
		alert('Month must be entered between 01(January) and 12(December) ');
		s.select();
		s.focus();
		return (false);
	}
	if (ckmonth == 2) {			
		if (!checkLeapMonth(ckmonth,ckday,ckyear)) {
			return (false);
		}			
	}
	if (!checkMonthLength(ckmonth,ckday)) {
		return (false);
	}

	return (true);
}  

//------------------------------------------------------------------------------------------
// Function Desc: validate date month
//------------------------------------------------------------------------------------------
function checkMonthLength(mm,dd) {
	var months = new Array("","January","February","March","April","May","June","July","August","September","October","November","December")
	parseInt(mm,10)
	if ((mm == 4 || mm == 6 || mm == 9 || mm == 11) && dd > 30) {
		alert(months[mm] + " has only 30 days.");
		s.select();
		s.focus();
		return false;
	}
	else if (dd > 31) {
		alert(months[mm] + " has only 31 days.");
		s.select();
		s.focus();
		return false;
	}
	return true;
}

//------------------------------------------------------------------------------------------
// Function Desc: validate date leap year
//------------------------------------------------------------------------------------------
function checkLeapMonth(mm,dd,yyyy) {
	if (yyyy % 4 > 0 && dd > 28) {
		alert("February of " + yyyy + " has only 28 days.");
		s.select();
		s.focus();
		return false;
	} 
	else if (dd > 29) {
		alert("February of " + yyyy + " has only 29 days.");
		s.select();
		s.focus();
		return false;
	}
return true;
}

function adjustTable() {
	var winW = document.getElementById("pbody").clientWidth - 130;
	var winH = document.getElementById("pbody").clientHeight - 65;
//        var winW = document.body.offsetWidth - 160;
//        var winH = document.body.offsetHeight - 85;
//        if(document.body.offsetWidth < 820) {
//                tabW = 120;
//        }
        document.write('<div style="overflow: scroll; height: ' + winH + 'px; width: ' + winW + 'px;" >');
//        document.write('<div style="overflow: scroll; height: ' + winH + 'px; width: ' + winW + 'px;" >');
}

function viewCal(fname) {
	setDateField(fname);
	top.newWin = window.open('include/calendar.html','cal','dependent=yes,width=210,height=230,screenX=200,screenY=300,titlebar=yes');
}

function getNoSecurePopUp(title,program,fnames) {
	var url = "popUpNoSecure.php?title=" + title + "&program=" + program + "&fnames=" + fnames;
	window.open(url, "popvals", "toolbar=yes,location=yes,directories no,status=no,menubar=yes,scrollbars=yes,resizable=yes,width=300,height=600").focus();
}

function getPopUp(title,program,fnames) {
	var url = "popUpWindow.php?title=" + title + "&program=" + program + "&fnames=" + fnames;
	window.open(url, "popvals", "toolbar=no,location=no,directories no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=700,height=400").focus();
}

function getPopUpWithHelp(title,program,fnames,help) {
	var url = "popUpWindow.php?title=" + title + "&program=" + program + "&fnames=" + fnames + "&help=" + help;
	window.open(url, "popvals", "toolbar=no,location=no,directories no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=700,height=400").focus();
}

function getHelpWindow(program) {
	window.open(program, "helpwin", "toolbar=yes,location=no,directories no,status=no,menubar=yes,scrollbars=yes,resizable=yes,width=700,height=400").focus();
}

function getPrintable(program) {
	window.open(program, "popvals", "toolbar=yes,location=no,directories no,status=no,menubar=yes,scrollbars=yes,resizable=yes,width=700,height=400").focus();
}
function zoomPic(pname) {
	var url = "popPic.php?pname=" + pname;
	window.open(url, "zoompic", "toolbar=no,location=no,directories no,status=no,menubar=yes,scrollbars=yes,resizable=yes,width=700,height=300").focus();
}

