function getvalue( frm, item )
{
	if(frm[item]) { return frm[item].value; }
	return "";
}
function checkform(frm)
{
    var fields = getvalue(frm, "requiredfields").split(",");
    var captions = getvalue( frm, "requiredfields_captions").split(",");
    var testZips = getvalue( frm, "requiredfields_zipcode").split(","); 
    var testEmails = getvalue( frm, "requiredfields_emails").split(",");
    
    var field,obj;
    var msg, checked=true;
    msg = '';
    try {
	    for(x=0;x<fields.length;x++) {
		field = fields[x];
		obj = frm[field];
		scontainer = null;
		if(!obj) { alert('There is an error in the form.\n\n  Field: ' + field + ' can not be found.'); return false; }
		if(obj.type==undefined && obj.length!=undefined && obj.length>1) {			
			if(obj[0].type.toLowerCase()=='radio' || obj[0].type.toLowerCase()=='checkbox') {
			
				var cntChecked = 0;
				obj[0].parentNode.style.backgroundColor= 'white';
				for(y = 0; y<obj.length; y++) {
					cntChecked= cntChecked +  obj[y].checked ? 1: 0;
					
				}
				
				if(cntChecked==0) { 
					scontainer = obj[0].parentNode;
					msg+=captions[x] + '\n';
					for(y = 0; y<obj.length; y++) {
						//obj[y].style.border = '1px solid red';
						
						
					}
				}
			}
				
		}
		else if(obj.type=='text') {
			obj.style.backgroundColor = 'white';
		    	if(obj.value.length==0) {		    	
		    		scontainer = obj;		    	
				msg+=captions[x] + '\n';
		    	}
		}
		else if(obj.tagName.toLowerCase()=='select') {
			obj.style.backgroundColor = 'white';
			if(obj.selectedIndex==0) {
				scontainer = obj;
				msg+=captions[x] + '\n';
			}
		}
		
		
		if(array_IndexOf(testZips, field) > -1 )
		{
			if(checkFormat( field, "^\k{5}([\-]\d{4})?$")==false ) 
			{
				scontainer = obj;
				msg+=captions[x] + ' format is not valid\n'
			}
		}
		if(array_IndexOf(testEmails, field) > -1 ) 
		{
			if(checkFormat( field, "^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$")==false ) 
			{
				scontainer = obj;
				msg+=captions[x] + ' format is not valid\n'
			}
		}
		if(scontainer) { scontainer.style.backgroundColor = '#ffc5c5'; }
	    }
	    
	}
	catch(e) {
	   	alert(e.description);
	}
    if(msg.length>0) {
        alert('Please complete the following areas:\n\n' + msg);
        return false;
    }
    return true;
}

function checkFormat(fld, regexformat )
{
	var re = new RegExp( regexformat, "igm");
	var obj = document.getElementById(fld );
	if(fld)
	{
		return re.test( obj.value );
	}
}

function array_IndexOf(ary, value)
{
	for(var i = 0; i<ary.length; i++)
	{
		if(ary[i]==value) {
			return i;
		}
	}
	return -1;
}