// JavaScript Document
var xmlhttp;
var allProducts = Array();
var products = Array();

function loadXMLProducts(url,productQuery) {
	xmlhttp=null;	
	//Build product here
	products = productQuery.split('-');
	// code for Mozilla, etc.
	if (window.XMLHttpRequest){		
  		xmlhttp=new XMLHttpRequest()
  	}
	// code for IE
	else if (window.ActiveXObject){
  		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  	}
	if (xmlhttp!=null){
  		xmlhttp.onreadystatechange=DataReceived;
  		xmlhttp.open("GET",url+'?r='+Math.random(),true);
  		xmlhttp.send(null);
  	}
	else{
  		alert("Your browser does not support XMLHTTP.")
  	}
  	
}

function checkReadyState(obj){
	if(obj.readyState == 4){
		if(obj.status == 200){
    		return true;
    	}
    	else {
    		alert("Problem retrieving XML data");
    	}
  	}
}

function checkReadyState(obj){
	if(obj.readyState == 4){
		if(obj.status == 200){
    		return true;
    	}
    	else {
    		alert("Problem retrieving XML data");
    	}
  	}
}

function DataReceived(){
	//alert("XML Received!");
	if(checkReadyState(xmlhttp)){		
		var x;
		var response;
		if (window.XMLHttpRequest){												
			if (window.ActiveXObject){			
				var xmlDocument = new ActiveXObject("Microsoft.XMLDOM");  		
				xmlDocument.loadXML(xmlhttp.responseText);
		  		x = xmlDocument.getElementsByTagName("product");
	  		}
	  		else{
	  			response = xmlhttp.responseXML;				
				x = response.getElementsByTagName("product");
	  		}	  					
	  	} else {
				var xmlDocument = new ActiveXObject("Microsoft.XMLDOM");  		
				xmlDocument.loadXML(xmlhttp.responseText);
		  		x = xmlDocument.getElementsByTagName("product");	  	
	  	} 
	  	//alert("data Len:" + x.length);
  		for (i=0;i<x.length;i++){
			var obj = new Object;
			for(j=0;j<x[i].childNodes.length;j++){
				var thisNode = x[i].childNodes[j];
				if(thisNode.tagName == 'id'){
					if(thisNode.firstChild){
						obj.id = thisNode.firstChild.data;
					}
				}
				if(thisNode.tagName == 'groupId'){
					if(thisNode.firstChild){
						obj.groupId = thisNode.firstChild.data;
					}
				}				
				if(thisNode.tagName == 'name'){
					obj.name = thisNode.firstChild.data;
					obj.checkName = thisNode.firstChild.data.toLowerCase().replace(/ /g, '_');
				}
				if(thisNode.tagName == 'applyNow'){
					if(thisNode.firstChild){
						obj.applyNow = thisNode.firstChild.data;
					}
				}
				if(thisNode.tagName == 'learnMore'){
					if(thisNode.firstChild){
						obj.learnMore = thisNode.firstChild.data;
					}
				}
				if(thisNode.tagName == 'rightFor'){
					if(thisNode.firstChild)
						obj.rightFor = thisNode.firstChild.data;
					else
						obj.rightFor = '&nbsp;';
				}
				if(thisNode.tagName == 'details'){
					if(thisNode.firstChild)
						obj.details = thisNode.firstChild.data;
					else
						obj.details = '&nbsp;';
				}
				if(thisNode.tagName == 'fixedPayment'){
					if(thisNode.firstChild)
						obj.fixedPayment = thisNode.firstChild.data;
					else
						obj.fixedPayment = '&nbsp;';
				}
				if(thisNode.tagName == 'personalCollateral'){
					if(thisNode.firstChild)
						obj.personalCollateral = thisNode.firstChild.data;
					else
						obj.personalCollateral = '&nbsp;';
				}
			}
			allProducts.push(obj);
		}
		if(products.length && products[0] != ""){			
			buildTable('select');
		}
		else{
			buildTable('all');
		}
	}
}

function buildTable(flag){
	//alert("Build table: " + flag);
	var output = '';
	output += "<div class=\"rounded\"><img src=\"/AMIPortal\/framework\/skins\/amigreen\/images\/bizoodle\/images\/bg_table717_top.gif\" width=\"717\" height=\"10\" alt=\"\" /></div>";
	output += "<table cellspacing=\"0\">";
	output += "<tr>";
    output += "<th><div>Apply</div>";
	output += "<div class=\"small-text\">Select all that<br />you wish to apply for</div></th>";
    output += "<th><div>Products</div></th>";
    output += "<th><div>This option is right for</div></th>";
    output += "<th><div>Details</div></th>";
    output += "<th><div>Payment Options </div></th>";
    output += "<th class=\"last\"><div>Personal collateral required</div></th>";
    output += "</tr>";	
	
	if(flag == 'select'){
		for(i=0;i<products.length;i++){
			output += "<tr>";
			if(allProducts[products[i]].applyNow){
				output += "<td class=\"check\"><input type=\"checkbox\" name=\"product" + allProducts[products[i]].id + "\" id=\"product" + allProducts[products[i]].id + "\" onclick=\"javascript:clickCheckBox('" + allProducts[products[i]].id + "','" + allProducts[products[i]].groupId + "')\"/></td>";
			}
			else{
				output += "<td class=\"check\"><input type=\"checkbox\" disabled=\"disabled\" /></td>";
			}
			if(allProducts[products[i]].learnMore){
				output += "<td><div><a href=\"" + allProducts[products[i]].learnMore + "\">" + allProducts[products[i]].name + "</a></div></td>";
			}
			else{
				output += "<td><div>" + allProducts[products[i]].name + "</div></td>";
			}
			output += "<td><div>" + allProducts[products[i]].rightFor + "</div></td>";
			output += "<td><div>" + allProducts[products[i]].details + "</div></td>";
			output += "<td><div>" + allProducts[products[i]].fixedPayment + "</div></td>";
			output += "<td><div>" + allProducts[products[i]].personalCollateral + "</div></td>";
			output += "</tr>";
		}
	}
	else if(flag == 'all'){
		for(i=0;i<allProducts.length;i++){
			output += "<tr>";
			if (allProducts[i].name == 'Merchant Cash Advance'){
				output += "<td class=\"check\"><input type=\"checkbox\" checked=\"checked\" name=\"product" + allProducts[i].id + "\" id=\"product" + allProducts[i].id + "\" onclick=\"javascript:clickCheckBox('" + allProducts[i].id + "','" + allProducts[i].groupId + "')\" /></td>";		
			} else if (allProducts[i].applyNow){
				output += "<td class=\"check\"><input type=\"checkbox\" name=\"product" + allProducts[i].id + "\" id=\"product" + allProducts[i].id + "\" onclick=\"javascript:clickCheckBox('" + allProducts[i].id + "','" + allProducts[i].groupId + "')\" /></td>";
			} else {
				output += "<td class=\"check\"><input type=\"checkbox\" disabled=\"disabled\" /></td>";
			}
			if(allProducts[i].learnMore){
				output += "<td><div><a href=\"" + allProducts[i].learnMore + "\">" + allProducts[i].name + "</a></div></td>";
			}
			else{
				output += "<td><div>" + allProducts[i].name + "</div></td>";
			}
			output += "<td><div>" + allProducts[i].rightFor + "</div></td>";
			output += "<td><div>" + allProducts[i].details + "</div></td>";
			output += "<td><div>" + allProducts[i].fixedPayment + "</div></td>";
			output += "<td><div>" + allProducts[i].personalCollateral + "</div></td>";
			output += "</tr>";
		}
	}
	
	output += "</table>";
	output += "<div class=\"rounded\"><img src=\"/AMIPortal\/framework\/skins\/amigreen\/images\/bizoodle\/images\/bg_table717_btm.gif\" width=\"717\" height=\"10\" alt=\"\" /></div>";
	document.getElementById('output-table').innerHTML = output;	
}