// JavaScript Document

// fDateFiff is equivalent to the DateDiff function of VBScript
function fDateDiff( start, end, interval, rounding ) {

    var iOut = 0;
    
    // Create 2 error messages, 1 for each argument. 
    var startMsg = "Check the Start Date and End Date\n"
        startMsg += "must be a valid date format.\n\n"
        startMsg += "Please try again." ;
		
    var intervalMsg = "Sorry the dateDiff function only accepts\n"
        intervalMsg += "d, h, m OR s intervals.\n\n"
        intervalMsg += "Please try again." ;

    var bufferA = Date.parse( start ) ;
    var bufferB = Date.parse( end ) ;
    	
    // check that the start parameter is a valid Date. 
    if ( isNaN (bufferA) || isNaN (bufferB) ) {
        //alert( startMsg ) ;
        return null ;
    }
	
    // check that an interval parameter was not numeric. 
    if ( interval.charAt == 'undefined' ) {
        // the user specified an incorrect interval, handle the error. 
        //alert( intervalMsg ) ;
        return null ;
    }
    
    var number = bufferB-bufferA ;
    
    // what kind of add to do? 
    switch (interval.charAt(0))
    {
        case 'd': case 'D': 
            iOut = parseInt(number / 86400000) ;
            if(rounding) iOut += parseInt((number % 86400000)/43200001) ;
            break ;
        case 'h': case 'H':
            iOut = parseInt(number / 3600000 ) ;
            if(rounding) iOut += parseInt((number % 3600000)/1800001) ;
            break ;
        case 'm': case 'M':
            iOut = parseInt(number / 60000 ) ;
            if(rounding) iOut += parseInt((number % 60000)/30001) ;
            break ;
        case 's': case 'S':
            iOut = parseInt(number / 1000 ) ;
            if(rounding) iOut += parseInt((number % 1000)/501) ;
            break ;
        default:
        // If we get to here then the interval parameter
        // didn't meet the d,h,m,s criteria.  Handle
        // the error. 		
        //alert(intervalMsg) ;
        return null ;
    }
    
    return iOut ;
}

// Deletes 'intNoChars' characters from 'strOrig' starting from 'intPos' and replaces them with 'strReplace'. 
// The function works even if 'strReplace.length' <> intNoChars. 
function replaceChars(strOrig, intPos, intNoChars, strReplace) {
	if (intPos < 0) intPos = 0;
	if (intPos >= strOrig.length) intPos = strOrig.length - 1;
	if (intNoChars < 0) intNoChars = 0;
	if (intNoChars > strOrig.length) intNoChars = strOrig.length;
	return (strOrig.substring(0, intPos) + strReplace + strOrig.substring(intPos + intNoChars));
}
	
// Replaces every instance of substring 'strFind' in 'strOrig' with 'strReplace'.
function replaceAll(strOrig, strFind, strReplace) {
	var intCount = strOrig.indexOf(strFind);
	while (intCount != -1)	{
		strOrig = replaceChars(strOrig, intCount, strFind.length, strReplace);
		intCount = strOrig.indexOf(strFind);
	}
	return strOrig;
}

//this function checks if there are any characters in 'strCheck', that are not in 'strValid'
function IsValidChars(strCheck, strValid) {
	var strChar = "";
	
	for (var intCount = 0; intCount < strCheck.length; intCount++) {
		strChar = strCheck.charAt(intCount);
		if (strValid.indexOf(strChar) == -1) 
			return false;
	}

	return true;
}
	
// Check to see if a form text field contains a valid email address
function IsValidEmailAddress(objTextField) {
	var intCount = "";
	var strCheck = "";

	//split part 1 and 2 into separate variables
	var arrEmail = objTextField.value.split('@');
	
	//check that there are at least 2 parts to the email
	if (arrEmail.length < 2)
		return false;
	
	var strPart2 = arrEmail[arrEmail.length-1];
	var strPart1 = replaceAll(objTextField.value,'@' + strPart2,'');
	
	//split the periods from each part into arrays
	var arrPart1 = strPart1.split('.');
	var arrPart2 = strPart2.split('.');
	
	// Check that Part 2 has at least 2 parts (this is not required for a valid email address, BUT 
	// it is required to test for a FQDN (Fully qualified domain name))
	if (arrPart2.length < 2) 
		return false;
	
	//check for validity in part 1 array
	for (intCount = 0; intCount < arrPart1.length; intCount++) {
		strCheck = arrPart1[intCount];

		//check for empty string
		if (strCheck.length <= 0) 
			return false;
		
		// Check for Valid Characters
		if (!IsValidChars(strCheck,'abcdefghijklmnopqrstuvwxyz0123456789@._-'))
			return false;
	}

	//check for validity in part 2 array
	for (intCount = 0; intCount < arrPart2.length; intCount++) {
		strCheck = arrPart2[intCount];

		//check for empty string
		if (strCheck.length <= 0) 
			return false;
		
		// Check for Valid Characters
		if (!IsValidChars(strCheck,'abcdefghijklmnopqrstuvwxyz0123456789._-'))
			return false;
	}
	
	// If all was ok, return true
	return true;
}

// Check to see if a form text field is empty
function IsTextFieldEmpty(objTextField) {
	if (objTextField.value.length > 0)
		return false
	else
		return true;		
}