// 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(s) {
	var datey = /^\d\d\d\d-\d\d-\d\d$/;
	return ! datey.test(s);
}

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
	}	
}


// 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 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 checkEmail(f, n) {
	return ( checkBlank(f,n) && checkMail(f,n) )
}

function checkPhone(f, n) {
	return ( checkBlank(f,n) && checkPhoneNumber(f,n) )
}

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 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();
}
