
/* *** COOKIE CHECK FUNCTIONS START *** */
	function getCookie() {
		var strName = 'varCook';
		if (document.cookie.indexOf(strName) == -1) {
   			document.write('<span style="color: #f00; font: bold 8.5pt Verdana,sans-serif;">Sorry but you cannot use the chefschool Casting website unless cookies are accepted by your browser.\n\nRDF Media uses cookies primarily for authenticating members for entry to the Casting websites. This cookie allows access to the website without requiring re-entry of logins and passwords. At no time is any personal information stored in a cookie, nor is any information made available to others for any reason.</span>');
   			return false;
		} else {
		    var cookieStart = document.cookie.indexOf(strName);
		    var cookieValStart = (document.cookie.indexOf('=', cookieStart) + 1);
		    var cookieValEnd = document.cookie.indexOf(';', cookieStart);
		    if (cookieValEnd == -1) 	{cookieValEnd = document.cookie.length;	}
		    var cookieValue = document.cookie.substring(cookieValStart, cookieValEnd);
		}
		
		if (cookieValue == 'True') {return true;}
	}


	function setCookie(name, value, expires, path, domain, secure) {
		var strDNS = getDNS();
	    	document.cookie = name + '=' +escape(value) + ( (expires) ? ';expires=' + expires.toGMTString() + 1000*60*20 : '') + ( (path) ? ';path=' + path : '') + ( (domain) ? ';domain=' + strDNS : '') + ( (secure) ? ';secure' : '');
		    return true;
	}


	function getDNS() {
		var cookieDomain = document.location.hostname;
	}

function IsCookiesEnabled() {			
    setCookie('varCook','True','','/','','');
    if (getCookie()){
        //alert('Cookies are enabled and you can properly use this website!');
    } else {		
        return false;	
    }	
    return true;	
}  
/* *** COOKIE CHECK FUNCTIONS END *** */

/* *** JAVASCRIPT CHECK FUNCTIONS START *** */
function IsJavaScriptEnabled() {
    if(document.getElementById('JSCheck')) {
        if(document.getElementById('JSCheck').value == 'true') {
            return true
        }
    }
    return false;
}
/* *** JAVASCRIPT CHECK FUNCTIONS END *** */

/* *** BROWSER CHECK FUNCTIONS START *** */
function IsBrowserEnabled() {
    // set hidden field value
    if(document.getElementById('JSCheck')) {
        document.getElementById('JSCheck').value = 'true';
    }
  
  /*  
    if(!IsCookiesEnabled()) {
        document.write('<span style="color: #f00; font: bold 8.5pt Verdana,sans-serif;"><p>Access Denied: Cookies Disabled</p></span>');
        return false;
    }
    */
    
    if(!IsJavaScriptEnabled()) {
        document.write('<span style="color: #f00; font: bold 8.5pt Verdana,sans-serif;"><p>Access Denied: Javascript Disabled</p></span>');
        return false;
    }
    
   // if(IsCookiesEnabled() && IsJavaScriptEnabled()) {return true;} 
    if(IsJavaScriptEnabled()) {return true;} 
    
    return false;
}
/* *** BROWSER CHECK FUNCTIONS END *** */

/* *** HELPER FUNCTIONS START *** */

function encodeURL(strIn) {
	var strOut = "";
	
	for(i = 0; i<strIn.length; i++) {
		if (strIn.charAt(i) == " ") strOut += "+";
		else strOut += strIn.charAt(i);
	}
	
	return escape(strOut);
}

function resetHomepage() {
    setElementValue('CurrentPage', '0');
    setElementValue('TotalPage', '0');
    setElementValue('Target', 'HomePage');
    setElementValue('Mode', 'Home');    
}

/*
// setElementValue
*/
function setElementValue(strId, strValue) {
    if(document.getElementById(strId)) {
	    document.getElementById(strId).value = strValue;
	}
}

function switchRowColour(obj) {
    var currColour = obj.style.backgroundColor;
    var defaultColour = 'white';
    var highlightColour = 'wheat';
    
    if(currColour.toLowerCase() == defaultColour) {
        obj.style.backgroundColor = highlightColour;
    } else {
        obj.style.backgroundColor = defaultColour;
    }
}

function switchIconColour(obj) {
    var currColour = obj.style.backgroundColor;
    var defaultColour = '#996600';
    var defaultColourRGB = 'rgb(153, 102, 0)'; // for Mozilla
    var highlightColour = 'Orange';
    
    if(currColour == '') {currColour = defaultColour;}
    
    if((currColour.toLowerCase() == defaultColour.toLowerCase()) || (currColour.toLowerCase() == defaultColourRGB.toLowerCase())) {
        obj.style.backgroundColor = highlightColour;
    } else {
        obj.style.backgroundColor = defaultColour;
    }
}

function switchLinkColour(obj) {
    if(obj) {
        var currColour = obj.style.color;
        var defaultColour = 'White';
        var defaultColourRGB = 'rgb(255, 255, 255)'; // for Mozilla
        var highlightColour = 'Orange';
        
        if(currColour == '') {currColour = defaultColour;}
        
        if((currColour.toLowerCase() == defaultColour.toLowerCase()) || (currColour.toLowerCase() == defaultColourRGB.toLowerCase())) {
            obj.style.color = highlightColour;
        } else {
            obj.style.color = defaultColour;
        }
    }
}

/*
// AgeDoB
// Work out Age from Date of Birth
*/
function AgeDoB(obj) {
   
    if(obj.value.length > 0) {
	    var TheDate = new Date();                     
	    var Month = TheDate.getMonth();              
	    var Day = TheDate.getDate();                 
	    var Year = TheDate.getYear();
    	
	    if (Year < 1000){Year+=1900;}
    	
	    var Hour = TheDate.getHours();
	    var Minute = TheDate.getMinutes();
	    var Second = TheDate.getSeconds(); 

	    // Parse date components 
	    var ArrDoB = obj.value.split("/");
    	
	    // Derive output name
	    var outputElementId = obj.id;	
	    outputElementId = outputElementId.replace('Date', 'Years');
	    
	    var by = ArrDoB[2];
	    var bm = ArrDoB[1];
	    var bd = ArrDoB[0];	
	    var bh = 11;
	    var bmn = 59;
	    var bs = 59;
    	
	    var BDate = Date.UTC(by,bm,bd,bh,bmn,bs);
	    var CDate = Date.UTC(Year,Month,Day,Hour,Minute,Second);	
	    var Age = (CDate-BDate) + (1000*60*60*24*30); 
	    
	    document.getElementById(outputElementId).value = parseInt(((((Age/1000)/60)/60)/24)/365,10).toString(); 
    }
    
}

/*
// DoBAge
// Work out Date of Birth from Age
*/
function DoBAge(obj) { 

    if(obj.value.length <= 0 || obj.value == '') { // there is no input
        alert('Please enter your age');
        obj.focus();
        obj.select();
        return false;
    }
    
    if(obj.value != '0') { // don't bother if the age is 0        
	    var TheDate = new Date();  
	    var Month = TheDate.getMonth()+1;              
	    var Day = TheDate.getDate();                 
	    var Year = TheDate.getYear();
	    var strDay = Day.toString();
	    var strMonth = Month.toString();
    	
	    if(strDay.length == 1) {strDay = '0'+strDay}
	    if(strMonth.length == 1) {strMonth = '0'+strMonth}
    	
	    if(obj.value.length > 0) { // if there is any input
    	
	        // Derive output name
		    var outputElementId = obj.id;		
		    outputElementId = outputElementId.replace('Years', 'Date');	
		    
		    if (Year < 1000){Year += 1900;}
		    
		    if(document.getElementById(outputElementId).value.length > 0) { // if a date exists, calculate year only
		        
		        // Parse date components 
	            var ArrDoB = document.getElementById(outputElementId).value.split("/");
	            var by = ArrDoB[2];
	            var bm = ArrDoB[1];
	            var bd = ArrDoB[0];
	            var thisYear = Year;
	            
	            if(parseInt(bm) < parseInt(strMonth)) {	                
	                thisYear = parseInt((thisYear) - (obj.value));
	            } else if((parseInt(bm) == parseInt(strMonth)) && (parseInt(bd) < parseInt(strDay))) {	                
	                thisYear = parseInt((thisYear) - (obj.value));	     
	            } else {
                    thisYear = parseInt((thisYear) - (obj.value) -1);
                }                
		        document.getElementById(outputElementId).value = bd+'/'+bm+'/'+thisYear.toString();
		        
		    } else { // calculate using today's date
		        		        
		        document.getElementById(outputElementId).value = strDay+'/'+strMonth+'/'+parseInt(Year - (obj.value)).toString();
		    }
		}
	}
	return true;
}	

//Capitalise the first letter of each word
function titleCaps(obj) {
    var strIn = Trim(obj.value).toLowerCase();
    strIn = strIn.replace(' - ', '-');
    var strOut = '';
    
    if (strIn.length > 0) {
    
        // create an array, delimited by line breaks		
		var arrLines = new Array();
		
		// create an array, delimited by spaces		
		var arrInLine = new Array();
		
		// create an array, delimited by hyphen
		var arrWords = new Array();
		
		arrLines = strIn.split('\n');	
		var strTmpLine = '';
		
		for(var k = 0; k < arrLines.length; k++) {

		    strIn = arrLines[k];
		    arrInLine = strIn.split(' ');	
		    strOut = '';
		    var strTmp = '';
		    var strTmpInner = '';
		    var strTmpState = '';		    
    			
		    for(var i = 0; i < arrInLine.length; i++) {
			    strTmp = '';
			    // remove double spaces
			    if(arrInLine[i].length > 0) {
    				
				    // This might be a hyphenated name
				    arrWords = arrInLine[i].split('-');	
    							
				    for(var j = 0; j < arrWords.length; j++) {
					    // if this is a special case (Mc, Mac, *'*, *-*)
					    if(arrWords[j].substring(0,2).toUpperCase() == 'MC') {
						    strTmp = arrWords[j].substring(0,2).toLowerCase() + arrWords[j].substring(2,3).toUpperCase() + arrWords[j].substring(3,arrWords[j].length) + ' ';
						    // capitalise like mcCartney
					    } else if (arrWords[j].substring(0,3).toUpperCase() == 'MAC') {
						    // capitalise like macIntyre
						    strTmp = arrWords[j].substring(0,3).toLowerCase() + arrWords[j].substring(3,4).toUpperCase() + arrWords[j].substring(4,arrWords[j].length) + ' ';
					    } else if (arrWords[j].substring(1,2) == '\'') {
						    // capitalise like o'Toole
						    strTmp = arrWords[j].substring(0,1).toLowerCase() + arrWords[j].substring(1,2) + arrWords[j].substring(2,3).toUpperCase() + arrWords[j].substring(3,arrWords[j].length) + ' ';					
					    } else {
						    strTmp = arrWords[j];
					    }
    					
					    // capitalise first letter
					    strTmp = strTmp.substring(0,1).toUpperCase() + strTmp.substring(1,arrInLine[i].length);

					    // update array
					    arrWords.splice(j, 1, strTmp);
					    strTmpState = strTmp;
					    strTmp = '';
				    }	
    				
				    // re-compile hyphenated names
				    if(arrWords.length > 1) {
					    for(j = 0; j < arrWords.length; j++) {
						    if(j < arrWords.length-1) {
							    strTmp += arrWords[j] + '-';
						    } else {
							    strTmp += arrWords[j];
						    }
					    }
					    // clear non-hyphenated text
					    strTmpState = '';
				    }
    						
				    if(i < arrInLine.length-1)	{
					    strOut += strTmp + strTmpState + ' ';
				    } else {
					    strOut += strTmp + strTmpState;
				    }
			    }			    
		    }
		    strTmpLine += strOut+'\n';
		}
        obj.value = strTmpLine;
    } 
    return true;
}

// TRIM FUNCTIONS START
function Trim(strIn, intNoise) {
    if(strIn.length < 1) {
        return '';
    }
    
    strIn = rightTrim(strIn);
    strIn = leftTrim(strIn);
    strIn = multiTrim(strIn);
    strIn = commaCancel(strIn);
    switch (intNoise)
    {
	case 1: //this is for the interview
		strIn = noiseCancel(strIn);
	case 2://this is for the first name noise words remain cancel and final s not removed
		break;
	case 3://this is for the surname, final s is removed before doing search
	if (strIn.length > 0){
			strIn = takeOffFinalS(strIn);
		}
	}
    strIn = rightTrim(strIn);
    
    if(strIn == '') {
        return '';
    } else {
        return strIn;
    }
}
/*
// get rid of noise words. Called from function trim
*/
function noiseCancel(strIn) {

	strIn = strIn.toLowerCase();
	var blnFlag = false;
	var strOut = '';
	var arrInLine = strIn.split(' ');
	var arrClean = new Array(1);
	var arrNoise = new Array('about', '1', 'after', '2', 'all', 'also', '3', 'an', '4', 'and', '5', 'another', '6', 'any', '7', 'are', '8', 'as', '9', 'at', '0', 'be', '$', 'because', 'been', 'before', 'being', 'between', 'both', 'but', 'by', 'came', 'can', 'come', 'could', 'did', 'do', 'does', 'each', 'else', 'for', 'from', 'get', 'got', 'has', 'had', 'he', 'have', 'her', 'here', 'him', 'himself', 'his', 'how', 'if', 'in', 'into', 'is', 'it', 'its', 'just', 'like', 'make', 'many', 'me', 'might', 'more', 'most', 'much', 'must', 'my', 'never', 'now', 'of', 'on', 'only', 'or', 'other', 'our', 'out', 'over', 're', 'said', 'same', 'see', 'should', 'since', 'so', 'some', 'still', 'such', 'take', 'than', 'that', 'the', 'their', 'them', 'then', 'there', 'these', 'they', 'this', 'those', 'through', 'to', 'too', 'under', 'up', 'use', 'very', 'want', 'was', 'way', 'we', 'well', 'were', 'what', 'when', 'where', 'which', 'while', 'who', 'will', 'with', 'would', 'you', 'your', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z');
	arrInLine.reverse(); // reverse array because when put into arrClean, goes in "backwards"
	while(arrInLine.length >0){

		for(var j=0; j<arrNoise.length; j++){
			if (arrInLine[0].toString() == arrNoise[j].toString()){
				blnFlag = true;
			}
		}
			if (blnFlag == false){
				arrClean.splice(0,0, arrInLine[0]);
			}
			blnFlag = false;
			arrInLine.splice(0,1);
	}
	strOut = arrClean.join(' ');

	return strOut;
}
function takeOffFinalS(strIn)//takes off final s off a string. Used for the surname in the search page
{
	var intLastLetter = strIn.length - 1;
	var intPenultimate = intLastLetter;

	if (strIn.charAt(intLastLetter) == 's'){
	strIn = strIn.substring(0,intPenultimate);
}
return strIn;
}

function commaCancel(strIn){
	while (strIn.search(/,/) != -1){
		strIn = strIn.replace(/,/,"");
	}
	return strIn; 
}
    
function rightTrim(strIn){
    var spaceChar = String.fromCharCode(32);
    var intStringLength = strIn.length;
    var strOut = '';
    
    if(intStringLength < 0){
        return '';
    }
    
    var i = intStringLength - 1;
    
    while(i > -1){
        if(strIn.charAt(i) != spaceChar) {
            strOut = strIn.substring(0, i +1);
            break;
        }
        i--;
    } 
    return strOut;
}
 

function leftTrim(strIn){
    var spaceChar = String.fromCharCode(32);
    var intStringLength = strIn.length;
    var strOut = '';
    
    if(intStringLength < 1) {
        return '';
    }    
    
    var i = 0;
    
    while(i < intStringLength) {
        if(strIn.charAt(i) != spaceChar){
            strOut = strIn.substring(i,intStringLength);
            break;
        }        
        i++;
    } 
    return strOut;
} 

// This function removes sequences of multiple spaces
function multiTrim(strIn) {
	var strOut = '';
	var arrInLine = strIn.split(' ');
	
	if(arrInLine.length > 1) {
		for(var i=0; i < arrInLine.length; i++) {
			if(arrInLine[i].length > 0) {
				if(i < arrInLine.length -1) {
					strOut += arrInLine[i] + ' ';
				} else {
					strOut += arrInLine[i];
				}
			}
		}
	} else {
		strOut = strIn;
	}
	
	return strOut;
}
// TRIM FUNCTIONS END

//put Postcodes into upper case
function allUpper(obj) {
	obj.value = Trim(obj.value.toUpperCase());
}

//put Email addresses into lower case
function allLower(obj) {
	obj.value = Trim(obj.value.toLowerCase());
}

function stripTags() {
    for(var i=0;i<document.forms[0].elements.length;i++) {
        if (document.forms[0].elements[i].type) {
            if (document.forms[0].elements[i].type == "text" || document.forms[0].elements[i].type == "textarea") {
                    if (document.forms[0].elements[i].value.indexOf("<") >= 0) {
                        do {
                            document.forms[0][i].value = document.forms[0].elements[i].value.replace("<","&lt;")
                        }
                        while (document.forms[0].elements[i].value.indexOf("<") >= 0); 
                    }
                    
                    if (document.forms[0].elements[i].value.indexOf(">") >= 0) {     
                        do {
                            document.forms[0][i].value = document.forms[0].elements[i].value.replace(">","&gt;")
                        }
                        while (document.forms[0].elements[i].value.indexOf(">") >= 0);
                    }
            }
        }
    }
}

/* *** HELPER FUNCTIONS END *** */

/* *** VALIDATION FUNCTIONS START *** */

function promptFinish() {
    if(!confirm('Clicking on the \'Finish\' button means that you are happy with your application and do not want to add or change anything in it.\n\nIf you want to go back to your application now to change something click \'Cancel\'.\n\nIf you want to submit your finished application to us click \'OK\'.\n')) {
        return false;
    } else {
        return true;
    }
    return true;
}

function validateAge(obj) {

    if(obj.value.length > 0) {
        if(obj.value > 150) {
            alert('Ages cannot be greater than 150 years!');
            obj.focus();
            obj.select();
            return false;
        }
        
        if(obj.value < 0) {
            alert('Ages cannot be less than 0 years!');
            obj.focus();
            obj.select();
            return false;
        }
    } else {
        //alert('Please enter your age');
        obj.focus();
        obj.select();
        return false;
    }
    
    return true
}

/*
	This function validates Date form fields
*/
function validateDate(obj) {
	
	if(obj) {
	
	    if(obj.value.length <= 0 || obj.value == '') { // if there is no input
	        //alert('Please enter your date of birth.');		
		    obj.focus();
		    obj.select();
		    return false;
	    }
	    
	    // Declare valid date regular expression
	    var regEx = /\b(0?[1-9]|[12][0-9]|3[01])\/(1[0-2]|0?[1-9])\/\d\d\d\d$/;
    	
	    // Declare variables for the current date, year and month
	    var now = new Date();
	    var thisYear = now.getYear();
	    if(thisYear < 2000) {thisYear = thisYear + 1900;}
	    var thisMonth = now.getMonth()+1;
	    var thisDay = now.getDate();
	    var blnIsFuture = false;
	    var minYear = thisYear - 150;
	    var dateArr = obj.value.split('/');
	    var inputDay = dateArr[0];
	    if(inputDay.indexOf('0') == 0) {inputDay = inputDay.substring(1)};
	    var inputMonth = dateArr[1]; 
	    if(inputMonth.indexOf('0') == 0) {inputMonth = inputMonth.substring(1)};
	    var inputYear = dateArr[2]; 
	    var strDay = thisDay.toString();
	    var strMonth = thisMonth.toString();
	    if(strDay.length == 1) {strDay = '0'+strDay}
	    if(strMonth.length == 1) {strMonth = '0'+strMonth}
	    var strToday = strDay+'/'+strMonth+'/'+thisYear.toString();

	    if(!regEx.test(obj.value)) {
		    alert('The date must be in the format \'dd/mm/yyyy\'\nPlease re-enter the date.');	
		    obj.focus();
		    obj.select();
		    return false;
	    }	

	    if(parseInt(inputYear) > parseInt(thisYear)) {
		    blnIsFuture = true;		
	    } else {
		    if(parseInt(inputYear) == parseInt(thisYear) && parseInt(inputMonth) > parseInt(thisMonth)){
			    blnIsFuture = true;			
		    } else {
			    if(parseInt(inputYear) == parseInt(thisYear) && parseInt(inputMonth) == parseInt(thisMonth) && parseInt(inputDay) > parseInt(thisDay)) {
				    blnIsFuture = true;				
			    }
		    }
	    }
    	
	    if(blnIsFuture) {
		    alert('The date cannot be later than today!\nPlease re-enter the date.');		
		    obj.focus();
		    obj.select();
		    return false;
	    }
    	
	    if(parseInt(inputYear) <= parseInt(minYear)) {
		    alert('The date cannot be more than 150 years in the past!\nPlease re-enter the date.');		
		    obj.focus();
		    obj.select();
		    return false;
	    }
	    
	    switch(parseInt(inputMonth)) {
	        case 9:
	        case 4:
	        case 6:
	        case 11:
	            if(parseInt(inputDay) > 30) {
	                alert('This month only has 30 days!\nPlease try again.');
	                obj.focus();
		            obj.select();
	                return false;
	            }
	            break;
	        case 2:
	            if((parseInt(inputYear)%4 == 0 && (parseInt(inputYear)%100 != 0)) || (parseInt(inputYear)%400 == 0)) {
	                if(parseInt(inputDay) > 29) {
	                    alert('This month only has 29 days!\nPlease try again.');
                        obj.focus();
                        obj.select();
	                    return false;
	                }
	            } else {
	                if(parseInt(inputDay) > 28) {
	                    alert('This month only has 28 days!\nPlease try again.');
                        obj.focus();
                        obj.select();
	                    return false;
	                }
	            }
	            break;
	    }
	    
	    return true;
	} else {
	    return false;
	}
}

// Check that the minimum Contact Details have been entered
function validateContactDetails() {
	if(document.getElementById('_ctl0_txtContactName') && document.getElementById('_ctl0_txtContactSurname')) {
	
		if(document.getElementById('_ctl0_txtContactName').value == ''){			
			alert('Please enter your first Name');
			document.getElementById('_ctl0_txtContactName').focus();
			return false;
		}
		
		if(document.getElementById('_ctl0_txtContactSurname').value == ''){			
			alert('Please enter your Surname');
			document.getElementById('_ctl0_txtContactSurname').focus();
			return false;
		}
		
		if(document.getElementById('_ctl0_txtPhoneHome').value == '' && document.getElementById('_ctl0_txtPhoneMobile').value == '' && document.getElementById('_ctl0_txtPhoneWork').value == ''){			
			alert('Please enter at least one Phone Number');
			document.getElementById('_ctl0_txtPhoneHome').focus();
			return false;
		}
		
	} else {
		return false;
	}	
	return true;
}

function validateEmail(obj) {
    // Declare valid email regular expression
	var regEx = /(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6}/;
	if(obj.value.length > 0) {
	    if(!regEx.test(obj.value)) {
		    alert('Sorry but your email address \''+obj.value+'\' is not in a valid format!\nA valid format should be something like \'username@domainname.com\' or \'firstname.surname@domainname.co.uk\'\nPlease re-enter your email address, making sure it contains no spaces.');	
		    obj.select();
		    return false;
	    }
	} else {
	    alert('Please enter your email address.');	
		obj.select();
	    return false;
	}
	return true;
}

/* *** VALIDATION FUNCTIONS END *** */


/**** IMAGE GALLERY FUNCTIONS START */

function highlightImage(obj) {
    // switch off any highlighted images (there are a maximum of 12)
    for(var i=1;i<=12;i++) {
        if(document.getElementById('_ctl0_img'+i)) {
            document.getElementById('_ctl0_img'+i).className = 'defaultImg';
        }
    }
    
    // highlight the selected image
    if(obj) {
        obj.className = 'highlightImg';
    }
}

function fullsizeImage(imageURL) {

    if (document.all && window.print) {//if ie5+ use a modal window
        window.showModalDialog(imageURL,'Argument #1','help:0;resizable:1;dialogWidth:1024px;dialogHeight:768px');
    } else {
        window.open(imageURL,'','status=no, toolbar=no, menubar=no, resizable=yes, scrollbars=auto, height=768px, width=1024px');
    }

}

/**** IMAGE GALLERY FUNCTIONS END */