/*
 File Name:		   CommonFunctions.asp
 Description:		This page contains functions used to generate date drop down
					   fields.
 Programmer:		Brulant Code Library
*/

// ********************************************************************************
// Function:         isValidPhoto()
// Description:      This function determines if the file type is OK to upload
// ********************************************************************************
function isValidPhoto(szFilename)
{  var szValidExt = 'gif, jpg, jpeg, png';
   var iVal, szFileExt;
   
	iVal = szFilename.lastIndexOf(".")+1;
	szFileExt = szFilename.slice(iVal);
   szFileExt = szFileExt.toLowerCase();

   iVal = szValidExt.indexOf(szFileExt);
   if (iVal == -1)
   {  return false;
   }
   else
   {  return true;
   }
}

// ********************************************************************************
// Function:         textCounter()
// Description:      This function cuts the entered text off at the maxlimit
// ********************************************************************************
function textCounter(field, maxlimit)
{  if (field.value.length > maxlimit) 
   {  // if too long...trim it!
      field.value = field.value.substring(0, maxlimit);
   }
}

/* If a form has a required fields with a name starting with RQD - this
function evaluates to see if the fields are empty */
function RQDFieldCheck(theForm) 
{
	var pass=true;
	var currObj, strError, tmpString, selIndex, selValue;
	strMsg = "The Following Fields are Required:\n";
	if (document.images) 
	{
		for (i = 0; i < theForm.length; i++) 
		{
			currObj=theForm.elements[i];
			fieldName = currObj.name.substring(3,30).toUpperCase();			
			if (currObj.name.substring(0,3)=="RQD") 
			{
				if ( (currObj.type=="text") || (currObj.type=="textarea") || (currObj.type=="password") )
				{
					if ( isTexEmpty(currObj) )
					{
						strMsg += "  - " + fieldName + "\n";
						pass = false;
					}
				} 						
				// Dealing with a Select
				if ( currObj.type.toString().charAt(0)=="s" )
				{
					if ( isSelEmpty(currObj) )
					{
						strMsg += "  - " + fieldName + "\n";
						pass = false;
					}
				}
			}
		}
	}
	if (!pass) 
	{
		alert(strMsg);
		return false;
	}
	else
	{
		return true;	
	}
}
function isIE()
{
	var bIE;
	if (navigator.appName == "NetScape")
	{
		return false; 
	}
	else
	{
		return true; 
	}
}

/* Evaluates to see if a text object is empty */
function isTexEmpty(obj)
{
	var tmpString;
	
	tmpString = obj.value;
	// Trim the string in a while loop
	while(''+ tmpString.charAt(0)==' ')
	{
		tmpString = tmpString.substring(1,tmpString.length);
	}
	if (tmpString == '')
	{
		return true; // Yes the text is empty
	}
	return false; // No The text is not empty
}

/* 
	Evaluates to see if a select object is empty 
	assumes that the first element if selected means an option is not selected
*/
function isSelEmpty(obj)
{
	var selIndex, selValue;
	
	selIndex = obj.selectedIndex;
	selValue = obj[selIndex].value; 
	// Either no option is selected or a default information value is selected
	// For default information value this functions expects its value to be "000"
	if ( ( selIndex == 0 ) || (selValue == "000") )
	{
		return true; // Yes the select is empty
	}
	return false; // No the select is not empty
}

/* 
	Evaluates to see if a multi-select object is empty 
*/
function isMultiSelEmpty(obj)
{
	var selIndex, selValue;
	
	selIndex = obj.selectedIndex;
	if ( selIndex == -1 ) 
	{
		return true; // Yes the select is empty
	}
	return false; // No the select is not empty
}

// Adds an element to a dropdown list
function addElement(aList, aKey, aValue)
{
    var oList = aList.options;
    var nIdx;
	if (oList.length < 0) //IE for Mac 4.5 sets length to -1 if list is empty
		nIdx = 0;
	else
		nIdx = oList.length;
		
	oList[nIdx] = new Option(aKey, aValue);
}

// Clears all the elements from a dropdown list
function clearList(aList)
{
    var iCount = 0;
    var oList = aList.options;

    for (iCount = oList.length; iCount >= 0; --iCount)
    {
		oList[iCount] = null;
	}
}

// Selects a specifield value from a specified dropdown list
function SetSelectOption(aObjList, aKeyValue)
{
	var loopCount, iSelIndex, selValue;
	for (loopCount = 0; loopCount <= aObjList.length; loopCount++)
	{
		selValue = aObjList[loopCount].value;
		if (selValue == aKeyValue)
		{
			aObjList.selectedIndex = loopCount;
			break;
		}
	}
}
// Checks if a passed string is empty
function isStringEmpty(str)
{
	var tmpString;
	
	tmpString = str;
	// Trim the string in a while loop
	while(''+ tmpString.charAt(0)==' ')
	{
		tmpString = tmpString.substring(1,tmpString.length);
	}
	if (tmpString == '')
	{
		return true; // Yes the text is empty
	}
	return false; // No The text is not empty
}

// Checks if a passed value is a valid telephone number - US Format
function isValidTelephoneNumber(passedVal)
{
	for (var i=0; i<passedVal.length; i++)
	{
		if ((passedVal.charAt(i) != "0") &&
			 (passedVal.charAt(i) != "1") &&
			 (passedVal.charAt(i) != "2") &&
			 (passedVal.charAt(i) != "3") &&
			 (passedVal.charAt(i) != "4") &&
			 (passedVal.charAt(i) != "5") &&
			 (passedVal.charAt(i) != "6") &&
			 (passedVal.charAt(i) != "7") &&
			 (passedVal.charAt(i) != "8") &&
			 (passedVal.charAt(i) != "9") &&
			 (passedVal.charAt(i) != "-") &&
			 (passedVal.charAt(i) != "(") &&
			 (passedVal.charAt(i) != ")") ) 
				return false;
	}
	return true;
}

// Checks if a object passed has a valid date in it.
function isValidDate(objName) 
{
	var strDate;
	var strDateArray;
	var strDay;
	var strMonth;
	var strYear;
	var intday;
	var intMonth;
	var intYear;
	var datefield = objName;
	var strSeparator = "/";
	strDate = datefield.value;
	if (strDate.length < 8) 
	{
		return false;
	}
	if (strDate.indexOf(strSeparator) != -1) 
	{
		strDateArray = strDate.split(strSeparator);
		if (strDateArray.length != 3) 
		{
			return false;
		}
		else 
		{
			strMonth = strDateArray[0];
			strDay = strDateArray[1];
			strYear = strDateArray[2];
		}
	}
	else
	{
		return false;
	}
	intMonth = parseInt(strMonth, 10);
	intDay = parseInt(strDay, 10);
	intYear = parseInt(strYear, 10);
	if (isNaN(intMonth) || isNaN(intYear) || isNaN(intDay)) 
	{
		return false;
	}
	if (strYear.length < 4) 
	{
		return false;
	}
	if ( (intMonth>12 || intMonth<1) || (intDay>31 || intDay<1) )
	{
		return false;
	}
	if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intDay > 30)) 
	{
		return false;
	}
	if (intMonth == 2) 
	{
		if (LeapYear(intYear) == true) 
		{
			if (intDay > 29) 
			{
				return false;
			}
		}
		else 
		{
			if (intDay > 28) 
			{
				return false;
			}
		}
	}
	return true;
}

// Checks if the passed year value is a leap year - used in isValidDate function
function LeapYear(intYear) 
{
	if (intYear % 100 == 0) 
	{
		if (intYear % 400 == 0) 
		{ 
			return true; 
		}
	}
	else 
	{
		if ((intYear % 4) == 0) 
		{ 
			return true; 
		}
	}
	return false;
}

// VALIDATE EMAIL ADDRESSES
function isValidEmail(passedVal) 
{
	if ((passedVal.indexOf('@')==-1) || (passedVal.indexOf('.')==-1))
	{
		return false;
	}
	if (passedVal.length < 5)
	{
		return false;
	}
	return true;
}

//Checks if a value is numeric
function isNum(passedVal)
{
	for (i=0; i<passedVal.length; i++)
	{
		if ((passedVal.charAt(i) != "0") &&
		    (passedVal.charAt(i) != "1") &&
		    (passedVal.charAt(i) != "2") &&
		    (passedVal.charAt(i) != "3") &&
	 		 (passedVal.charAt(i) != "4") &&
			 (passedVal.charAt(i) != "5") &&
			 (passedVal.charAt(i) != "6") &&
			 (passedVal.charAt(i) != "7") &&
			 (passedVal.charAt(i) != "8") &&
			 (passedVal.charAt(i) != "9") )
  			return false;
	}
	return true;
}

function getSelValue(obj)
{
	var selIndex, selValue;
	
	selIndex = obj.selectedIndex;
	selValue = obj[selIndex].value;
	return selValue;
}

function containsNumOrAlpha(passedVal)
{
	var strValidValues = "0123456789abcdefghijklmnopqrstuvwxyz";

	for (i=0; i<passedVal.length; i++)
	{
		if (strValidValues.indexOf(passedVal.charAt(i).toLowerCase()) < 0) 
  			return false;
	}
	return true;
}

function openPopupWindow(theURL,winName,features) 
{
  window.open(theURL,winName,features);
}

