// ********************** M I S C *******************
// **************************************************
// function to display a popup window
// **************************************************
function popsawindow(url,name)
{
	var height = 600
	var width = 750

	var winheight = 600
	var winwidth = 750

	var pop = null

	LeftPosition=(screen.width)?(screen.width-width)/2:100;

	TopPosition=(screen.height)?(screen.height-height)/2:100;

	paramstp="height="+winheight+",width="+winwidth+",top="+TopPosition+
	",left="+LeftPosition+",scrollbars=yes,location=no"+
	",directories=no,status=no,menubar=no,toolbar=no,resizable=no"

	pop = window.open(url,name,paramstp);

	if(pop.focus)
	{
		pop.focus();
	}
}


// ***** E M A I L   V A L I D A T I O N ************
// **************************************************
// checks and validates email format
// **************************************************
function checkEmail(emailAddr)
{
	var checkEmail = emailAddr
	var isValid = false;

	if ( (checkEmail.indexOf('@') < 0)
	|| ( (checkEmail.charAt(checkEmail.length-4) != '.')
	&& (checkEmail.charAt(checkEmail.length-3) != '.') )
	)
	{
		isValid = false;
	}
	else
		isValid = true;

	return isValid;
}




// ***** D A T E   V A L I D A T I O N **************
// **************************************************
// Validates the date value entered in mm/dd/yyyy format
// **************************************************
function validateDate(strText)
{
	// The format of the date is: mm/dd/yyyy
	// The format of the date is: mm/dd/yyyy
	// return codes
	// 0: OK 
	// 1: bad month
	// 2: bad day
	// 3: bad year
	var listArray = strText.split('/');

	// Check for basic correct shape and all numbers
	if(listArray.length != 3)
		return 1;

	if(isNaN(listArray[0]))
		return 1;

	if(isNaN(listArray[1]))
		return 2;

	if(isNaN(listArray[2]))
		return 3;

	// Get the month, day, and year
	var month = new Number(listArray[0]).valueOf();
	var day = new Number(listArray[1]).valueOf();
	var year = new Number(listArray[2]).valueOf();

	// Check for month, day, and year ranges
	if(month < 1 || month > 12)
		return 1;

	if(day < 1 || day > 31)
		return 2;

	if(year < 2005 || year > 2099)
		return 3;

	// See if invalid day in the month i.e. 02/31/2004
	var newDate = new Date(month + "/" + day + "/" + year);
	var newDateStr = newDate.toGMTString();
	var newListArray = newDateStr.split(' ');

	if (new Number(newListArray[1].valueOf()) != day)
		return 2;

	// OK
	return 0;
}

// **************************************************
// checks date format
// **************************************************
function check_ldate_text(TextValue)
{
	var checkstr = "0123456789";
	var Datevalue = "";
	var DateTemp = "";
	var seperator = "/";
	var day;
	var month;
	var year;
	var leap = 0;
	var err = 0;
	var i;
	var slash1 ;
	var slash2 ;

	err = 0;
	DateValue = TextValue ;
	if(DateValue.length < 10)
		err = 27 ;

	slash1 = DateValue.substr(2,1);
	if(slash1!="/")
		err = 28 ;

	slash2 = DateValue.substr(5,1);
	if(slash2!="/")
		err = 29 ;

	/* Delete all chars except 0..9 */
	for (i = 0; i < DateValue.length; i++)
	{
		if (checkstr.indexOf(DateValue.substr(i,1)) >= 0)
		{
			DateTemp = DateTemp + DateValue.substr(i,1);
		}
	}

	DateValue = DateTemp;
	/* Always change date to 8 digits - string*/
	/* if year is entered as 2-digit / always assume 20xx */
//    if (DateValue.length == 6)
//    {
//      DateValue = DateValue.substr(0,4) + '20' + DateValue.substr(4,2);
//    }

	if (DateValue.length != 8)
	{
	  err = 19;
	}

	/* year is wrong if year = 0000 */
	year = DateValue.substr(4,4);
	if (year == 0)
	{
	  err = 20;
	}

	/* Validation of month*/
	month = DateValue.substr(0,2);
	if ((month < 1) || (month > 12))
	{
	  err = 21;
	}

	/* Validation of day*/
	day = DateValue.substr(2,2);
	if (day < 1)
	{
	 err = 22;
	}

	/* Validation leap-year / february / day */
	if ((year % 4 == 0) || (year % 100 == 0) || (year % 400 == 0))
	{
	  leap = 1;
	}
	if ((month == 2) && (leap == 1) && (day > 29))
	{
	  err = 23;
	}
	if ((month == 2) && (leap != 1) && (day > 28))
	{
	  err = 24;
	}

	/* Validation of other months */
	if ((day > 31) && ((month == "01") || (month == "03") || (month == "05") || (month == "07") || (month == "08") || (month == "10") || (month == "12"))) {
	  err = 25;
	}

	if ((day > 30) && ((month == "04") || (month == "06") || (month == "09") || (month == "11"))) {
	  err = 26;
	}

	/* if 00 ist entered, no error, deleting the entry */
	if ((day == 0) && (month == 0) && (year == 00)) {
	  err = 0; day = ""; month = ""; year = ""; seperator = "";
	}

	/* if no error, write the completed date to Input-Field (e.g. 13.12.2001) */
	if (err == 0) {
		return true;
	}

	/* Error-message if err != 0 */
	else
	{
		return false ;
	}
}

// **************************************************
// Confirms if the date is in future.
// **************************************************
function dateInFuture(strText)
{
	// The format of the date is: mm/dd/yyyy
	var newDate = new Date(strText);

	// 
	var rightNow = new Date();

	if (rightNow.getTime() < newDate.getTime()) 
	{
		return true;
	}

	// OK
	return false;
}

// **************************************************
// Compares two dates.
// **************************************************
function isBeginDatePriorToEndDate(beginDate, endDate)
{
	// The format of the date is: mm/dd/yyyy
	var bDate = new Date(beginDate);
	var eDate = new Date(endDate);

	if (eDate.getTime() < bDate.getTime()) 
	{
		return false;
	}

	// OK
	return true;
}



// ***** S T R I N G   V A L I D A T I O N **********
// **************************************************
// trims the input text and returns back
// **************************************************
function trim(strText)
{
	// Trim a string 
	while (strText.substring(0,1) == ' ') 
		strText = strText.substring(1, strText.length);

	while (strText.substring(strText.length-1,strText.length) == ' ')
		strText = strText.substring(0, strText.length-1);

	return strText;
}


// **************************************************
// Provide generic selection of a select on the basis
// after load
// **************************************************
function loadSelection(objSelect, objText)
{
	var inputStr = objText;
	var atLeastOne = 0

	if(inputStr == null || inputStr == '')
	{
		inputStr = '';
	}
	for(var i = 0; i < objSelect.length; i++) 
	{
		if(inputStr == objSelect.options[i].value) 
		{
			objSelect.options[i].selected = true
			atLeastOne = 1
		}
	}

	if(atLeastOne == 0) 
	{
	// default select the first one
		objSelect.options[0].selected = true
	}

	return true;
}


// ***** N U M E R I C   V A L I D A T I O N ********
// **************************************************
// function to check if the value is numeric, 
// with preceding + or -
// **************************************************
function isNumeric(sText)
{
	var ValidChars = "0123456789.-+";
	var IsNumber=true;
	var theChar;


	// if the string contains sign, it better be the first char
	if(sText.lastIndexOf("-") > 0)
	{
		return false;
	}

	for (i = 0; i < sText.length && IsNumber == true; i++)
	{
		theChar = sText.charAt(i);
		if (ValidChars.indexOf(theChar) == -1)
		{
			IsNumber = false;
		}
	}
	return IsNumber;
}



// **************************************************
// function to check basic phone number
// **************************************************
function isPhone(sText)
{
	var ValidChars = "0123456789-";
	var isPhoneNum = true;
	var theChar;


	// if the string contains sign, it better not be the first char
	if(sText.lastIndexOf("-") == 0)
	{
		return false;
	}

	for (i = 0; i < sText.length && isPhoneNum == true; i++)
	{
		theChar = sText.charAt(i);
		if (ValidChars.indexOf(theChar) == -1)
		{
			isPhoneNum = false;
			break;
		}
	}

	return isPhoneNum;

}



// **************************************************
// function to check basic alpha numeric, no signs
// **************************************************
// **************************************************
// function to check basic alpha numeric, no signs
// "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
// **************************************************
function isAlphaNumeric(inputStr)
{
	var numeric = inputStr;

	for(var j=0; j <numeric.length; j++)
	{
		var alphaa = numeric.charAt(j);
		var hh = alphaa.charCodeAt(0);

		if(!((hh > 47 && hh < 58) || (hh > 64 && hh < 91) || (hh > 96 && hh < 123)))
		{
			 return false;
		}
	}
	
	return true;
}

// **************************************************
// function to check basic alphabets only
// "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
// **************************************************
function isAlphabetic(inputStr)
{
	var numeric = inputStr;

	for(var j=0; j <numeric.length; j++)
	{
		var alphaa = numeric.charAt(j);
		var hh = alphaa.charCodeAt(0);

		if(!((hh > 64 && hh < 91) || (hh > 96 && hh < 123)))
		{
			 return false;
		}
	}
	
	return true;
}


// **************************************************
// function to check basic alphabets only and "." and " "
// **************************************************
function isAlphabeticOrSpaceOrDot(inputStr)
{
	var numeric = inputStr;

	for(var j=0; j <numeric.length; j++)
	{
		var alphaa = numeric.charAt(j);
		var hh = alphaa.charCodeAt(0);

		if(!( (hh > 64 && hh < 91) || (hh > 96 && hh < 123) || (hh == 46) || (hh == 32) ))
		{
			 return false;
		}
	}
	
	return true;
}
