function validate_empty(field, fieldName) {
    var error = "";

    with (field)
    {
    	if (value.length == 0) {
        	style.background = 'Yellow';
        	error = fieldName + " field is required.\n";
    	} else {
        	style.background = 'White';
    	}
   	}
    return error;
}


function validate_email(field, fieldName)
 {
    var error = "";

    with (field)
    {
	    if (value == "") {
	        error = fieldName + " must be filled.\n";
	        style.background = 'Yellow';
			return error;
	   	}
        apos = value.indexOf("@");
        dotpos = value.lastIndexOf(".");
        if (apos < 1 || dotpos - apos < 2)
        {
			error = fieldName + " is not a valid e-mail address!\n";
            style.background = 'Yellow';
        }
        else {
	        style.background = 'White';
        }
    }
    return error;
}

function validate_phone(field, fieldName)
 {
    var error = "";

    with (field)
    {
	    if (value == "") {
	        error = "You didn't enter a phone number.\n";
	        style.background = 'Yellow';
	    } else if (isNaN(parseInt(stripped))) {
	        error = "The phone number contains illegal characters.\n";
	        style.background = 'Yellow';
	    } else if (! (stripped.length == 10)) {
	        error = "The phone number is the wrong length. Make sure you included an area code.\n";
	        style.background = 'Yellow';
	    }
    }
    return error;
}

function validate_registration(thisform, validateFile, hashKey)
 {
    var reason = "";
    
//	reason += validate_empty(thisform.firstName, "First Name");
//	reason += validate_empty(thisform.lastName, "Last Name");
//	reason += validate_empty(thisform.firmName, "Firm Name");
//	reason += validate_empty(thisform.title, "Title");
//	reason += validate_empty(thisform.addressStreet, "Firm Address 1");
//	reason += validate_empty(thisform.addressCity, "Firm City");
//	reason += validate_empty(thisform.stateId, "Firm State");
//	reason += validate_empty(thisform.addressZip, "Firm Zip");
//    reason += validate_email(thisform.email, "Email Address"); 
//    reason += validate_empty(thisform.username, "User ID");
//    reason += validate_empty(thisform.password, "Password");		

	if (reason != "") {
		alert (reason);
		return false;
	}
	
	var hashedPassWithKey=b64_sha1(thisform.password.value + hashKey);
	thisform.password.value=hashedPassWithKey;  
}

function validate_reset_password(thisform)
{
   var reason = "";
   
   reason += validate_email(thisform.emailAddress, "Email Address"); 

	if (reason != "") {
		alert (reason);
		return false;
	}
}

function validate_login(thisform, validateFile, hashKey)
{	
   var reason = "";
   
   reason += validate_empty(thisform.logon, "User ID");
   reason += validate_empty(thisform.password, "Password");		

	if (reason != "") {
		alert (reason);
		return false;
	}

	var hashedPassWithKey=b64_sha1(thisform.password.value + hashKey);
	thisform.password.value=hashedPassWithKey;  
	
}

function validate_confirm(field1, field2) {
    var error = "";

	if (field1.value != field2.value) {
		field1.style.background = 'Yellow';
		field2.style.background = 'Yellow';
		
    	error = "Passwords do not match.\n";
	} else {
    	field1.style.background = 'White';
    	field2.style.background = 'White';
	}

    return error;
}



function validate_change_password(thisform, validateFile, hashKey)
{	
   var reason = "";
   
   reason += validate_empty(thisform.password, "New Password");
   reason += validate_empty(thisform.password2, "Confirm Password");
   reason += validate_confirm(thisform.password, thisform.password2);
   

	if (reason != "") {
		alert (reason);
		return false;
	}

	var hashedPassWithKey=b64_sha1(thisform.password.value + hashKey);
	thisform.password.value=hashedPassWithKey;  
	
}


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

