// preload the conrner images used
image1 = new Image();
image1.src = "images/img_topleft.gif";
image2 = new Image();
image2.src = "images/img_topright.gif";
image3 = new Image();
image3.src = "images/img_bottomright.gif";
image4 = new Image();
image4.src = "images/img_bottomleft.gif";


// validates emails
function isValidEmail(emailAddress) {					
	// declare variables
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9])+$/;
						
	// the email was validated
	if (filter.test(emailAddress)){
		return true;
	}
	// the email was invalid
	else {
		// alert the user
		return false;

	}
				
}

// validates filenames
function isValidFilename(filename) {					
	// declare variables
	var filter  = /^[^\\\/\:\*\?\"\<\>\|\.]+(\.[^\\\/\:\*\?\"\<\>\|\.]+)+$/;
						
	// the filename was validated
	if (filter.test(filename)){
		return true;
	}
	// the filename was invalid
	else {
		// alert the user
		return false;
	}				
}

// verifies field value
function isWhitespace (s){   	
	// whitespace characters
	var whitespace = ' \t\n\r';

	var i;
	// Search through string's characters one by one
	// until we find a non-whitespace character.
	// When we do, return false; if we don't, return true.

	for (i = 0; i < s.length; i++)
	{
		// Check that current character isn't whitespace.
		var c = s.charAt(i);

		if (whitespace.indexOf(c) == -1) return false;
	}

	// All characters are whitespace.
	return true;
}	

// function used to verify phone numbers
function verifyPhone(source_fld) {
	// convert phone alphas to corresponding numbers
	re = /[abc]/gi;
	source_fld.value = source_fld.value.replace(re, "2");
	re = /[def]/gi;
	source_fld.value = source_fld.value.replace(re, "3");
	re = /[ghi]/gi;
	source_fld.value = source_fld.value.replace(re, "4");
	re = /[jkl]/gi;
	source_fld.value = source_fld.value.replace(re, "5");
	re = /[mno]/gi;
	source_fld.value = source_fld.value.replace(re, "6");
	re = /[prs]/gi;
	source_fld.value = source_fld.value.replace(re, "7");
	re = /[tuv]/gi;
	source_fld.value = source_fld.value.replace(re, "8");
	re = /[wxy]/gi;
	source_fld.value = source_fld.value.replace(re, "9");
	// get rid of other non-numerics
	re = /[^0-9]/gi;
	source_fld.value = source_fld.value.replace(re, "");
	// only verify if present
	if (!source_fld.value) {
		return true;
	}
	// append area code if required
	if (source_fld.value.length == 7) {
		source_fld.value = "908" + source_fld.value;
	}
	// drop the long distance 1
	if (source_fld.value.length == 11 && source_fld.value.substring(0,1) == "1") {
		source_fld.value = source_fld.value.substring(1,12);
	}
	// if not a full number, alert and set focus
	if (source_fld.value.length != 10) {
		alert("Improper phone number format, please correct.");
		source_fld.focus();
	}
	else {
		source_fld.value = source_fld.value.substring(0,3) + "-" + source_fld.value.substring(3,6) + "-" + source_fld.value.substring(6,10);
	}
}

// function used to verify a number value
function verifyNumber(source_fld, dec_right, val_range){
	
	// check for char data
	if (source_fld.value.length >= 1 && isNaN(source_fld.value)){
		alert("Improper number format, please correct.");
		source_fld.focus();	
	}
				
	// check for format
	if (source_fld.value.indexOf('.') != -1) {
		if (source_fld.value.length - (source_fld.value.indexOf('.') + 1) > dec_right) {
			alert("Improper number format, please correct.");
			source_fld.focus();		
		}
	}

	// check for range
	if (val_range != ''){
		if (val_range == '+' && parseFloat(source_fld.value) < 0){
			alert("A number greater than 0 is required, please correct.");
			source_fld.focus();		
		} 

		if (val_range == '-' && parseFloat(source_fld.value) > 0){
			alert("A number less than 0 is required, please correct.");
			source_fld.focus();		
		} 
	}
}

	var dtCh= "/";
	var minYear=2000;
	var maxYear=2015;
	
// function used to verify a date
function verifyDate(source_fld){
	
	// if the field is blank, ignore  onblur
	if (isWhitespace(source_fld.value)) {
		return true;
	}
	
	var daysInMonth = DaysArray(12)
	var pos1=source_fld.value.indexOf(dtCh)
	var pos2=source_fld.value.indexOf(dtCh,pos1+1)
	var strMonth=source_fld.value.substring(0,pos1)
	var strDay=source_fld.value.substring(pos1+1,pos2)
	var strYear=source_fld.value.substring(pos2+1)
	var currdate = new Date();
	var targetdate = new Date(source_fld.value);
	var now = new Date(currdate.getFullYear(), currdate.getMonth(), currdate.getDate())

	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,10)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("The date format should be : mm/dd/yyyy")
		source_fld.value="";
		source_fld.focus();
		return false
	}
	if (month<1 || month>12){
		alert("Please enter a valid month.")
		source_fld.value="";
		source_fld.focus();
		return false
	}
	if (day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day.")
		source_fld.value="";
		source_fld.focus();
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear +".")
		source_fld.value="";
		source_fld.focus();
		return false
	}
	if (source_fld.value.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(source_fld.value, dtCh))==false){
		alert("Please enter a valid date.")
		source_fld.value="";
		source_fld.focus();
		return false
	}
	if ( targetdate < now)
	{
		alert("Please enter a date greater than or equal to today.")
		source_fld.value="";
		source_fld.focus();
		return false
	}
	
	return true
}

// function called by the verify date function
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 called by the verify date function
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 called by the verify date function
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 called by the verify date function
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 used to limit the contents of a field
function textLimit(infield,limit) {
   var result = true;
   if (infield.value.length >= limit)
      result = false;
   if (window.event)
      window.event.returnValue = result;
   return result;
}

// function used to trim the contents of a field
function textTrim(infield,limit) {
   //re = /\r\n/gi;
   //infield.value = infield.value.replace(re," ");
   infield.value = infield.value.substring(0,limit);
}

// this function updates the preview field to show the color
function updatePreview(formName, fieldName, fieldPreviewName){
	document.getElementById(fieldPreviewName).style.backgroundColor = document.getElementById(fieldName).value;
	//document.forms(formName).item(fieldPreviewName).style.backgroundColor = document.forms[formName].elements[fieldName].value;
}

// function used to open the date selector window			
function openDateSelect(formName, fieldName){
	// open the date selector window
	window.open('/dateselect.aspx?form=' + formName + '&cntrl=' + fieldName + '&initd1=' + document.forms[formName].elements[fieldName].value, 'DateSelect', 'status=yes,toolbar=no,menubar=no,location=no,scrollbars=no,resizable=no,width=260,height=290,top=' + document.forms[formName].elements[fieldName].offsetTop);
}

// function used to open the color selector window			
function openColorSelect(formName, fieldName){
	// open the selector window
	window.open('/colorselect.aspx?form=' + formName + '&cntrl=' + fieldName, 'ColorSelect', 'status=yes,toolbar=no,menubar=no,location=no,scrollbars=no,resizable=no,width=370,height=225,top=' + document.forms[formName].elements[fieldName].offsetTop);
}

// function used to open the file selector window			
function openFileSelect(fileType, formName, fieldName){
//fileselect
	// open the selector window
	window.open('link_manager.php?reqtype=' + fileType + '&form=' + formName + '&cntrl=' + fieldName, 'FileSelect', 'status=yes,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes,width=650,height=425');
}

function openFileSelect2(fileType, functionName){
	// open the selector window
	window.open('link_manager.php?reqtype=' + fileType + '&function=' + functionName, 'FileSelect', 'status=yes,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes,width=650,height=425');	
}

// function used to open the link selector window			
function openLinkSelect(formName, fieldName){
	// open the selector window
	window.open('/linkselect.php?form=' + formName + '&cntrl=' + fieldName, 'LinkSelect', 'status=yes,toolbar=no,menubar=no,location=no,scrollbars=no,resizable=no,width=300,height=250');
}