//***************************************************************************
//     Web Page: Common.js
//       Author: SupplyCore
//         Date: 02/15/2006
//  Description: Common JavaScript Functions
//***************************************************************************


//***************************************************************************
//  VARIABLE DECLARATIONS
//***************************************************************************
var whitespace = " \t\n\r";			// whitespace characters


function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

function getRadioSelectedValue(radioButton)
{

	for (var i = radioButton.length-1; i > -1; i--) 
		if (radioButton[i].checked) 
			return radioButton[i].value;
    
}


//***************************************************************************
//  FUNCTION: PopUp
//   PURPOSE:	Opens the file specified in the input in a new window
//***************************************************************************
function PopUp(sFileName, iHeight, iWidth, iLeft, iTop, iScreenX, iScreenY, iWindowNo)
{
	var sFeatures;
	var sWindowName = "SupplyCore" + iWindowNo
	
	sFeatures = 'height=' + iHeight + ',width=' + iWidth + ',left=' + iLeft + ',top=' + iTop 
	sFeatures = sFeatures + ',screenX=' + iScreenX + ',screenY=' + iScreenY 
	sFeatures = sFeatures + ',menubar=no,status=no,scrollbars=yes,toolbar=no,location=no,resizable=yes'

	self.open(sFileName, sWindowName, sFeatures)
	//window.open(sFileName, sWindowName, sFeatures)

}
//***************************************************************************


//***************************************************************************
//  FUNCTION: ViewHide
//   PURPOSE:	
//***************************************************************************
function ViewHide(sName)
{
  var oObj 
  
  oObj= document.getElementById(sName)
  if(!oObj) return;
  oObj.style.display=(oObj.style.display=="none"?"block":"none");

}
//***************************************************************************


//***************************************************************************
//  FUNCTION:	truncateTextArea
//  PURPOSE:	Determines if calling textarea has a value greater than the value
//				    passed in, if so, it truncates the value and alerts the user
//***************************************************************************
function truncateTextArea(sName, imax)
{
	var ele;
	var e;
	var eleVal;
	var eleLength;
	var tmpString;
	
	ele = document.forms[0];
	//find the element in the form
	for(var i = 0; i < ele.length; i++){
		e = ele.elements[i];
		if(e.name == sName){
			//found it
			eleVal = e.value;
			eleLength = eleVal.length;
			break;
		}
	}
	
	//check the length of the value in the text field
	if(eleLength < imax){
		eleVal = eleVal;
	}else{
		alert('You have reached the maximum number of characters for this field.');
		tmpString = eleVal
		eleVal = '';
		e.value = tmpString.substring(0, imax);
	}
	
	e.focus();
}
//***************************************************************************


//***************************************************************************
//  FUNCTION:	isValidDate
//   PURPOSE:	Checks to make sure that a valid date has been entered
//***************************************************************************
function isValidDate(datein)
{
        
  var indate = datein;

  if (indate.indexOf("-")!=-1)
		var sdate = indate.split("-")
  else
    var sdate = indate.split("/")

  var chkDate = new Date(Date.parse(indate))
        
  var cmpDate = (chkDate.getMonth()+1)+"/"+(chkDate.getDate())+"/"+(chkDate.getYear())
  var indate2 = (Math.abs(sdate[0]))+"/"+(Math.abs(sdate[1]))+"/"+(Math.abs(sdate[2]))
  if (indate2 != cmpDate){
    return false;
  } else {
		if (cmpDate == "NaN/NaN/NaN"){
			return false;
		} else {
			return true;
		}       
  }

}
//***************************************************************************


//***************************************************************************
//  FUNCTION:	isValidTime
//   PURPOSE:	Checks if time is in HH:MM:SS AM/PM format.  The seconds and AM/PM are optional
//***************************************************************************
function isValidTime(strTime) {

	var timePat = /^(\d{1,2}):(\d{2})(:(\d{2}))?(\s?(AM|am|Am|aM|PM|pm|Pm|pM))?$/;
	var matchArray = strTime.match(timePat);

	if (matchArray == null) {
		return false;
	}

	hour = matchArray[1];
	minute = matchArray[2];
	second = matchArray[4];

	if (second=="") { second = null; }

	if (hour < 0  || hour > 23) {
		return false;
	}

	if (minute<0 || minute > 59) {
		return false;
	}
	if (second != null && (second < 0 || second > 59)) {
		return false;
	}
	return true;
}
//***************************************************************************


//***************************************************************************
//  FUNCTION: isValidDateTime
//   PURPOSE:	Checks to make sure that a valid date/time has been entered
//***************************************************************************
function isValidDateTime(strDateTime)
{
	var dt = Trim(strDateTime);
	var intMatch;
	var intDateOnly = false;

	//if (strDateTime.toLowerCase()=="today" || strDateTime.toLowerCase()=="now"){return true;}
	
	intMatch = dt.indexOf(":");
	if (intMatch < 0)
	{
		intDateOnly = true;
		intMatch = dt.length;
	}
	else
	{
		intMatch = dt.indexOf(" ");
	}

	if (intMatch < 0) {return false;}
	
	// check date
	if (!isValidDate(dt.substr(0,intMatch))){return false;}
	
	// check time
	if (!intDateOnly) {
		if (!isValidTime(dt.substr(intMatch+1,dt.length-intMatch))){return false;}
	}
	
	return true;
}
//***************************************************************************


//***************************************************************************
//  FUNCTION: round
//   PURPOSE:	Rounds number to X decimal places, defaults to 2
//***************************************************************************
function round(number,X) 
{
    X = (!X ? 2 : X);
    return Math.round(number*Math.pow(10,X))/Math.pow(10,X);
}
//***************************************************************************


//***************************************************************************
//  FUNCTION:	isInteger
//   PURPOSE:	Returns true if the input is an integer (whole number)
//***************************************************************************
function isInteger(s)
{
  if (isEmpty(s)) return false;
	s = stripWhitespace(s);
	
	for (var i = 0; i < s.length; i++) 
	{
		var c = s.charAt(i)
		if (!isDigit(c)) 
			return false;	
	}
	
	return true
}
//***************************************************************************


//***************************************************************************
//  FUNCTION:	isFloat
//   PURPOSE:	Returns true if the input is a floating point number 
//***************************************************************************
function isFloat(s)
{
  var bDecimalPoint = false;
  
  if (isEmpty(s)) return false;
	s = stripWhitespace(s);

//	if (s.charAt(0) == "$")
//		s = s.slice(1)
  
  if (s == ".") 
		return false;

	for (var i = 0; i < s.length; i++) 
	{
		var c = s.charAt(i)

    if ((c == ".") && !bDecimalPoint) 
			bDecimalPoint = true;
    else if (!isDigit(c)) 
			return false;
	}
	
	return true
}
//***************************************************************************


//***************************************************************************
//  FUNCTION:	isDigit
//   PURPOSE:	Returns true if the input is a digit
//***************************************************************************
function isDigit (c)
{   
	return ((c >= "0") && (c <= "9"))
}
//***************************************************************************


//***************************************************************************
//  FUNCTION:	isEmpty
//   PURPOSE:	Returns true if the input is empty
//***************************************************************************
function isEmpty(s)
{   
	return ((s == null) || (s.length == 0))
}
//***************************************************************************


//***************************************************************************
//  FUNCTION: isWhitespace
//   PURPOSE:	Returns true if the input contains all whitespace characters
//***************************************************************************
function isWhitespace (s)
{   
	s = stripWhitespace(s);
  if (isEmpty(s)) return true;

  for (var i = 0; i < s.length; i++)
  {   
		var c = s.charAt(i);
		if (whitespace.indexOf(c) == -1) 
			return false;
  }

  return true;
}
//***************************************************************************
	

//***************************************************************************
//  FUNCTION:	stripWhitespace
//   PURPOSE:	Search through string's characters one by one and remove any.
//            whitespace characters
//***************************************************************************
function stripWhitespace (s)
{   
	var returnString = "";

	for (var i = 0; i < s.length; i++)
	{   
		var c = s.charAt(i);
		if (whitespace.indexOf(c) == -1) returnString += c;
	}

	return returnString;

}
//***************************************************************************


//***************************************************************************
//  FUNCTION:	isObject
//   PURPOSE:	Returns TRUE if the input paramater is an object
//***************************************************************************
function isObject( r ) 
{ 

  return( r && (( 'object' == typeof r ) || ( 'function' == typeof r ))); 

} 
//***************************************************************************


//***************************************************************************
//  FUNCTION:	Trim
//   PURPOSE:	remove leading and trailing spaces
//***************************************************************************
function Trim(s)
{
	var trimmed="";
	var leading = true;
	var trailing = true;
	
	// strip leading spaces
    for(var i = 0; i < s.length; i++) 
		{
        var c = s.charAt(i);
        if (c == ' ') 
        { 
					if (leading==false) trimmed=trimmed+c;
				}
				else
				{	
				leading=false;
				trimmed=trimmed+c;
			}
    }
    
    // strip trailing spaces
    for (var i = trimmed.length-1; i>0; i--) 
    {
			var c = trimmed.charAt(i);
			if (c != " ")	
				return trimmed;
			else
				trimmed=trimmed.substr(0,trimmed.length-1);
		}
	
	return trimmed;
}
//***************************************************************************


//***************************************************************************
//  FUNCTION:	ConvertToDate
//   PURPOSE:	Returns the number of days between 2 dates
//***************************************************************************
function ConvertToDate(date)
{

	var date1 = date.substring(0, date.indexOf(" "));
  if (date1.indexOf("-")!=-1)
		date1 = date1.split("-")
  else
    date1 = date1.split("/")
  
  return date1[0]+"/"+date1[1]+"/"+date1[2];
  
}
//***************************************************************************


//***************************************************************************
//  FUNCTION:	DaysDiff
//   PURPOSE:	Returns the number of days between 2 dates
//***************************************************************************
function DaysDiff(date1, date2)
{

  var sDate = ConvertToDate(date1)
  var eDate = ConvertToDate(date2)
  
  return Math.round((sDate-eDate)/86400000);
  
}
//***************************************************************************



//***************************************************************************
//  FUNCTION:	BusinessDays
//   PURPOSE:	Returns the number of business days between 2 dates
//***************************************************************************
function BusinessDays(date1, date2) {

  var sDate = ConvertToDate(date1);
  var eDate = ConvertToDate(date2);
	
	if(sDate.getYear < 2000 || eDate.getYear < 2000)
		return NULL
		
	if(sDate > eDate) {
		var dTemp = sDate;
		sDate = eDate;
		eDate = dTemp;
	}
	
	var iDateDiff = Math.round((eDate-sDate)/86400000);
	var iWeekEndDays = 0;
	var dCntr = sDate;

	while(dCntr < eDate) {
		if(dCntr.getDay() == 6 || dCntr.getDay() == 0) 
			iWeekEndDays = iWeekEndDays + 1;	
		dCntr.setDate(dCntr.getDate() + 1)
	}

	iDateDiff = iDateDiff - iWeekEndDays;

	if(iDateDiff == 0 && iWeekEndDays != 0)
		iDateDiff = 0;
	
	return iDateDiff;

}
//***************************************************************************


//***************************************************************************
//  FUNCTION:	DateOnly
//   PURPOSE:	Returns the date portion of a date variable
//***************************************************************************
function DateOnly(date)
{
	
	var date1 = date.substring(0, date.indexOf(" "));
  if (date1.indexOf("-")!=-1)
		date1 = date1.split("-")
  else
    date1 = date1.split("/")
  
  return date1[0]+"/"+date1[1]+"/"+date1[2];
  
}
//***************************************************************************


//***************************************************************************
//  FUNCTION:	GetCurrentDateTime
//   PURPOSE:	Returns the current date and time
//***************************************************************************
function GetCurrentDateTime (oObj, iType) 
{
	var sReturn = '';
	var now = new Date();
	var hours = now.getHours();
	var minutes = now.getMinutes();
	var seconds = now.getSeconds();
	var dateValue = (now.getMonth() + 1) + "/" + now.getDate() + "/" + now.getFullYear() + " ";
	var timeValue = "" + ((hours >12) ? hours -12 :hours)
	if (timeValue == "0") timeValue = 12;
	timeValue += ((minutes < 10) ? ":0" : ":") + minutes
	timeValue += ((seconds < 10) ? ":0" : ":") + seconds
	timeValue += (hours >= 12) ? " PM" : " AM"
	
	switch (iType)
	{
		case 1:		//Return Date only
			sReturn = dateValue;
			break;
			
		case 2:		//Return Time Only
			sReturn = timeValue;
			break;
				
		default:	//Otherwise return both date and time
			sReturn = dateValue + timeValue;
			break;
	}

	eval(oObj + ".value = sReturn;");

}
//***************************************************************************


//***************************************************************************
//  FUNCTION:	IsValidEmailAddress
//   PURPOSE:	Returns TRUE if the input paramater is a valid email address
//***************************************************************************
function IsValidEmailAddress(sEmail) 
{

	if (isWhitespace(sEmail) == true)
		return false
	else {
		var at="@"
		var dot="."
		var lat=sEmail.indexOf(at)
		var lstr=sEmail.length
		var ldot=sEmail.indexOf(dot)
				
		if (sEmail.indexOf(at)==-1)
		   return false

		if (sEmail.indexOf(at)==-1 || sEmail.indexOf(at)==0 || sEmail.indexOf(at)==lstr)
		   return false

		if (sEmail.indexOf(dot)==-1 || sEmail.indexOf(dot)==0 || sEmail.indexOf(dot)==lstr)
		    return false

		 if (sEmail.indexOf(at,(lat+1))!=-1)
		    return false

		 if (sEmail.substring(lat-1,lat) == dot || sEmail.substring(lat+1,lat+2) == dot)
		    return false

		 if (sEmail.indexOf(dot,(lat+2)) == -1)
		    return false
				
		 if (sEmail.indexOf(" ")!=-1)
		    return false

		 return true
	}
}
//***************************************************************************



