var iframeId = null;

function GenerateQueryString(searchValues){
	var queryArray = searchValues.split("??");
	var url = "";
	for (i = 0; i < queryArray.length; i++){
		var keyValue = queryArray[i].split("##");
		if (keyValue.length == 2){
			url += '&' + keyValue[0] + "=" + keyValue[1];
		}
	}
	return url;
}

function InvokeLongProcessP(frameId,id,searchvalues){
	var searchQueryString = GenerateQueryString(searchvalues);
	loadXMLDoc("/CMSSource/Templates/Ravensdown/IFrameHtml.aspx?ID=" + id + searchQueryString,frameId) ;
}

function InvokeLongProcess(frameId,id){
	loadXMLDoc("/CMSSource/Templates/Ravensdown/IFrameHtml.aspx?ID="+id,frameId) ;
}

function loadXMLDoc(url,frameId) {
	iframeId = frameId;
	writeImage();
	//document.getElementById(iframeId).innerHTML = "<img src=/CMSSource/images/season/ProgressBar.gif>"
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest){
		req = new XMLHttpRequest();
		req.onreadystatechange = processReqChange;
		req.open("GET", url, true);
		req.send(null);
    // branch for IE/Windows ActiveX version
    }
    else if (window.ActiveXObject){
		isIE = true;
		req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChange;
            req.open("GET", url, true);
            req.send();
        }
    }
}

function processReqChange() {
    // only if req shows "loaded"	
    if (req.readyState == 4) {
		// only if "OK"
		//document.getElementById(iframeId).innerHTML = req.responseText;
		if (req.status == 200) {
			writeHtml(req.responseText);
		} else {
			writeErrorMessage();
		}
    }
}

function writeHtml(text){
	//var iframeDoc = window.frames[iframeId].document;
	var iframeDoc = null;
	/*try{
		iframeDoc = document.getElementById(iframeId).window.document;
	}catch(e){
		alert(e);
	}*/
	if (window.frames[iframeId])
		iframeDoc = window.frames[iframeId].document;
	else
		iframeDoc = document.getElementById(iframeId).contentDocument;
	if (iframeDoc){
		iframeDoc.clear();
		iframeDoc.open();
		iframeDoc.write(text);
		iframeDoc.close();
	}
	else
		alert(iframeDoc + " Null?");
}

function writeImage(){
	/*var iframeDoc = window.frames[iframeId].document;
	if (iframeDoc){
		iframeDoc.clear();
		iframeDoc.open();
		var html = '<table width=100%><tr><td>';
		html += '<b>Thank you</b><br>We are currently processing your request. This may take a few moments';
		html += '</td></tr>';
		html += '<tr><td align=center><img src=/CMSSource/images/season/ProgressBar.gif></td></tr>';
		html += '</table>';
		//iframeDoc.write('<table width=100%><tr><td align=center><img src=/CMSSource/images/season/ProgressBar.gif></td></tr></table>');
		iframeDoc.write(html);
		iframeDoc.close();
	}
	else
		alert(iframeDoc + " Null?");*/
	document.getElementById(iframeId).src = "/CMSSource/ProgressBar.aspx";
}

function writeErrorMessage(){
	/*var iframeDoc = window.frames[iframeId].document;
	if (iframeDoc){
		iframeDoc.clear();
		iframeDoc.open();
		var html = '<table width=100%><tr><td>';
		html += 'There was a problem retrieving the data. Please try again or contact Ravensdown';
		html += '</td></tr></table>';
		//iframeDoc.write('<table width=100%><tr><td align=center><img src=/CMSSource/images/season/ProgressBar.gif></td></tr></table>');
		iframeDoc.write(html);
		iframeDoc.close();
	}
	else
		alert(iframeDoc + " Null?");*/
	document.getElementById(iframeId).src = "/CMSSource/LongProcessError.aspx";
}

function shout(obj){
	alert(obj);
}
        
function test(obj){
	var properties = "";
	for(prop in obj)
		properties += prop + "\t";
	alert(properties);
}

function writeHtmlToDiv(text){
	if (document.getElementById(iframeId)){
		document.getElementById(iframeId).innerHtml = text;
	}
	else
		alert("Couldn't Find Div With ID " + iframeId);
}
