var MenuHeight;
var menue = new Array();
var Timer;
var ifram;
MenuHeight = 0;

function addslashes(str) {
str=str.replace(/\\/g,'\\\\');
str=str.replace(/\'/g,'\\\'');
str=str.replace(/\"/g,'\\"');
str=str.replace(/\0/g,'\\0');
return str;
}
function stripslashes(str) {
str=str.replace(/\\'/g,'\'');
str=str.replace(/\\"/g,'"');
str=str.replace(/\\0/g,'\0');
str=str.replace(/\\\\/g,'\\');
return str;
}

function Expand()
{
  MenuHeight = MenuHeight + 5;
  if (MenuHeight > 205) {
    MenuHeight = 205;
  }
  for (x=0; x<menue.length; x++) {
    menue[x].style.height = MenuHeight+"px";
  }
  var ifram = document.getElementById('iframe');
  ifram.style.height = (65+MenuHeight)+"px";
  if (MenuHeight < 205) {
    clearTimeout(Timer);
    Timer = setTimeout("Expand()",1);
  }
}
function Contract()
{
  MenuHeight = MenuHeight - 50;
  if (MenuHeight < 0) {
    MenuHeight = 0;
  }
  for (x=0; x<menue.length; x++) {
    menue[x].style.height = MenuHeight+"px";
  }
  if (MenuHeight > 0) {
    clearTimeout(Timer);
    Timer = setTimeout("Contract()",1);
  }
}

function MenuShow(){
  ifram = document.getElementById('iframe');
  ifram.style.width  = "1000px";
  menue = [document.getElementById('Menu1'), document.getElementById('Menu2'),document.getElementById('Menu3'),document.getElementById('Menu4'),document.getElementById('Menu5')];
  clearTimeout(Timer);
  Expand();
}
function MenuShowb(){   //stop the blog and home button from removing the menu
  menue = document.getElementsByName("MenuUnder");
  clearTimeout(Timer);
}

function MenuHide(){
  ifram = document.getElementById('iframe');
  ifram.style.height = "0px";
  ifram.style.width  = "0px";
  menue = [document.getElementById('Menu1'), document.getElementById('Menu2'),document.getElementById('Menu3'),document.getElementById('Menu4'),document.getElementById('Menu5')];
  for (x=0; x<menue.length; x++) {
    menue[x].style.height = "0px";
  }
  Timer = setTimeout("Contract()",1000);
}

function checkField(whatType, field) {

	var fieldData = document.getElementById(field).value;
	var returnValue = true;

	if (whatType == 'email' || whatType == 'dd-mm-yyyy' || whatType == 'dd/mm/yyyy') {

		if (whatType == 'email') {

			var at="@"
			var dot="."
			var lat=fieldData.indexOf(at)
			var lstr=fieldData.length
			var ldot=fieldData.indexOf(dot)
			if (fieldData.indexOf(at)==-1){
				returnValue = false;
			}
			if (fieldData.indexOf(at)==-1 || fieldData.indexOf(at)==0 || fieldData.indexOf(at)==lstr){
				returnValue = false;
			}//There cannot be a . in the last 2 characters of an email address.
			if (fieldData.indexOf(dot)==-1 || fieldData.indexOf(dot)==0 || fieldData.indexOf(dot)==lstr  || fieldData.lastIndexOf(dot)==(lstr-1)  || fieldData.lastIndexOf(dot)==(lstr-2)){
				returnValue = false;
			}
			if (fieldData.indexOf(at,(lat+1))!=-1){
				returnValue = false;
			}
			if (fieldData.substring(lat-1,lat)==dot || fieldData.substring(lat+1,lat+2)==dot){
				returnValue = false;
			}
			if (fieldData.indexOf(dot,(lat+2))==-1){
				returnValue = false;
			}
			if (fieldData.indexOf(" ")!=-1){
				returnValue = false;
			}
		} else if (whatType == 'dd-mm-yyyy' || whatType == 'dd/mm/yyyy') {

			var aChar1 = "0123";
			var aChar2 = "0123456789";
			var aChar3 = "01";

			var temp = new Array();
			if (whatType == 'dd-mm-yyyy') {
				temp = fieldData.split('-');
			} else if (whatType == 'dd/mm/yyyy') {
				temp = fieldData.split('/');
			}

			if (temp.length != 3) {
				returnValue = false;
			} else {
				// check first value is correct (days)
				if (temp[0].length == 2 & temp[0] > 0 && temp[0] < 32) {
					if (returnValue) {
						if (aChar1.indexOf(temp[0].charAt(0)) == -1 || aChar2.indexOf(temp[0].charAt(1)) == -1) {
							returnValue = false;
						}
					}
				} else {
					returnValue = false;
				}
				if (returnValue) {
					// check the second value (months)
					if (temp[1].length == 2 & temp[1] > 0 && temp[1] < 13) {
						if (returnValue) {
							if (aChar3.indexOf(temp[1].charAt(0)) == -1 || aChar2.indexOf(temp[1].charAt(1)) == -1) {
								returnValue = false;
							}
						}
					} else {
						returnValue = false;
					}
				}
				if (returnValue) {
					// check the third value (year)
					if (temp[2].length == 4 && temp[2] > 1950 && temp[2] < 2020) {
						// okay
					} else {
						returnValue = false;
					}
				}
			}
		}

	} else {


		if (whatType == 'text') {
			var aChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 -'\"";
		} else if (whatType == 'plaintext') {
			var aChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
		} else if (whatType == 'phone') {
			var aChars = "0123456789 -+";
		} else if (whatType == 'int') {
			var aChars = "0123456789";
		}

		if (fieldData.length > 0) {
			for (var i = 0; i < fieldData.length; i++) {
				 if (aChars.indexOf(fieldData.charAt(i)) == -1) {
					 returnValue = false;
					 break;
				 }
			}
		} else {
			returnValue = false;
		}


	}

	return returnValue;


}

function checkInstitution(selected) {

	if (selected == '3' || selected == '4' || selected == '5' || selected == '6') {
		jQuery("#institution_div").slideDown(250);
		jQuery("#course_div").slideDown(250);
	} else {
		jQuery("#institution_div").slideUp(250);
		jQuery("#course_div").slideUp(250);
	}
}

function registrationForm() {
	var errors = 0;
	var error_string = '';
	if (!checkField('text', 'first_name') || document.getElementById('first_name').value.length < 1) {
		errors++;
		error_string = error_string + " - Check First Name"+"\n";
	}
	if (!checkField('text', 'last_name') || document.getElementById('last_name').value.length < 1) {
		errors++;
		error_string = error_string + " - Check Last Name"+"\n";
	}
	if (!checkField('email', 'email') || document.getElementById('email').value.length < 1) {
		errors++;
		error_string = error_string + " - Check Email"+"\n";
	} else {
		if (document.getElementById('email').value != document.getElementById('confirm_email').value) {
			errors++;
			error_string = error_string + " - Emails Don't Match"+"\n";
		}
	}
	if (document.getElementById('current_level_study').value == '' || document.getElementById('last_year').value == '') {
		errors++;
		error_string = error_string + " - Check Study Details"+"\n";
	} else {
		if (document.getElementById('current_level_study').value == '3' || document.getElementById('current_level_study').value == '4' || document.getElementById('current_level_study').value == '5' || document.getElementById('current_level_study').value == '6') {
			if (document.getElementById('institution').value == '') {
				errors++;
				error_string = error_string + " - Check Institution"+"\n";
			}
			if (document.getElementById('course').value == '') {
				errors++;
				error_string = error_string + " - Check Course"+"\n";
			}
		}
	}
	if (!checkField('plaintext', 'passwrd')) {
		errors++;
		error_string = error_string + " - Please only use Letters and Numbers (A-Z 0-9) for your password\n";
  } else if (document.getElementById('passwrd').value.length < 5) {
		errors++;
		error_string = error_string + " - Check Password (minimum 5 characters)"+"\n - (Currently "+document.getElementById('passwrd').value.length+")\n";
	} else {
		if (document.getElementById('passwrd').value != document.getElementById('confirm_password').value) {
			errors++;
			error_string = error_string + " - Passwords Don't Match"+"\n";
		}
	}


	if (error_string != '') {
		alert("ERROR: "+"\n\n"+error_string);
	} else {
		document.forms["registration_form"].submit();
	}
}

function az_change_industry(url, type) {
	if (type == 'industry') {
		window.location = url + ".html";
	} else if (type == 'recruiter') {
		window.location = "/Jobs/" + url + "/";
	}
}

function destroySession(){
	if (confirm('are you sure?')) {
		jQuery.ajax({
			url: '/includes/Browser/ajax/generic.php',
			data: 'source=destroy_session',
			type: 'POST',
			cache: false,
			success: function(response){
				alert("Cleared");
			},
			failure: function(response){
				alert("Something went wrong (function destroySession in file js/generic.js)");
			}
		});
	}
}

String.prototype.replaceAll = function(
strTarget, // The substring you want to replace
strSubString // The string you want to replace in.
){
var strText = this;
var intIndexOfMatch = strText.indexOf( strTarget );

// Keep looping while an instance of the target string
// still exists in the string.
while (intIndexOfMatch != -1){
// Relace out the current instance.
strText = strText.replace( strTarget, strSubString )

// Get the index of any next matching substring.
intIndexOfMatch = strText.indexOf( strTarget );
}

// Return the updated string with ALL the target strings
// replaced out with the new substring.
return( strText );
}
