// true if blank or only whitespace - takes string as arg
function isBlank(s) {
	if ((s == null) || (s.length == 0)) return true;
	var whittle = /\S/;
	return ! whittle.test(s);
}

var isBlankOK = true;

// false if valid phone number : "+ -()" and digits
function notPhone(s) {
	if ((s == null) || (s.length == 0)) return false;
	// check for non digits and special chars
	var phoney = /[^\s\d\(\)\+-]/;
	return phoney.test(s);
}

// true if a valid email address
function notEmail(s) {
	if ( isBlank(s) ) return true;
	var mints = /^.+\@.+\..+$/;
	var badcha = /[\(\)\<\>\,\;\:\\\/\"\[\]]/;
	return !( mints.test(s) && ! badcha.test(s) );
}

// removes leading and trailing whitespace
function trim(s) {
	var foresp = /^\s+/;
	var backsp = /\s+$/;
	var neuter = s.replace(foresp, "");
	neuter = s.replace(backsp, "");
	return neuter;
}

function isInteger (s) {   
	var i;
    for (i = 0; i < s.length; i++)     {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (!isDigit(c)) return false;
    }
    // All characters are numbers.
    return true;
}

function notProperDate(f) {
	if (/\d{4}\-{1}\d{1,2}\-{1}\d{1,2}/.test(f))
		return false;
	else
		return true;
	
}

function isDigit (c) {   
	return ( ((c >= "0") && (c <= "9")) || (c == '-'))
}

function str_replace( text, expression, value){
	var exp = new RegExp(expression,'g')
	return text.replace(exp,value)
}


function checkProperDate(f, n) {
	if ( notProperDate(f.value) ) {
		f.value = ''
		msg = n + ' is not properly formatted. Please enter the date in yyyy-mm-dd format.'
		alert(msg)
		f.focus()		
		return false
	}  else {
		return true
	}	
}

function checkADate(f, n) {
	if (f=='--')
	{
		msg = n + ' is a required field. Please enter the date in yyyy-mm-dd format.';
		alert(msg);	
		//return false;
	}
	
	if ( notProperDate(f) ) {
		msg = n + ' is not properly formatted. Please enter the date in yyyy-mm-dd format.';
		alert(msg);	
		return false;
	}  else {
		return true;
	}	
}

// here are the uber functions that use the little checks above.
function checkBlank( f, n ) {
	if ( isBlank(f.value) ) {
		msg = '"' +  n + '" is a required field. Please fill it in'
		alert(msg)
		f.focus()
		return false
	}  else {
		return true
	} 
}

function checkLongerThan( f, n, reqLen ) {
	if ( trim(f.value).length <= reqLen) {
		var plural = 'character'
		if (reqLen+1 > 1)
		{
			plural += 's';
		}
		msg = '"' +  n + '" needs to be ' + (reqLen+1) + ' ' + plural + ' or longer.'
		alert(msg)
		f.focus()
		return false
	}  else {
		return true
	} 
}

function checkLength( f, n, reqLen ) {
	if ( trim(f.value).length != reqLen) {
		var plural = 'character'
		if (reqLen+1 > 1)
		{
			plural += 's';
		}
		msg = '"' +  n + '" needs to be ' + (reqLen+1) + ' ' + plural + ' long.'
		alert(msg)
		f.focus()
		return false
	}  else {
		return true
	} 
}

function checkInteger( f, n ) {
	if ( ! isInteger(f.value) ) {
		msg = n + ' needs to be numerical. Please use digits only. Spaces, commas and periods are not allowed.'
		alert(msg)
		f.focus()		
		return false
	}  else {
		return true
	} 
}

function checkMail( f, n ) {
	if ( notEmail(f.value) ) {
		msg = n + ' is not properly formatted. Please correct it.'
		alert(msg)
		f.focus()		
		return false
	}  else {
		return true
	} 
}

function checkPhoneNumber( f, n ) {
	if ( notPhone(f.value) ) {
		msg = n + ' is not properly formatted. Please use only numerical digits,(,),+ and -.'
		alert(msg)
		f.focus()		
		return false
	}  else {
		return true
	} 
}

function checkSame( f1, f2 ) {
	if ( !( f1.value == f2.value ) ) {
		msg = f1.value + ' does not match ' + f2.value;
		alert(msg)
		f2.focus()		
		return false
	}  else {
		return true
	} 
}

function checkConfirmPassword( pwd, cnf ) {
	if ( !( pwd.value == cnf.value ) ) {
		msg = 'Confirmation password does not match password. ';
		alert(msg)
		f2.focus()		
		return false
	}  else {
		return true
	} 
}

function checkEmail(f, n) {
	return ( checkBlank(f,n) && checkMail(f,n) )
}

function checkName(f, n) {
	return ( checkBlank(f,n) && checkLongerThan( f, n, 1 ) )
}

function checkCellPhone(f, n) {
	return ( checkBlank(f,n) && checkPhoneNumber(f,n) && checkLongerThan( f, n, 8 ) )
}

function checkPhone(f, n) {
	return ( checkBlank(f,n) && checkPhoneNumber(f,n) && checkLongerThan( f, n, 8 ) )
}

function checkRSAID(f, n) {
	return ( checkBlank(f,n) && checkInteger( f, n ) && checkLongerThan( f, n, 12 ) )
}

function checkRSAIDDate (f, n)
{

	var id = f.value;

	var year = parseInt( id.substr(0,2), 10 );
	var month = parseInt( id.substr(2,2), 10 );
	var day = parseInt( id.substr(4,2), 10 );

	month = month-1;

	var da = new Date( year, month, day );

	var y = da.getFullYear();
	var m = da.getMonth();
	var d = da.getDate();


	if ( y != parseInt( '19' + year ) && y != parseInt( '20' + year ) )
	{
		alert( n + ' is invalid.' );
		return false;
	}

	if ( m != month )
	{
		alert( n + ' is invalid.' );
		return false;
	}

	if (d != day)
	{
		alert( n + ' is invalid.' );
		return false;
	}
	
	return true;
}

function checkeredDate(f, n) {
	return ( checkBlank(f,n) && checkProperDate(f,n) )
}

function checkInt(f, n) {
	return ( checkBlank(f,n) && checkInteger(f,n) )
}

function checkBlint(f, n) {
	if (f.value == '') {
		return true;
	} else {
		return checkInteger(f,n)
	}
}

function isRadioChecked(f,n) {
	for(i=0; i<f.length; i++) {
		if( f[i].checked ) {
			return true
		} 
	}
	msg = n + ' is a required field. Please check your desired option.'
	alert(msg)
	return false
}

function checkSelected(f,n) {

	if ( f.selectedIndex == 0 ) {
		msg = n + ' is a required field. Please select your province.';
		alert( msg );
		f.focus();
		return false;
	} 
	
	return true;	
}

function checkChecked(f,n) {
	if ( f.checked ) {
		return true;
	}
	msg = n
	alert(msg)
	return false
}

function disAble(fit) {
	fit.disabled=true;
	fit.value=''
}

function enAble(fit) {
	fit.disabled=false;
}

function naanMe(sl) {
	if ( isNaN(sl)  ) {
		return 0
	} else {
		return sl
	}
}

function openWin(theURL) {
	var mainWin = window.open(theURL,'mainWindow','toolbar=0,status=0,scrollbars=1,resizable=no,width=400,height=200');
	mainWin.focus();
}
