function isEmptyText(e) {
	if (isSet(e)) {
		e.value = e.value.replace(/\s+$/,"");
		if (e.value.length > 0) {
			return false;
		} else {
			return true;
		}
	} else {
		return false;
	}
}

function isSet(variable) {
	if (typeof(variable) != 'undefined' && variable != null) {
		return true;
	} else {
		return false;
	}
}

function isMandatoryFieldEntered(e) {
	return !isEmptyText(e);
}

function setCursorBusy() {
	$('body').css('cursor', 'wait'); 
}

function setCursorNormal() {
	$('body').css('cursor', 'default'); 
}

function setDateFormat(element) {
	if (isSet(element)) {
		element.mask("99/99/9999");
	}
}

function setErrorMessage(ErrorHeader,ErrorMessage,ButtonLeftCaption,ButtonRightCaption,ButtonLeftFunction,ButtonRightFunction) {
	$('#ErrorHeader').html(ErrorHeader);
	$('#ErrorMessage').html(ErrorMessage);
	$('#ErrorButtonLeft').val(ButtonLeftCaption);
	
	if (isSet(ButtonRightCaption)) {
		$('#ErrorButtonRight').show();
		$('#ErrorButtonRight').val(ButtonRightCaption);
	} else {
		$('#ErrorButtonRight').hide(); 
	}
	
	if (isSet(ButtonLeftFunction)) {
		$("#ErrorButtonLeft").click(ButtonLeftFunction);
	} else {
		$("#ErrorButtonLeft").click(closeErrorMessage);
	}
	
	if (isSet(ButtonRightFunction)) {
		$('#ErrorButtonRight').show();
		$("#ErrorButtonRight").click(ButtonRightFunction);
	} else {
		$('#ErrorButtonRight').hide(); 
	}
}

function setURL(url) {
	if (url.trim().length) {
		location.href= url;
	}
}

function showErrorMessage() {
	$.blockUI({ message: $('#ErrorPrompt'),css: {width:'500px'} }); 
}

function setFlashPrompt(shtml) {
	$('#FlashPrompt').html(shtml);
	$.blockUI({ message: $('#FlashPrompt')}); 
}

function closeErrorMessage() {
	$.unblockUI();
	return false; 
}

function setToolTip(element,content) {
	element.qtip({
				 content: content, // Give it some content
				 position: "topLeft", // Set its position
				 hide: {
					fixed: true // Make it fixed so it can be hovered over
				 },
				 style: {
					padding: "5px 15px", // Give it some extra padding
					name: "dark" // And style it with the preset dark theme
				}
	});
}

function getCurrentDate() {
	var date= new Date();
	return date.getDate();
}
	
function getCurrentMonth() {
	var date = new Date();
	
	switch(date.getMonth()) {
		case 0: return "Jan"; 	break;
		case 1: return "Feb"; 	break;
		case 2: return "March"; break;
		case 3: return "April"; break;
		case 4: return "May"; 	break;
		case 5: return "June"; 	break;
		case 6: return "July"; 	break;
		case 7: return "Aug"; 	break;
		case 8: return "Sept"; 	break;
		case 9: return "Oct";	break;
		case 10:return "Nov"; 	break;
		case 11: return "Dec"; 	break;
		default: return "Jan"; 	break;
	}
}
	
function getCurrentYear() {
	var date = new Date();
	return date.getFullYear();
}

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","select").val("");
	ClearErrorMessage();
}

function DisplayErrorMessage(message) {
	$('#errorMessage').html(message.trim()).css("display","block");
}

function getForgotPassResponse() {
	if ($('#userEmail').val()) {
		$.get("includes/php/ajax/emailpassword.php",
				{email:  $('#userEmail').val()}, 
				function(data) { 
					setErrorMessage("",data,"Ok");	
					showErrorMessage();
					$('#errorMessage').html(data);
				});
	} else {
		setErrorMessage("Oops an Error has Occured","The email address field appears to be empty.","Ok");	
		showErrorMessage();
	}
}
	
function isEmailValid(e) {

	var r = /^[^@]+@[a-zA-Z0-9._-]+\.[a-zA-Z]+$/;
	
	if (isSet(e)) {
		if (r.test(e.value)) {
			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 ($.trim(data).length > 0 ) {
			var str= data.split('|');
			$('#dlResponse').html('Processing server return');

			if (str.length > 0) {
				$('#id').val($.trim(str[0]));
				$('#firstName').val($.trim(str[1]));
				$('#lastName').val(str[2].trim());
				$('#title').val($.trim(str[3]));
				$('#email').val(str[4].trim());
				$('#telephone').val(str[5].trim());
				$('#ext').val($.trim(str[6]));
				$('#fax').val($.trim(str[7]));
				$('#cell').val($.trim(str[8]));
				$('#directLine').val($.trim(str[9]));
				$('#alternatePhone').val($.trim(str[10]));
				
				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 formSendYourResume() {
	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");
		showErrorMessage();
		setCursorNormal();
		return false;
	} else {
		return true;
	}
}

function formResearcherResume() {
	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 (!$('#MysteryShopping').val()) {
		message += 'Your mystery shopping experience cannot be empty.';
	}
	
	if (message.trim().length) {
		setErrorMessage("Oops an Error has Occured. ",message,"Ok");
		showErrorMessage();
		setCursorNormal();
		return false;
	}else {
		return true;
	}
}

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;
}				

function eventRegistration() {
	$("#formbox").submit(function() {
		var element = $("input").not(":submit");
		var ErrorMessage = "";
				
		element.each(function(i, selected){
			if (!$(selected).val().length && $.trim(ErrorMessage) == "") {
				ErrorMessage = "Oops you missed a field. ";
			}
		});
					
		if ($.trim(ErrorMessage) != "") {
			setErrorMessage("Oops an Error has Occured. ",ErrorMessage,"Ok","Ok",function() { 
				closeErrorMessage(); 
			});
				
			showErrorMessage();
			return false;
		} else {
			setCursorBusy();
			$.post( 
				"includes/php/ajax/eventregistration.php",
				{	FirstName: $("#firstName").val(),LastName: $("#lastName").val(), 
					Email: $("#email").val(),Telephone: $("#telephone").val(),
					Company: $("#company").val(),JobTitle: $("#jobPosition").val()
				}, 
				function(data) {alert(data);setCursorNormal(); }
			);
					
			return false;
		}
	});
}

function ifrViewTrialRequest() {
	$("#formbox").submit(function() {
		var element = $("input").not(":submit");
		var ErrorMessage = "";
				
		element.each(function(i, selected){
			if (!$(selected).val().length && $.trim(ErrorMessage) == "") {
				ErrorMessage = "Oops you missed a field. ";
			}
		});
					
		if ($.trim(ErrorMessage) != "") {
			setErrorMessage("Oops an Error has Occured. ",ErrorMessage,"Ok","Ok",function() { 
				closeErrorMessage(); 
			});
				
			showErrorMessage();
			return false;
		} else {
			setCursorBusy();
			$.post( 
				"includes/php/ajax/ifrviewtrial.php",
				{	FirstName: $("#firstName").val(),LastName: $("#lastName").val(), 
					Email: $("#email").val(),Telephone: $("#telephone").val(),
					Company: $("#company").val(),JobTitle: $("#jobPosition").val(),
					Categories: $("#category").html()
				}, 
				function(data) {alert(data);setCursorNormal(); }
			);
					
			return false;
		}
	});
}

function openBrochure() {
	if ($("#productbrochure option:selected").val() != "Select Language") {
		windowOpener($("#productbrochure option:selected").val())
	}
}

function mainFlash() {
	so = new SWFObject("includes/flash/video-shopping-en.swf",'flashmain', "608","150","8","#F1F0EE");
	so.addParam("wmode","transparent");
	so.addParam("quality","high");
	so.addParam("scale","noborder");
	so.write("imagebox");
}

function showMovie() {
	$("#FlashPrompt").html('<iframe src="http://www.gfk.com/group/image_trailer/index.en.html"><p>Iframe not supported</p></iframe><div style="float:left;"><input type="button" onClick="hideMovie()" value="Close"/></div>');
	$("#FlashPrompt").css("width","810px");
	$("#FlashPrompt").css("margin-left","-400px");
	$("#FlashPrompt").show();
}

function hideMovie() {
	$('#FlashPrompt').hide();
	$('#FlashPrompt').html('');
}