var tc = false;
try {
    tc = true;
} catch(f) { }


var xmlHttpError = 'XML HTTP Request: OK';  // for trouble shooting

// This is the driver routine to call a php file to get the values
function getProperty(obj, pUrl, pElementId) {
	var tgt = pUrl + '?houseID=' + obj.value;
	doAJAX(tgt, pElementId);
}

function getSubdivision(obj, pUrl, pElementId) {
	var tgt = pUrl + '?subID=' + obj.value;
	//alert (tgt);
	doAJAX(tgt, pElementId);
}

function getUserID(obj, pUrl, pElementId) {
	var tgt = pUrl + '?userID=' + obj.value;
	//alert (tgt);
	doAJAX(tgt, pElementId);
}

function getTestimonial(obj, pUrl, pElementId) {
	var tgt = pUrl + '?testID=' + obj.value;
	//alert (tgt);
	doAJAX(tgt, pElementId);
}

function getMember(obj, pUrl, pElementId) {
	var tgt = pUrl + '?netID=' + obj.value;
	//alert (tgt);
	doAJAX(tgt, pElementId);
}

function getMemberDisplay(obj, pUrl, pElementId) {
	if (obj.value == 0) {
		document.getElementById('properties_listing_wrapper').innerHTML = '';
		document.getElementById('memberPics').innerHTML = '';
	}	else {
		var tgt = pUrl + '?netID=' + obj.value;
	//alert (tgt);
		doAJAX(tgt, pElementId);
	}
}

// This is the routine to put all the values into the elements
function processString(txt) {
	// We will have formulated the string as pairs by element name and value.  It will 
	// appear as   tag1:valTag1?tag2:ValTag2  etc.  so
	var first26 = txt.substring(0, 26);
	if (first26 == 'properties_listing_wrapper') {
		processDisplayString(txt);
		return;
	}
	var pairs = txt.split('?');
	var numPairs = pairs.length;
	// Now process each pair
	for (i=0; i<numPairs; i++) {
		var parts = pairs[i].split(':');
		obj = document.getElementById('edit_' + parts[0]);
		if (obj != null) {
			if (parts[0] == "featured") {
				if (parts[1] == 1) {
					obj.checked = true; 
				} else {
					obj.checked = false;
				}
			}  else if ( (parts[0] == 'subName') && (parts[1] == '-- Choose a Subdivision --') ) {
					obj.value = '';
				
			}  else if ( (parts[0] == 'address') && (parts[1] == '-- Choose a Property --') ) {
					obj.value = '';	
			} else {
					obj.value = parts[1];
			}
		}
	}
}

function processDisplayString(txt) {
	var pairs = txt.split('`');
	var numPairs = pairs.length;
	for (i=0; i<numPairs; i++) {
		var parts = pairs[i].split('|');
		obj = document.getElementById(parts[0]);
		obj.innerHTML = parts[1];
	}
}


// This is the start of the AJAX stuff -- treat as a black box
function doAJAX(pUrl,pElementId,pImageSrc) {
    if (arguments.length==3) {  // if an image source is sent
        if (pImageSrc) {
						// load the image while waiting
            document.getElementById(pElementId).innerHTML='<img src="'+pImageSrc+'">';  
        }
    }
    if (arguments.length>=3) {  // if a targetElement ID is sent
        if (pImageSrc) {
						// load the image while waiting
            document.getElementById(pElementId).innerHTML='<img src="'+pImageSrc+'">';  
        }
    }

    var objRequest = getRequestObject();  // create a new request object
    if (typeof(objRequest)=='object') {  // if we have an object, and
        if (objRequest.readyState>=0) {  // if the object supports the correct properties...proceed
						// specifiy the function to handle the results
						 // the correct request object is passed for us.
            objRequest.onreadystatechange = 
								function() { 
									handleHttpResponse(objRequest, pElementId); 
								};            
						objRequest.open('POST', pUrl, true);
						objRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
						objRequest.send(pUrl);
            //objRequest.send(null);
        } else {
            xmlHttpError = 'XML HTTP Request Object Unavailable';
            return false;
        }
    } else {
        xmlHttpError = 'XML HTTP Request Object Not Supported';
        return false;
    }
}

function getRequestObject() {
    var objRequest;
    if (window.ActiveXObject) {
        if (tc) {
            try {
                objRequest = new ActiveXObject('Msxml2.XMLHTTP');  // IE 6+
            }
            catch(e) {
                try {
                    objRequest = new ActiveXObject('Microsoft.XMLHTTP');  // IE 5.5
                }
                catch(f) { }
            }
        } else {
            objRequest = new ActiveXObject('Microsoft.XMLHTTP');  // ? IE 5.0 ?
        }
    } else if (window.XMLHttpRequest) {
        objRequest = new XMLHttpRequest();  // All other browsers, inc. IE 7 and DOM 3
    }
    return objRequest;
}

function includeFile(pUrl,pElementId,pImageSrc) {
    if (arguments.length==3) {  // if an image source is sent
        if (pImageSrc) {
						// load the image while waiting
            document.getElementById(pElementId).innerHTML='<img src="'+pImageSrc+'">';  
        }
    }
    if (arguments.length>=3) {  // if a targetElement ID is sent
        if (pImageSrc) {
						// load the image while waiting
            document.getElementById(pElementId).innerHTML='<img src="'+pImageSrc+'">';  
        }
    }

    var objRequest = getRequestObject();  // create a new request object

    if (typeof(objRequest)=='object') {  // if we have an object, and
        if (objRequest.readyState>=0) {  // if the object supports the correct properties...proceed
						// specifiy the function to handle the results
						 // the correct request object is passed for us.
            objRequest.onreadystatechange = 
								function() { 
									handleHttpResponse(objRequest, 
									pElementId); 
								};            
						objRequest.open('GET', pUrl, true);
						objRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
						objRequest.send(pUrl);
            //objRequest.send(null);
        } else {
            xmlHttpError = 'XML HTTP Request Object Unavailable';
            return false;
        }
    } else {
        xmlHttpError = 'XML HTTP Request Object Not Supported';
        return false;
    }
}

function handleHttpResponse(pObjRequest, pElementId) {
    if (pObjRequest.readyState==4) {
        if (pObjRequest.status==200) {
						// load the results into the element
						resp = pObjRequest.responseText;
						//alert('resp=' + resp);
						processString(resp);  
            pObjRequest=null;  // dispose of the now un-needed object.
        }
    }
}

