function trim(s) {

    if((s==null)||(typeof(s)!='string')||!s.length) {
        return '';
    }
    return s.replace(/^\s+/,'').replace(/\s+$/,'');

}

function check_blank(field) {

    if (field == '') {
        return false;
    }
    if (trim(field) == '') {
        return false;
    }
    return true;
}

function isDigit(num) {

    if (num.length>1) {
        return false;
    }
    var string="1234567890";
    if (string.indexOf(num)!=-1) {
        return true;
    }
    return false;
}

function isInteger(val){

    for(var i=0;i<val.length;i++) {
        if(!isDigit(val.charAt(i))) {
            return false;
        }
    }
    return true;
}

function validate_seat(age, feet, inches, weight) {
    
    if (!check_blank(age)) {
	alert("Age cannot be blank.");
	return false;
    }
    else if (!isInteger(age)) {
	alert("Age must be an integer value.");
	return false;
    }
    else if (!check_blank(feet)) {
        alert("Feet value cannot be blank.");
	return false;
    }
    else if (!isInteger(feet)) {
        alert("Feet value must be an integer.");
        return false;
    }
    else if (!check_blank(inches)) {
        alert("Inches value cannot be blank.");
        return false;
    }
    else if (!isInteger(inches)) {
        alert("Inches value must be an integer.");
        return false;
    }
    else if(inches > 11) {
	alert("Inches value must be less than 12.");
	return false;
    }
    else if (!check_blank(weight)) {
        alert("Weight cannot be blank.");
        return false;
    }
    else if (!isInteger(weight)) {
        alert("Weight value must be an integer.");
        return false;
    }

    return true;
}

function calculate_seat() {
    
    var age = document.calcForm.age.value;
    var feet = document.calcForm.feet.value;
    var inches = document.calcForm.inches.value;
    var weight = document.calcForm.weight.value;

    var totalInches = (feet*12)+inches;

    if (validate_seat(age, feet, inches, weight)) {

   	if ((weight >= 20) && (weight < 40)) {
	    window.location = "forward_facing.php";
	}

	else if ((age < 1) && (weight < 20)) {
	    window.location = "rear_facing.php";
	}

	else if (((age >= 1) && (age < 9)) || ((weight >= 40) && (weight < 80)) || (totalInches < 57)) {
	    window.location = "booster_seats.php";
	}

        //if ((age >= 9) || (weight >= 80) || (totalInches >= 57)) {
	else {
	    window.location = "seat_belts.php";
	}
    }

    else {
        return false;
    }
}

