// JavaScript Document
function init(p) {
	if (p == 'index' || p == 'viewbid') { loadXML(); }
	
}

function loadXML(p) {
	var frm = document.forms[0];
	//var svr = frm.Server_Name.value;
	//var thisdb = frm.ThisDB.value;
	//var user = frm.User.value;
	var objResults = document.all['proctable'];
	
	var XmlUrl = 'xml/procsAsXML.xml';
	var XslUrl = 'xslt/pgcountyprocs.xsl';
	
	xmlDoc = new ActiveXObject('MSXML2.FreeThreadedDOMDocument');
	//xmlDoc = new ActiveXObject('MSXML2.DOMDocument.3.0');
	xmlDoc.async = false;
	xmlDoc.validateOnParse = true;
	xmlDoc.load (XmlUrl);
	if (xmlDoc.parseError.errorCode != 0) {
		objResults.innerHTML = showError(xmlDoc);	
		return false;
	}
	
	xslDoc = new ActiveXObject('MSXML2.FreeThreadedDOMDocument');
	//xslDoc = new ActiveXObject('MSXML2.DOMDocument.3.0');
	xslDoc.async = false;
	xslDoc.validateOnParse = true;
	xslDoc.load (XslUrl);
	if (xslDoc.parseError.errorCode != 0) {
		objResults.innerHTML = showError(xslDoc);
		return false;	
	}
	
	//var strResult = xmlDoc.transformNode(xslDoc);
	//objResults.innerHTML = strResult;
	
	var transformer = new ActiveXObject("MSXML2.XSLTemplate");
     transformer.stylesheet = xslDoc.documentElement;
     
	var xslproc = transformer.createProcessor();
     xslproc.input = xmlDoc;
     
    //var roles = frm.UserRoles.value;
	var role = querystring('role') == null  | querystring('role') == "" ? "user" : querystring('role');
	var bidno = querystring('bidno') == null  | querystring('bidno') == "" ? "." : escape(querystring('bidno'));
	var mode = querystring('mode') == null  | querystring('mode') == "" ? "view" : querystring('mode');
	var dl = querystring('dl') == null  | querystring('dl') == "" ? "0" : querystring('dl');
	var f = querystring('filter') == null  | querystring('filter') == "" ? "all" : querystring('filter');
	var s = querystring('sort') == null  | querystring('sort') == "" ? "bidnum" : querystring('sort');
	var d = querystring('dir') == null  | querystring('dir') == "" ? "ascending" : querystring('dir');
	//var roles = "user";
    //var role = new Array();
    //role = roles.split(',');
    //for (i=0; i<role.length;i++) {
    		//alert(role[i]); 
			/*   		
    		if (role=='vendor' || role=='admin') {
				xslproc.addParameter("showDL", "y", "");
				//exit;	
			}
			*/
			xslproc.addParameter("role", role, "");
			xslproc.addParameter("bid", unescape(bidno), "");
			xslproc.addParameter("mode", mode, "");
			xslproc.addParameter("dl", dl, "");
			xslproc.addParameter("filter", f, "");
			xslproc.addParameter("srt", s, "");
			xslproc.addParameter("srt_dir", d, "");		
    //}    
     
     xslproc.transform();
     objResults.innerHTML = xslproc.output;
     	
	return true;
}

querystring.keys = new Array();
querystring.values = new Array();

function querystring(key) {
	var value = null;
	for (var i=0;i<querystring.keys.length;i++) {
		if (querystring.keys[i]==key) {
			value = querystring.values[i];
			break;
		}
	}
	return value;
}

function querystring_Parse() {
	var query = unescape(window.location.search.substring(1));
	var pairs = query.split("&");
	//document.write(pairs.length + '<br>');	

	for (var i=0;i<pairs.length;i++) {
		var pos = pairs[i].indexOf('=');
		if (pos >= 0) {
			var argname = pairs[i].substring(0,pos);
			var value = pairs[i].substring(pos+1);
			querystring.keys[querystring.keys.length] = argname;
			querystring.values[querystring.values.length] = value;
			//document.write(argname + ' = ' + value + '<br>');
		}
	}
}

login_names = new Array("bidder","admin","user","buyer","clerk");
login_pwds = new Array("passwordb","passworda","passwordu","passwordy","passwordc");
login_emails = new Array("bidder@co.pg.md.us","admin@co.pg.md.us","user@co.pg.md.us","buyer@co.pg.md.us","clerk@co.p.md.us");
login_qs = new Array("What is your pet's name?","What is your mother's maiden name?","What is your mother's maiden name?","In what city were you born?", "What is your mother's maiden name?")
login_answer = new Array("redskin", "maidena", "maidenu","landover","maidenc");

function welcome() {
	//var objWelcome = document.all['welcome'];
	var w = querystring('role') == null  | querystring('role') == ""  | querystring('role') == "user" ? "Login" : "Welcome " + querystring('role');
	document.write(w);
}

function navigation() {
	var r = querystring('role') == null  | querystring('role') == ""  ? "user" : querystring('role');
	var bidno = querystring('bidno') == null  | querystring('bidno') == "" ? "." : escape(querystring('bidno'));
	var col1 = "<tr><td class='l' valign=top scope=row width='5%'><font class=aSmall>&gt;</font></td>";
	var col1b = "<tr><td class='lb' valign=top scope=row width='5%'><font class=aSmall>&gt;</font></td>";
	
	var login = "<td class='r' valign=top scope=row width='95%'><a href='login.aspx'>Login</a></td></tr>";
	var forgot = "<td class='r' valign=top scope=row width='95%'><a href='forgot.aspx'>Forgot your Password</a></td></tr>";
	var register = "<td class='r' valign=top scope=row width='95%'><a href='register.aspx'>Register for New Account</a></td></tr>";
	var registerb = "<td class='rb' valign=top scope=row width='95%'><a href='register.aspx'>Register for New Account</a></td></tr>";
	var add = "<td class='r' valign=top scope=row width='95%'><a href='viewbid.htm?role=" + r + "&mode=new&bidno=new'>Add New Bid</a></td></tr>";
	var walkin = "<td class='rb' valign=top scope=row width='95%'><a href='register.htm?role=" + r + "&mode=new&bidno=" + unescape(bidno) + "&regtype=walk-in'>Walk-In Registration</a></td></tr>";
	
	if (r == 'admin' | r == 'buyer') {
		document.write(col1 + add);
	}
	document.write(col1 + login + col1 + forgot);
	if (r == 'admin' | r == 'clerk') {
		document.write(col1 + register);
		document.write(col1b + walkin);
	} else {
		document.write(col1b + registerb);
	}
}

function proc_filters() {
	var r = querystring('role') == null  | querystring('role') == ""  ? "user" : querystring('role');
	var cell1 = "<TR><TD scope=row>&nbsp;&nbsp;&nbsp;</TD>";
	var cell2 = "<TD vAlign=top scope=row width='5%'><FONT class=aSmall>&nbsp;</FONT></TD>";
	var cell3 = "<TD vAlign=top scope=row width='95%'>";
	var endrow = "</TD></TR>";
	
	var txt1 = "<FONT class=aSmall>&gt;&nbsp;&nbsp;</FONT><A href='index.aspx?filter=construction_only'>Construction Only Bids</A>";
	var txt2 = "<FONT class=aSmall>&gt;&nbsp;&nbsp;</FONT><A href='index.aspx?filter=mbe_restriction_only'>MBE Only Bids</A>";
	
	document.write(cell1 + cell2 + cell3 + txt1 + endrow);
	document.write(cell1 + cell2 + cell3 + txt2 + endrow);

}

function showFilter() {
	var f = querystring('filter') == null  | querystring('filter') == "" ? "all" : querystring('filter');
	var filterHTML = "";
	
	switch (f) {
		case "mbe":
			filterHTML = " - MBE Only";
			break;
		case "constr":
			filterHTML = " - Construction Only";
			break;
		default:
		
	}
	document.write(filterHTML);
}

function validate() {
	var frm = document.forms["login"];
	var nme = frm.username.value;
	var pwd = frm.pwd.value;
	var bidno = querystring('bidno') == null  | querystring('bidno') == "" ? "." : escape(querystring('bidno'));
	var dl = querystring('dl') == null  | querystring('dl') == "" ? "0" : querystring('dl');
	var c=true;
	var found = false;
	
	if (nme=="" | pwd=="") {
		alert("Please enter a valid username and password");
		c=false;
		frm.username.value="";
		frm.pwd.value=""
	}
	
	if (c) {
		for (var i=0;i<login_names.length;i++) {
			//alert(login_names[i]);
			if (login_names[i]==nme) {
				found = true;
				if (login_pwds[i]==pwd) {
					if (dl=="1") {
						document.location.href = "viewbid.htm?role="+login_names[i]+"&bidno="+bidno+"&dl=1";
						break;
					} else {
						if (login_names[i]=="admin") {
							document.location.href = "admin.htm?role="+login_names[i];
						}
						else {
							document.location.href = "index.htm?role="+login_names[i];
						}						
						break;
					}
				} else {
					//invalid password
					document.location.href = "login.htm?error="+escape('Your password does not match the one we have on file');
					frm.username.value="";
					frm.pwd.value=""
					break;		
				}
			} 
		}
		if (!(found)) {
			//invalid user
			document.location.href = "login.htm?error="+escape(nme + ' is not a registered user of the system');
			frm.username.value="";
			frm.pwd.value=""
		}
	}

}

function validateUser() {
	var frm = document.forms['forgot'];
	
}

function registerUser() {
	var frm = document.forms['register'];
	var bidno = querystring('bidno') == null  | querystring('bidno') == "" ? "." : escape(querystring('bidno'));
	var dl = querystring('dl') == null  | querystring('dl') == "" ? "0" : querystring('dl');
	var nme = frm.contactname.value;
	document.location.href="thankyou_v.htm?nme=" + nme;
}
function walkinReg() {
	var r = querystring('role') == null  | querystring('role') == ""  ? "user" : querystring('role');
	var bidno = querystring('bidno') == null  | querystring('bidno') == "" ? "." : escape(querystring('bidno'));
	document.location.href="register.htm?role=" + r + "&bidno=" + bidno + "&regtype=walk-in";
}
function registerWalkin() {
	var frm = document.forms['register'];
	var role = querystring('role') == null  | querystring('role') == "" ? "user" : querystring('role');
	var bidno = querystring('bidno') == null  | querystring('bidno') == "" ? "." : escape(querystring('bidno'));
	var regtype = querystring('regtype') == null  | querystring('regtype') == "" ? "online" : querystring('regtype');
	if (regtype == "walk-in") {
		var bidHTML = "<tr>";  
		bidHTML += "<td class='lb' width='25%' bgColor=#666699>";
		bidHTML += "<font face='trebuchet ms' color=#ffffff size=3>Bid/Proposal No.</font>";
		bidHTML += "</td>";
		bidHTML += "<td class='rb'>";
		bidHTML += "<input type='text' name='bidno' size='10' value='" + unescape(bidno) +"'>";
		bidHTML += "</td>";
		bidHTML += "</tr>";
		document.write(bidHTML);
	}
}
function registerUI(){
	var frm = document.forms['register'];
	var role = querystring('role') == null  | querystring('role') == "" ? "user" : querystring('role');
	var bidno = querystring('bidno') == null  | querystring('bidno') == "" ? "." : escape(querystring('bidno'));
	var regtype = querystring('regtype') == null  | querystring('regtype') == "" ? "online" : querystring('regtype');
	
	var pwdHTML = "<tr>";  
		pwdHTML += "<td class='lb' width='25%' bgColor=#666699>";
		pwdHTML += "<font face='trebuchet ms' color=#ffffff size=3>Password</font>";
		pwdHTML += "</td>";
		pwdHTML += "<td class='rb'>";
		pwdHTML += "<input type='password' name='password1' size='12'>";
		pwdHTML += "&nbsp;&nbsp;please re-type for confirmation&nbsp;&nbsp;";
		pwdHTML += "<input type='password' name='password2' size='12'>";
		pwdHTML += "</td>";
		pwdHTML += "</tr>";
	var secHTML = "<tr>";
		secHTML += "<td class='lb' width='25%' bgColor=#666699 align='right'>";
		secHTML += "<font face='trebuchet ms' color=#ffffff size=3>Security Question</font>";
		secHTML += "</td>";
		secHTML += "<td class='rb'>";
		secHTML += "<select name='question'>";
		secHTML += "<option value='0' selected></option>";
		secHTML += "<option value='1'>What is your mother's maiden name?</option>";
		secHTML += "<option value='2'>In what city were you born?</option>";
		secHTML += "<option value='3'>What is your pet's name?</option>";
		secHTML += "</select>";
		secHTML += "</td>";
		secHTML += "</tr>";
	var ansHTML = "<tr>";
		ansHTML += "<td class='lb' width='25%' bgColor=#666699 align='right'>";
		ansHTML += "<font face='trebuchet ms' color=#ffffff size=3>Answer</font>";
		ansHTML += "</td>";
		ansHTML += "<td class='rb'><input type='text' name='answer' size='20'></td>";
		ansHTML += "</tr>";
	var walkHTML = "<tr>";
		walkHTML += "<td colspan='2' bgcolor=#666699 align='center'>";
		walkHTML += "<font face='trebuchet ms' color=#ffffff size=3><b>Walk-In Registration</b></font>";
		walkHTML += "</td>"
		walkHTML += "</tr>"
		walkHTML += "<tr>"
		walkHTML += "<td class='lb' width='25%' bgColor=#666699>";
		walkHTML += "<font face='trebuchet ms' color=#ffffff size=3>Paid by</font>";
		walkHTML += "</td>";
		walkHTML += "<td class='rb'>";
		walkHTML += "<select name='paidby'>";
		walkHTML += "<option value='0' selected></option>";
		walkHTML += "<option value='cash'>Cash</option>";
		walkHTML += "<option value='check'>Check</option>";
		walkHTML += "</select>"
		walkHTML += "</td>";
		walkHTML += "</tr>";
		walkHTML += "<tr>"
		walkHTML += "<td class='lb' width='25%' bgColor=#666699>";
		walkHTML += "<font face='trebuchet ms' color=#ffffff size=3>Amount</font>";
		walkHTML += "</td>";
		walkHTML += "<td class='rb'>";
		walkHTML += "<input type='text' name='amountpaid' size='6'>";
		walkHTML += "</td>";
		walkHTML += "</tr>";
	var roleHTML = "<tr>";
		//roleHTML += "<td colspan='2' bgcolor=#666699 align='center'>";
		//roleHTML += "<font face='trebuchet ms' color=#ffffff size=3><b>role</b></font>";
		//roleHTML += "</td>"
		//roleHTML += "</tr>"
		
		//roleHTML += "<tr>"
		roleHTML += "<td class='lb' width='25%' bgColor=#666699>";
		roleHTML += "<font face='trebuchet ms' color=#ffffff size=3>Role</font>";
		roleHTML += "</td>";
		roleHTML += "<td class='rb'>";
		roleHTML += "<select name='role'>";
		roleHTML += "<option value='0' selected></option>";
		roleHTML += "<option value='clerk'>Clerk</option>";
		roleHTML += "<option value='buyer'>Buyer</option>";
		roleHTML += "<option value='admin'>Admin</option>";
		roleHTML += "</select>"
		roleHTML += "</td>";
		roleHTML += "</tr>";	
	
	switch (regtype) {
		case "online":
			document.write(pwdHTML + secHTML + ansHTML);
			break;
		case  "walk-in":
			document.write(walkHTML);
			break;
		case "user":
			document.write(pwdHTML + roleHTML);
			break;
		default:	
			document.write(pwdHTML + secHTML + ansHTML);
	}	
		
}

function reportMenu() {
	var r = querystring('role') == null  | querystring('role') == "" ? "user" : querystring('role');
	
	var col1 = "<tr><td class='l' valign=top scope=row width='5%'><font class=aSmall>&gt;</font></td>";
	var col1b = "<tr><td class='lb' valign=top scope=row width='5%'><font class=aSmall>&gt;</font></td>";
	var ques = "<td class='r' valign=top scope=row width='95%'><a href='#?role=" + r + "'>All Submitted Questions</a></td></tr>";
	var receive = "<td class='r' valign=top scope=row width='95%'><a href='#?role=" + r + "'>All Bidders that have Received Info.</a></td></tr>";
	var receiveb = "<td class='rb' valign=top scope=row width='95%'><a href='#?role=" + r + "'>All Bidders that have Received Info.</a></td></tr>";
	var walkin = "<td class='r' valign=top scope=row width='95%'><a href='#?role=" + r + "'>On-Line vs. Walk-In Registration</a></td></tr>";
	var walkinb = "<td class='rb' valign=top scope=row width='95%'><a href='#?role=" + r + "'>On-Line vs. Walk-In Registration</a></td></tr>";
	
	var rptHTML = "<table borderColor=#666699 cellSpacing=0 cellPadding=1 width='100%' border=0>";
		rptHTML += "<tr>";
		rptHTML += "<th class='b' bgColor=#666699 colSpan=2>";
		rptHTML += "<font face='trebuchet ms' color=#ffffff size=3>";
		rptHTML += "Reports"
		rptHTML += "</font>";
		rptHTML += "</th>";
		rptHTML += "</tr>";
		
		//rptHTML += "</table>";
	switch (r) {
		case "buyer":
			document.write("<br>" + rptHTML + col1 + ques + col1b + receiveb + "</table>");
			break;
		case "admin":
			document.write("<br>" + rptHTML + col1 + ques + col1 + receive + col1b + walkinb + "</table>");
			break;
	}
								
}

function submitQues() {
	var frm = document.forms['viewbid'];
	document.location.href="submitques.htm?role="+frm.role.value+"&bidno="+frm.bid.value;
}
function saveQues() {
	var frm = document.forms['ques'];
	var bid = querystring('bidno');
	var role = querystring('role');
	var d = new Date().toLocaleString();
	var q = frm.question.value;
	
	//var url="procsQuesAsXML.xml";
	//xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
	//xmlDoc.async = false;
	//xmlDoc.load (url);
	//var doc = xmlDoc.selectSingleNode("/pgcountyprocs/procurement/bidnum");
	//alert (doc.text);
	//xmlDoc.save(url);	
	
	document.location.href="thankyou.htm?role="+role+"&bidno="+bid;
}
function viewQues() {
	var frm = document.forms['viewbid'];
	document.location.href="viewbid.htm?mode=ques&role="+frm.role.value+"&bidno="+frm.bid.value;
}
function sendQues() {
	var frm = document.forms['ques'];
	var bid = querystring('bidno');
	var txtHTML = "<a href=\'mailto:bids@co.pg.md.us?subject=questions about bid no." + bid + "\'>bids@co.pg.md.us</a>";
	document.write(txtHTML);
}
function editBid() {
	var frm = document.forms['viewbid'];
	document.location.href="viewbid.htm?mode=edit&role="+frm.role.value+"&bidno="+frm.bid.value;
}
function updateBid() {

}
function downloadBid() {
	var frm = document.forms['viewbid'];
	var role = querystring('role') == null  | querystring('role') == "" ? "user" : querystring('role');
	var bid = querystring('bidno');
	var url = "confirm.htm?role="+frm.role.value+"&bidno="+frm.bid.value;
	var winOpts = "height=300,width=500";
	switch (role) {
		case "user":
			window.open(url,"confirm", winOpts);
			//document.location.href="confirm.htm?role="+frm.role.value+"&bidno="+frm.bid.value;
			break;
		default:
			document.location.href="viewbid.htm?role="+frm.role.value+"&bidno="+frm.bid.value+"&dl=1";
	}
}
function confirmRegister(c) {
	var frm = document.forms['confirm'];
	var bid = querystring('bid_id');
	switch (c) {
		case "yes":
			opener.document.location.href="login.aspx?bid_id="+ bid
			break;
		default:
			opener.document.location.href="register.aspx?bid_id="+ bid
	}
	//alert(c);
	window.close();
}
