//Javascript for IFRMonitoring.US
//Last Maintained by Brian R. Cline 
	String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g,""); };
	
	function ChangeCheckBoxColor(idToChange) {
		$('#' + idToChange).css("background-color","green");	
		$('#' + idToChange + ':checked').css("background-color","green");
	}
	
	function ClearErrorMessage() {
		$('#errorMessage').html("").css("display","none");
	}
	
	function ClearForm() {
		$(':input').not(':button, :submit, :reset, :hidden') .val('') .removeAttr('checked').removeAttr('selected');
		$("textarea").val("");
		$("select").val("");
		ClearErrorMessage();
	}
	
	function DisplayErrorMessage(message) {
		$('#errorMessage').html(message.trim()).css("display","block");
	}
	
	function getForgotPassResponse() {
		$.get("includes/php/ajax/emailpassword.php",
				{email:  $('#userEmail').val()}, 
				function(data) { 
					setErrorMessage("",data,"Ok","Ok",function() { closeErrorMessage();});	
					showErrorMessage();
					$('#passMessage').html(data);
				});
	}
	
	function isEmailValid(e) {
		var r = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}/i;
		
		if (isSet(e)) {
			if (r.test(e)) {
				e = e.toLowerCase();
				return true;
			} else if (e.length == 0){
				DisplayErrorMessage('<b>An email address is required! </b>');
				return false;
			} else {
				DisplayErrorMessage('<b>An invalid email has been entered </b>');
				return false;
			}
		}
	}

	function isValidFirstName(e) {
		var	regex = /^[A-Z]+[a-zA-Z-]*$/;
				
		if (isSet(e) && e.value.trim().length >= 2 ) {
			MakeFirstCharacterUpperCase(e);
			
			if (regex.test(e.value)) {
				ClearErrorMessage();
			} else {
				DisplayErrorMessage("<b>The first name should include only letters, and hyphens. </b>");
			}
		}
		else {
			DisplayErrorMessage('<b>The first name must be at least two characters long. </b>');
		}
	}
	
	function isValidSurname(e) {
		var	regex = /^[A-Za-z-]*$/;
		
		//Minimum 2 Characters to account for Chinese lastnames like Yu
		if (IsMinimumCharacters(e,2)) {
			MakeFirstCharacterUpperCase(e);
			
			if (regex.test(e.value)) {
				ClearErrorMessage();
			} else {
				DisplayErrorMessage("<b>The last name should include only letters, and hyphens. </b>");
			}
		}
		else {
			DisplayErrorMessage('<b>The last name must be at least two characters long.</b>');
		}
	}
	
	function isValidTelephone(e) {
		var regex = /^[0-9-]*$/;
		
		if (isSet(e) & e.value.trim().length > 0) {
			e.value = e.value.trim();
			
			if (regex.test(e.value)) {
				ClearErrorMessage();
			} else {
				DisplayErrorMessage("<b>" + e.value.trim() + " is not a valid phone number </b>");
			}
		}
	}
	
	function IsMinimumCharacters(dom, minimumNumChars) {	
		if (isSet(dom) & isSet(minimumNumChars)) {
			if (dom.value.length < minimumNumChars) {
				return false;
			} else {
				return true;
			}
		} else {
			return false;
		}
	}
	
	function MakeFirstCharacterUpperCase(e) {
		if (isSet(e) && e.value.length) {
			var strToChange = e.value;
			strToChange = strToChange.replace(strToChange.charAt(0), strToChange.charAt(0).toUpperCase());
			e.value = strToChange.trim();
			strToChange = null;
		}
	}
	
	function ChangePasswordFormVerify() {
		var eOriginalPassword = $('#originalPassword')[0];
		var eNewPassword = $('#newPassword')[0];
		var eNewPasswordVerify = $('#newPasswordVerify')[0];
		var eUserName = $('#userName')[0];

		eOriginalPassword.value = eOriginalPassword.value.trim();
		eNewPassword.value = eNewPassword.value.trim();
		eNewPasswordVerify.value = eNewPasswordVerify.value.trim();
		eUserName.value = eUserName.value.trim();
		
		if (eNewPassword.value == eNewPasswordVerify.value) {
			if (eOriginalPassword.value != eNewPassword.value && eOriginalPassword.value != eNewPasswordVerify.value) {		
				$('#passMessage').html("Currently contacting server ");
				$.post("includes/php/ajax/authenticatedpasswordchange.php",{userName: eUserName.value,oldPassword: eOriginalPassword.value, newPassword: eNewPassword.value},
						function(data){
							$('#passMessage').html("Loading ... "); 
							$('#passMessage').html(data);
							setErrorMessage("",	data, "Ok","Ok", function(){closeErrorMessage();});
							showErrorMessage();
						}
				);
			} else {
				var ErrorMessage = "";
				
				if (isEmptyText(eOriginalPassword) || isEmptyText(eNewPassword) || isEmptyText(eNewPasswordVerify)) {
					ErrorMessage = "<p><strong>It appears that at least one of the password fields has been left blank. Please confirm all of the required password fields have been completed.</strong></p>";
				} else {
					ErrorMessage = "<p><strong>It appears that the new password is trying to be set to the current password. Please change the new password something other than the old password.</strong><p>";
				}
					
				setErrorMessage("Oops an Error has Occured. ",ErrorMessage,"Ok","Ok",function() { 
					closeErrorMessage(); 
				});
					
				showErrorMessage();
			}	
		} else {
			setErrorMessage("Oops an Error has Occured. ",
							"<strong>It appears that the new password and the password confirmation text do not match. Please confirm these fields match before submitting.</strong>",
							"Ok","Ok",function() { closeErrorMessage(); });		
			showErrorMessage();
		}
	} 

function RequestTrainingVerify() {
		var email = $('#email')[0];
		var fullName = $('#userFullName')[0];
		var firstname = $('#userFirstName')[0];
		var lastname = $('#userLastName')[0];
		var trgType = $('#trainingType')[0];
		var comments = $('#userComments')[0];
		var tdate = $('#trainingDate')[0];
		var telephone = $('#telephone')[0].value;
		
		 setCursorBusy();
		
	//isDropDownSelected(trgType) &&
		if (!isEmptyText(comments)) {		
			$.ajax({
			   type: "POST", 
			   url: "includes/php/ajax/authenticatedrequesttraining.php",
			   data: 'userID=' + $('#userID').val() + '&userName='+ email.value + '&fullname=' + fullName.value +'&userLastName=' + lastname.value + '&userFirstName=' + firstname.value + '&training=' + trgType.value + '&comments=' + comments.value + '&date=' + tdate.value + '&telephone=' + telephone,
			   success: function(msg){
					$('#response').html(msg);				
					setErrorMessage("",msg,"Ok","Ok",function(){closeErrorMessage();});
					showErrorMessage();
				}
			});
			
		} else {
			var s ='';
			
			if (isEmptyText(firstname)) {
				s = " First name cannot be blank or invalid. Please ensure you have provided a first name. <br />";
			}
			
			if (isEmptyText(firstname)) {
				s += " Last name cannot be blank or invalid. Please ensure you have provided a last name. <br />";
			}
			
			if (!isEmailValid(email)) {
				s += 'An Email cannot be blank or invalid. Please ensure a valid and nonblank email is submitted. <br />';
			}
			 
			 /*if (!isDropDownSelected(trgType)) {
				s = s + 'You must select a training type. <br />';
			 }*/
			 
			if (isEmptyText(comments)) {
				s += 'Please ensure that you include comments with your request regarding the issue you need training or assistance with.<br />';
			}
			 
			if (isEmptyText(tdate)) {
				s += ' The date field cannot be blank. Please ensure that you select a valid date. <br />'; 
			}
			 
			$('#response').html("<p><strong>Oops an Error has Occured</strong><br />" + s + "</p>");
			setErrorMessage("Oops an Error has Occured",s,"Ok","Ok",function() { 
									closeErrorMessage(); 
								});
							
			showErrorMessage();
		}
		
		setCursorNormal();
	}
	
	function getClientResponse(contactId) {
		$.get("includes/php/ajax/authenticatedgetStaff.php",{id:contactId}, function(data) {
			if (data.trim().length > 0 ) {
				var str= data.split('|');
				$('#dlResponse').html('Processing server return');

				if (str.length > 0) {
					$('#id').val(str[0]);
					$('#firstName').val(str[1].trim());
					$('#lastName').val(str[2].trim());
					$('#title').val(str[3].trim());
					$('#email').val(str[4].trim());
					$('#telephone').val(str[5].trim());
					$('#ext').val(str[6].trim());
					$('#fax').val(str[7].trim());
					$('#cell').val(str[8].trim());
					$('#directLine').val(str[9].trim());
					$('#alternatePhone').val(str[10].trim());
					
					if (str[11].trim() != "0") {
						$('#enabled').attr('checked', true);
					} else {
						$('#enabled').attr('checked', false);
					}
							
					$('#dlResponse').html('');
				}
			} else {
				$('#dlResponse').html("Oops the server didn't return an expected value");
				setErrorMessage("Oops an Error has Occured",$('#dlResponse').html(),"OK","Cancel",function() {closeErrorMessage();});
				showErrorMessage();
			}
		});			
	}

function formCareerVerify() {
	var message = '';

	if (!$('#FirstName').val()) {
		message += 'First Name cannot be empty. <br />';
	}
	
	if (!$('#LastName').val()){
		message +='Last Name cannot be empty. <br />';
	}
	
	if (!$('#EmailAddress').val()) {
		message +='Email cannot be empty. <br />';
	}
	
	if (!$('#Telephone').val()) {
		message +='Telephone cannot be empty. <br />';
	}
	
	if (!$('#Address1').val()) {
		message +='Address1 cannot be empty. <br />';
	}
	
	if (!$('#City').val()) {
		message +='City cannot be empty. <br />';
	}
	
	if (!$('#State').val()) {
		message +='State/Province cannot be empty. <br />';
	}
	
	if (!$('#Zip').val()) {
		message +='ZIP/Postal Code cannot be empty. <br />';
	}
	
	if (!$('#Country').val()) {
		message +='Country cannot be empty. <br />';
	}
	
	if (!$('#DesiredCountry').val()) {
		message += 'Your desired working location cannot be empty. <br />';
	}
	
	if (message.trim().length) {
		setErrorMessage("Oops an Error has Occured. ",message,"Ok","Ok",function() { 
			closeErrorMessage(); 
		});
			
		showErrorMessage();
		setCursorNormal();
		return false;
	}
}

function windowOpener(winUrl,winWidth,winHeight,winName,winX,winY,winScrollbars,winLocation,winStatus,winPersonalbar,winResizable,winToolbar,winMenubar) {
	// set defaults
	if (!winWidth) { winWidth = 415 ;}
	if (!winHeight) { winHeight = 450 ;}
	if (!winName) {winName = 'popupWindow' ;}
	if (!winX) { winX = 0 ;}
	if (!winY) { winY = 0 ;}
	if (!winScrollbars) { winScrollbars = "no";}
	if (!winLocation) { winLocation = "no";}
	if (!winStatus) { winStatus = "no"; }
	if (!winPersonalbar) { winPersonalbar = "no";}
	if (!winResizable) { winResizable = "no";}
	if (!winToolbar) { winToolbar = "no";}
	if (!winMenubar) { winMenubar = "no";}

	// open pop-up window
	var windowName  = window.open(winUrl,winName,"width=" + winWidth + ",height=" + winHeight + ",location=" + winToolbar + ",status=" + winStatus + ",scrollbars=" + winScrollbars);
	windowName.focus();
	return false;
}