/**
 * PURPOSE:       Submits login form.
 *                If either login or password is missing, or entered information does not 
 *                correspond to a valid user in the database, displays an error message.
 * PARAMETERS:    form_name            - name of the form
 *                login_elt_name       - name of the login element
 *                password_elt_name    - name of the password element
 *                error_elt_name       - name of the HTML element to display the error message
 *                target_path          - path to the installation of the core components. The form will be submitted to
 *                                       the file login/login_handler.php inside this path.
 *                success_redirect_url - redirect to this url on successful login
 * RETURN VALUE:  true - if both login and password are entered
 *                false - otherwise
 * REVISIONS:     02/26/2006 - created
 *                04/22/2009 - updated
 */
function submit_login(form_name, login_elt_name, password_elt_name, error_elt_name, target_path, success_redirect_url) {
  if (!validate_login(form_name, login_elt_name, password_elt_name, error_elt_name)) return;
	
  var o_form
  var o_login_elt
	var o_password_elt
	var o_error_elt
	
  o_form = document.forms[form_name]
  if (!o_form) return false
  o_login_elt = o_form[login_elt_name]
  if (!o_login_elt) return false
  o_password_elt = o_form[password_elt_name]
  if (!o_password_elt) return false
	if (error_elt_name != "") {
		o_error_elt = document.getElementById(error_elt_name)
		if (o_error_elt) {
		  o_error_elt.innerHTML = ""
			o_error_elt.style.display = "none"
		}
	}
	
	var post_data = "login=" + o_login_elt.value  + "&password=" + o_password_elt.value

	var handleSuccess = function(o){ 
	    var result_string = ""
			var result_arr
			var o_error_elt
			
	    if(o.responseText !== undefined) { 
			  /*
	        div = "Transaction id: " + o.tId; 
	        div += "\nHTTP status: " + o.status; 
	        div += "\nStatus code message: " + o.statusText; 
	        div += "\nHTTP headers: \n" + o.getAllResponseHeaders; 
	        div += "\nPHP response: " + o.responseText; 
	        div += "\nArgument object: " + o.argument; 
					alert(div)
				*/
				result_string = o.responseText.toString().replace(/\n/gi,'');
				// alert(result_string)
				result_arr = result_string.split(String.fromCharCode(1))
				if ((result_arr[0] == "0") || (result_arr[0] == "-1")) {
					if (error_elt_name != "") {
						o_error_elt = document.getElementById(error_elt_name)
						if (o_error_elt) {
						  o_error_elt.innerHTML = result_arr[1]
							o_error_elt.style.display = ""
						}
					}
					o_login_elt.focus()
				} else {
				  if (success_redirect_url != "") {
					  if (success_redirect_url.substr(0,11) == "javascript:") { 
							eval(success_redirect_url.substr(11))
						} else {
					  	location.href  = decode_hidden_string_2(success_redirect_url)
						}
					}
				}
	    } 
	} 
	
	var handleFailure = function(o) {
	  
	}
	
	var callback = 
	{ 
	  success:handleSuccess, 
	  failure: handleFailure, 
	  argument: [] 
	}; 
	
	var request = YAHOO.util.Connect.asyncRequest('POST', target_path + 'login/login_handler.php', callback, post_data); 
}

/**
 * PURPOSE:       Checks whether login and password are entered for the login form.
 *                If either one is missing, displays an error message.
 * PARAMETERS:    form_name          - name of the form
 *                login_elt_name     - name of the login element
 *                password_elt_name  - name of the password element
 *                error_elt_name     - name of the HTML element to display the error message
 * RETURN VALUE:  true - if both login and password are entered
 *                false - otherwise
 * REVISIONS:     02/26/2006 - created
 *                04/22/2009 - updated
 */
function validate_login(form_name, login_elt_name, password_elt_name, error_elt_name) {
  var o_form
  var o_login_elt
	var o_password_elt
  var o_error_elt
	
	if (form_name == "") return false
	if (login_elt_name == "") return false
	if (password_elt_name == "") return false
	
  o_form = document.forms[form_name]
  if (!o_form) return false
  
  o_login_elt = o_form[login_elt_name]
  if ( (!o_login_elt) || (o_login_elt.value.length == 0) ) {
	  if (error_elt_name != "") {
		  o_error_elt = document.getElementById(error_elt_name)
 	    if (o_error_elt) {
			  o_error_elt.innerHTML = '<span class="login_error">Please enter your account number.</span>'
				o_error_elt.style.display = ""
			}
	  }
    if (o_login_elt) o_login_elt.focus()
    return false
  }

  o_password_elt = o_form[password_elt_name]
  if ( (!o_password_elt) || (o_password_elt.value.length == 0) ) {
	  if (error_elt_name != "") {
		  o_error_elt = document.getElementById(error_elt_name)
 	    if (o_error_elt) {
			  o_error_elt.innerHTML = '<span class="error">Please enter your password.</span>'
				o_error_elt.style.display = ""
			}
		}
    if (o_password_elt) o_password_elt.focus()
    return false
  }

  return true 
}

/**
 * PURPOSE:       Submits request to retrieve forgotten password.
 *                If login/account info/email address is missing, or entered information does not 
 *                correspond to a valid user in the database, displays an error message.
 *                Otherwise displays a message that instructions to reset password have been sent 
 *                to the specified email address.
 * PARAMETERS:    form_name           - name of the form
 *                login_elt_name      - name of the login element
 *                error_elt_name      - name of the HTML element to display the error or info message
 *                target_path         - path to the installation of the core components. The form will be submitted to
 *                                      the file login/retrieve_password.php inside this path. 
 *                from_email          - email address the retrieve password information should be sent from
 *                change_password_url - url of the change password form. Email will contain link to this url
 *                subject             - subject of the email
 * RETURN VALUE:  true - if both login and password are entered
 *                false - otherwise
 * REVISIONS:     02/26/2006 - created
 *                04/22/2009 - updated
 */
function submit_retrieve_password(form_name, login_elt_name, error_elt_name, target_path, from_email, change_password_url, subject) {
  if (!validate_retrieve_password(form_name, login_elt_name, error_elt_name)) return;
	
  var o_form
  var o_login_elt
	var o_error_elt
	
  o_form = document.forms[form_name]
  if (!o_form) return false
  o_login_elt = o_form[login_elt_name]
  if (!o_login_elt) return false
	if (error_elt_name == "") return false
	o_error_elt = document.getElementById(error_elt_name)
	if (!o_error_elt) return false

  o_error_elt.innerHTML = ""
	o_error_elt.style.display = "none"
	
	var post_data = "login=" + o_login_elt.value  + "&fromemail=" + from_email + "&changepwdurl=" + change_password_url + "&subject=" + subject

	var handleSuccess = function(o){ 
	    var result_string = ""
			var result_arr
			var o_error_elt
			
	    if (o.responseText !== undefined) { 
				if (error_elt_name != "") {
					o_error_elt = document.getElementById(error_elt_name)
					if (o_error_elt) {			  
						/*
						div = "Transaction id: " + o.tId; 
						div += "\nHTTP status: " + o.status; 
						div += "\nStatus code message: " + o.statusText; 
						div += "\nHTTP headers: \n" + o.getAllResponseHeaders; 
						div += "\nPHP response: " + o.responseText; 
						div += "\nArgument object: " + o.argument; 
						alert(div)
						*/
						result_string = o.responseText.toString().replace(/\n/gi,'');
						result_arr = result_string.split(String.fromCharCode(1))
						if ((result_arr[0] == "0") || (result_arr[0] == "-1")) {
							o_error_elt.innerHTML = result_arr[1]
							o_error_elt.style.display = ""
							o_login_elt.focus()
						} else {
							o_error_elt.innerHTML = result_arr[1]
							o_error_elt.style.display = ""
						}
					}
				}
	    } 
	} 
	
	var handleFailure = function(o) {

	}
	
	var callback = 
	{ 
	  success:handleSuccess, 
	  failure: handleFailure, 
	  argument: [] 
	}; 
	
	var request = YAHOO.util.Connect.asyncRequest('POST', target_path + 'login/retrieve_password.php', callback, post_data); 
}

/**
 * PURPOSE:       Checks whether login/account info/email address is entered for the login form.
 *                If it is missing, displays an error message.
 * PARAMETERS:    form_name          - name of the form
 *                login_elt_name     - name of the login element
 *                error_elt_name     - name of the HTML element to display the error 
 * RETURN VALUE:  true - if login/account info/email address is entered
 *                false - otherwise
 * REVISIONS:     04/22/2009 - created
 */
function validate_retrieve_password(form_name, login_elt_name, error_elt_name) {
  var o_form
  var o_login_elt
  var o_error_elt
	
	if (form_name == "") return false
	if (login_elt_name == "") return false
	
  o_form = document.forms[form_name]
  if (!o_form) return false
  
  o_login_elt = o_form[login_elt_name]
  if ( (!o_login_elt) || (o_login_elt.value.length == 0) ) {
	  if (error_elt_name != "") {
		  o_error_elt = document.getElementById(error_elt_name)
 	    if (o_error_elt) {
			  o_error_elt.innerHTML = '<span class="error">Please enter your login or email address.</span>'
				o_error_elt.style.display = ""
			}
	  }
    if (o_login_elt) o_login_elt.focus()
    return false
  }

  return true 
}


/**
 * PURPOSE:       Submits request to change password.
 *                If either new password ord its confirmation is not entered, or are not equal, or entered information does not 
 *                correspond to a valid user in the database, displays an error message.
 *                Otherwise displays a message that the password has been changed and displays a link to the login page.
 * PARAMETERS:    form_name             - name of the form
 *                new_password_name     - name of the login element
 *                password_confirm_name - name of the element containing the password confirmation
 *                user_field_name       - name of the field containing user ID/login
 *                password_field_name   - name of the field containing encrypted old password
 *                error_elt_name        - name of the HTML element to display the error or info message
 *                target_path           - path to the installation of the core components. The form will be submitted to
 *                                        the file login/change_password.php inside this path. 
 *                success_redirect_url  - redirect to this url on successful login
 * RETURN VALUE:  true - if both login and password are entered
 *                false - otherwise
 * REVISIONS:     04/22/2009 - created
 */
function submit_change_password(form_name, new_password_name, password_confirm_name, user_field_name, password_field_name, error_elt_name, target_path, success_redirect_url) {
  if (!validate_change_password(form_name, new_password_name, password_confirm_name, error_elt_name)) return;

  var o_form
	var o_user_elt
	var o_old_password_elt
  var o_new_password_elt
	var o_password_confirm_elt
	var o_error_elt
	
  o_form = document.forms[form_name]
  if (!o_form) return false
  o_user_elt = o_form[user_field_name]
  if (!o_user_elt) return false
  o_old_password_elt = o_form[password_field_name]
  if (!o_old_password_elt) return false
  o_new_password_elt = o_form[new_password_name]
  if (!o_new_password_elt) return false
  o_password_confirm_elt = o_form[password_confirm_name]
  if (!o_password_confirm_elt) return false
	if (error_elt_name == "") return false
	o_error_elt = document.getElementById(error_elt_name)
	if (!o_error_elt) return false

  o_error_elt.innerHTML = ""
	o_error_elt.style.display = "none"
	
	var post_data = "user_id=" + o_user_elt.value  + "&rnd=" + o_old_password_elt.value + "&new_password=" + o_new_password_elt.value + "&password_confirm=" + o_password_confirm_elt.value + "&success_url=" + success_redirect_url

	var handleSuccess = function(o){ 
	    var result_string = ""
			var result_arr
			var o_error_elt
			
	    if (o.responseText !== undefined) { 
				if (error_elt_name != "") {
					o_error_elt = document.getElementById(error_elt_name)
					if (o_error_elt) {			  
						/*
						div = "Transaction id: " + o.tId; 
						div += "\nHTTP status: " + o.status; 
						div += "\nStatus code message: " + o.statusText; 
						div += "\nHTTP headers: \n" + o.getAllResponseHeaders; 
						div += "\nPHP response: " + o.responseText; 
						div += "\nArgument object: " + o.argument; 
						alert(div)
						*/
						result_string = o.responseText.toString().replace(/\n/gi,'');
						result_arr = result_string.split(String.fromCharCode(1))
						if ((result_arr[0] == "0") || (result_arr[0] == "-1")) {
							o_error_elt.innerHTML = result_arr[1]
							o_error_elt.style.display = ""
							if (o_new_password_elt) o_new_password_elt.focus()
						} else {
							o_error_elt.innerHTML = result_arr[1]
							o_error_elt.style.display = ""
						}
					}
				}
	    } 
	} 
	
	var handleFailure = function(o) {

	}
	
	var callback = 
	{ 
	  success:handleSuccess, 
	  failure: handleFailure, 
	  argument: [] 
	}; 
	
	var request = YAHOO.util.Connect.asyncRequest('POST', target_path + 'login/change_password.php', callback, post_data); 
}

/**
 * PURPOSE:       Checks whether new password and a confirmation of the new password are entered and are equal.
 *                If not, displays an error message.
 * PARAMETERS:    form_name             - name of the form
 *                new_password_name     - name of the element containing the new password
 *                password_confirm_name - name of the element containing the password confirmation
 *                error_elt_name        - name of the HTML element to display the error 
 * RETURN VALUE:  true - if new password and a confirmation of the new password are entered and are equal
 *                false - otherwise
 * REVISIONS:     04/22/2009 - created
 */
function validate_change_password(form_name, new_password_name, password_confirm_name, error_elt_name) {
  var o_form
  var o_elt
	var o_error_elt
	var new_password
	var confirm_password
	
  o_form = document.forms[form_name]
  if (!o_form) return false

  o_elt = o_form[new_password_name]
  if ( (!o_elt) || (o_elt.value.length == 0) ) {
		o_error_elt = document.getElementById(error_elt_name)
		if (o_error_elt) {			  
	  	o_error_elt.innerHTML = '<span class="login_error">Please enter your new password.</span>'
			o_error_elt.style.display = ""
		}
    if (o_elt) o_elt.focus()
    return false
  }
  new_password = o_elt.value
	
  o_elt = o_form[password_confirm_name]
  if ( (!o_elt) || (o_elt.value.length == 0) ) {
		o_error_elt = document.getElementById(error_elt_name)
		if (o_error_elt) {			  
	  	o_error_elt.innerHTML = '<span class="login_error">Please confirm your password.</span>'
			o_error_elt.style.display = ""
		}
    if (o_elt) o_elt.focus()
    return false
  }
  confirm_password = o_elt.value
  if (confirm_password != new_password) {
		o_error_elt = document.getElementById(error_elt_name)
		if (o_error_elt) {			  
	  	o_error_elt.innerHTML = '<span class="login_error">Password confirmation failed.</span>'
			o_error_elt.style.display = ""
		}
    if (o_elt) o_elt.focus()
    return false
  }
  
  return true 
}
