var isIE = false;
var req;
var divID;
function getXMLDoc(url, method, myID, variables, funct) {
	divID = (myID != null) ? myID : 'companies';
	if(funct == null) { funct = processReqChange; }
	req = (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
	req.onreadystatechange = funct;
	req.open(method, url, true);
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	req.setRequestHeader('If-Modified-Since', 'Wed, 15 Nov 1995 00:00:00 GMT');
	req.send(variables);
}
function processReqChange() {
    // only if req shows "complete"
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
			document.getElementById(divID).innerHTML = req.responseText;
        } else {
	        alert("There was a problem retrieving the XML data:\n" + req.statusText + " "+ req.status + ")");
        }
    }
}
function loadRFP() {
    // only if req shows "complete"
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
			document.getElementById('rfp').innerHTML = req.responseText;
			document.getElementById('rfp-bottom').innerHTML = req.responseText;
        } else {
	        alert("There was a problem retrieving the XML data:\n" + req.statusText + " "+ req.status + ")");
        }
    }
}
function loadCompanies(targ,selObj, year) {
	var cat = selObj.options[selObj.selectedIndex].value;
	//document.getElementById(divID). =
	getXMLDoc('/planner/lib/companies.php?cat=' + cat + '&year=' + year, 'GET');
}
function gotoPage(theForm) {
	//window.location='item.php?id=' + theForm.company.value;
	window.location=theForm.company.value;
}
function addRFP(id, name, cat) {
	var variables = 
		'id=' + id + 
		'&company_name=' + escape(name) +
		'&cat=' + escape(cat);
	getXMLDoc('/planner/rfp.php', 'POST', 'rfp', variables, loadRFP);
}
function selectall (me, theForm) {
	for(i=0;i < theForm.length; i++){
		if(theForm[i].name.indexOf(me.value) != -1) {
			if(theForm[i].checked) {
				theForm[i].checked = false;
			} else {
				theForm[i].checked = true;
			}
		}
	}
}
function submitForm() {
	document.getElementById('submit').innerHTML = '<input type="button" value="Processing...">';
	var submit = 0;
	var theForm = document.MYFORM;
	var error = "Following required fields are missing:\n";
	if(theForm.req_fname.value == "") {
		error += "First Name\n";
		submit = 1;
		document.getElementById('req_fname').style.backgroundColor = "#d7e6f2"
	} else {
		document.getElementById('req_fname').style.backgroundColor = "#FFFFFF"
	}
	if(theForm.req_lname.value == "") {
		error += "Last Name\n";
		submit = 1;
		document.getElementById('req_lname').style.backgroundColor = "#d7e6f2"
	} else {
		document.getElementById('req_lname').style.backgroundColor = "#FFFFFF"
	}
	if(theForm.req_sendertitle.value == "") {
		error += "Your Title\n";
		submit = 1;
		document.getElementById('req_sendertitle').style.backgroundColor = "#d7e6f2"
	} else {
		document.getElementById('req_sendertitle').style.backgroundColor = "#FFFFFF"
	}
	if(theForm.req_sendercompany.value == "") {
		error += "Your Company\n";
		submit = 1;
		document.getElementById('req_sendercompany').style.backgroundColor = "#d7e6f2"
	} else {
		document.getElementById('req_sendercompany').style.backgroundColor = "#FFFFFF"
	}
	if(theForm.req_sendercity.value == "") {
		error += "Your City\n";
		submit = 1;
		document.getElementById('req_sendercity').style.backgroundColor = "#d7e6f2"
	} else {
		document.getElementById('req_sendercity').style.backgroundColor = "#FFFFFF"
	}
	if(theForm.req_senderstate.value == "") {
		error += "Your State\n";
		submit = 1;
		document.getElementById('req_senderstate').style.backgroundColor = "#d7e6f2"
	} else {
		document.getElementById('req_senderstate').style.backgroundColor = "#FFFFFF"
	}
	if(theForm.req_challenge.value == "") {
		error += "Your Challenge\n";
		submit = 1;
		document.getElementById('req_challenge').style.backgroundColor = "#d7e6f2"
	} else {
		document.getElementById('req_challenge').style.backgroundColor = "#FFFFFF"
	}
	if(!validEmail(theForm.req_senderemail.value)) {
		error += "Email\n";
		submit = 1;
		document.getElementById('req_senderemail').style.backgroundColor = "#d7e6f2"
	} else {
		document.getElementById('req_senderemail').style.backgroundColor = "#FFFFFF"
	}
	if(submit == 0) {
		return true;
	} else {
		alert(error);
		document.getElementById('submit').innerHTML = '<input type="submit" value="Submit RFP" />';
		return false;
	}
}
function validEmail(email) {
	invalidChars = " /:,;"
	if (email == "") {
		return false
	}
	for (i=0; i<invalidChars.length; i++) {
		badChar = invalidChars.charAt(i)
		if (email.indexOf(badChar,0) > -1) {
			return false
		}
	}
	atPos = email.indexOf("@",1)
	if (atPos == -1) {
		return false
	}
	if (email.indexOf("@",atPos+1) != -1) {	
		return false
	}
	periodPos = email.indexOf(".",atPos)
	if (periodPos == -1) {
		return false
	}
	if (periodPos+3 > email.length)	{
		return false
	}
	return true
}

