//JavaScript utility functions for RMWEBPORTAL
// (c) I.V.Developments 2004

function capitalise_initial(fld){
	//capitalises first letter of each word in string
	
	fld.value=fld.value.replace(/\b\w+\b/g, function(word){return word.substring(0,1).toUpperCase() + word.substring(1);});
}
	
function capitalise_all(fld){
	//capitalises whole string
	fld.value=fld.value.toUpperCase();
    }
	
function tel_check(fld){
	//allows 0-9,*,#
	//needs changing to stop editing adding inval chars
	
	if (fld.value.charCodeAt(fld.value.length -1) > 47 && fld.value.charCodeAt(fld.value.length -1) < 58 || fld.value.charCodeAt(fld.value.length -1)== 42 || fld.value.charCodeAt(fld.value.length -1)== 35) {
	    return true;
	}
	
	fld.value = fld.value.slice(0,fld.value.length-1)
	
}
	
function clear_all(fld){
    //function clears field
    fld.value="";
}

function num_check(fld){
	//allows 0-9
	//needs changing to stop editing adding inval chars
	if (fld.value.charCodeAt(fld.value.length -1) > 47 && fld.value.charCodeAt(fld.value.length -1) < 58 ) {
	    return true;
	}
	else {
	    fld.value = fld.value.slice(0,fld.value.length-1)
	}
}

function valhours(fld){
    //limits hours to max 23
    //makes sure field has leading 0 if single digit entered
    if (fld.value.length > 1){
		if (fld.value > "23"){
			fld.value = "23";
		}	
	}

    else {
	    if (fld.value > "") {
			fld.value = "0" + fld.value
		}
		else {
		    fld.value = "00"
		}
	}
}

function valmins(fld) {
    //limits minutes to 59
    //makes sure there is a leading 0 if only one digit entered
    if (fld.value.length > 1){
		if (fld.value > "59"){
			fld.value = "59";
		}	
	}
    else {
	    if (fld.value > "") {
		    fld.value = "0" + fld.value;
	    }
	    else {
		    fld.value = "00";
	    }
	}
}

// SJ 2008 04 25
// Client side validator for pwd length
function validateFieldLength (source, args)
{
    // Default Value

    args.IsValid = true;

    // Simulating a check against a database

    if (args.Value.length < 8)
        args.IsValid = false;
}


function PostcodeChars(fld){
	//Removes space character
	if (fld.value.charCodeAt(fld.value.length -1) == 32) {
	    fld.value = fld.value.slice(0,fld.value.length-1);
	}
	// Allow NUMBERS
	else if (fld.value.charCodeAt(fld.value.length -1) > 47 && fld.value.charCodeAt(fld.value.length -1) < 58 ){
	    return true;
	}
	// Allow CAPITAL LETTERS
	else if (fld.value.charCodeAt(fld.value.length -1) > 64 && fld.value.charCodeAt(fld.value.length -1) < 91 ){
	    return true;
	}
	// Allow LOWERCASE LETTERS
	else if (fld.value.charCodeAt(fld.value.length -1) > 96 && fld.value.charCodeAt(fld.value.length -1) < 123 ){
	    return true;
	}
	else {
	    fld.value = fld.value.slice(0,fld.value.length-1);
	}
}


//function enableCheckBox(txtbox, strCbox){
function enableCheckBox(txtbox){
    if (txtbox.value.length > 0) {
        document.getElementById('cbPhone').parentElement.disabled = false;
        document.getElementById('cbPhone').disabled = false;
    }
    else {
        document.getElementById('cbPhone').disabled = true;
        document.getElementById('cbPhone').parentElement.disabled = true;
    }
}
