var xmlHttp
var div_idx
var div_id
var strx
var pagex
var originalcontent;
var connectionMode;
if (document.images){
  pic1= new Image(); 
  pic1.src=  CLIENT_WEB_PATH + "/include/default/images/progress-bar.gif"; 
}


function ivanajax(action, page, str, div_id, ext_str, oOptions){ 
if(!document.getElementById(div_id)){
  if(div_id !='nanozonecontent'){
  alert('Error occured: Target frame  ' +div_id+'  not available!');
  }
return false;
}

//catch additional options by nicholas
//======================
  var oOptions = augment({
      //connectionMode: true,
      //bOptTwo: false
      connectionMode: true
    }, oOptions );

  // sWhatever is a regular argument. We assume it's always going to be passed

  // sOptOne and bOptTwo are optional;
  // they are defaulted to "foo" and false, respectively
  //alert(oOptions.connectionMode);

	//action  = $_GET method:get  /  $_POST method:post
	//page    = page to be call
	//str     = ajax parameter querystring
	//div_id  = ajax output id
	//ext_str = external parameter
		//-------------------------------
		// Using $_GET method
		//-------------------------------
		strx = str
		if (action=='GET'){
			div_idx 			= div_id
			pagex   			= page
			strx    			= str
			connectionMode = oOptions.connectionMode
			xmlHttp=GetXmlHttpObject()
			if (xmlHttp==null){
				alert ("Browser does not support HTTP Request")
				return
			}
			var url=page
			url=url+"?param="+str
			url=url+ext_str
			originalcontent = document.getElementById(div_idx).innerHTML;
			//if connection mode is true, meaning that it will run as async, other program will continue to exec 
			//if it is false, the browser have to wait until the value return then only will run next line.
			if(connectionMode){
				xmlHttp.onreadystatechange=stateChanged
				xmlHttp.open("GET",url,true)
			}else{
				showprogressbar();
				xmlHttp.open("GET",url,false)
				stateChanged()
			}
			xmlHttp.send(null)
		}
		//-------------------------------
		// Using $_POST method
		//-------------------------------
		if (action=='POST'){
			div_idx=div_id
			xmlHttp=GetXmlHttpObject()
			if (xmlHttp==null){
				alert ("Browser does not support HTTP Request")
				return
			}
			var url=page			
			xmlHttp.open("POST", url, true)
			if(str=='9999999779999997'){ //upload code
			
			}else{
				xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
			}
				xmlHttp.onreadystatechange=stateChanged
				xmlHttp.send(ext_str);
		}
}


function loadURL(url) {
	var content = '';
	var value = false;
	request2= new GetXmlHttpObject();
	if (request2) {
		request2.open("GET", url, false);
		request2.send(null);
		if (request2.readyState==4){
			return  request2.responseText;
		}else{
			return 'Error: ' + request2.status + ' ' + request2.statusText;
		}
	}
}

function stateChanged() 
{ 
	if (xmlHttp.readyState==4){ 
			document.getElementById(div_idx).style.display = 'none';
			if(document.getElementById("ajax")){
					document.body.innerHTML = xmlHttp.responseText;
			}else{
					document.getElementById(div_idx).innerHTML = xmlHttp.responseText;
					document.getElementById(div_idx).style.display = 'inline';
			}	
			  
			  var browser=navigator.appName;

				  var thediv = document.getElementById(div_idx);
				  var st = thediv.getElementsByTagName('SCRIPT');
				  for(var y=0; y <st.length; y++){
					  if(st[y].src != ""){
						  	var tempdir = loadURL(st[y].src);
							if (browser=="Microsoft Internet Explorer") {
							   window.execScript(tempdir);
							} else {
							    window.eval(tempdir);
							}
					  }
					  if (browser=="Microsoft Internet Explorer") {
						window.execScript(st[y].text);
					  } else {
					      window.eval(st[y].textContent);
					  }				  
				  }

			  
				  for(var y=0; y <st.length; y++){
				      
					  if(st[y].src != "" ){
						  	var tempdir = loadURL(st[y].src);
							if (browser=="Microsoft Internet Explorer") {
							   retval = window.execScript(tempdir);
							} else {
							    window.eval(tempdir);
							}
					  }
					  if (browser=="Microsoft Internet Explorer") {
					      retval = window.execScript(st[y].text);
					  } else {
					      window.eval(st[y].textContent);
					  }				  
				  }
	}else if (xmlHttp.readyState==1){	
			showprogressbar();
	}
	return;
}

function showprogressbar(){
	document.getElementById(div_idx).innerHTML='<img border="0" src="'+ CLIENT_WEB_PATH +'/include/default/images/progress-bar.gif">';
}


function GetXmlHttpObject(){
    var xmlHttp = null;
    
    if (window.XMLHttpRequest){
        try{
            xmlHttp = new XMLHttpRequest();
        }catch (e){
            xmlHttp = false;
        }
    }else if (window.createRequest){
        try{
            xmlHttp = new window.createRequest();
        }catch (e){
            xmlHttp = false;
        }
    }else if (window.ActiveXObject){
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        }catch (e){
            try {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }catch (e){
                xmlHttp = false;
            }
        }
    }
 
    return xmlHttp;
}


function augment (oSelf, oOther) {
    if (oSelf == null) {
        oSelf = {};
    }
    for (var i = 1; i < arguments.length; i++) {
        var o = arguments[i];
        if (typeof(o) != 'undefined' && o != null) {
            for (var j in o) {
                oSelf[j] = o[j];
            }
        }
    }
    return oSelf;
}
