var prefix = "ajax";

// Do not Edit

var xmlHttp = false;

// constants
var REQUEST_GET        = 0;
var REQEST_POST        = 2;
var REQUEST_HEAD    = 1;
var REQUEST_XML        = 3;


var identi_edit = prefix+"_edit"; // Article and Quantity id's must have same length
var identi_quan = prefix+"_quan";

function getXMLRequester( )
{
    var xmlHttp = false;      
    try
    {
        // Internet Explorer
        if( window.ActiveXObject )
        {
            for( var i = 5; i; i-- )
            {
                try
                {
                    if( i == 2 )
                    {
                        xmlHttp = new ActiveXObject( "Microsoft.XMLHTTP" );    
                    }
                    else
                    {
                        xmlHttp = new ActiveXObject( "Msxml2.XMLHTTP." + i + ".0" );
                    }
                    break;
                }
                catch( excNotLoadable )
                {                        
                    xmlHttp = false;
                }
            }
        }
        // Mozilla, Opera und Safari
        else if( window.XMLHttpRequest )
        {
            xmlHttp = new XMLHttpRequest();
        }
    }
    catch( excNotLoadable )
    {
        xmlHttp = false;
    }
    return xmlHttp ;
}

function sendRequest( strSource, strData, intType, intID )
{
    if( !strData )
        strData = '';

    // default type (0 = GET, 1 = xml, 2 = POST )
    if( isNaN( intType ) )
        intType = 0; // GET

    if( xmlHttp && xmlHttp.readyState )
    {
        xmlHttp.abort( );
        xmlHttp = false;
    }
        
    if( !xmlHttp )
    {
        xmlHttp = getXMLRequester( );
        if( !xmlHttp )
            return;
    }
    
    if( intType != 1 && ( strData && strData.substr( 0, 1 ) == '&' || strData.substr( 0, 1 ) == '?' ) )
        strData = strData.substring( 1, strData.length );

    var dataReturn = strData ? strData : strSource;
    
    switch( intType )
    {
        case 1:    // xml
            strData = "xml=" + strData;
        case 2: // POST
            xmlHttp.open( "POST", strSource, true );
            xmlHttp.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' );
            xmlHttp.setRequestHeader( 'Content-length', strData.length );
            break;
        case 3: // HEAD
            xmlHttp.open( "HEAD", strSource, true );
            strData = null;
            break;
        default: // GET
            var strDataFile = strSource + (strData ? '?' + strData : '' );
            xmlHttp.open( "GET", strDataFile, true );
            strData = null;
    }
    
    xmlHttp.onreadystatechange = new Function( "", "processResponse(" + intID + ")" ); ;

    xmlHttp.send( strData );    // param = POST data
    
    return dataReturn;
}
    
function processResponse( intID )
{
    switch( xmlHttp.readyState )
    {
        // uninitialized
        case 0:
        // loading
        case 1:
        // loaded
        case 2:
        // interactive
        case 3:
            break;
        // complete
        case 4:    
            // check http status
            if( xmlHttp.status == 200 )    // success
            {
                processData( xmlHttp, intID );
            }
            // loading not successfull, e.g. page not available
            else
            {
               var statusdiv = document.getElementById(prefix+'_status');
               	if (statusdiv) {
      				statusdiv.innerHTML='Fehler bei der Kommunikation mit dem Server!';
      				statusdiv.style.visibility = "visible";
      			 	statusdiv.style.display = "block";
               	}
            }
    }
}

function processData( xmlHttp, intID )
{
	if (xmlHttp.responseXML) {
		//identi_edit
			var i = xmlHttp.responseXML.getElementsByTagName("field")[0].getAttribute('id');
			var textdiv = document.getElementById(prefix+'_field_text'+i);
			//var epdiv = document.getElementById(prefix+'_field_ep'+i);
			//var gpdiv = document.getElementById(prefix+'_field_gp'+i);
			var ok = xmlHttp.responseXML.getElementsByTagName("field")[0].getAttribute('ok');
			//var ep = xmlHttp.responseXML.getElementsByTagName("field")[0].getAttribute('ep');
			//var gp = xmlHttp.responseXML.getElementsByTagName("field")[0].getAttribute('gp');
			var text = xmlHttp.responseXML.getElementsByTagName("field")[0].childNodes[0].nodeValue;
			textdiv.innerHTML=URLDecode(text);
			//epdiv.innerHTML=ep;
			//gpdiv.innerHTML=gp;
			if (ok == "false") {
				//gpdiv.style.color = "#CC3333";
				textdiv.style.color = "#CC3333";
				//epdiv.style.color = "#CC3333";
			} else {
				//gpdiv.style.color = "#FFFFFF";
				textdiv.style.color = "#FFFFFF";
				//epdiv.style.color = "#FFFFFF";
			}
     } else {
     var statusdiv = document.getElementById(prefix+'_status');
     if (statusdiv) {
      statusdiv.innerHTML='Fehler bei der Datenverarbeitung!';
      statusdiv.style.visibility = "visible";
      statusdiv.style.display = "block";
     }
     }
      		
}

function getAllData(aNumC) {
	
}

function getData(aObj)
{
	var anid = aObj.id.substr(identi_edit.length);
	var edit = document.getElementById(identi_edit+anid);
	var quant = document.getElementById(identi_quan+anid);
	var url = "check.htm?ref=dp" + "&id=" + anid + "&edit=" + encodeURI(edit.value) + "&quant=" + encodeURI(quant.value);
    sendRequest( url );
}

function checkData(aObj) {
	if ((aObj.value.length == 0) || ((aObj.value.length > 4) && (aObj.value.length < 9))) {
	   getData(aObj);
	}
}

function URLDecode( encoded )
{
   // Replace + with ' '
   // Replace %xx with equivalent character
   // Put [ERROR] in output if %xx is invalid.
   var HEXCHARS = "0123456789ABCDEFabcdef"; 
   var plaintext = "";
   var i = 0;
   while (i < encoded.length) {
       var ch = encoded.charAt(i);
	   if (ch == "+") {
	       plaintext += " ";
		   i++;
	   } else if (ch == "%") {
			if (i < (encoded.length-2) 
					&& HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 
					&& HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
				plaintext += unescape( encoded.substr(i,3) );
				i += 3;
			} else {
				//alert( 'Bad escape combination near ...' + encoded.substr(i) );
				plaintext += "%[ERROR]";
				i++;
			}
		} else {
		   plaintext += ch;
		   i++;
		}
	} // while
   return plaintext;
}

/**********/

function getElementY(element){
	var targetTop = 0;
	if (element.offsetParent) {
		while (element.offsetParent) {
			targetTop += element.offsetTop;
            element = element.offsetParent;
		}
	} else if (element.y) {
		targetTop += element.y;
    }
	return targetTop;
}

function getElementX(element){
	var targetTop = 0;
	if (element.offsetParent) {
		while (element.offsetParent) {
			targetTop += element.offsetLeft;
            element = element.offsetParent;
		}
	} else if (element.x) {
		targetTop += element.x;
    }
	return targetTop;
}