//*****************************************************************
//  Prompt.js v 1.0
//
//	These functions support component ABTPrompt.WSC
//	They ensure masking and validation of user input
//*****************************************************************
//use these variables for NS's sake, NS does not work well with some regular expressions
var IE = (document.all) ? true : false;
var NS = (document.getElementById && !document.all) ? true : false;
var savedValue = "";

function checkFocus(obj) 
{
	//Blank out box for user to enter formatted data
	//Save the initial prompt just in case we need it
	var promptAtt = obj.getAttribute("prompt");
	savedValue = obj.value;
	if(obj.value == promptAtt) obj.value = "";
}

function checkBlur(obj) 
{
	//If they leave it blank, put the data prompt back in when they jump off
	if (obj.value == "")
	{
		var promptAtt = obj.getAttribute("prompt");
		obj.value = promptAtt;
	}
}

function resetPrompt(obj)
{
	var promptAtt = obj.getAttribute("prompt");
	obj.value = promptAtt;
	obj.focus();
	obj.select();
}

function checkValid(obj,type)
{
	var test;
	var newvalue = "";
	test = false;
	newvalue = "" + obj.value + "";

	switch(type)
	{
		case '0': //email field
			if(checkEmail(newvalue))
			{
				test = true;
			}
			break;
		case '1': //date field
			if(checkDate(obj,newvalue))
			{
				test = true;
			}
			break;
		default:
			alert("Check component's data type")
			break;
		}
	if(test != true)
	{
		resetPrompt(obj);
		return false;
	}
	return true;
}

function checkDate(obj,date)
{
	var theDate = date;
	var newMonth;
	//If they enter in format m/d/yyyy put it in correct format
	if(theDate.length == 8)
	{
		theDate = "0" + theDate;
		newMonth = theDate.substring(0,3);
		theDate = newMonth + "0" + theDate.substring(3,10);
		obj.value = theDate;
	}
	//If they enter in format m/dd/yyyy or mm/d/yyyy put it in correct format
	if(theDate.length == 9)
	{
		var testSep;
		testSep = theDate.substring(2,3);
		if(testSep != '/')
		{   // m/dd/yyyy
			theDate = "0" + theDate;
			obj.value = theDate;
		}
		else
		{	// mm/d/yyyy
			var newMonth = theDate.substring(0,3);
			theDate = newMonth + "0" + theDate.substring(3,10);
			obj.value = theDate;
		}
	}
	if(theDate.length != 10) 
	{
		return false;
	}
	
	var month = theDate.substring(0, 2)// month
	var firstSep = theDate.substring(2, 3)// '/'
	var day = theDate.substring(3, 5)// day
	var secSep = theDate.substring(5, 6)// '/'
	var year = theDate.substring(6, 10)// year

	//Basic format
	if ((month < 1 || month > 12) || (firstSep != '/') || (day < 1 || day > 31) || (secSep != '/') || (year < 1900 || year > 2099))
	{
		return false;
	}

	//Months with 30 days
	if (month == 4 || month == 6 || month == 9 || month == 11)
	{
		if (day == 31)
		{
			return false;
		}
	}

	//Leap year
	if (month == 2)
	{
		var leap = parseInt(year/4)
		if (isNaN(leap)) 
		{
			return false;
		}

		if ((day > 29) || (day == 29 && ((year/4) != parseInt(year/4))))
		{
			return false;
		}
	}
	return true;
}

function checkEmail(newvalue)
{	
	var s = "";
	var regExp;
	newvalue = "" + newvalue + "";
	
	//Check for leading and trailing spaces, and smallest possible email(6)
	newvalue = newvalue.replace(/^\s*|\s*$/g, "");
	if(newvalue == "undefined" || newvalue.length < 6 || newvalue.length > 255) 
	{
		return false;
	}

	//Check for spaces, no good in email address
	regExp = /\s+/g
	if(regExp.test(newvalue)) 
	{
		return false;
	}
	
	//Check for @@
	regExp = /\@\@/
	if(regExp.test(newvalue)) 
	{
		return false;
	}
	
	//Check for x@x...com
	regExp = /\.\./
	if(regExp.test(newvalue))
	{
		return false;
	}
	
	//Check valid top level domains out now - January 2003
	//Can be used at a later time
	//regExp = /\.(a[c-gil-oq-uwz]|b[a-bd-jm-or-tvwyz]|c[acdf-ik-orsuvx-z]|d[ejkmoz]|e[ceghr-u]|f[i-kmorx]|g[abd-ilmnp-uwy]|h[kmnrtu]|i[delm-oq-t]|j[emop]|k[eg-imnprwyz]|l[a-cikr-vy]|m[acdghk-z]|n[ace-giloprtuz]|om|p[ae-hk-nrtwy]|qa|r[eouw]|s[a-eg-ort-vyz]|t[cdf-hjkm-prtvwz]|u[agkmsyz]|v[aceginu]|w[fs]|y[etu]|z[admrw]|com|edu|net|org|mil|gov)$/i;
	/*var domainpattern = "";
	domainpattern+="\.(";
	domainpattern+="a[c-gil-oq-uwz]|";     
	domainpattern+="b[a-bd-jm-or-tvwyz]|"; 
	domainpattern+="c[acdf-ik-orsuvx-z]|"; 
	domainpattern+="d[ejkmoz]|";           
	domainpattern+="e[ceghr-u]|";          
	domainpattern+="f[i-kmorx]|";          
	domainpattern+="g[abd-ilmnp-uwy]|";    
	domainpattern+="h[kmnrtu]|";           
	domainpattern+="i[delm-oq-t]|";        
	domainpattern+="j[emop]|";             
	domainpattern+="k[eg-imnprwyz]|";      
	domainpattern+="l[a-cikr-vy]|";        
	domainpattern+="m[acdghk-z]|";         
	domainpattern+="n[ace-giloprtuz]|";    
	domainpattern+="om|";                  
	domainpattern+="p[ae-hk-nrtwy]|";      
	domainpattern+="qa|";                  
	domainpattern+="r[eouw]|";             
	domainpattern+="s[a-eg-ort-vyz]|";     
	domainpattern+="t[cdf-hjkm-prtvwz]|";  
	domainpattern+="u[agkmsyz]|";          
	domainpattern+="v[aceginu]|";          
	domainpattern+="w[fs]|";               
	domainpattern+="y[etu]|";              
	domainpattern+="z[admrw]|";            
	domainpattern+="com|";                 
	domainpattern+="edu|";                 
	domainpattern+="net|";                 
	domainpattern+="org|";                 
	domainpattern+="mil|";                 
	domainpattern+="gov|";                 
	domainpattern+="biz|";                 
	domainpattern+="pro|";                 
	domainpattern+="aero|";                
	domainpattern+="coop|";                
	domainpattern+="info|";                
	domainpattern+="name|";                
	domainpattern+="int|";                 
	domainpattern+="museum";               
	domainpattern+=")$/i";
	regExp = domainpattern;
	var isIn1 = regExp.test(newvalue); */

	//Check email syntax - x@x.xx, etc....
	//If IE, Netscape is funky with the validation, had to strip the regexp down for NS for it to work
	if(!NS)
	{
		regExp = /^(\w|[^_]\.|[\-])+((\@){1}([^_]))(([a-z]|[\d]|[\-]|\.)+|([^_]\.[^_])*)+\.[a-z]{2,6}$/i;
	}
	else
	{
		regExp = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,6})(\]?)$/;	
	}
	
	if(!regExp.test(newvalue))
	{
		return false;
	}
	return true;
}

function checkRegExp(obj, expression)
{
	if(expression == "error")
	{
		alert("Parameters invalid in 'CheckNumeric' method")
		resetPrompt(obj);
		return false;
	}
	
	var regExp = "";
	var newvalue = "";
	var regExp = new RegExp(expression);
	newvalue = "" + obj.value + ""; 
	if(regExp.test(newvalue))
	{
		return true;
	}
	else
	{
		resetPrompt(obj);
		return false;
	}
}

function checkText(obj,min,max)
{
	if(obj.value.length < min || obj.value.length > max)
	{	
		resetPrompt(obj);
		return false;
	}
	else
	{
		return true;
	}
}

function checkTime(obj,standardtime,hms)
{
	var newHours;
	var regExp;
	var newvalue = "";
	newvalue = "" + obj.value + "";
	var theTime = newvalue;
	
	//If they enter in format "#" instead of "##", put it in correct format
	if(theTime.length == 1)
	{
		//Can't put just 0 as the hour
		if(hms.toUpperCase() == "H" && theTime == "0")
		{
			resetPrompt(obj);
			return false;
		}
		else
		{
			theTime = "0" + theTime;
			newHours = theTime;
			obj.value = theTime;
		}
	}
	if(theTime.length != 2) 
	{
		resetPrompt(obj);
		return false;
	}

	switch(hms.toUpperCase())
	{
		case 'H':
			if(standardtime == "True" || standardtime == "true")
			{
				regExp = /^(([0])([1-9])|([1])([0-2]))$/;
			}
			else
			{ //It's Military
				regExp = /^(([0-2])([0-3])|([0-1])([0-9]))$/;
			}
			checkRegExp(obj, regExp);
			break;
		case 'M':
			regExp = /^[0-5]\d$/;
			checkRegExp(obj, regExp);
			break;
		case 'S':
			regExp = /^[0-5]\d$/;
			checkRegExp(obj, regExp);
			break;
		default:
			alert("Check component's time data type")
			resetPrompt(obj);
			return false;
			break;
	}
	return 0;
}

function checkMove(obj, nextField)
{
	var valuelength = obj.value.length;
	if (valuelength < 2)
	{
		obj.focus()
	}
	else
	{
		eval(nextField + '.focus()');
	}
}