var BeginningWhiteSpaceRegExp = /^\s+/;
var EndingWhiteSpaceRegExp = /\s+$/;

function CheckField(Required,WhichField,FieldType,DisplayName,ReqLength,Numeric) {
	var RegExp;
	var TestVal;
	var MatchChar;
	var ErrMsg = 'None';
		
	//Replace any whitespace at beginning and end of string with nothing	
	if (FieldType == 'Text' || FieldType == 'Alpha'  || FieldType == 'Date' || FieldType == 'Money' ||
		FieldType == 'MoneyWhole' || FieldType == 'Phone' || FieldType == 'Email' || FieldType == 'Password') {
		TestVal = WhichField.value.replace(BeginningWhiteSpaceRegExp,'');
		TestVal = TestVal.replace(EndingWhiteSpaceRegExp,'');
	} else { 
		if (WhichField.selectedIndex == -1 || WhichField.options[WhichField.selectedIndex].value == 0) {
			TestVal = '';
		} else {	
			TestVal = WhichField.options[WhichField.selectedIndex].text;
		}
	}
	if (TestVal == '' && Required == true) {
		ErrMsg = DisplayName + ' must be entered';
	} 
	
	
	//If required check has been passed, continue with field type edits 
	
	if (ErrMsg == 'None' && TestVal != '') {	
		
		//Check required length 	
		if (ReqLength > 0) {
			if (TestVal.length != ReqLength) {
				ErrMsg = DisplayName + ' must be ' + ReqLength + ' characters';
			}
		}  
	
		//For money, remove all commas and one decimal, then set to Numeric to force numeric check
		if (FieldType == 'Money') {
			RegExp = /,/g;
			TestVal = TestVal.replace(RegExp,'');
			RegExp = /\./;
			TestVal = TestVal.replace(RegExp,'');
			Numeric = true;
		}
		
		//For money whole dollars, remove all commas then set to Numeric to force numeric check
		if (FieldType == 'MoneyWhole') {
			RegExp = /,/g;
			TestVal = TestVal.replace(RegExp,'');
			Numeric = true;
		}


		//Numeric check - match on any non-numeric character. If found, return Error.
		if (Numeric == true) {
			RegExp = /\D/;
			MatchChar = TestVal.match(RegExp);
			if (MatchChar != null) {
				ErrMsg = DisplayName + ' must be numeric';
			}
		}
	
		//Alpha check - match on any non A-Z character. If found, return Error.
		if (FieldType == 'Alpha') {
			RegExp = /[^A-Z]/;
			TestVal = TestVal.toUpperCase();
			MatchChar = TestVal.match(RegExp);
			if (MatchChar != null) {
				ErrMsg = DisplayName + ' must be alphabetic';
			}
		}
	
		//Check for valid date
		if (FieldType == 'Date') {
			if (CheckDate(TestVal) == 'Error') {
				ErrMsg = DisplayName + ' must be a valid date in mm/dd/yy format';
			}
		}
		
		//Check for valid Phone
		if (FieldType == 'Phone') {
			var c;
			var numLth=0;
			for(i=0;i<TestVal.length;i++) {
			    c=TestVal.charAt(i);
			    //Allow left bracket in 1st position only, and only if right bracked entered too, ie. (617)
			    if (c == '(') { 
					if (i==0 && TestVal.charAt(4) == ')') {
						continue;
					} else {
						ErrMsg = DisplayName + ' contains invalid brackets.';
						break;
					}
				} 
				
				//Allow right bracket in 5th position only ie. (617)
			    if (c == ')') {	
					if (i==4) {
						continue;
					} else {
						ErrMsg = DisplayName + ' contains invalid brackets.';
						break;
					}
				}
			    
			    if (c == ' ') {
					//Allow space only after area code right bracket
					if (i==5 && TestVal.charAt(4) == ')') {
						 continue; 
					} else {   
					   ErrMsg = 'Embedded spaces are not allowed in ' + DisplayName + '.\nUse hyphen, period, or comma to seperate numbers.';
					   break;
					}
			    }
			    
			    //Comma, hyphen or period allowed
			    if (c==',' || c=='-' || c==".") {
					continue;
				} else {
					if (isNaN(c)) {  
						ErrMsg = DisplayName + ' cannot have non-numeric digits.'; 
					    break;
					} else {
						numLth = numLth + 1;
					}
				}
			}
			
		}		
	
		//Check for valid email address
		if (FieldType == 'Email') {
			var no=0;
			var c;
    
			for(i=0;i<TestVal.length;i++) {
				c=TestVal.charAt(i);
			    if(i==0) { 
			        /*if(!isNaN(c)) {
						no=2;  
					}*/
				}
			    if(c==' ') no=2;
			    if(c =='@')  no++;
			}

			if(no!=1 || TestVal.indexOf('@')<2) {
				ErrMsg = TestVal + ' is not a valid E-mail Address';
			}
				   
			if (TestVal.indexOf('.')<1) {
				ErrMsg = TestVal + ' is not a valid E-mail Address';
			}
			
			if ((TestVal.length-TestVal.lastIndexOf('.'))<3) {
				ErrMsg = TestVal + ' is not a valid E-mail Address';
			}	
		}
	}
	
	if (ErrMsg == 'None') {
		return('OK');
	} else {
		if (FieldType == 'List') {
			WhichField.selectedIndex = 0;
		}
		WhichField.focus();
		alert(ErrMsg);
		return('Error');
	} 
}

	
function trimSpaces(WhichField) {
	
	var TestVal;
	
	//Replace any whitespace at beginning and end of string with nothing	
	TestVal = WhichField.value.replace(BeginningWhiteSpaceRegExp,'');
	TestVal = TestVal.replace(EndingWhiteSpaceRegExp,'');
	
	return(TestVal);
}

function CheckDate(WhichDate) {
	
	var SplitStr;
	var SplitDate;
	var NumSplits;
	
	//Check for date separator either / or -
	SplitStr = WhichDate.indexOf("/");
	if (SplitStr == -1) {
		SplitStr = "-";
	} else {
		SplitStr = "/";
	}
	
	//Split by separator into mm dd yy
	SplitDate = WhichDate.split(SplitStr);
	NumSplits = SplitDate.length;
	if (NumSplits != 3) {
		return('Error');
	} else {
		mm = SplitDate[0];
		dd = SplitDate[1];
		yy = SplitDate[2];
	}
	
	if (yy.length != 2) {
		if(yy.length!=4){
			return('Error');
		}else{
			if (isNaN(yy) || yy<0  || yy>2100) return('Error');
		}
	}else{
		if (isNaN(yy) || yy<0  || yy>99) return('Error');
	}
			
	//basic error checking - check for number and proper range
	if (isNaN(mm) || mm<1  || mm>12) return('Error');
	if (isNaN(dd) || dd<1  || dd>31) return('Error');
	

	
	//advanced error checking

	// months with 30 days
	if (mm==4 || mm==6 || mm==9 || mm==11) {
		if (dd==31) return('Error');

	}

	// february, leap year
	if (mm==2) {
		if (dd>29) 	return('Error');

		if (dd==29) {
			if ((yy % 4 == 0 && yy % 100 != 0) || yy % 400 == 0) {
				return('OK');
			} else {
				return('Error');
			}
		}
	}
	return('OK');
}

function getMMDDCCYY(DateMMDDYY) {
	var Date1;
	var dd;
	var mm;
	var yr;
	Date1 = new Date(DateMMDDYY);
	dd = Date1.getDate();
	mm = Date1.getMonth();
	yr = Date1.getYear();
	if (yr > 0 && yr < 50) {
		yr = yr + 2000;
	}
	return (new Date(yr,mm,dd));
}

function MoveUpAndDown(selName,action,frmName)
{
	var lngLength = eval("document." + frmName + "." + selName + ".options.length");
	var ctrl = eval("document." + frmName + "." + selName);
	var i;
	var selCount;
	selCount = 0;
	
	
	//checking how many items are selected for movement.
	for (i = 0; i < lngLength; i++)  
		{
			if (ctrl.options[i].selected == true) 
			{
				selCount = selCount + 1;
			}
		}	
	
	//only one item can be selected for movement
	if (selCount == 0)
	{
		alert('No item is selected for movement.');
	} 
	else if (selCount > 1)
	{
		alert('Only one item can be selected for movement.');
	}
	else if (selCount == 1)
	{
	
		if (action == 'U')
		{
			funMoveUP(selName,frmName);
		}
		else if (action == 'D')
		{
			funMoveDown(selName,frmName); 
		}
	} 
}


function funMoveUP(strsctrlname,frmname)
{
//this function is used to move the options one step above.

	var object;				//store the object of the new option element
	var intidx;             //store the index of the selected option
	var objelement;         //stores the selected option element 
	var objsctrl = eval("document." + frmname + "." + strsctrlname);			//object of the selected drop down
	var arrmoveup;
	var intarrindex = 0;
	var intselectedidx;
	arrmoveup = new Array(); 
	
		if ( objsctrl.selectedIndex != -1 && objsctrl.selectedIndex > 0 )
		 {
			intselectedidx = objsctrl.selectedIndex;
			
			intarrindex = 0;
			if (intselectedidx > 0 )
			{
				for ( i = 0 ; i < objsctrl.length ; i ++ )
				{
					if ( i == intselectedidx - 1 )
					{
					arrmoveup[intarrindex]  = new Array();
					arrmoveup[intarrindex][0] = objsctrl[i+1].value;
					arrmoveup[intarrindex][1] = objsctrl[i+1].text;
					intarrindex = intarrindex + 1;
					}
					else if ( i == intselectedidx )
					{
					arrmoveup[intarrindex]  = new Array();
					arrmoveup[intarrindex][0] = objsctrl[i-1].value;
					arrmoveup[intarrindex][1] = objsctrl[i-1].text;
					intarrindex = intarrindex + 1;
					}
					else
					{
					arrmoveup[intarrindex]  = new Array();
					arrmoveup[intarrindex][0] = objsctrl[i].value;
					arrmoveup[intarrindex][1] = objsctrl[i].text;
					intarrindex = intarrindex + 1;
					}
					
				}
				
				objsctrl.length = 0;
				
				for ( i = 0 ; i < arrmoveup.length ; i++ )
				{
					objsctrl.options[i] = new Option( arrmoveup[i][1] , arrmoveup[i][0] )
				}
				
				objsctrl.selectedIndex = intselectedidx - 1 ;
					
			}	
			
		}	

}

function funMoveDown(strsctrlname,frmname)
{
//this function is used to move the options one step below.

	var object;						//store the object of the new option element
	var intidx;						//store the index of the selected option
	var objelement;                 //stores the selected option element 
	
	var objsctrl = eval("document." + frmname + "." + strsctrlname);		//object of the selected drop down
	var arrmovedown;
	var intarrindex = 0;
	var intselectedidx;
	arrmovedown = new Array(); 
	
		if ( objsctrl.selectedIndex != -1  )
		 {
			intselectedidx = objsctrl.selectedIndex;
			
			intarrindex = 0;
			if (intselectedidx < objsctrl.length - 1 )
			{
				
				for ( i = 0 ; i < objsctrl.length ; i ++ )
				{
					if ( i == intselectedidx + 1 )
					{
					arrmovedown[intarrindex]  = new Array();
					arrmovedown[intarrindex][0] = objsctrl[i-1].value;
					arrmovedown[intarrindex][1] = objsctrl[i-1].text;
					intarrindex = intarrindex + 1;
					}
					else if ( i == intselectedidx )
					{
					arrmovedown[intarrindex]  = new Array();
					arrmovedown[intarrindex][0] = objsctrl[i+1].value;
					arrmovedown[intarrindex][1] = objsctrl[i+1].text;
					intarrindex = intarrindex + 1;
					}
					else
					{
					arrmovedown[intarrindex]  = new Array();
					arrmovedown[intarrindex][0] = objsctrl[i].value;
					arrmovedown[intarrindex][1] = objsctrl[i].text;
					intarrindex = intarrindex + 1;
					}
					
				}
				
				objsctrl.length = 0;
				
				for ( i = 0 ; i < arrmovedown.length ; i++ )
				{
					objsctrl.options[i] = new Option( arrmovedown[i][1] , arrmovedown[i][0] )
				}
				
				objsctrl.selectedIndex = intselectedidx + 1 ;
					
			}	
			
		}	
	

}