//library.js - holds regex and functions used across validation routines

//Basic Regex validations (cross-form)
function checkEmail(formElement) {
	if (!(/^[\w-]+(\.[\w-]+)*@([a-z0-9-]+(\.[a-z0-9-]+)*?\.[a-z]{2,6}|(\d{1,3}\.){3}\d{1,3})(:\d{4})?$/.test($(formElement).value))){				
		return false;	
	} else {
		return true;
	}
}

function checkDate(formElement) {	
	if(!(/^(0?[1-9]|1[0-2])[\/](0?[1-9]|[12][0-9]|3[01])[\/]((19|20)[0-9]{2})$/.test($(formElement).value))) {
		return false;
	} else {
		return true;
	}
}

function checkYear(formElement) {	
	if(!(/^((19|20)[0-9]{2})$/.test($(formElement).value))) {		
		return false;	
	} else {
		return true;
	}
}

function checkZip(formElement) {
	return(/^([0-9]{5})(-[0-9]{4})?$/.test($(formElement).value));	
}

function checkPhone(formElement) {
	return (/^([0-9]{3}-)?([0-9]{3}-){1}([0-9]{4}){1}$/.test($(formElement).value));	
}

function checkCheckbox(formElement) {	
	return formElement.checked;
}

function checkRadio(formElement) {
	var cnt = -1;
	
	for (var i=formElement.length-1; i > -1; i--) {
        if (formElement[i].checked) {cnt = i; i = -1;}
    }
	
    if (cnt == -1) {		
		return false;
	}
		
	return true;	
}

function getRadioValue(formElement) {
	var value = false;
	for (var i=formElement.length-1; i > -1; i--) {
        if (formElement[i].checked) {value = formElement[i].value; break;}
    }	
	
	return value;
}

function checkSelected(formElement) {
	var selections = 0;
	
	for(var i = 0; i < formElement.options.length; i++) {
		if(formElement.options[i].selected)
		{		  
			selections++;
			return true;		
		}
	}
	
	return false;
}

function Left(str, n){
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}

function Right(str, n){
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}

function help(x){
	var MyWin = open(x, "displayWindow", "left=100,top=100,height=500,width=410,toolbar=no,menubar=no,statusbar=no,scrollbars=yes,resizable=yes");
}

function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
		num = "0";
	
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
		cents = "0" + cents;
	
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num = num.substring(0,num.length-(4*i+3))+','+ num.substring(num.length-(4*i+3));

	return (((sign)?'':'-') + '$' + num + '.' + cents);
}