//Adaptive Image - javascript form validation support functions - Clickpoints */

//validate email
function ValidEmail(email) {
var invalidChars = ' /:,;';
	
	if (email == '') { return false; }
	if (email.length  > 50) { return false;	}
	for (i=0; i<invalidChars.length; i++) {	//scan through and reject matching invalid chars
		if (email.indexOf(invalidChars.charAt(i),0) > -1) {	return false; }	//bad char in email string
	}
	atPos = email.indexOf('@',1);	//check for @ from second char onward
	if (atPos == -1) { return false; } // no @ found
	if (email.indexOf('@',atPos+1) > -1) { return false; }	//check for @ from next char onward - another @ found
	periodPos = email.indexOf('.',atPos)	//check for . from next char onward
	if (periodPos == -1) { return false; }	//no . found
	if (periodPos+3 > email.length)	{ return false; }	//check for at least 2 chars after .
	
	return true;
}

//phone number check - ensure string is trimmed first
function IsPhoneNum(num) {
var validChars = '0123456789()+ '; //NB allows space - must trim first
	if (num == '') return false;
	for (i=0; i<num.length; i++) {	//scan through and reject nonmatching valid chars
		if (validChars.indexOf(num.charAt(i),0) < 0) return false;	//bad char in number string
	}
	return true;
}

//is number check
function IsNum(num) {
var validChars = '0123456789.';
var dpcount = 0;
	if (num == '') return false;
	for (i=0; i<num.length; i++) {	//scan through and reject nonmatching valid chars
		if (validChars.indexOf(num.charAt(i),0) < 0) return false;	//bad char in number string
		if (num.charAt(i)=='.') dpcount++; //tally decimal points
		if (dpcount > 1) return false; //invalid number
	}
	return true;
}

//is integer number check
function IsInt(num) {
var validChars = '0123456789';
var dpcount = 0;
	if (num == '') return false;
	for (i=0; i<num.length; i++) {	//scan through and reject nonmatching valid chars
		if (validChars.indexOf(num.charAt(i),0) < 0) return false;	//bad char in number string
	}
	return true;
}

//is uk postcode check
function IsPcode(code) {
var pcodeRE = new RegExp('^[A-Z]{1,2}[0-9][0-9A-Z]? ?[0-9][ABDEFGHJLNPQRSTUWXYZ]{2}$');
return(pcodeRE.test(code));
}

//password check
function ValidPword(pword) {
//##
	if (pword == '') return false;
	return true;
}

//password confirmation check
function ValidPConf(pconf,pword) {
//##
	if (pconf != pword) return false;
	return true;
}

//email confirmation check
function ValidEConf(econf,email) {
//##
	if (econf != email) return false;
	return true;
}

//validate weblink url
function ValidURL(url) {
	if (url != '' || url.substr(0,7) == 'http://' || url.substr(0,7) == 'HTTP://') return true;
return false;
}

//form input block enter/return key submitting form
function block_enter(e) {
var code;
	if (!e) var e = window.event;
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;
	if (code == 13) { //if character code is equal to ascii 13 (if enter key)
		return false;
	}
	else return true; //return true to the event handler
return true;
}

//form input block chars except 0-9, BS, DEL - need Left, Right cursor, enter
function block_non_numeric(e) {
var code;
	if (!e) var e = window.event;
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;
	if (code == '') return false;
	if (((code>47) && (code<58)) || code==8 || code==127 || code==13) return true;
	else return false;
}

//form textarea limit max chars - use onkeyup="limit_chars(this,[number])"
function limit_chars(txtarea,maxchars) {
	if(txtarea.value.length > maxchars) { //over limit
		txtarea.value = txtarea.value.substr(0,maxchars); //truncate
	}
}

//trim whitespace from beginning and end of string
function trim(str)
{
    while (str.substring(0, 1) == " "
            || str.substring(0, 1) == "\n"
            || str.substring(0, 1) == "\r")
    {
        str = str.substring(1, str.length);
    }

    while (str.substring(str.length - 1, str.length) == " "
            || str.substring(str.length - 1, str.length) == "\n"
            || str.substring(str.length - 1, str.length) == "\r")
    {
        str = str.substring(0, str.length - 1);
    }

    return str;
}

//cope with javascript rounding disability!
/* This script is Copyright (c) Paul McFedries and 
Logophilia Limited (http://www.mcfedries.com/).
Permission is granted to use this script as long as 
this Copyright notice remains in place.*/
function round_decimals(original_number, decimals) {
    var result1 = original_number * Math.pow(10, decimals)
    var result2 = Math.round(result1)
    var result3 = result2 / Math.pow(10, decimals)
    return pad_with_zeros(result3, decimals)
}
function pad_with_zeros(rounded_value, decimal_places) {
    // Convert the number to a string
    var value_string = rounded_value.toString()
    // Locate the decimal point
    var decimal_location = value_string.indexOf(".")
    // Is there a decimal point?
    if (decimal_location == -1) {
        // If no, then all decimal places will be padded with 0s
        decimal_part_length = 0
        // If decimal_places is greater than zero, tack on a decimal point
        value_string += decimal_places > 0 ? "." : ""
    }
    else {
        // If yes, then only the extra decimal places will be padded with 0s
        decimal_part_length = value_string.length - decimal_location - 1
    }
    // Calculate the number of decimal places that need to be padded with 0s
    var pad_total = decimal_places - decimal_part_length
    if (pad_total > 0) {
        // Pad the string with 0s
        for (var counter = 1; counter <= pad_total; counter++) 
            value_string += "0"
        }
    return value_string
}
