function validateLoginInput(){
		var userName = document.getElementById("loginForm:userName");
		var userPassword = document.getElementById("loginForm:userPassword");
		var errorString = document.getElementById("loginForm:errorString");
		
		if(!validateInputNull(userName,errorString,'!!User Name Required!!'))
			return false;
		
		if(!validateInputNull(userPassword,errorString,'!!Password Required!!'))
			return false;
		errorString.innerHTML = '';
		return true;
}

function TrimString(sInString) {
 	sInString = sInString.replace( /^\s+/g, "" );// strip leading
 	return sInString.replace( /\s+$/g, "" );// strip trailing
}
	
function validateInputNull(txtField,msgField,msg){
	if(TrimString(txtField.value)== ''){
			msgField.innerHTML = msg;
			txtField.value = '';
			return false;
	}
	return true; 
}

function validateEmailField(emailField,msgField,msg){
	var str = TrimString(emailField.value);
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	if (filter.test(str)){
		return true;
	}else{
		msgField.innerHTML = msg;
		return false;
	}

}

function confirmPassword(passwordField,confirmPasswordField,errorStringTxtField){

			if(passwordField.value != confirmPasswordField.value){
				errorStringTxtField.innerHTML = '!!Password Mismatch!!';
				return false;
			}else{
				return true;
			}
}

//**************************************************************************
// Register User
//**************************************************************************
function validateRegisterForm(){
	var userNameTxtField = document.getElementById("registerForm:userNameTxtField");	
	var userFullNameTxtField = document.getElementById("registerForm:userFullNameTxtField");	
	var userPasswordTxtField = document.getElementById("registerForm:userPasswordTxtField");	
	var userConfirmPasswordTxtField = document.getElementById("registerForm:userConfirmPasswordTxtField");	
	var userEmailTxtField = document.getElementById("registerForm:userEmailTxtField");	
	var userQuestionTxtArea = document.getElementById("registerForm:userQuestionTxtArea");	
	var userAnswerTxtArea = document.getElementById("registerForm:userAnswerTxtArea");	
	var errorStringTxtField = document.getElementById("registerForm:errorStringTxtField");	
	errorStringTxtField.innerHTML = '';
	if(!validateInputNull(userNameTxtField,errorStringTxtField,'**Username is a required field'))
		return false;
	if(!validateInputNull(userFullNameTxtField,errorStringTxtField,'**Full Name is a required field'))
		return false;
	if(!validateInputNull(userPasswordTxtField,errorStringTxtField,'**Password is a required field'))
		return false;
	if(!validateInputNull(userConfirmPasswordTxtField,errorStringTxtField,'**Confirm Password is a required field'))
		return false;
	if(!confirmPassword(userPasswordTxtField,userConfirmPasswordTxtField,errorStringTxtField))	
		return false;
	if(!validateInputNull(userEmailTxtField,errorStringTxtField,'**Email Address is a required field'))
		return false;
	if(!validateEmailField(userEmailTxtField,errorStringTxtField,'**Email Address not Valid'))	
		return false;
	if(!validateInputNull(userQuestionTxtArea,errorStringTxtField,'**Secret question is a required field'))
		return false;
	if(!validateInputNull(userAnswerTxtArea,errorStringTxtField,'**Secret answer is a required field'))
		return false;
	
	return true;
}
//**************************************************************************
// End Register User
//**************************************************************************
//**************************************************************************
// Check Auth Code
//**************************************************************************

var authCode = /\w{5}-\w{5}-\w{5}-\w{5}/
function validateCheckAuthCodeForm(){
	var authCodeText = document.getElementById("checkAuthForm1:authCodeText");
	var errorString = document.getElementById("checkAuthForm1:errorString");
	if(TrimString(authCodeText.value).length < 23){
		errorString.innerHTML = 'Authorization code is 23 characters length\n Format:XXXXX-XXXXX-XXXXX-XXXXX';
		return false;
	}
	if (!authCode.test(authCodeText.value)){
		errorString.innerHTML = 'Please check authorization code format:' + authCodeText.value + '<br/>Actual format: xxxxx-xxxxx-xxxxx-xxxxx';
		return false;
	}
	return true;	
}
	
//**************************************************************************
// End Check Auth Code
//**************************************************************************

//**************************************************************************
// Change Password
//**************************************************************************
function validatePasswordChangeForm(){
	var newPassword = document.getElementById("passwordChangeForm:newPasswordText");
	var confirmPassword = document.getElementById("passwordChangeForm:confirmPasswordText");
	var errorString = document.getElementById("passwordChangeForm:errorStringText");

	if(TrimString(newPassword.value)== ''){
		errorString.innerHTML = '!!Password Required!!';
		newPassword.value = '';
		return false;
	}
	if(newPassword.value != confirmPassword.value){
		errorString.innerHTML = '!!Password Mismatch!!';
		return false;
	}else{
		return true;
	}
}

//**************************************************************************
// End Change Password
//**************************************************************************
//**************************************************************************
// Update Profile
//**************************************************************************
function validateUpdateProfileForm(){
	var userFullNameTxtField = document.getElementById("updateProfileForm:userFullNameTxtField");	
	var userEmailTxtField = document.getElementById("updateProfileForm:userEmailTxtField");	
	var userQuestionTxtArea = document.getElementById("updateProfileForm:userQuestionTxtArea");	
	var userAnswerTxtArea = document.getElementById("updateProfileForm:userAnswerTxtArea");	
	var errorStringTxtField = document.getElementById("updateProfileForm:errorStringTxtField");	
	if(!validateInputNull(userFullNameTxtField,errorStringTxtField,'Full Name is a required field'))
		return false;
	if(!validateInputNull(userEmailTxtField,errorStringTxtField,'Email Address is a required field'))
		return false;
	
	if(!validateInputNull(userQuestionTxtArea,errorStringTxtField,'Secret question is a required field'))
		return false;
	if(!validateInputNull(userAnswerTxtArea,errorStringTxtField,'Secret answer is a required field'))
		return false;
	return true;
}
//**************************************************************************
// End Update Profile
//**************************************************************************
//**************************************************************************
// Forgot Password
//**************************************************************************

function validateForgotPasswordForm(){

	var emailIdAddressTxtField = document.getElementById("forgotPasswordForm:emailIdAddressTxtField");	
	var errorStringTxtField = document.getElementById("forgotPasswordForm:errorStringTxtField");	
	errorStringTxtField.innerHtml = '';
	if(!validateInputNull(emailIdAddressTxtField,errorStringTxtField,'Email Address is a required field'))
		return false;
	if(!validateEmailField(emailIdAddressTxtField,errorStringTxtField,'**Email Address not Valid'))	
		return false;	
	
	return true;
}
//**************************************************************************
// End Forgot Password
//**************************************************************************
//**************************************************************************
// AuthCode Edit, Create User, Edit User,Create Auth Code
//**************************************************************************
function modifySearchControlsState1(radioButton,vendorMenu,vendorSearchButton,vendorAddButton,userTypeHdnField,informationText){
	var vendorPickListDiv = document.getElementById("vendorPickListDiv");
	var infotable = document.getElementById("infotable");
	if(radioButton.value == 'Vendor'){
		vendorMenu.disabled = false;
		vendorSearchButton.disabled = false;
		vendorAddButton.disabled = false;		
		informationText.innerHTML = "***Use the Pick List to filter the available vendor-districts for the vendor.";
		vendorPickListDiv.style.display ='';
		infotable.style.height="10px";
	}else{
		vendorMenu.disabled = true;
		vendorSearchButton.disabled = true;	
		vendorAddButton.disabled = true;	
		if(radioButton.value == 'biloUser')
			informationText.innerHTML = "'Bilo User' has access to all the available vendor-district combinations.";
		else
			informationText.innerHTML = 
				"'Bilo Admin' has access to all vendor-district combinations as well as the administrative tasks. <br/><br/> Administrative Tasks include: <br /> 1. User Management <br /> 2. Authorization Codes Management";
			
		vendorPickListDiv.style.display ='none';
		infotable.style.height="200px";	
	}
	userTypeHdnField.value=radioButton.value;
}
function enableVendorControls(userTypeHdnField,vendorMenu,vendorSearchButton,vendorAddButton,informationText){
	var vendorPickListDiv = document.getElementById("vendorPickListDiv");
	var infotable = document.getElementById("infotable");
	if(userTypeHdnField.value == 'Vendor'){
		informationText.innerHTML = "***Use the Pick List to filter the available vendor-districts for the vendor.";
		vendorMenu.disabled = false;
		vendorSearchButton.disabled = false;
		vendorAddButton.disabled = false;
		vendorPickListDiv.style.display ='';
		infotable.style.height="10px";		
	}else{
		vendorMenu.disabled = true;
		vendorSearchButton.disabled = true;	
		vendorAddButton.disabled = true;	
		if(userTypeHdnField.value == 'biloUser')
			informationText.innerHTML = "'Bilo User' has access to all the available vendor-district combinations.";
		else
			informationText.innerHTML = "'Bilo Admin' has access to all vendor-district combinations as well as the administrative tasks. <br/><br/> Administrative Tasks include: <br /> 1. User Management <br /> 2. Authorization Codes Management";
		infotable.style.height="200px";
		vendorPickListDiv.style.display ='none';
	}
	
}


function openVendorList(){
	showPopWin("vendorList.jsp?searchVendor=N", 660,435,returnVendor);	
	return false;
}

function modifySearchControlsState(radioButton,vendorMenu,vendorSearchButton){
//	var vendorMenu = document.getElementById("authCodeEditForm:vendorMenu");
//	var vendorSearchButton = document.getElementById("authCodeEditForm:vendorSearchButton");
	if(radioButton.value == 'Search'){
		vendorMenu.disabled = false;
		vendorSearchButton.disabled = false;	
	}else{
		vendorMenu.disabled = true;
		vendorSearchButton.disabled = true;	
	}
}
/*
function addVendorsToSelect(vendorMenu,selectVendorListBox,finalVendorListBox){
	
	if(vendorMenu.disabled){
		clearSelectVendorListBox(selectVendorListBox);
		vendorMenu.disabled = false;
		for(var i=0; i < vendorMenu.length;i++){
			var vendorValue =vendorMenu[i].value;
			var vendorValueArray=vendorValue.split("|");
			for(var j = 0; j < vendorValueArray.length-1;j++){
				var vendorDistrictValue = vendorValueArray[j];
				addOption(selectVendorListBox,vendorDistrictValue , vendorDistrictValue);
			}
		}
		vendorMenu.disabled = true;
	}else{
		var vendorValue = vendorMenu[vendorMenu.options.selectedIndex].value;
		var vendorValueArray=vendorValue.split("|");
		for(var j = 0; j < vendorValueArray.length-1;j++){
			var vendorDistrictValue = vendorValueArray[j];	
			addOption(selectVendorListBox,vendorDistrictValue , vendorDistrictValue );
		}
	}	
	return false;
}
*/

function addVendorsToSelect(vendorMenu,selectVendorListBox,finalVendorListBox){
	
	if(vendorMenu.disabled){
		clearSelectVendorListBox(selectVendorListBox);
		vendorMenu.disabled = false;
		for(var i=0; i < vendorMenu.length;i++){
			var vendorValue =vendorMenu[i].value;
			//var vendorValueArray=vendorValue.split("|");
			//for(var j = 0; j < vendorValueArray.length-1;j++){
				//var vendorDistrictValue = vendorValueArray[j];
				addOption(selectVendorListBox,vendorValue , vendorValue);
			//}
		}
		vendorMenu.disabled = true;
	}else{
		var vendorValue = vendorMenu[vendorMenu.options.selectedIndex].value;
		//var vendorValueArray=vendorValue.split("|");
		//for(var j = 0; j < vendorValueArray.length-1;j++){
			//var vendorDistrictValue = vendorValueArray[j];	
			addOption(selectVendorListBox,vendorValue , vendorValue );
		//}
	}	
	return false;
}


function addOption(listBox,text,value){
	var optn = document.createElement("OPTION");
	optn.text = text;
	optn.value = value;
	listBox.options.add(optn);
}

function clearSelectVendorListBox(selectVendorListBox){
//	var selectVendorListBox = document.getElementById('authCodeEditForm:selectVendorListBox');
	for(var i =selectVendorListBox.length-1; i >=0 ;i--){
		selectVendorListBox.options[i] = null;
	}
	return false;
}

function removeVendors(selectVendorListBox,finalVendorListBox){
	//var selectVendorListBox = document.getElementById('authCodeEditForm:selectVendorListBox');
	//var finalVendorListBox = document.getElementById('authCodeEditForm:finalVendorListBox');
	moveOptions(finalVendorListBox,selectVendorListBox);
	return false;
}

function addVendors(selectVendorListBox,finalVendorListBox){
//	var selectVendorListBox = document.getElementById("authCodeEditForm:selectVendorListBox");
//	var finalVendorListBox = document.getElementById("authCodeEditForm:finalVendorListBox");
	moveOptions(selectVendorListBox,finalVendorListBox);	
	return false;
}

function moveOptions(theSelFrom, theSelTo)
{
  
  var selLength = theSelFrom.length;
  var selectedText = new Array();
  var selectedValues = new Array();
  var selectedCount = 0;
  
  var i;
  
  for(i=selLength-1; i>=0; i--)
  {
    if(theSelFrom.options[i].selected)
    {
      selectedText[selectedCount] = theSelFrom.options[i].text;
      selectedValues[selectedCount] = theSelFrom.options[i].value;
      deleteOption(theSelFrom, i);
      selectedCount++;
    }
  }
  
  for(i=selectedCount-1; i>=0; i--)
  {
    addOption(theSelTo, selectedText[i], selectedValues[i]);
  }
}

function deleteOption(theSel, theIndex)
{ 
  var selLength = theSel.length;
  if(selLength>0)
  {
    theSel.options[theIndex] = null;
  }
}

function validateAuthCodeEditForm(){
	var finalVendorListBox = document.getElementById("authCodeEditForm:finalVendorListBox");
	for(var i =finalVendorListBox.length-1; i >=0 ;i--){
		finalVendorListBox.options[i].selected = true;
	}
	return true;
}

function confirmDelete(){
	input_box=confirm("Are you sure you want to delete???");
	if (input_box==true){ 
		return true;
	}else{
		return false;
	}
}

function moveAllVendorSelect(selectVendorListBox,finalVendorListBox){
//	var selectVendorListBox = document.getElementById('authCodeEditForm:selectVendorListBox');
//	var finalVendorListBox = document.getElementById('authCodeEditForm:finalVendorListBox');
	for(var i =selectVendorListBox.length-1; i >=0 ;i--){
		selectVendorListBox.options[i].selected = true;
	}
	moveOptions(selectVendorListBox,finalVendorListBox);
	return false;
}

function moveAllFinalVendorSelect(selectVendorListBox,finalVendorListBox){
//	var selectVendorListBox = document.getElementById("authCodeEditForm:selectVendorListBox");
//	var finalVendorListBox = document.getElementById("authCodeEditForm:finalVendorListBox");
	for(var i =finalVendorListBox.length-1; i >=0 ;i--){
		finalVendorListBox.options[i].selected = true;
	}
	moveOptions(finalVendorListBox,selectVendorListBox);
	return false;
}
function checkPasswordForCreateUserForm(){
		var newPassword = document.getElementById("createUserForm:userPasswordTxtField");
		var confirmPassword = document.getElementById("createUserForm:userConfirmPasswordTxtField");
		var errorString = document.getElementById("createUserForm:txtErrorString");

		if(TrimString(newPassword.value)== ''){
			errorString.innerHTML = '!!Password Required!!';
			newPassword.value = '';
			return false;
		}
		if(newPassword.value != confirmPassword.value){
			errorString.innerHTML = '!!Password Mismatch!!';
			return false;
		}else{
			return true;
		}
	}

function validateSaveForCreateUserForm(){
	var userNameTxtField = document.getElementById("createUserForm:userNameTxtField");	
	var userFullNameTxtField = document.getElementById("createUserForm:userFullNameTxtField");	
	var userPasswordTxtField = document.getElementById("createUserForm:userPasswordTxtField");	
	var userEmailTxtField = document.getElementById("createUserForm:userEmailTxtField");	
	var userQuestionTxtArea = document.getElementById("createUserForm:txtSecretQuest");	
	var userAnswerTxtArea = document.getElementById("createUserForm:txtSecretAns");	
	var errorStringTxtField = document.getElementById("createUserForm:txtErrorString");
	var txtInfoString = document.getElementById("createUserForm:txtInfoString");
	errorStringTxtField.innerHTML = '';
	
 	txtInfoString = '';
	if(!validateInputNull(userNameTxtField,errorStringTxtField,'Username is a required field'))
		return false;
	if(!validateInputNull(userFullNameTxtField,errorStringTxtField,'Full Name is a required field'))
		return false;
	

	if(!validateInputNull(userEmailTxtField,errorStringTxtField,'Email Address is a required field'))
		return false;
	if(!validateEmailField(userEmailTxtField,errorStringTxtField,'Email Address is not valid'))
		return false;	
	if(!validateInputNull(userPasswordTxtField,errorStringTxtField,'Password is a required field'))
		return false;
	if(!checkPasswordForCreateUserForm())
		return false;	
	if(!validateInputNull(userQuestionTxtArea,errorStringTxtField,'Secret question is a required field'))
		return false;
	if(!validateInputNull(userAnswerTxtArea,errorStringTxtField,'Secret answer is a required field'))
		return false;
	
	
	
	var finalVendorListBox = document.getElementById("createUserForm:finalVendorListBox");
	for(var i =finalVendorListBox.length-1; i >=0 ;i--){
		finalVendorListBox.options[i].selected = true;
	}
	return true;
}

function validateSaveForEditUserForm(){
	var finalVendorListBox = document.getElementById("editUserForm:finalVendorListBox");
	for(var i =finalVendorListBox.length-1; i >=0 ;i--){
		finalVendorListBox.options[i].selected = true;
	}
	return true;
}
function validateSaveForCreateAuthCodeForm(){
	var finalVendorListBox = document.getElementById("createAuthCodeForm:finalVendorListBox");
	for(var i =finalVendorListBox.length-1; i >=0 ;i--){
		finalVendorListBox.options[i].selected = true;
	}
	return true;
}


//**************************************************************************
// AuthCode Edit, Create User, Edit User,Create Auth Code
//**************************************************************************
