
// Notes : Add div element dynamically. FF isn't recognising the contents of the div and hence disply:none isn;t working
var d = document;
var divObjId = "";
var flashContent2 = "";
var flashContent3 = "";

function getAbsX(elt) { return parseInt(elt.x) ? elt.x : getAbsPos(elt,"Left"); };
function getAbsY(elt) { return parseInt(elt.y) ? elt.y : getAbsPos(elt,"Top"); };
function getAbsPos(elt,which) {
	iPos = 0;
	while (elt != null) {
  		iPos += elt["offset" + which];
  		elt = elt.offsetParent;
 	}
 	return iPos;
};
function getO(id, css){
	var o = (document.getElementById)?document.getElementById(id):document.all[id];
	return o;
};
function HttpRequestComplete(str){
	getO('detailsContent').innerHTML = str;
}
function initDetails(className, styleTop){
	var top = (styleTop)?styleTop:0;
	if (getO('flashcontent2')) {
		flashContent2 = getO('flashcontent2').innerHTML;
		getO('flashcontent2').innerHTML = '';
	}
	if (getO('flashcontent3')) {
		flashContent3 = getO('flashcontent3').innerHTML;
		getO('flashcontent3').innerHTML = '';
	}
	var sel = document.getElementsByTagName('select');
	for (i = 0; i<sel.length; i++){
		sel[i].style.display = 'none';
	}
	getO('detailsContent').style.top = top + 'px';
	getO('detailsContent').className = className;
	getO('detailsContent').style.display = 'block';
	getO('more_details_container').style.display = 'block';
	
}
function getDetails(serverPath,mapping,handsetId,servicePlanId,id,servicePlanName, bgHeight){
	
	if(bgHeight != null && bgHeight > document.body.offsetHeight) {
		getO('more_details_bg').style.height = bgHeight + 'px';
	} else {
		getO('more_details_bg').style.height = document.body.offsetHeight + 'px';
	}
	
	getO('more_details_bg').style.width = document.body.offsetWidth -2 + 'px';
	getO('more_details_bg').style.display = 'block';
	getO('more_details_container').style.display = 'block';
	// using 'featureId' to switch navigation off
	window.httpRequest.call({url:servletPath + '/yourplan/selection',params:{'mapping':mapping,'handsetId':handsetId,'servicePlanId':servicePlanId,'featureId':'1'}, method:'post', onComplete:HttpRequestComplete, response:'TEXT'});
	initDetails(servicePlanName);

}
function removeDetails(){
	if (getO('flashcontent2')) {
		getO('flashcontent2').innerHTML = flashContent2;
	}
	if (getO('flashcontent3')) {
		getO('flashcontent3').innerHTML = flashContent3;
	}
	getO('detailsContent').innerHTML = '<span class="loading">loading ...</span>';
	getO('more_details_bg').style.display = 'none';
	getO('more_details_container').style.display = 'none';
	var sel = document.getElementsByTagName('select');
	for (i = 0; i<sel.length; i++){
		sel[i].style.display = 'block';
	}
}
function showComparison(id, bgHeight){
	if (id) {
		var topPos = getAbsY(getO(id));
		var diffInHeight = parseInt(getO('more_details_bg').style.height) - 800;
		topPos = (topPos > diffInHeight)?diffInHeight:getAbsY(getO(id));
	}
	if(bgHeight != null && bgHeight > document.body.offsetHeight) {
		getO('more_details_bg').style.height = bgHeight + 'px';
	} else {
		getO('more_details_bg').style.height = document.body.offsetHeight + 'px';
	}
	getO('more_details_bg').style.width = document.body.offsetWidth -2 + 'px';
	getO('more_details_bg').style.display = 'block';
	getO('more_details_container').style.display = 'block';
	getO('detailsContent').innerHTML = getO('plansComparison').innerHTML;
	initDetails('compare',100);
}
function showMonkeyOverlay(id){
	if (id) {
		var topPos = getAbsY(getO(id));
		var diffInHeight = parseInt(getO('more_details_bg').style.height) - 800;
		topPos = (topPos > diffInHeight)?diffInHeight:getAbsY(getO(id));
	}
	getO('more_details_bg').style.height = document.body.offsetHeight + 'px';
	getO('more_details_bg').style.width = document.body.offsetWidth -2 + 'px';
	getO('more_details_bg').style.display = 'block';
	getO('more_details_container').style.display = 'block';
	getO('detailsContent').innerHTML = getO('plansMonkey').innerHTML;
	initDetails('compare',100);
}
function showBenefits(){
	getO('more_details_bg').style.height = document.body.offsetHeight + 'px';
	getO('more_details_bg').style.width = document.body.offsetWidth -2 + 'px';
	getO('more_details_bg').style.display = 'block';
	getO('more_details_container').style.display = 'block';
	getO('detailsContent').innerHTML = getO('planDetails').innerHTML;
	initDetails('benefits','240');
}
var HttpRequest = function (){
    this.url = false;
    this.method = 'GET';
    this.response = 'XML';
    this.contentType = 'application/x-www-form-urlencoded';
    this.eventReadyState = ['Uninitialized', 'Loading', 'Loaded', 'Interactive', 'Complete'];
    this.httpObject = function(){
        var o;
        try{
            o = new ActiveXObject("Msxml2.XMLHTTP");
        }catch(e){
            try{
                o = new ActiveXObject("Microsoft.XMLHTTP");
            }catch(e){
                o = false;
            }
        }
	    if(!o && typeof XMLHttpRequest!='undefined'){
		    o = new XMLHttpRequest();
	    }
	    if(typeof o=='object'){
	        return o;
	    }else{
	        //alert('Sorry your browser doesn\'t support the latest xml object\nBrowsers supported are:\nOn windows: IE5.5+, Mozilla Firefox\nOn Mac: Mozilla Firefox\nPlease upgrade your browser to use the editing interface.');
	        // Insert non JavaSript link here.
	        return false;
	    }
    }
    
    this.call = function(o){
        var url = (o.url)? o.url : this.url;
        var method = (o.method)? o.method : this.method;
        var params = (o.params)? o.params : false;
        var param = "";
        var response = (o.response)? o.response : this.response;
        var contentType = (o.contentType)? o.contentType : this.contentType;
        if(!url){alert('You must at list specify an url in the call method.'); return false}
        var httpObject = this.httpObject();
        if(typeof httpObject != 'object'){return false;}
		    httpObject.onreadystatechange = function(){
			if (window.httpRequest.eventReadyState[httpObject.readyState] == 'Complete') {
				if (httpObject.status == 200) {					
				    if(response == 'TEXT'){
    					o.onComplete(httpObject.responseText);
    				}else if(response=='JS'){
    				    eval(httpObject.responseText)
				    }else{
    					o.onComplete(httpObject.responseXML.documentElement);
				    }
				} else {
			        alert('Sorry There was a problem making the request.\nThe request returned the following error code : ' + httpObject.status);
				}
			}
		}
		httpObject.open(method, url, true);
		httpObject.setRequestHeader('Content-Type', contentType);
		httpObject.setRequestHeader('Connection','close');
		if(params){
		    for(var key in params){
		        param += key + '=' + params[key] + '&';
		    }
		}
		// alert('param = ' + param);
		httpObject.send(param);
    }
    window.httpRequest = this;
}
new HttpRequest();

