// global flag
//var isIE = false;

// global request and XML document objects
var req;
var domain = false;
var ipClient;
var ipList;
var guideNb;

// convert a Decimal Coded Binary number as string (ip address) to a Decimal
function convertDCB2Dec(dcb){
	tab=dcb.split('.');
	ipDec = tab[0]*Math.pow(2, 24) + tab[1]*Math.pow(2, 16) + tab[2]*Math.pow(2, 8) + tab[3]*Math.pow(2, 0);
	return ipDec;
}



function checkLocal(guideNb){
	checkResol();
	//Check if P: is mounted by an image preloading
	//alert(document.images.length);
    try {
		document.images[0].src = "P:/common/img/pixel.gif";
	}catch(e){
		var msg = (typeof e == "string") ? e : ((e.message) ? e.message : "Unknown Error");
		alert("Unable to load Img : " + msg);
		//return;
	}
	return document.images['0'].complete;

//	document.images[0].src = "P:/common/img/pixel.gif";
//alert(document.images[0].complete);
//	return document.images[0].complete;
}

function loadXMLDoc(url) {
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
		//alert('IE false');
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChange;
        req.open("GET", url, true);
        req.send(null);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        isIE = true;
        //alert('IE true');
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChange;
            req.open("GET", url, true);
            req.send();
        }
    }
}

// handle onreadystatechange event of req object
function processReqChange() {
    // only if req shows "loaded"
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
            loadGuide();
         } else {
            alert("Erreur lors du rapatriement des données XML : \n" + req.statusText);
         }
    }
}



// retrieve text of an XML document element, including elements using namespaces
function getElementTextNS(prefix, local, parentElem, index) {
//	alert(local);
    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
        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";
    }
}
/*
function appendToSelect(select, value, content) {
    var opt;
    opt = document.createElement("option");
    opt.value = value;
    opt.appendChild(content);
    select.appendChild(opt);
}
*/
//retrieve the IP list from the XML document
function buildIpList(){
	//alert('buildIPList');
	var ipList = req.responseXML.getElementsByTagName("PLAGE");
	//for (var i = 0; i < ipList.length; i++) {
	//        appendToSelect(select, i, document.createTextNode(getElementTextNS("", "title", items[i], 0)));
    //}
	//alert(ipList);
	return ipList;
}

function init(ip, gNb) {
	ipClient = convertDCB2Dec(ip);
	guideNb = gNb;
	loadXMLDoc("../../common/xml/list.xml");
}

function getGuide(ip, gNb){
	ipClient = convertDCB2Dec(ip);
		guideNb = gNb;
	loadXMLDoc("../../common/xml/list.xml");

}

function loadGuide(){
	ipList = buildIpList();

	for(i=0; i<ipList.length; i++){
		if((ipClient>=convertDCB2Dec(getElementTextNS("", "LBOUND", ipList[i], 0))) && (ipClient<=convertDCB2Dec(getElementTextNS("", "UBOUND", ipList[i], 0)))){
			domain = getElementTextNS("", "@LABEL", ipList[i], 0);
		}
	}

	switch (domain)
		{
			case "Mediatheque":
				if(domain == 'Mediatheque' && checkLocal(guideNb)){
					path="P:/" + guideNb + "/dswmedia/";
				}else{
					path='';
				}
			break;
			case "Extranet":
				path="C:/CMDA/" + guideNb + "/dswmedia/";
			break;
			case "CIMU":
				path='';
			break;

			default:
				path='';
			break;
	}

	data=domain + ' ' + path;
	data='<object classid="clsid:166B1BCA-3F9C-11CF-8075-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/director/sw.cab#version=8,5,0,0" width="1015" height="760">';
	data+='<param name="src" value="' + path + 'metaScore.dcr">';
	data+='<embed src="' + path + 'metaScore.dcr" width="1015" height="760" pluginspage="http://www.macromedia.com/shockwave/download/"></embed>';
	data+='</object>';

		div = document.getElementById("guide");
		div.innerHTML = data;

		//divPath = document.getElementById("chemin");
		//divPath.innerHTML = "path : " + path;
		//divPath = document.getElementById("chemin");
		//divPath.innerHTML = "domain : "+ domain + " path : " + path;
}

function checkResol(){

	//alert(this.window.location);
	//alert("toto");
	//alert(objWindow.location);
	//alert(window.name);
	if(window.name != 'MetaScore'){
		if (screen.width >= 1280 && screen.height >= 1024) {
			window.name='MetaScore';
			window.location=this.window.location;
		} else {
			window.location="/mediacomposite/cmda/";
			win = window.open(this.window.location,'MetaScore','Fullscreen')
		}
	}
}
