/*********************************************************************
'***    Program: validateStateAgainstCountry()
'***    Type: Function
'***
'***    Function: true if state belongs to country, 
'***              false otherwise
'***    
'***    Parameters:
'***		strStateCode - state code
'***        strCountryID - country id
'***        strStateCodeCountryID - combination of statecode,contryid
'***                       value of the State Name,Country Code listbox
'***
'***    Returns: boolean
'***	
'***    Created by: sofiav
'***    Changed by: sofiav
'***    Last change: 01/11/01
'*********************************************************************/
function validateStateAgainstCountry(strStateCode, strCountryID, strStateCodeCountryID) {
	var blnResult = true;

	if (strStateCode) {
		if (strCountryID) {
			if (strStateCodeCountryID) {
				blnResult = (strStateCode + "," + strCountryID == strStateCodeCountryID);
			}
		}
	}
	return blnResult;
}

/*********************************************************************
    Function: validates State/Province/Country input
			  returns appropriate error message if invalid
    Parameters: strStateCode - state code (from States dropdown)
				strProvince  - province (from province textbox)
				strCountryID - country id (from Countries dropdown)
				strStateCodeCountryID - state/country combination (value of States dropdown)

    Returns:  text of the error message or empty string
    Remarks: 

    Created by: sofiav
    Changed by: sofiav
    Last change: 02/27/02
'*********************************************************************/
function getValidationErrorMessageForStateProvinceCountryCombination(strStateCode, strProvince, strCountryID, strStateCodeCountryID) {
	var blnThrowException = false;
	var strExceptionText  = "";
	
	// cannot leave country blank
	if (strCountryID == "0") {
		blnThrowException = true;
		strExceptionText = "Please enter your Country information.";
	}
	
	// cannot enter both state and province
	if (strStateCode != "" && strProvince != "") {
		blnThrowException = true;
		strExceptionText = "Cannot enter both state and province";
	}
	
	// if specified country is USA or Canada, force to make selection from states listbox
	if (!blnThrowException && ((strProvince != "" || strStateCode == "") && (strCountryID == "1" || strCountryID == "2"))) {
		blnThrowException = true;
		strExceptionText = "Please select a state from the states dropdown";
	}
	
	// do not allow to save if state does not belong to the country
	if (!blnThrowException && !validateStateAgainstCountry(strStateCode, strCountryID, strStateCodeCountryID)) {
		blnThrowException = true;
		strExceptionText = "Selected state does not belong to the selected country"
	}
	
	return strExceptionText;

}

/*********************************************************************
'***    Function: 
'***		function returns state code portion
'***        of the StateCode,CountryID combination
'***        (value of the StateName/CountryCode listbox)
'***
'***    Parameters: 
'***		strStateCodeCountryID - combination of StateCode,CountryID
'***
'***	Return: state code value
'***
'***
'***    Created by: sofiav
'***    Changed by: sofiav
'***    Last change: 01/11/01
'*********************************************************************/
function getStateCodeFromStateCountryCombination(strStateCodeCountryID) {
	var strResult = "";
	if (strStateCodeCountryID) {
		strResult = String(strStateCodeCountryID).substr(0,String(strStateCodeCountryID).indexOf(","))
	}
	return strResult;
}

/*********************************************************************
'***    Function: Returns min length of password for site
'***	
'***    Parameters: 
'***		none
'***
'***	Return: number
'***
'***
'***    Created by: dimab
'***    Changed by: dimab
'***    Last change: 02/28/02
'*********************************************************************/
function getMinPasswordLength() {
	return 4;
}
/*********************************************************************
'***    Function: Returns min length of login for site
'***	
'***    Parameters: 
'***		none
'***
'***	Return: number
'***
'***
'***    Created by: dimab
'***    Changed by: dimab
'***    Last change: 02/28/02
'*********************************************************************/
function getMinLoginLength() {
	return 4;
}
/*********************************************************************
'***    Function: Returns max length of password for site
'***	
'***    Parameters: 
'***		none
'***
'***	Return: number
'***
'***
'***    Created by: dimab
'***    Changed by: dimab
'***    Last change: 02/28/02
'*********************************************************************/
function getMaxPasswordLength() {
	return 50;
}
/*********************************************************************
'***    Function: Returns max length of login for site
'***	
'***    Parameters: 
'***		none
'***
'***	Return: number
'***
'***
'***    Created by: dimab
'***    Changed by: dimab
'***    Last change: 02/28/02
'*********************************************************************/
function getMaxLoginLength() {
	return 100;
}
/*********************************************************************
'***    Program: isAlphaAndDigitString(strToCheck) 
'***    Type: Function
'***
'***    Function: Tests a string for presence of alpha and digit chars only
'***
'***    Parameters: 
'***		strToCheck - sting to check for validity
'***
'***    Returns: True/False
'***    Remarks: none
'***
'***    Created by: Dimab
'***    Changed by: jeffl
'***    Last change: 03/01/02
'*********************************************************************/
function isAlphaAndDigitString(strToCheck) {
  var checkOK = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.@%*()#"';
  var checkStr = strToCheck;
  var allValid = true;
  for (i = 0;  i < checkStr.length;  i++) {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length) {
      allValid = false;
      break;
    }
  }
  return (allValid);
}
/*********************************************************************
'***    Program: isDigitString(strToCheck) 
'***    Type: Function
'***
'***    Function: Tests a string for presence of digit chars only
'***
'***    Parameters: 
'***		strToCheck - sting to check for validity
'***
'***    Returns: True/False
'***    Remarks: none
'***
'***    Created by: jeffl
'***    Changed by: jeffl
'***    Last change: 04/01/02
'*********************************************************************/
function isDigitString(strToCheck) {
  var checkOK = "0123456789.";
  var checkStr = strToCheck;
  var allValid = true;
  for (i = 0;  i < checkStr.length;  i++) {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length) {
      allValid = false;
      break;
    }
  }
  return (allValid);
}

/*********************************************************************
'***    Program: isStringValidForLogin(strToCheck) 
'***    Type: Function
'***
'***    Function: Tests if string contains characters restricted in login
'***
'***    Parameters: 
'***		strToCheck - sting to check for validity
'***
'***    Returns: True/False
'***    Remarks: none
'***
'***    Created by: sofiav
'***    Changed by: sofiav
'***    Last change: 05/02/03
'*********************************************************************/
function isStringValidForLogin(strToCheck) {
  var restrictedChars = '%*/\()_+=,;#"';
  var checkStr = strToCheck;
  var allValid = true;
  for (i = 0;  i < checkStr.length;  i++) {
    ch = checkStr.charAt(i);
    for (j = 0;  j < restrictedChars.length;  j++) {
    
      if (ch == restrictedChars.charAt(j)) {
		allValid = false;
        break;
      }
      
      if (!allValid) {
		break;
      }
    }
  }
  return (allValid);
}

/*********************************************************************
'***    Program: getCleanKeywords
'***    Type: Function
'***
'***    Function: Analized incoming string for signs of full text search syntax
'***		and attempts to fix syntax issues by adding "AND" operator if
'***		users do not specify full text search syntax.
'***
'***    Parameters: 
'***		strKeywords - string to analyze
'***
'***    Returns: String
'***    Remarks: none
'***
'***    Created by: Dimab
'***    Changed by: Dimab
'***    Last change: 8/7/2001
'*********************************************************************/
function getCleanKeywords( strDirtyKeywords ) {

	var sValue = strDirtyKeywords;
        sValue = " " + strDirtyKeywords;

        // remove leading and trailing spaces
	sValue = sValue.replace(/(^ *)|( *$)/g, "");

        // replace any number of spaces with just one space
	sValue = sValue.replace(/( +)/g, " ");

        if ((sValue.toLowerCase().indexOf(" and ") == -1) && (sValue.toLowerCase().indexOf(" or ") == -1) && (sValue.toLowerCase().indexOf(" near ") == -1)) {

		// no "OR"s and "AND"s
		if (sValue.indexOf('"') == -1)  {
	  
			// replace spaces with " and "
			sValue = sValue.replace(/ /g, " and ");
		}
		else {
			// replace expression in quotes+space with itself following "and "
			re = /(".*" )/g;
			sValue = sValue.replace(re, "$1and ");
		}
	}
	return sValue;
}
/*********************************************************************
'***    Program: validateDeleteItem
'***    Type: Sub
'***
'***    Function: confirms delete of an individual item
'***			  and redirects to the deleting url
'***
'***    Parameters: 
'***		strRedirectURL - URL for redirect
'***
'***    Returns: String
'***    Remarks: none
'***
'***    Created by: Dimab
'***    Changed by: Dimab
'***    Last change: 8/7/2001
'*********************************************************************/

function validateDeleteItem(strRedirectURL){
	
	if (!strRedirectURL || strRedirectURL == '') {
		alert('URL for redirect is not found');
		return;
	}
	if (confirm('Are you sure you want to delete the selected item?')) {
		self.document.FormDeleteItem.action = strRedirectURL;
		self.document.FormDeleteItem.submit();
	}
	return;
}



/*********************************************************************
'***    Program: validatePlaceOrder
'***    Type: Sub
'***
'***    Function: confirms placing an order 
'***			  and redirects to the place order url
'***
'***    Parameters: 
'***		strRedirectURL - URL for redirect
'***
'***    Returns: String
'***    Remarks: none
'***
'***    Created by: rburdan
'***    Changed by: rburdan
'***    Last change: 04/30/02
'*********************************************************************/

function validatePlaceOrder(strRedirectURL){
	
	if (!strRedirectURL || strRedirectURL == '') {
		alert('URL for redirect is not found');
		return;
	}
	if (confirm('Are you sure you would like to place this order?')) {
		self.document.FormDeleteItem.action = strRedirectURL;
		self.document.FormDeleteItem.submit();
	}
	return;
}


/*********************************************************************
'***    Program: validateCloneJob
'***    Type: Sub
'***
'***    Function: confirms placing an order 
'***			  and redirects to the place order url
'***
'***    Parameters: 
'***		strRedirectURL - URL for redirect
'***
'***    Returns: String
'***    Remarks: none
'***
'***    Created by: rburdan
'***    Changed by: rburdan
'***    Last change: 05/07/02
'*********************************************************************/

function validateCloneJob(strRedirectURL){
	
	if (!strRedirectURL || strRedirectURL == '') {
		alert('URL for redirect is not found');
		return;
	}
	if (confirm('Are you sure you would like to clone this job?')) {
		self.document.FormDeleteItem.action = strRedirectURL;
		self.document.FormDeleteItem.submit();
	}
	return;
}

/*********************************************************************
'***    Program:  validateAndSubmitBulkAction()
'***    Type: Procedure
'***
'***    Function: on View pages that contain FormBulk, validates input
'***
'***    Parameters: none
'***
'***    Returns: none
'***    Remarks: 
'***
'***    Created by: sofiav
'***    Changed by: sofiav
'***    Last change: 03/27/01
'*********************************************************************/
function validateAndSubmitBulkAction(){
	var blnSelectionMade       = false;
	//var strBulkOperation     = "";
	var strBulkOperationSelect = false;
	var strBulkOperationName   = "";
	var strOpenNewWindow       = false;
	
	with (document.FormBulk){
		//strBulkOperation = BulkOperationType.options[BulkOperationType.selectedIndex].value;
		switch (BulkOperationType.options[BulkOperationType.selectedIndex].value) {
			case "DeleteSelected":
				strBulkOperationSelect = true;
				strBulkOperationName = "delete";
				strOpenNewWindow = false;
				break;
			case "DeleteFilter":
				strBulkOperationSelect = false;
				strBulkOperationName = "delete";
				strOpenNewWindow = false;
				break;
			case "DisableSelected":
				strBulkOperationSelect = true;
				strBulkOperationName = "disable";
				strOpenNewWindow = false;
				break;
			case "DisableFilter":
				strBulkOperationSelect = false;
				strBulkOperationName = "disable";
				strOpenNewWindow = false;
				break;
			case "EnableSelected":
				strBulkOperationSelect = true;
				strBulkOperationName = "enable";
				strOpenNewWindow = false;
				break;
			case "EnableFilter":
				strBulkOperationSelect = false;
				strBulkOperationName = "enable";
				strOpenNewWindow = false;
				break;
			case "ExportSelected":
				strBulkOperationSelect = true;
				strBulkOperationName = "export";
				strOpenNewWindow = true;
				break;
			case "ExportFilter":
				strBulkOperationSelect = false;
				strBulkOperationName = "export";
				strOpenNewWindow = true;
				break;

		}
		if (strBulkOperationSelect){
			
			if (elements.length > 0) {
				for (var i=0; i < elements.length; i++) {
					if (elements[i].type == "checkbox") {
						if (elements[i].checked) {
							blnSelectionMade = true;
							break;
						}
					}
				}
			}
			if (!blnSelectionMade) {
				alert("Please select at least one item.")
				return false;
			}

		   	if (confirm('Are you sure you want to ' + strBulkOperationName + ' the selected items?')) {
				//Form name on calling page must be named "FormBulk"
				if (strOpenNewWindow) {
					target = "NewWindow";
				}
				submit();
			}
		}
		else{
		   	if (confirm('Are you sure you want to ' + strBulkOperationName + ' all items that\nqualify the filter condition?\n\nNOTE: This action can ' + strBulkOperationName + ' all items depending on your filter.')) {
				//Form name on calling page must be named "FormBulk"
				if (strOpenNewWindow) {
					target = "NewWindow";
				}
				submit();
			}
		}
	}
}
/*********************************************************************
'***    Program: validatePublishCompanyProfile
'***    Type: Sub
'***
'***    Function: confirms publish/un-publish company profile request
'***			  and redirects to the publishing/unpublishing url
'***
'***    Parameters: 
'***		strRedirectURL - URL for redirect
'***        blnUnpublish   - optional parameter, indicates un-publish request
'***
'***    Returns: String
'***    Remarks: none
'***
'***    Created by: sofiav
'***    Changed by: sofiav
'***    Last change: 05/01/02
'*********************************************************************/

function validatePublishCompanyProfile(strRedirectURL, blnUnpublish){
	var strActionName = '';
	
	strActionName = blnUnpublish ? 'un-publish' : 'publish';
	if (!strRedirectURL || strRedirectURL == '') {
		alert('URL for redirect is not found');
		return;
	}
	if (confirm('Are you sure you want to ' + strActionName + ' the selected company profile?')) {
		self.document.FormPublish.action = strRedirectURL;
		self.document.FormPublish.submit();
	}
	return;
}


/*********************************************************************
'***    Program: ConfirmJobSlotStatusChange
'***    Type: Sub
'***
'***    Function: confirms delete of an individual item
'***			  and redirects to the deleting url
'***
'***    Parameters: 
'***		strRedirectURL - URL for redirect
'***
'***    Returns: String
'***    Remarks: none
'***
'***    Created by: rburdan
'***    Changed by: rburdan
'***    Last change: 6/11/02
'*********************************************************************/

function ConfirmJobSlotStatusChange(strRedirectURL,strAction){
	
	if (!strRedirectURL || strRedirectURL == '') {
		alert('URL for redirect is not found');
		return;
	}
	if (confirm('Are you sure you want to ' + String(strAction).toLowerCase() + ' job slot?')) {
		self.document.FormDeleteItem.action = strRedirectURL;
		self.document.FormDeleteItem.submit();
	}
	return;
}

/*********************************************************************
'***    Program: validateEmail()
'***    Type: Sub
'***
'***    Function: validates an email address and returns result
'***
'***    Parameters: 
'***		strEmail - email to validate
'***
'***    Returns: String
'***    Remarks: none
'***
'***    Created by: dimab
'***    Changed by: dimab
'***    Last change: 6/28/02
'*********************************************************************/
function validateEmail( strEmail ) {
	var strEmailString = new String(strEmail);
	return (strEmailString.indexOf("@")!= -1 && strEmailString.indexOf(".") != -1);
}

/*********************************************************************
'***    Program: validateExtendAgentExpirationDate
'***    Type: Sub
'***
'***    Function: confirms extend of an individual agent
'***			  and redirects to the extending url
'***
'***    Parameters: 
'***		strRedirectURL - URL for extending
'***
'***    Returns: String
'***    Remarks: calling page must have FormExtendExpirationDate form
'***
'***    Created by: sofiav
'***    Changed by: sofiav
'***    Last change: 6/11/02
'*********************************************************************/
function validateExtendAgentExpirationDate(strRedirectURL){
		
		if (!strRedirectURL || strRedirectURL == '') {
			alert('URL for redirect is not found');
			return;
		}
		if (confirm('Are you sure you want to extend the selected agent?')) {
			self.document.FormExtendExpirationDate.action = strRedirectURL;
			self.document.FormExtendExpirationDate.submit();
		}
		return;
	}

/*********************************************************************
'***
'***    Parameters: 
'***		strRedirectURL - URL for extending
'***
'***    Returns: String
'***    Remarks: calling page must have FormExtendExpirationDate form
'***
'***    Created by: sofiav
'***    Changed by: rburdan
'***    Last change: 8/12/03
'*********************************************************************/
function validateExpiredExpirationDate(strRedirectURL){
		
		if (!strRedirectURL || strRedirectURL == '') {
			alert('URL for redirect is not found');
			return;
		}
		if (confirm('Are you sure you want to deactivate the selected agent?')) {
			self.document.FormExtendExpirationDate.action = strRedirectURL;
			self.document.FormExtendExpirationDate.submit();
		}
		return;
	}

/*********************************************************************
'***    Program: canDeleteCompanyProfile
'***    Type: Function
'***
'***    Function: confirms delete of a company profile
'***
'***    Parameters: none
'***		
'***
'***    Returns: boolean
'***    Remarks: none
'***
'***    Created by: sofiav
'***    Changed by: sofiav
'***    Last change: 8/7/2001
'*********************************************************************/

function canDeleteCompanyProfile(){
	return confirm('One or more jobs are attached to this company profile.\nThose jobs will be detached from this profile once deleted. \nDo you wish to proceed?');
}

/*********************************************************************
'***    Program: canBulkDeleteCompanyProfiles
'***    Type: Function
'***
'***    Function: confirms bulk delete of company profiles
'***
'***    Parameters: none
'***
'***    Returns: boolean
'***    Remarks: none
'***
'***    Created by: sofiav
'***    Changed by: sofiav
'***    Last change: 8/7/2001
'*********************************************************************/

function canBulkDeleteCompanyProfiles(blnFilter){
	var strText = "";
	if (!blnFilter)
	{
		strText  = 'Jobs may be attached to one or more of the selected company profiles. \n';
		strText += 'Those jobs will be detached from this profile once deleted. \n';
		strText += 'Do you wish to proceed?';
	}
	else
	{
	    strText  = 'Jobs may be attached to one or more of the company profiles that qualify the filter. \n';
	    strText += 'Those jobs will be detached from this profile once deleted. \n';
		strText += 'Do you wish to proceed?\n\n';
		strText += 'NOTE: This action can delete all company profiles depending on your filter.'
	}
	return confirm(strText);
}

/*********************************************************************
'***    Program: validateBulkActionCompanyProfiles
'***    Type: Function
'***
'***    Function: confirms bulk delete of company profiles
'***
'***    Parameters: none
'***
'***    Returns: boolean
'***    Remarks: none
'***
'***    Created by: sofiav
'***    Changed by: sofiav
'***    Last change: 8/7/2001
'*********************************************************************/
function validateBulkActionCompanyProfiles() {
	var strBulkOperation = "";
	var blnSelectionMade = false;

	with (document.FormBulk)
	{
		strBulkOperation = BulkOperationType.options[BulkOperationType.selectedIndex].value;
		if (strBulkOperation == "DeleteSelected" || strBulkOperation == "DeleteFilter") 
		{
			if (strBulkOperation == "DeleteSelected")
			{
				if (elements.length > 0) 
				{
					for (var i=0; i < elements.length; i++) 
					{
						if (elements[i].type == "checkbox")
						{
							if (elements[i].checked) 
							{
								blnSelectionMade = true;
								break;
							}
						}
					}
				}
				if (!blnSelectionMade) 
				{
					alert("Please select at least one item.")
					return false;
				}

			}
			if (canBulkDeleteCompanyProfiles(strBulkOperation == "DeleteFilter"))
			{
				submit();
			}
		}
		else
		{
			validateAndSubmitBulkAction();
		}
	}

}

/*********************************************************************
'***    Program: validateBulkActionCompanyProfiles
'***    Type: Function
'***
'***    Function: confirms bulk delete of company profiles
'***
'***    Parameters: none
'***
'***    Returns: boolean
'***    Remarks: none
'***
'***    Created by: sofiav
'***    Changed by: sofiav
'***    Last change: 8/7/2001
'*********************************************************************/
function validateBulkActionEmployerInboxItems() {
	var strBulkOperation = "";
	var blnSelectionMade = false;
	var strCheckBoxName  = "";

	with (document.FormBulk)
	{
		strBulkOperation = BulkOperationType.options[BulkOperationType.selectedIndex].value;
		if (strBulkOperation == "DeleteSelectedItems" || strBulkOperation == "DeleteSelectedReferences") 
		{
			strCheckBoxName = (strBulkOperation == "DeleteSelectedItems") ? "blkInboxItemID" : "blkInboxItemReferenceID";
			if (elements.length > 0) 
			{
				for (var i=0; i < elements.length; i++) 
				{
					if (elements[i].type == "checkbox" && elements[i].name == strCheckBoxName)
					{
						if (elements[i].checked) 
						{
							blnSelectionMade = true;
							break;
						}
					}
				}
			}
			if (!blnSelectionMade) 
			{
				if (strBulkOperation == "DeleteSelectedItems") {
					alert("Please select at least one item.")
				}
				else {
					alert("Please select at least one related job.")
				}
				return false;
			}
			else 
			{
				if (confirm('Are you sure you want to delete the selected ' + ((strBulkOperation == "DeleteSelectedItems") ? "items" : "references") + '?')) {
				//Form name on calling page must be named "FormBulk"
				submit();
			}

			}
		}
		else
		{
			validateAndSubmitBulkAction();
		}
	}

}