// override IE's native getElementById as it is badly broken

if (/msie/i.test (navigator.userAgent)) //only override IE
{
	document.nativeGetElementById = document.getElementById;
	document.getElementById = function(id)
	{
		var elem = document.nativeGetElementById(id);
		if(elem)
		{
			//make sure that it is a valid match on id
			if(elem.attributes['id'].value == id)
			{
				return elem;
			}
			else
			{
				//otherwise find the correct element
				for(var i=1;i<document.all[id].length;i++)
				{
					if(document.all[id][i].attributes['id'].value == id)
					{
						return document.all[id][i];
					}
				}
			}
		}
		return null;
	};
}



function check_form(formid) {
    var ids_list = document.getElementsByName('_mandatory_ids_'+formid);
    var names_list = document.getElementsByName('_mandatory_names_'+formid);

    if (names_list.length == 0) {
        names_list = [];
    }
    if (ids_list.length == 0) {
        ids_list = [];
    }

    var submit_ok = 1;
    // for most fields use ID
    for(var i = 0; i < ids_list.length; i++) {
        var thisinput = document.getElementById(ids_list[i].value);
        // do stuff based on form control type
        if ((thisinput.type == 'text') || (thisinput.type == 'textarea')){
            if (thisinput.value == '') {
                alert('Please fill in the form field marked "'+ thisinput.title + '"');
                submit_ok = 0;
            }  
        } else if (thisinput.type == 'select-one') {
            if (thisinput.selectedIndex == -1) {
                alert('Please fill in the form field marked "' + thisinput.title + '"');
                submit_ok = 0;
            }
        }
    }
    // for checkboxes use field name
    for(var j = 0; j < names_list.length; j++) {
        var inputs = document.getElementsByName(names_list[j].value);
        var nothing_checked = 1;
        for(var k = 0; k < inputs.length; k++) {
            if (inputs[k].checked) {
                nothing_checked = 0;
            }
        }
        if (nothing_checked == 1) {
            alert('Please fill in the form field marked "'+ inputs[j].title + '"');
            submit_ok = 0;
        }
    }   

    if (submit_ok == 1) {
        document.getElementById(formid).submit();
	
    }
}


function show_menu(cellid) {
    var thiscell = document.getElementById(cellid);
    thiscell.className = 'navOption visible';
}

function hide_menu(cellid) {
    var thiscell = document.getElementById(cellid);
    thiscell.className = 'navOption';
}

function checkcanoeform() {
    var namefield = document.getElementById('_sendername');
    var emailaddress = document.getElementById('_emailaddress');
    var where = document.getElementById('where');
    var firstmodel = document.getElementById('_1_makemodel');
    var firstvalue = document.getElementById('_1_value');
    var trailervalue = document.getElementById('trailers');
    
    var ok = 1;
    if (namefield.value == '') {
        ok = 0;   
        alert('Please fill in your name');    
    }
    if (emailaddress.value == '') {
        ok = 0;
        alert('Please fill in your email address');
    }
    if (trailervalue.value != '') {
	var trailermatch = (/^[0-9]*$/).test(trailervalue.value);
	if (trailermatch == false) {
	    alert('Please include numbers only in the Further Cover field');
	    ok = 0;
	}
    }

    var empty = 1;
    for (var i=1; i<11; i++) {
        var thismodel = document.getElementById('_'+i+'_makemodel');
        var thisvalue = document.getElementById('_'+i+'_value');
        var thisyear = document.getElementById('_'+i+'_year');
        if ((thismodel.value != '') || (thisvalue.value != '')) {
            empty = 0;
        }
        if ((thismodel.value != '' && thisvalue.value == '') || (thismodel.value == '' && thisvalue.value != '')) { 
            alert('Please complete make/model row '+i);
            ok = 0;
	}
	if (thismodel.value != '' && thisyear.value == '') {
	    alert('Please complete the Year field for make/model row '+i);
	    ok = 0;
	}
	if (thisvalue.value != '') {
	    var match = (/^[0-9]*$/).test(thisvalue.value);
	    if (match == false) {
		ok = 0;
		alert('Please include numbers only for the Value field on row '+i);
		break;
	    }
	}
    }
    if (empty == 1) {
        ok = 0;
        alert('Please fill in the make/model and value of your canoe(s)');
    }
    if (where.value == '') {
        ok = 0;
        alert('Please tell us where you heard about us');
    }
    if (ok == 1) {
        document.mainform.submit();
    }
}

