// Four Corners Modules Javascript Code

			function mailinglist_subscribe_validate() {
							if(document.getElementById('mailinglist_first_name') && document.getElementById('mailinglist_last_name')) mailinglist_concat_name();
				if (document.getElementById("mailinglist_name").value == '' || document.getElementById("mailinglist_name").value == ' ' || document.getElementById("mailinglist_name").value == 'Name') {
					alert('You must enter your name');
				} else				if (document.getElementById("mailinglist_email").value == '' || document.getElementById("mailinglist_email").value == 'Email') {
					alert('You must enter your email address');
				} else {return true;
					}
					return false;				
			}
			function javascriptformsubmit(name,email,title,group,phone) {
				AjaxRequest.get(
					{
					'url' : 'http://www.fourcorners.net/modules/display.php?modulename=Mailinglist&mailinglist_subscribe=yes&mailinglist_title='+title+'&mailinglist_phone='+phone+'&mailinglist_name='+name+'&mailinglist_email='+email+'&mailinglist_group='+group
						,'onSuccess':function(req){
							if(req.responseText == 'success') {
								document.getElementById("successtext").innerHTML = '<div class="">Thank you for subscribing to our Newsletter</div>';
								document.getElementById("mailinglist_form").style.display = "none";
								_gaq.push(['_trackPageview', '/newsletter-sign-up.php']);
							} else {
								document.getElementById("successtext").innerHTML = req.responseText;
							}
						} 
					}
				);
				return false;
			}
			function mailinglist_unsubscribe_validate() {				if (document.getElementById("mailinglist_email").value == '' || document.getElementById("mailinglist_email").value == 'Email') {
					alert('You must enter your email address');
				} else {
					return true;
				}
				return false;			}
			function javascriptunsubscribeformsubmit(email) {
				AjaxRequest.get(
					{
					'url' : 'http://www.fourcorners.net/modules/display.php?modulename=Mailinglist&mailinglist_unsubscribe=yes&mailinglist_email='+email	
						,'onSuccess':function(req){
							if(req.responseText == 'success') {
								document.getElementById("unsubscribesuccesstext").innerHTML = '<div class="">You have been unsubscribed from our mailing list.</div>';
								document.getElementById("mailinglist_form").style.display = "none";
							} else {
								document.getElementById("unsubscribesuccesstext").innerHTML = req.responseText;
							}
						} 
					}
				);
				return false;
			}
			function mailinglist_concat_name() {
				document.getElementById('mailinglist_name').value = document.getElementById('mailinglist_first_name').value + ' ' + document.getElementById('mailinglist_last_name').value;
			}		var menuHeight = 0;
		$(function() {
			if($('#csa-content').length) {
				Migrationbureau_resize();
				$(window).resize(function() {
					Migrationbureau_resize();
				});
				// Attach calendar widget
				$('.jCalendar').datepicker({
					changeMonth: true,
					changeYear: true,
					dateFormat: 'dd/mm/yy',
					showOn: 'button',
					buttonImage: '/admin/images/common_calendar_16.png',
					buttonImageOnly: true
				});
			}
		});
		
		function migrationbureau_checkDateReference(theForm) {
				var status = true;
				if(status == true) status = migrationbureau_requiredDate(theForm);
				if(status == true) status = migrationbureau_requiredReference(theForm);
				return status;
			}

			function migrationbureau_requiredDate(theForm) {
				if (theForm.requireddate.value == "") {
					alert("Please enter the date to continue.");
					return false;
				} else {
					return true;
				}
			}

			function migrationbureau_requiredReference(theForm) {
				var countryCodes = new Array("GB","NL","NLR","D","ZA","NZ","OS","AU");
				var theReferenceNumber = theForm.requiredreference.value;
				
				var success = 0;
				
				for (var count = 0; count < countryCodes.length; count++) {
		
					var theirCountryCode = theReferenceNumber.substring(0,countryCodes[count].length).toUpperCase();
					
					if (theirCountryCode == countryCodes[count]) {
						success = 1;
					}
				}
				
				if (success == 0) {
					alert("Your Client Reference appears to be invalid.\nIf you are a Dutch client whose reference does not have a 'NL' prefix, please add it and try again (eg. NL12345). Otherwise, please e-mail support@oceaniadevelopment.com for assistance.");
					return false
				} else {
					return true;
				}
			}

			function migrationbureau_requiredName(theForm) {
				if (theForm.requiredname.value == "") {
					alert("Please enter your consultant's name to continue.");
					theForm.requiredname.focus();
					return false;
				} else {
					return true;
				}
			}

			function migrationbureau_requiredID(theForm) {
				if (theForm.required_applicationid.value == "") {
					alert("Please enter your customer # to continue.");
					theForm.required_applicationid.focus();
					return false;
				} else {
					return true;
				}
			}
            function migrationbureau_checkGoAheadForm(theForm) {
                if (theForm.client_number.value == "") {
                    alert("Please enter your client number to continue.");
                    return false;
                } 
              
				var countryCodes = new Array("GB","NL","NLR","D","ZA","NZ","OS","AU");
				var theReferenceNumber = theForm.client_number.value;
				var success = 0;

                for (var count = 0; count < countryCodes.length; count++) {
                    var theirCountryCode = theReferenceNumber.substring(0,countryCodes[count].length).toUpperCase();
                    if (theirCountryCode == countryCodes[count]) {
                        success = 1;
                    }
                }
				if(success == 0) {
					alert("Your client number appears to be invalid.\nIf you are a Dutch client whose reference does not have a 'NL' prefix, please add it and try again (eg. NL12345). Otherwise, please e-mail support@oceaniadevelopment.com for assistance.");
                    return false;
				}
				return true;
            }
            function migrationbureau_checkGoAheadClientLoginForm(theForm) {
                if (theForm.first_name.value == "") {
                    alert("Please enter your first name to continue.");
                    return false;
                }
				if (theForm.last_name.value == "") {
                    alert("Please enter your last name to continue.");
                    return false;
                }
                if (document.getElementById('destination_country').value == '') {
                    alert('You must select your destination county');
                    return false;
                } else if (document.getElementById('location').value == '') {
                    alert('You must select the location of your agency');
                    return false;
                } else if (theForm.amount.value == '') {
                    alert('You must enter the sign on fee amount');
                    return false;
                } 
				return true;
			}
			function migrationbureau_checkOnlinePaymentForm(theForm) {
                if (theForm.first_name.value == "") {
                    alert("Please enter your first name to continue.");
                    return false;
                }
				if (theForm.last_name.value == "") {
                    alert("Please enter your last name to continue.");
                    return false;
                }
                if (theForm.client_number.value == "") {
                    alert("Please enter your reference number to continue.");
                    return false;
                } else if (document.getElementById('total_amount').value == '') {
                    alert('You must enter your total amount');
                    return false;
                }
				if (theForm.email.value == "") {
					alert('You must enter your email address');
					return false;
				}
                var countryCodes = new Array("GB","NL","NLR","D","ZA","NZ","OS","AU");
                var theReferenceNumber = theForm.client_number.value;
                var success = 0;
                for(var count = 0; count < countryCodes.length; count++) {
                    var theirCountryCode = theReferenceNumber.substring(0,countryCodes[count].length).toUpperCase();
                    if (theirCountryCode == countryCodes[count]) {
                        success = 1;
                    }
                }
				 if(success == 0) {
                    alert("Your Client Reference appears to be invalid.\nIf you are a Dutch client whose reference does not have a 'NL' prefix, please add it and try again (eg. NL12345). Otherwise, please e-mail support@oceaniadevelopment.com for assistance.");
                    return false;
                }
				return true;
			}
			function migrationbureau_checkGoAheadClientLoginForm2(theForm) {
                if (theForm.authorising_payment.checked == false) {
                    alert("Please Authorise Payment");
                    return false;
                }
				return true;
            }
		
		function Migrationbureau_resize() {
			var height = $(window).height();
			var bodyTop = $('#csa-content .body').offset().top;
			var menuTop = $('#csa-menu').offset().top;
			// Check the size of the menu as some users will have more menu items then the window height available
			if(!menuHeight) menuHeight = $('#csa-menu').height() + 10;
			if(menuTop + menuHeight > height) {
				height = height + ((menuTop + menuHeight) - height); 
			}
			// Resize main body after the title
			$('#csa-content .body').css({
				height: (height - bodyTop - 28)
			});
			// Resize the menu to be full height
			$('#csa-menu').css({
				height: (height - menuTop - 28)
			});
			// No idea how these 4px are being adding in the first place?????
			if($('#csa-menu').html() == '&nbsp;') {
				$('#csa-content').css({
					marginLeft: 1
				});
			}
		}
		
		function confirmMBDelete(strName, url) {
			if(confirm("Confirm deletion of " + strName + "?")) window.location.href = url;
		}
		function Migrationbureau_loadTinyMCE(id, version) {
			$('#' + id).tinymce({
				mode : "exact",
				elements : id,
				theme : "advanced",
				plugins : "paste",
				theme_advanced_buttons1 : "bold,italic,underline,|,justifyleft,justifycenter,justifyright,justifyfull,|,cut,copy,paste,pastetext,|,undo,redo,|,link,unlink,",
				theme_advanced_buttons2 : "",
				theme_advanced_buttons3 : "",
				theme_advanced_toolbar_location : "bottom",
				theme_advanced_toolbar_align : "left",
				paste_use_dialog : false,
				theme_advanced_resizing : true,
				theme_advanced_resize_horizontal : false,
				theme_advanced_link_targets : "_blank=blank;",
				cleanup_on_startup : true,
				cleanup : true,
				verify_html : false,
				apply_source_formatting : false,
				convert_urls : false,
				relative_urls : false,
				remove_script_host : false
			});
		}
		function mb_addClientNumber() {
			html = '<label>Number</label> <input type="text" id="client_number" /> <input class="submit" type="button" onclick="mb_submitClientNumber();" value="Submit" />';
			return html;
		}
		
		function mb_submitClientNumber() {
			window.location.href = window.location.href + '&client_number=' + $('#client_number').val() + '&update_client_number=1';
		}
		
		function mb_addAssessmentWriter() {
			html = '';
			$.ajax({
				url: window.location.href + '&get_assessment_writers=1',
				async: false,
				success: function(response) {
					html = response;
				}
			});
			return html;
		}
		
		function mb_submitAssessmentWriter() {
			window.location.href = window.location.href + '&assessment_writerid=' + $('#assessment_writerid').val() + '&update_assessment_writer=1';
		}
		
		function mb_addAssessmentSupervisor() {
			html = '';
			$.ajax({
				url: window.location.href + '&get_assessment_supervisors=1',
				async: false,
				success: function(response) {
					html = response;
				}
			});
			return html;
		}
		
		function mb_submitAssessmentSupervisor() {
			window.location.href = window.location.href + '&assessment_supervisorid=' + $('#assessment_supervisorid').val() + '&update_assessment_supervisor=1';
		}
		
		function mb_uploadFile(flags) {
			html = '<form method="post" action="' + window.location.href + '" enctype="multipart/form-data" onsubmit="mb_pleaseWait(\'Please wait while your file is uploaded\');"><p><label style="width: 80px; display: inline-block;">Select a file</label> <input type="file" name="application_file" /></p>';
			if(flags > 0) {
				html += '<p><label style="width: 80px; display: inline-block;">Flag As</label>';
				if(flags & 1) html += '<input id="upload_cv" type="radio"name="type" value="1" /> <label for="upload_cv">CV</label> ';
				if(flags & 2) html += '<input id="upload_assessment" type="radio" name="type" value="2" /> <label for="upload_assessment">Assessment Report</label> ';
				html += '</p>';
			}
			html += '<input class="submit" type="submit" name="upload_application_file" value="Upload File" /></form>';
			return html;
		}
		
		function mb_pleaseWait(comment) {
			$("#overlay-box .content").fadeOut(function() {
				$("#overlay-box .content").html("<p>" + comment + "</p>").fadeIn();
			});
		}
		
		function mb_openDialog(label, functionToCall, vals) {
			// Add overlay
			mb_toggleOverlay();
			// Call the function to customise the box
			var content = window[functionToCall](vals);
			// Setup the box div
			$('<div id="overlay-box"><div class="container"><img class="delete" src="/admin/images/edit_page_delete.png" alt="" title="Close" onclick="mb_toggleOverlay();" /><h2>' + label + '</h2><div class="content">' + content + '</div></div></div>').appendTo(document.body);
			// Center the box in the center of the screen
			var boxHeight = $('#overlay-box').outerHeight();
			var winHeight = $(window).height();
			var top = (winHeight / 3) - (boxHeight / 2);
			// Fade in the box
			$('#overlay-box').css({
				top: top
			}).fadeIn('fast', function() {
				$('#overlay-box input:first').focus();
				$('#overlay-box input[type=text]').keypress(function(event) {
					if(event.which == 13 ) {
						$('#overlay-box .submit').trigger('click');
					}
				});
			});
		}
		
		function mb_toggleOverlay() {
			if($('#overlay').length) {
				$('#overlay').animate({
					opacity: 0
				}, 'fast', function() {
					$(this).detach();
				});
				$('#overlay-box').fadeOut('fast', function() {
					$(this).detach();
				});
			} else {
				$('<div id="overlay"></div>').appendTo(document.body);
				$('#overlay').css({
					height: $(document).height()
				}).animate({
					opacity: 0.6
				}, 'fast');
			}
		}
		function mb_fcCheckCountry(form, dd) {
			var country1 = form.country1.value;
			var country2 = form.country2.value;
			var country3 = form.country3.value;
			var boolOk = true;
		
			if(dd == 1) {
				if(country1 == country2 || country1 == country3) boolOk = false;					
			} else if(dd == 2) {
				if(country2 == country1 || country2 == country3) boolOk = false;
			} else {
				if(country3 == country1 || country3 == country2) boolOk = false;
			}
			if(!boolOk) {
				if(dd == 1) form.country1.value = '';
				else if(dd == 2) form.country2.value = '';
				else form.country3.value = '';
				alert('You are unable to select the same destination twice.');
			}
			mb_countPaymentCountries();
		}
		
		function mb_countPaymentCountries() {
			var countries = 0;
			if($('#country1').val()) countries++;
			if($('#country2').val()) countries++;
			if($('#country3').val()) countries++;
			$('#country_count').val(countries);
			mb_updateFCRate();
		}
		
		function mb_updateFCRate() {
			if($('#countryid').val()) {
				$('#country_total_value').val('Calculating...');
				$('#country_total_value').attr('disabled', 'disabled');
				$('#submit_fc_payment_confirm').attr('disabled', 'disabled');
				$.ajax({
					url: '/modules/display.php?modulename=Migrationbureau&ajax=1&f=updateFCRate&discount_code=' + $('#discount_code').val() + '&countryid=' + $('#countryid').val() + '&country1=' + $('#country1').val() + '&country2=' + $('#country2').val() + '&country3=' + $('#country3').val(),
					success: function(data) {
						var response = data.split("|");
						if($('#discount_code').val() == response[1]) {
							$('#country_total_value').val(response[0]);
							$('#country_total_value').attr('disabled', '');
							$('#submit_fc_payment_confirm').attr('disabled', '');
						}
					}
				});
			}
		}
		
		function migrationbureau_openFiles() {
			width	= 500;
			height	= 350;
			my_field = "id_document";
			my_win = self;
			window.open("../admin/files_dialog.php", "file_manager", "modal,scrollbars=yes,width=" + width + ",height=" + height + ",top=" + (screen.height/2 - height/2) + ",left=" + (screen.width/2 - width/2));
		}
		
		function mb_addPRAgent() {
			html = '';
			$.ajax({
				url: window.location.href + '&get_pra_agents=1',
				async: false,
				success: function(response) {
					html = response;
				}
			});
			return html;
		}
		
		function mb_PRAgent() {
			window.location.href = window.location.href + '&pra_userid=' + $('#pra_userid').val() + '&update_pra_userid=1';
		}function news_confirmItemDelete(id, heading) {
				if(confirm('Are you sure you want to delete the "' + heading + '" news item?')) window.location.href="http://www.fourcorners.net/index.php?action=news_delete&id=" + id;
			}function Users_passwordType(boolHide) {
	var username = document.getElementById('password_text');
	var password = document.getElementById('password_pass');
	if(password.value != '' || boolHide) {
		password.style.display = 'inline';
		username.style.display = 'none';
		if(boolHide) password.focus();
	} else {
		password.style.display = 'none';
		username.style.display = 'inline';
		username.value = 'Password';
	}
}
