/* new version*/
var brands =  ["Motorola", "Nokia", "Samsung", "SonyEricsson","Xda"] ;

function showBrand(brandName) {
	for (var i=0; i < brands.length; i++ ) {
		if ( brands[i] == brandName) {
		
		showDiv(brands[i]);
		}
		else {
		hideDiv(brands[i]);
		}
	}
}


/* for SG version */
function showMobile(maker) {
	try {
		loadXMLDoc (maker+".xml");
	}
	catch(e) {
		var msg = (typeof e == "string") ? e : ((e.message) ? e.message : "Unknown Error");
		alert("Unable to get XML data:\n" + msg);
		return;
	}
}

// global flag
var isIE = false;

// global request and XML document objects
var req;

function setReqOldIE() {
	var xmlhttp;
	if (window.ActiveXObject) {
		isIE = true;
		try {
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e) {
			xmlhttp = false;
			}
		}
	}
	return xmlhttp;
}

// retrieve XML document (reusable generic function);
// parameter is URL string (relative or complete) to
// an .xml file whose Content-Type is a valid XML
// type, such as text/xml; XML source must be from
// same domain as HTML file
function loadXMLDoc(url) {
    // branch for native XMLHttpRequest object
	var isOldIE = (document.all && !window.opera)? true:false;
	
	if (!isOldIE) { req = new XMLHttpRequest(); }

	if (isOldIE) { req=setReqOldIE(); }
	if(req) {
		req.onreadystatechange = processReqChange;
		req.open("GET",url,true);
		req.send(null);
	} else {
		alert(navigator.appName+' Does Not Seem To Support HttpRequest');
	}
}

// handle onreadystatechange event of req object
function processReqChange() {
    // only if req shows "loaded"
    if (req.readyState == 4) {
	   // only if "OK"
        if (req.status == 200) {
            clearModelSelector();
            buildModelSelector();
         } else {
            alert("There was a problem retrieving the XML data:\n" +
                req.statusText);
         }
    }
}

// retrieve text of an XML document element, including
// elements using namespaces
function getElementTextNS(prefix, local, parentElem, index) {
    var result = "";
    if (prefix && isIE) {
        // IE/Windows way of handling namespaces
        result = parentElem.getElementsByTagName(prefix + ":" + local)[index];
    } else {
        // the namespace versions of this method 
        // (getElementsByTagNameNS()) operate
        // differently in Safari and Mozilla, but both
        // return value with just local name, provided 
        // there aren't conflicts with non-namespace element
        // names
        var tmp = parentElem.getElementsByTagName(local);
        result = parentElem.getElementsByTagName(local)[index];
    }
    if (result) {
        // get text, accounting for possible
        // whitespace (carriage return) text nodes 
        if (result.childNodes.length > 1) {
            return result.childNodes[1].nodeValue;
        } else {
            return result.firstChild.nodeValue;    		
        }
    } else {
        return "n/a";
    }
}

// empty Topics select list content
function clearModelSelector() {
      document.getElementById("modelSelector").innerHTML = "";
}

// fill Topics select list with items from
// the current XML document
function buildModelSelector() {
    var select = document.getElementById("modelSelector");
    var items = req.responseXML.getElementsByTagName("item");
    var oldmodels = req.responseXML.getElementsByTagName("oldmodel");
	
	var header;

	// show 'Current Models' label if need
	if (oldmodels.length > 0) {
		header = document.createElement('h5');
		header.appendChild(document.createTextNode('Current Models'));
		select.appendChild(header);
	}

	var t=document.createElement('table');  	
	var tb=document.createElement('tbody');  	// - create a tbody element 
	t.style.border='1';
	t.style.width='100%';
	t.style.textAlign='left';
  	var tr=document.createElement('tr');  
	var td, modelURL, modellink,modelText, modelThumb,div;

    // loop through <item> elements, and add each nested
    // <title> element to models table element
    for (var i = 0; i < items.length; i++) {
		td=document.createElement('td');
		td.style.textAlign='right';
		td.style.padding='5px';

		modellink = document.createElement("a");
		modelURL = "javascript:showDetail('item'," +i+")";
		modellink.setAttribute('href',modelURL);
		modellink.style.textDecoration='none';
		modelText= document.createTextNode(getElementTextNS("", "title", items[i], 0));
		modelThumb=document.createElement("img");
		modelThumb.setAttribute('src', getElementTextNS("", "thumb", items[i], 0));
		modelThumb.setAttribute('alt', getElementTextNS("", "title", items[i], 0));
		modelThumb.setAttribute('border',0);
		
		modellink.appendChild(modelThumb);
		modellink.appendChild(document.createElement("br"));
		modellink.appendChild(modelText);

		div = document.createElement("div");
		div.setAttribute('align','center');
		div.appendChild(modellink);
    
		td.appendChild(div);  					
		tr.appendChild(td);
		if ((i+1)%6 ==0) {
			tb.appendChild(tr); 						
			tr=document.createElement('tr'); 
		}
    }
	if (i%6 >0) {
		for (var j=i%6; j<6 ; j++) {
			td=document.createElement('td');
			td.style.width="15%";
			td.appendChild(document.createTextNode(' '));
			tr.appendChild(td);
		}
	}
	tb.appendChild(tr); 						
	t.appendChild(tb);						
    select.appendChild(t);

    // show old models
	if (oldmodels.length > 0) {
		header = document.createElement('h5');
		header.appendChild(document.createTextNode('Old Models'));
		select.appendChild(header);
		
		t=document.createElement('table');  	
		tb=document.createElement('tbody');  	// - create a tbody element 
		t.style.border='0';
		t.style.width='100%';
		t.style.textAlign='left';
		tr=document.createElement('tr');  
		
		// loop through <oldmodel> elements, and add each nested
		// <title> element to models table element
		for (var i = 0; i < oldmodels.length; i++) {
			td=document.createElement('td');
			td.style.textAlign='right';
			td.style.padding='5px';
	
			modellink = document.createElement("a");
			modelURL = "javascript:showDetail('oldmodel'," +i+")";
			modellink.setAttribute('href',modelURL);
			modellink.style.textDecoration='none';
			modelText= document.createTextNode(getElementTextNS("", "title", oldmodels[i], 0));
			modelThumb=document.createElement("img");
			modelThumb.setAttribute('src', getElementTextNS("", "thumb", oldmodels[i], 0));
			modelThumb.setAttribute('alt', getElementTextNS("", "title", oldmodels[i], 0));
			modelThumb.setAttribute('border',0);
			
			modellink.appendChild(modelThumb);
			modellink.appendChild(document.createElement("br"));
			modellink.appendChild(modelText);
	
			div = document.createElement("div");
			div.setAttribute('align','center');
			div.appendChild(modellink);
		
			td.appendChild(div);  					
			tr.appendChild(td);
			if ((i+1)%6 ==0) {
				tb.appendChild(tr); 						
				tr=document.createElement('tr'); 
			}
		}
		if (i%6 >0) {
			for (var j=i%6; j<6 ; j++) {
				td=document.createElement('td');
				td.style.width="15%";
				td.appendChild(document.createTextNode(' '));
				tr.appendChild(td);
			}
		}
		tb.appendChild(tr); 						
		t.appendChild(tb);						
		select.appendChild(t);
	}

	// clear header and details display
	clearDetail();
}

// display details retrieved from XML document
function showDetail(itemname, value) {
    var item = req.responseXML.getElementsByTagName(itemname)[value];

	var t=document.createElement('table');  	
	var tb=document.createElement('tbody');  	
	t.style.border='0';
	t.style.width='600px';
	t.style.height='30px';
	t.style.backgroundColor='#011545';
	t.style.color='#FFFFFF';

	var tr=document.createElement('tr');  	
	var td=document.createElement('td');
	td.style.width='540px';
	var h5=document.createElement("h5");
	h5.appendChild(document.createTextNode(" Setting up "+ getElementTextNS("", "description", item, 0) + " for GPRS"));
    td.appendChild(h5);  					
	tr.appendChild(td); 						
   	td=document.createElement('td');

	var closelink = document.createElement("a");
    closelink.setAttribute('href','javascript:clearDetail()');
	closelink.style.color = '#FFFFFF';
	closelink.style.textDecoration = 'none';
	closelink.style.fontWeight = 'bold';
	
    var closeImg=document.createElement("img");
    closeImg.setAttribute('src', '/repository/images/common/icons/icon_2nd_cancel.gif');
    closeImg.setAttribute('border',0);
	closeImg.style.verticalAlign = 'middle';
	closelink.appendChild(closeImg);
	closelink.appendChild(document.createTextNode(' Close'));

	td.appendChild(closelink);

	tr.appendChild(td); 
	tb.appendChild(tr); 
	t.appendChild(tb);		
		
	var header_div = document.getElementById("header");
	header_div.innerHTML = "";
	header_div.appendChild(t);
	header_div.style.display='block';
	
	var instruction = getElementTextNS("", "instruction", item, 0);
	var details_div = document.getElementById("details");
	details_div.innerHTML = "";
	details_div.innerHTML = instruction;
	details_div.style.display='block';
}

// clear header and details display
function clearDetail() {
      document.getElementById("header").style.display = 'none';
      document.getElementById("header").innerHTML = "";
      document.getElementById("details").style.display = 'none';
      document.getElementById("details").innerHTML = "";
}