function modalWin(url) {
	var w = 300;
	var h = 360;
	var l = (screen.availWidth - w) / 2;
	var t =  (screen.availHeight - h) / 2;
	window.open(url,'Browser','height=400,width=400,left='+l+', top='+t+',toolbar=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,modal=yes');
}

function OpenPopupHelp(pouphelp) {
	var w = 475;
	var h = 500;
	var l = (screen.availWidth - w) / 2;
	var t =  (screen.availHeight - h) / 2;
	var url = "/phplib/cci_popuphelp.php?ID="+pouphelp;
	if (window.showModalDialog) {
	var params='dialogWidth='+w+'px;dialogHeight='+h+'px;dialogLeft='+l+'px;resizable:yes;scroll:yes';
	window.showModalDialog(url, "mywindow", params);
	} else {
	var params='height='+h+',width='+w+',left='+l+', top='+t+',toolbar=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,modal=1';
	window.open(url,"mywindow",params);
	}
	
}

function OpenPopupHowTo(howto) {
	var w = 640;
	var h = 450;
	var l = (screen.availWidth - w) / 2;
	var t =  (screen.availHeight - h) / 2;
	var url = "/phplib/cci_hthelp_rd.php?ID="+howto;
	if (window.showModalDialog) {
	var params='dialogWidth='+w+'px;dialogHeight='+h+'px;dialogLeft='+l+'px;resizable:yes;scroll:yes';
	window.showModalDialog(url, "mywindow", params);
	} else {
	var params='height='+h+',width='+w+',left='+l+', top='+t+',toolbar=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,modal=yes';
	window.open (url,"mywindow",params);
	}
	
}

function cci_replace(StrOrg,strFind,strReplace){
	st = new String;
	s = new String(StrOrg);
	s.replace(strFind,strReplace);
	var st =  s.replace(strFind,strReplace);
	return st.toString();
}

function AutoSubmit(frmName) {
	var vTest = document.getElementById('reload');
	vTest.value = 'yes';
	var formObject = document.forms[frmName];
	formObject.submit();
	}
	
function leftTrim(sString){
	while (sString.substring(0,1) == ' '){
		sString = sString.substring(1, sString.length);
	}
	return sString;
}

function rightTrim(sString){
	while (sString.substring(sString.length-1, sString.length) == ' '){
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}

function trimAll(sString){
	while (sString.substring(0,1) == ' '){
		sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == ' '){
		sString = sString.substring(0,sString.length-1);
	}
return sString;}

var req;
var req2;

function ExecAjax(url){
	if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.open("POST", url, true);
        req.send(null);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.open("POST", url, true);
            req.send();
        }
    }
}

function AjaxGetDesc(url, code, maintlink){
	// branch for native XMLHttpRequest object
    oInput = document.getElementById(code);
	url = url + '&code=' + oInput.value + '&maintlink=' + maintlink;
	
	if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = AjaxSetDesc;
        req.open("GET", url, true);
        req.send(null);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = AjaxSetDesc;
            req.open("GET", url, true);
            req.send();
        }
    }
}

function AjaxGetList(url, code, dropdown){
    // branch for native XMLHttpRequest object
    oInput = document.getElementById(code);
	url = url + '&code=' + oInput.value + '&maintlink=' + dropdown;
	
	if (window.XMLHttpRequest) {
         req2 = new XMLHttpRequest();
         req2.onreadystatechange = AjaxSetList;
         req2.open("GET", url, true);
         req2.send(null);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
         req2 = new ActiveXObject("Microsoft.XMLHTTP");
        if ( req2) {
             req2.onreadystatechange = AjaxSetList;
             req2.open("GET", url, true);
             req2.send();
        }
    }
}
  
  
  
  
  
function AjaxSetDesc() {
if (req.readyState == 4) { // Complete

  if (req.status == 200) { // OK response
	xml = req.responseXML;
	if(xml!=null) {
		t = xml.getElementsByTagName("entity");
		sText = "";
		if(t==null){
			alert('Houston we have a problem');
		} else {
			if (t.length>0) {
			maintlink = t[0].getAttribute('maintlink')
			document.getElementById(maintlink).innerHTML = t[0].getAttribute('desc');
			document.getElementById(maintlink).href = t[0].getAttribute('url');
			}
		}
	  } else {
	  }
	}
}
}
  
function AjaxSetList() {
	var t;
	var maintlink;
	var emptyfirst;
	var bEmptyFirst;
	var i=0;
	var oDropDown;
	var iMinimal;
	var title;
	var st;

	if (req2.readyState == 4) { // Complete

	  if (req2.status == 200) { // OK response
		xml =  req2.responseXML;
		if(xml!=null) {
			t = xml.getElementsByTagName("entity");
			sText = "";
			if(t==null){
				alert('Houston we have a problem');
			} else {
				
				if (t.length>0) {
					bEmptyFirst = true;
					maintlink = t[0].getAttribute('maintlink');
					emptyfirst = t[0].getAttribute('emptyfirst');
					if (emptyfirst=="1"){
						bEmptyFirst	= true;
					} else {
						bEmptyFirst	= false;
					}
					
					oDropDown = document.getElementById(maintlink);
					oDropDown.length=0;

					iMinimal = 0;
					if (bEmptyFirst) {
						st = new Option('', "0");
						iMinimal = 1;
						oDropDown.options.add(st);
					}
				
					if((t.length>iMinimal)) {
						for (i=0; i<t.length; i++ )
						   {
						   if (bEmptyFirst) {
								var a = t[i].getAttribute('code');
//								alert(a.length);
								if (a.length == 0){
									title = t[i].getAttribute('desc');
								} else {
									title = t[i].getAttribute('code') + ' - ' + t[i].getAttribute('desc');
								}
						   } else {
								title = t[i].getAttribute('desc');
						   }
						   st = new Option(title, t[i].getAttribute('id'));
						   oDropDown.options.add(st);
						   }
					} else {
						
						oDropDown.length=0;
						// always add the first for no selection
						st = new Option('None available', "0");	
						oDropDown.options.add(st);						
					}
				}
			}
		  } else {
		  }
		}
	}
} 

function AjaxUpdateStats( url, country, kind, user ){
    // branch for native XMLHttpRequest object
    oInput = document.getElementById(kind+"_"+country);
	url = url + '&code=' + oInput.value + '&field=' + kind+ '&user=' + user+ '&country=' + country;
	
	if (window.XMLHttpRequest) {
         req2 = new XMLHttpRequest();
         req2.onreadystatechange = AjaxSetList;
         req2.open("GET", url, true);
         req2.send(null);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
         req2 = new ActiveXObject("Microsoft.XMLHTTP");
        if ( req2) {
             req2.onreadystatechange = AjaxSetList;
             req2.open("GET", url, true);
             req2.send();
        }
    }
}
