﻿

//    <id></id>
//    <name></name>
//    <cheque_book></cheque_book>
//    <monthly_statement></monthly_statement>
//    <earns_interest></earns_interest>


/* POPULATE osd_products FROM osd_products.xml --------------------------------- */

if (window.XMLHttpRequest) {
    xhttp=new XMLHttpRequest();
}
else {
    xhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.open("GET","../../personal-banking/banking/osd_products.xml",false);
xhttp.send("");
xmlDoc = xhttp.responseXML; 

var root1 = xmlDoc.getElementsByTagName('Table');

if(root1.length > 0) {

    var rows = root1[0].getElementsByTagName('Row');

    var osd_products = new Array(rows.length + 1);

    // Skip first row as header
    for(i = 1; i < rows.length; i++) {
        
        osd_products[i] = new Array(20);
        var row = rows[i];
        
        //alert(row.getElementsByTagName('Cell')[1].firstChild.nodeValue);
        if(row.getElementsByTagName('Cell')[0].firstChild!=null) {
            
            // ADD HERE, when new attributes added in XML
            
            var product_id = row.getElementsByTagName('Cell')[0].firstChild.childNodes[0].nodeValue;
            
            
            //var aaa = row.getElementsByTagName('Cell')[0].firstChild.childNodes[0].nodeValue;
            //alert(aaa);
            //alert(product_id);
        
            if(row.getElementsByTagName('Cell')[1]!=null) {
                if(row.getElementsByTagName('Cell')[1].firstChild!=null) {
                    var name = row.getElementsByTagName('Cell')[1].firstChild.childNodes[0].nodeValue;
                    osd_products[product_id]["name"] = name;
                }
            }
            
            if(row.getElementsByTagName('Cell')[2]!=null) {
                if(row.getElementsByTagName('Cell')[2].firstChild!=null) {
                    var category = row.getElementsByTagName('Cell')[2].firstChild.childNodes[0].nodeValue;
                    osd_products[product_id]["category"] = category;
                }
            }
            
            if(row.getElementsByTagName('Cell')[3]!=null) {
                if(row.getElementsByTagName('Cell')[3].firstChild!=null) {
                    var description = row.getElementsByTagName('Cell')[3].firstChild.childNodes[0].nodeValue;
                    osd_products[product_id]["description"] = description;
                }
            }
            
            if(row.getElementsByTagName('Cell')[4]!=null) {
                if(row.getElementsByTagName('Cell')[4].firstChild!=null) {
                    var suitability = row.getElementsByTagName('Cell')[4].firstChild.childNodes[0].nodeValue;
                    osd_products[product_id]["suitability"] = suitability;
                }
            }
            
            if(row.getElementsByTagName('Cell')[5]!=null) {
                if(row.getElementsByTagName('Cell')[5].firstChild!=null) {
                    var cheque_book = row.getElementsByTagName('Cell')[5].firstChild.childNodes[0].nodeValue;
                    osd_products[product_id]["cheque_book"] = cheque_book;
                }
            }
            
            if(row.getElementsByTagName('Cell')[6]!=null) {
                if(row.getElementsByTagName('Cell')[6].firstChild!=null) {
                    var monthly_statement = row.getElementsByTagName('Cell')[6].firstChild.childNodes[0].nodeValue;
                    osd_products[product_id]["monthly_statement"] = monthly_statement;
                }
            }
            
            if(row.getElementsByTagName('Cell')[7]!=null) {
                if(row.getElementsByTagName('Cell')[7].firstChild!=null) {
                    var earns_interest = row.getElementsByTagName('Cell')[7].firstChild.childNodes[0].nodeValue;
                    osd_products[product_id]["earns_interest"] = earns_interest;
                }
            }
            //!!
            if(row.getElementsByTagName('Cell')[8]!=null) {
                if(row.getElementsByTagName('Cell')[8].firstChild!=null) {
                    var debit_card = row.getElementsByTagName('Cell')[8].firstChild.childNodes[0].nodeValue;
                    osd_products[product_id]["debit_card"] = debit_card;
                }
            }
            
            if(row.getElementsByTagName('Cell')[9]!=null) {
                if(row.getElementsByTagName('Cell')[9].firstChild!=null) {
                    var smartchange = row.getElementsByTagName('Cell')[9].firstChild.childNodes[0].nodeValue;
                    osd_products[product_id]["smartchange"] = smartchange;
                }
            }
            
            if(row.getElementsByTagName('Cell')[10]!=null) {
                if(row.getElementsByTagName('Cell')[10].firstChild!=null) {
                    var salary_credit_giro = row.getElementsByTagName('Cell')[10].firstChild.childNodes[0].nodeValue;
                    osd_products[product_id]["salary_credit_giro"] = salary_credit_giro;
                }
            }
            
            if(row.getElementsByTagName('Cell')[11]!=null) {
                if(row.getElementsByTagName('Cell')[11].firstChild!=null) {
                    var atm = row.getElementsByTagName('Cell')[11].firstChild.childNodes[0].nodeValue;
                    osd_products[product_id]["atm"] = atm;
                }
            }
            
            if(row.getElementsByTagName('Cell')[12]!=null) {
                if(row.getElementsByTagName('Cell')[12].firstChild!=null) {
                    var internet_mobile_banking = row.getElementsByTagName('Cell')[12].firstChild.childNodes[0].nodeValue;
                    osd_products[product_id]["internet_mobile_banking"] = internet_mobile_banking;
                }
            }
            
            if(row.getElementsByTagName('Cell')[13]!=null) {
                if(row.getElementsByTagName('Cell')[13].firstChild!=null) {
                    var ealerts = row.getElementsByTagName('Cell')[13].firstChild.childNodes[0].nodeValue;
                    osd_products[product_id]["ealerts"] = ealerts;
                }
            }
            
            if(row.getElementsByTagName('Cell')[14]!=null) {
                if(row.getElementsByTagName('Cell')[14].firstChild!=null) {
                    var phone_banking = row.getElementsByTagName('Cell')[14].firstChild.childNodes[0].nodeValue;
                    osd_products[product_id]["phone_banking"] = phone_banking;
                }
            }
            
            if(row.getElementsByTagName('Cell')[15]!=null) {
                if(row.getElementsByTagName('Cell')[15].firstChild!=null) {
                    var fees_charges = row.getElementsByTagName('Cell')[15].firstChild.childNodes[0].nodeValue;
                    osd_products[product_id]["fees_charges"] = fees_charges;
                }
            }
            
            if(row.getElementsByTagName('Cell')[16]!=null) {
                if(row.getElementsByTagName('Cell')[16].firstChild!=null) {
                    var age = row.getElementsByTagName('Cell')[16].firstChild.childNodes[0].nodeValue;
                    osd_products[product_id]["age"] = age;
                }
            }
            
            if(row.getElementsByTagName('Cell')[17]!=null) {
                if(row.getElementsByTagName('Cell')[17].firstChild!=null) {
                    var minimum_open_balance = row.getElementsByTagName('Cell')[17].firstChild.childNodes[0].nodeValue;
                    osd_products[product_id]["minimum_open_balance"] = minimum_open_balance;
                }
            }

            if(row.getElementsByTagName('Cell')[18]!=null) {
                if(row.getElementsByTagName('Cell')[18].firstChild!=null) {
                    var apply_url = row.getElementsByTagName('Cell')[18].firstChild.childNodes[0].nodeValue;
                    osd_products[product_id]["apply_url"] = apply_url;
                }
            }
            
        }
        
        
        
    }

}

//alert(osd_products[1]["name"]);
//alert(osd_products[2]["name"]);

//if(osd_products[1]==null) { alert('NULL'); }
//else { alert('ok!'); }


/* COOKIE FUNCTIONS ----------------------------------------------------------- */

function addProduct(name, value) {

    var name1 = "osdproduct_" + name
	var days = 1;
	var date = new Date();
	date.setTime(date.getTime()+(days*24*60*60*1000));
	var expires = "; expires="+date.toGMTString();
	document.cookie = name1+"="+value+expires+"; path=/";
	
	var checkedProducts = countCheckedProducts();
	//alert(checkedProducts);
	if(checkedProducts < 3) {
		var name2 = "osdproduct_checked_" + name
		document.cookie = name2+"="+value+expires+"; path=/";
	}
	
	updateProductBox();
	
}

function checkProduct(name) {
    name = "osdproduct_" + name
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0)
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function removeProduct(name) {
	addProduct(name, '');
	updateProductBox();
}


/* GENERAL FUNCTIONS ----------------------------------------------------------- */

function updateProductBox() {

    var output = "";
    var count = 0;
    
    if(osd_products.length > 0 ) {
        
        output += "\n\n<table width=\"174\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" align=\"center\">\n";
    
        for(i = 1; i <= osd_products.length; i++) {
            if(checkProduct(i)) {
                //document.getElementById("prodcompare_holder").innerHTML += osd_products[i]['name'] + "<br />";
                
                
				output += "<tr>\n";
						
                if(checkProduct('checked_' + i)) {
				    output += "<td><input type=\"checkbox\" name=\"compare\" value=\"" + i + "\" onclick=\"checkboxProduct('" + i + "', this);\" checked=\"checked\" /></td>\n";
                    //output += "<input type=\"checkbox\" name=\"compare\" value=\"" + i + "\" onclick=\"checkboxProduct('" + i + "', this);\" checked=\"checked\" /> "
                }
                else {
				    output += "<td><input type=\"checkbox\" name=\"compare\" value=\"" + i + "\" onclick=\"checkboxProduct('" + i + "', this);\" /></td>\n";
                    //output += "<input type=\"checkbox\" name=\"compare\" value=\"" + i + "\" onclick=\"checkboxProduct('" + i + "', this);\" /> "
                }
                
                //output += osd_products[i]['name'];
                //output += " <input type=\"button\" value=\"remove\" onclick=\"removeProduct('" + i + "'); removeProduct('checked_" + i + "');\" />";
                //output += "<br />";
                
				output += "<td>" + osd_products[i]['name'] + "</td>\n";
				output += "<td align=\"center\"><a href=\"javascript:void(0);\" onclick=\"removeProduct('" + i + "'); removeProduct('checked_" + i + "');\">Delete</a></td>\n";
				output += "</tr>\n";
				
                count++;
            }
        }
    }
    if(count == 0) {
        //output = "No products selected";
    }
    else {
    
		output += "<tr><td>&nbsp;</td><td>&nbsp;</td><td align=\"center\">&nbsp;</td></tr>\n";
		//output += "<tr><td colspan=\"3\" align=\"right\"><input type=\"image\" id=\"prodcompare_compare_btn\" src=\"images/compare-btn.gif\" onclick=\"return compareBtnCheck();\" /></td></tr>\n";
		output += "<tr><td colspan=\"3\" align=\"right\"><img src=\"/image/deposits/compare-btn.gif\" onclick=\"compareBtnCheck();\" style=\"cursor:pointer; _cursor:hand;\" /></td></tr>\n";
		//output += "<br /><input id=\"prodcompare_compare_btn\" type=\"submit\" value=\"COMPARE\" disabled=\"disabled\" />";
		output += "</table>\n\n";
		
    }
		
    document.getElementById("prodcompare_holder").innerHTML = output;
    
    checkProductLimit();

}

function checkboxProduct(name, cbox) {

    if(cbox.checked == true) {
        addProduct('checked_' + name);
    }
    else {
        removeProduct('checked_' + name);
    }
    
    checkProductLimit();

}

function countCheckedProducts() {
    
    var check_count = 0;
    var prodcompare_holder = document.getElementById("prodcompare_holder");
	var elms = prodcompare_holder.getElementsByTagName("*");
	
    for (var i = 0; i < elms.length; i++ ) {
        if (elms[i].type == 'checkbox') {
            if (elms[i].checked == true) {
                //alert(elms[i].name);
                check_count++;
            }
        }
    }
    
    return check_count;
    
}

function checkProductLimit() {

    var check_count = countCheckedProducts();
    
    var prodcompare_holder = document.getElementById("prodcompare_holder");
	var elms = prodcompare_holder.getElementsByTagName("*");
    
    //alert(check_count);
    if(check_count >= 3) {
        // Disable all checkboxes
        for (var i = 0; i < elms.length; i++ ) {
            if (elms[i].type == 'checkbox') {
                if (elms[i].checked == false) {
                    elms[i].disabled = "disabled";
                }
            }
        }
    }
    else {
        // Enable all checkboxes
        for (var i = 0; i < elms.length; i++ ) {
            if (elms[i].type == 'checkbox') {
                if (elms[i].checked == false) {
                    disabled = "";
                }
            }
        }
    }
    
    /*if(check_count >= 2) {
        // Enable Compare button
        document.getElementById("prodcompare_compare_btn").disabled = "";
    }
    else {
        // Disable Compare button
        document.getElementById("prodcompare_compare_btn").disabled = "disabled";
    }*/

}

function compareBtnCheck() {
    
    var count = countCheckedProducts();
    if(count > 1) {
        //document.prodcompare_form.submit();
		
		var products = "";
		var prodcompare_holder = document.getElementById("prodcompare_holder");
		var elms = prodcompare_holder.getElementsByTagName("*");
		for (var i = 0; i < elms.length; i++ ) {
			if (elms[i].type == 'checkbox') {
				if (elms[i].checked == true) {
					if(products=="") {
						products = "?compare=" + elms[i].value;
					}
					else {
						products += "&compare=" + elms[i].value;
					}
				}
			}
		}
		
		window.location.href = "/personal-banking/banking/Bnk_Dep_Compare.shtm" + products;
        
		return true;
		
    }
    else {
        alert("Please select at least two products to compare.");
        return false;
    }
    
}

function getCompareProducts() {

    var url = window.location.href;
    
    var compareProducts1 = url.split("?");
    var compareProducts2 = "&" + compareProducts1[1];
    //alert(compareProducts2);

    compareProducts2 = compareProducts2.replace(/compare=/g, "");
    var compareProducts = compareProducts2.split("&");
    if(compareProducts[1]!=null) {
        compareProducts[1] = compareProducts[1].replace("&", "");
    }
    if(compareProducts[2]!=null) {
        compareProducts[2] = compareProducts[2].replace("&", "");
    }
    if(compareProducts[3]!=null) {
        compareProducts[3] = compareProducts[3].replace("&", "");
    }
    //alert(compareProducts[1]);
    //alert(compareProducts[2]);
    //alert(compareProducts[3]);
    
    return compareProducts;
    
}

//addProduct(1);
//addProduct(2,"");

//addProduct(1);
//addProduct(2);
//removeProduct(2);
//addProduct(3);
//addProduct(4);
//addProduct(5);


