﻿var selected_radio="B";
            var selected_region="0";
            var selected_city="0";
            $(document).ready(function(){
                //getRegion();
			});
            function getRegion()
            {
                    //alert(selected_radio+"--"+selected_region+"--"+selected_city);
                 /* $.ajax({
                           type: "GET",
                          // url: "xml/rbAreas.xml",
                           //dataType: "xml",
                           complete: function(data) {
						   
						   		 $("#chooseRegion").empty();
								 $("#choosecity").empty();
								 $("#searchResult").empty();
								 $("#choosecity").append("<option value='0'>Choose City</option>");
								 $("#chooseRegion").append("<option value='0'>Choose Region</option>");
								 $("#chooseRegion").append("<option value='A1'>Central Region</option>");
								 $("#chooseRegion").append("<option value='A2'>Western Region</option>");
								 $("#chooseRegion").append("<option value='A3'>Eastern Region</option>");
								 $("#chooseRegion").append("<option value='0'>-------------</option>");
								 $("#chooseRegion").append("<option value='B7'>Islamic Banking</option>");
								 $("#chooseRegion").append("<option value='B8'>Ladies Banking</option>");
								 $("#chooseRegion").append("<option value='B9'>Ladies Islamic Banking</option>");
                                 
                           }


                        });*/
              }
              function getCity()
              {
                   //alert(selected_radio+"--"+selected_region+"--"+selected_city);
                    var file_name = "";
                    file_name = 'rbBranches.xml';
					$.ajax({
                               type: "GET",
                               url: "xml/"+file_name,
                               dataType: "xml",
                               //Make sure that you specify the type of file you expecting (XML)
                               complete: function(data) {
							   		 $("#choosecity").empty();
	 							     $("#choosecity").append("<option value='0'>--Select City--</option>");
                                     var json = $.xmlToJSON(data.responseXML); 
                                     json.Branch.SortByNode("CITY_EN"); 
                                     var out = "",prev="",curr="";
								var str =selected_region.substring(0,1);
								//alert(str);
								if(str == 'A')
								{
                                     for(var i = 0; i < json.Branch.length; i++) {
                                           curr = json.Branch[i].CITY_EN[0].Text;
                                           if(curr != prev)
                                           {
										           if(json.Branch[i].AREA[0].Text == selected_region.substring(1,2))
                                                       $("#choosecity").append("<option value="+curr+">"+curr+"</option>");
                                           }
                                           prev = curr;

                                     }
								 }
								 else
								 {
								 	for(var i = 0; i < json.Branch.length; i++) {
                                            curr = json.Branch[i].CITY_EN[0].Text;
                                           if(curr != prev)
                                           {
                                                   if(json.Branch[i].BRANCHTYPE[0].Text == selected_region.substring(1,2))
                                                       $("#choosecity").append("<option value="+curr+">"+curr+"</option>");
                                               
                                           }
                                           prev = curr;

                                     }
								 
								 }
								 
                               }

                            });
					
             }
              function getSearchResult()
              {
                    //alert(selected_radio+"--"+selected_region+"--"+selected_city);
                    var file_name = "";
                    if(selected_radio == 'B')
                    {
						
					    file_name = 'rbBranches.xml';
						$.ajax({
								   type: "GET",
								   url: "xml/"+file_name,
								   dataType: "xml",
								   //Make sure that you specify the type of file you expecting (XML)
								   complete: function(data) {
												 var tableString="";
												 var json = $.xmlToJSON(data.responseXML); //Please notice that we use responseXML here which is DOMDocument object
												 //Now I want to sort all rows by node col1
												 json.Branch.SortByNode("CODE"); //Default order is ASC
												 $("#searchResult").empty();
												 if(selected_city == 0)
												 {
												 	tableString = "";
												 }
												 else
												 {
												 	tableString = '<table class="txt" border="0"><tr><th width="20px"></th><th width="60px" align="left">Code</th><th width="280px" align="left">Branch Name</th><th width="260px" align="left">Address</th><th style="text-align:center;width:80px;margin-left:10px; margin-right:10px">Map</th></tr>';					
												 }
												 var str =selected_region.substring(0,1);
											//alert(str);
											if(str == 'A')
											{
												//alert("hi");
												 for(var i = 0; i < json.Branch.length; i++)
												 {
													if(selected_region.substring(1,2) ==  json.Branch[i].AREA[0].Text)
													{
														if(selected_city == json.Branch[i].CITY_EN[0].Text)
														{
															if(json.Branch[i].MAP[0].Text)
															{
												//image map
												var url ="'"+json.Branch[i].MAP[0].Text+"/E"+json.Branch[i].CODE[0].Text+".gif'";
															 var map='<td style="text-align:center;width:80px;margin-left:10px; margin-right:10px"><a class="MnuNavLink" href='+url+' onclick="popup('+url+'); return false">View</a></td>';
															}
															else
															{
																var map='</td><td width="80px"></td>';
															}
															tableString += '<tr><td><img id="dropdownimageInner'+i+'" src="../images/collapseplus.gif" onclick="unfoldSubResultDiv('+i+','+json.Branch[i].CODE[0].Text+');" /></td><td width="60px">'+json.Branch[i].CODE[0].Text+'</td><td width="280px">'+json.Branch[i].NAME_EN[0].Text+'</td><td width="260px">'+json.Branch[i].ADRS_EN[0].Text+'</td>'+map+'</tr><tr><td colspan="5"><div id="outer_subSearchResult'+i+'"></div></td></tr>';
														}
                                      				}
												}
											}
											else
											{
												
												for(var i = 0; i < json.Branch.length; i++)
												 {
													if(selected_region.substring(1,2) ==  json.Branch[i].BRANCHTYPE[0].Text)
													{
														if(selected_city == json.Branch[i].CITY_EN[0].Text)
														{
															if(json.Branch[i].MAP[0].Text)
															{
												//image map
												var url ="'"+json.Branch[i].MAP[0].Text+"/E"+json.Branch[i].CODE[0].Text+".gif'";
															 var map='<td style="text-align:center;width:80px;margin-left:10px; margin-right:10px"><a class="MnuNavLink" href='+url+' href='+url+' onclick="popup('+url+'); return false">View</a></td>';
															}
															else
															{
																var map='<td width="80px"></td>';
															}
															tableString += '<tr><td><img id="dropdownimageInner'+i+'" src="../images/collapseplus.gif" onclick="unfoldSubResultDiv('+i+','+json.Branch[i].CODE[0].Text+');" style="width:15px; height:15px;"/></td><td width="60px">'+json.Branch[i].CODE[0].Text+'</td><td width="280px">'+json.Branch[i].NAME_EN[0].Text+'</td><td width="260px">'+json.Branch[i].ADRS_EN[0].Text+'</td>'+map+'</tr><tr><td colspan="5"><div id="outer_subSearchResult'+i+'"></div></td></tr>';
														}
                                      				}
												}
											}
											
							
												tableString += "</table>";
    											 $("#searchResult").append(tableString);
											}
									});
						}
	                    else
    	                {
							file_name = 'rbATMs.xml';
							$.ajax({
									   type: "GET",
									   url: "xml/"+file_name,
									   dataType: "xml",
									   complete: function(data) {
									   		  var tableString="";
											 var json = $.xmlToJSON(data.responseXML); 
											 json.ATMDetails.SortByNode("CITY_EN");
											 $("#searchResult").empty();
											 if(selected_city == 0)
											 {
											 	 tableString ="";
											 }
											 else
											 {
											 tableString = '<table class="txt" border="0"><tr><th width="60px">code</th><th style="text-align:center;width:280px;margin-left:10px; margin-right:10px">ATM Name</th><th style="text-align:center;width:270px;margin-left:10px; margin-right:10px">Location</th><th width="90px" align="left">Map</th></tr>';
											 }
											 //alert(selected_city);
											 for(var i = 0; i < json.ATMDetails.length; i++)
											 {
												if(selected_city == json.ATMDetails[i].CITY_EN[0].Text)
													tableString += "<tr></td><td width='95px'></td><td style='text-align:left;width:280px;margin-left:10px; margin-right:10px'>"+json.ATMDetails[i].NAME_EN[0].Text+"</td><td style='text-align:Center;width:270px;margin-left:10px; margin-right:10px'>"+json.ATMDetails[i].LOCATION[0].Text+"</td><td width='90px'></td></tr>";
                                        	 }
		                                 	tableString += "</table>";
	    		                            $("#searchResult").append(tableString);
										}
    	                        });
                   	   }
					   
              }
			  
              function foldSubResultDiv(thisId)
              {
			  		//alert("fold");
                  document.getElementById("outer_subSearchResult"+thisId).setAttribute("style","width:100%;height:5px;");
                  document.getElementById("outer_subSearchResult"+thisId).style.visibility = 'hidden';
                  document.getElementById("dropdownimageInner"+thisId).setAttribute("src","../images/collapseplus.gif");
                  document.getElementById("dropdownimageInner"+thisId).setAttribute("onclick","unfoldSubResultDiv('"+thisId+"');");
              }
              function unfoldSubResultDiv(thisId,code)
              {
			  	file_name = 'rbBranches.xml';
			  	$.ajax({
								   type: "GET",
								   url: "xml/"+file_name,
								   dataType: "xml",
								   //Make sure that you specify the type of file you expecting (XML)
								   complete: function(data)
								   {
												 var tableString="";
												 var json = $.xmlToJSON(data.responseXML);
												 var temp =json.Branch[0].CODE[0].Text;
												 //alert(code);
												 for(var i = 0; i < json.Branch.length; i++)
												 {
												 	if(code == json.Branch[i].CODE[0].Text)
													{
														//alert(thisId);
														var temp =json.Branch[i].POBOX_EN[0].Text;
														//alert(temp);
														tableString+='<div id="outer_subSearchResult'+thisId+'" style="width:100%;margin-bottom:20px;"><table class="txt"><tr><td style="width:60px;"></td><td style="width:95px;">P.O. Box:</td><td style="width:320px;">'+json.Branch[i].POBOX_EN[0].Text+'</td></tr><tr><td style="width:95px;"></td><td style="width:280px;">Manager:</td><td style="width:320px;">'+json.Branch[i].GMNAME_EN[0].Text+'</td></tr><tr><td style="width:95px;"></td><td style="width:280px;">Tel.No.:</td><td style="width:320px;">'+json.Branch[i].GMTEL[0].Text+'</td></tr><tr><td style="width:95px;"></td><td style="width:280px;">Branch Tel.:</td><td style="width:320px;">'+json.Branch[i].TEL[0].Text+'</td></tr><tr><td style="width:95px;"></td><td style="width:280px;">Fax:</td><td style="width:320px;">'+json.Branch[i].FAX[0].Text+'</td></tr></table>';
													}
												 }
												 tableString += "</div>";
    											 $("#outer_subSearchResult"+thisId).append(tableString);
								   }
						}) 
												 
			  	
				
					
                  document.getElementById("outer_subSearchResult"+thisId).setAttribute("style","width:100%;height:230px;");
                  document.getElementById("outer_subSearchResult"+thisId).style.visibility = 'visible';
				  
                  document.getElementById("dropdownimageInner"+thisId).setAttribute("src","../images/collapseminus.gif");
                  document.getElementById("dropdownimageInner"+thisId).setAttribute("onclick","foldSubResultDiv('"+thisId+"');");
              }