
function validate_and_submit_form(){
    var ids = new Array('cr_bldg','cr_floor','cr_contact','cr_contact_ph','cr_contact_mc','cr_dept','cr_prob_code','cr_detail');
    var key;
    var errors_found = 0;
    for(key in ids){
        if(document.getElementById(ids[key]).value == ''){
            errors_found++;
            document.getElementById(ids[key]).style.border='2px solid Red';
            document.getElementById(ids[key]).style.color='Red';
        }
        else{
            document.getElementById(ids[key]).style.border='1px solid #777777';
            document.getElementById(ids[key]).style.color='Black';
        }
    }
    
    if(errors_found > 0){
        alert('You left ' + errors_found + ' required field(s) blank!');
    }
    else if(! check_email(document.getElementById('cr_contact_mc').value)){
        alert('That is not a valid email address!');
        document.getElementById('cr_contact_mc').style.border='2px solid Red';
        document.getElementById('cr_contact_mc').style.color='Red';
    }
    else{
        document.getElementById('request_form').submit();
    }
}




// Check if a string is in valid email format. 
function check_email(str){
    var regex = /^[-_.a-z0-9]+@(([-_a-z0-9]+\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i;
    return regex.test(str);
}

// Check for correct phone number
function check_phone(str) {
     rePhoneNumber = new RegExp(/^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/);
 
     if (!rePhoneNumber.test(s)) {
          alert("Phone Number Must Be Entered As: (xxx) xxx-xxxx");
          return false;
     }
	return true;
}

//only display buildings that exist in selected department...
function refresh_bldg_select(){   
    
	//proceed only if object is not busy...
	if(xmlHttp.readyState == 4 || xmlHttp.readyState == 0){
        var dept,bldg;
        dept=document.getElementById('cr_dept').value;
        bldg=document.getElementById('cr_bldg').value;
        url_str="ajax.php?mode=bldg_select&dept=" + dept + '&bldg=' + bldg;
        
		//process the request
		xmlHttp.open("GET", url_str, true);
		xmlHttp.onreadystatechange = update_location_select;
		xmlHttp.send(null);
	}
	else{
		//if busy, try again in 1 second
		setTimeout('refresh_bldg_select()',1000);
	}
}

//only display floors that exist in selected building...
function refresh_floor_select(){
    
	//proceed only if object is not busy...
	if(xmlHttp.readyState == 4 || xmlHttp.readyState == 0){
        var dept, bldg,floor;
        dept=document.getElementById('cr_dept').value;
        bldg=document.getElementById('cr_bldg').value;
        floor=document.getElementById('cr_floor').value;
        url_str='ajax.php?mode=floor_select&dept=' + dept + '&bldg=' + bldg + '&floor=' + floor;
        
		//process the request
		xmlHttp.open("GET", url_str, true);
		xmlHttp.onreadystatechange = update_location_select;
		xmlHttp.send(null);
	}
	else{
		//if busy, try again in 1 second
		setTimeout('refresh_floor_select()',1000);
	}
}

//only display floors that exist in selected building...
function refresh_loc_select(){
   
	//proceed only if object is not busy...
	if(xmlHttp.readyState == 4 || xmlHttp.readyState == 0){
        var dept, bldg,floor,loc;
        dept=document.getElementById('cr_dept').value;
        bldg=document.getElementById('cr_bldg').value;
        floor=document.getElementById('cr_floor').value;         
        loc=document.getElementById('cr_loc').value;         
        url_str='ajax.php?mode=loc_select&dept=' + dept + '&bldg=' + bldg + '&floor=' + floor + '&loc=' + loc;
        
        //alert(url_str);
		
		//process the request
		xmlHttp.open("GET", url_str, true);
		xmlHttp.onreadystatechange = update_location_select;
		xmlHttp.send(null);
	}
	else{
		//if busy, try again in 1 second
		setTimeout('refresh_loc_select()',1000);
	}
}
function update_location_select(){
	//move forward only if transaction has completed...
	if(xmlHttp.readyState == 4){
		//status of 200 indicates successful completion
		if(xmlHttp.status == 200){
			xmlResponse = xmlHttp.responseXML;
			xmlRoot = xmlResponse.documentElement;
			
            id_array = xmlRoot.getElementsByTagName('target');
			code_array = xmlRoot.getElementsByTagName('code');
            
            id_value = id_array.item(0).firstChild.data;
            code_value = code_array.item(0).firstChild.data;

    		document.getElementById(id_value).innerHTML=decodeURIComponent(code_value);
            if(id_value=='bldg_container'){
                setTimeout('refresh_floor_select()',10);
            }
            if(id_value=='floor_container'){
                setTimeout('refresh_loc_select()',10);
            }
		}
		else{
			alert('Problem accessing the server: ' + xmlHttp.statusText);
		}
	}
}