var mode="";
var dontConfirm=false; // Use to neglact the success messagebox after the "popupPost" or "popupGet" call. Used in popupUpdate function.

var ajaxFetch=new ajaxWrapper(popupCatch, popupError);
var ajaxUpdate=new ajaxWrapper(popupUpdate, popupError);

// Used by popup move (popupStartMove() and popupStopMove())
var popupX=0;
var popupY=0;

var disableFade=false;
var fireOnLoadEvent=false;
 var blank = new Image();
 blank.src = 'images/blank.gif';  
function popupFetch(name, params, disableInterfaceFade, disablePopupFade, onLoadEvent) 
{
	disableInterfaceFade=(disableInterfaceFade==undefined) ? false : disableInterfaceFade;
	disablePopupFade=(disablePopupFade==undefined) ? false : disablePopupFade;
	onLoadEvent=(onLoadEvent==undefined) ? false : onLoadEvent;

	fireOnLoadEvent=onLoadEvent; // used in popupCatch function.

	disableFade=disablePopupFade; // used in showPopup function.

	showLoader();

	if(disableInterfaceFade) {
		popupMakeCall(name, params);
	}
	else  
	{
		 //popupMakeCall(name, params);
		//$("#interface").fadeOut("slow",function() { popupMakeCall(name, params); });
		var fade = new fadeObject(document.getElementById("interface"), function() { popupMakeCall(name, params); });
		fade.fadeOut(3);
	}
}

function popupMakeCall(name, params) {
	//ajaxFetch.ajax_doGetXML(name, params);	
	$.ajax({
		type: "GET",
		url: name,
		cache: false,
		data: params,
		dataType: "text",
		success: function(responseText, statusText) { 
			var objDom = new XMLDoc(responseText, popupError); // Requires xmldom.js (XML for <script> Parser)
			var data = objDom.docNode;
			popupCatch(data);
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			hideLoader();
			//alert("ERROR: " + XMLHttpRequest.statusText + " " + name);
			alert("VirtuallyShow.com was not able to process your request\n\nPlease retry.");
		}
	});
}

function popupCatch(data) {

	if(data.tagName=="response") {
		alert(data.getElements("msg")[0].getText());
		window.location=data.getElements("location")[0].getText();
		return;
	}

	// Add the dynamic javascript which will be validated by the browsers. (Important Step)
	var head = document.getElementsByTagName("head")[0];

	var script=document.createElement('script');
	script.id="scripts";
   	script.type = 'text/javascript';
	script.language="javascript";
	script.text=data.getElements("scripts")[0].getText();
   	head.appendChild(script);
	hideLoader();
	hideCombos();

	document.getElementById("popup").innerHTML=data.getElements("data")[0].getText();
	// Need Help content
	if(data.getElements("help").length==1) {
		document.getElementById("helpcontent").innerHTML=data.getElements("help")[0].getText();
	}

	//hideObjects();
	showPopup();

	if(fireOnLoadEvent) {
		onPopupLoad();	
	}
}

function popupError(err) {
	//hideLoader();
	//hideUpdater();

	//alert("VirtuallyShow.com was not able to process your request\n\nInternal error: " + err +"\n\nPlease retry.");
	alert("VirtuallyShow.com is not able to communicate with server at this moment. Please retry.\n\nWe apologize for the inconvinience.");
	window.location.reload();
}

function popupSend(id, disablePopupFade) {
	disablePopupFade=(disablePopupFade==undefined) ? false : disablePopupFade;

	disableFade=disablePopupFade; // used in popupUpdate function too.

	showUpdater();
	if(disableFade) {
		popupPost(id);
	}
	else {
		var fade = new fadeObject(document.getElementById("popup"), function() { popupPost(id); });
		fade.fadeOut(3);
	}
}

function popupUpdate(data) {
	hideUpdater();
	
	if(disableFade) {
		if(!dontConfirm) 
		{
			alert(data.getElements("msg")[0].getText()); 
		} 
		dontConfirm=false;
	}
	else {
		var fade = new fadeObject(document.getElementById("popup"), function() { if(!dontConfirm) { alert(data.getElements("msg")[0].getText()); } dontConfirm=false; }  );
		fade.fadeIn(10, 3);
	}

	if(data.getElements("data").length==1) {
		callBackData(data.getElements("data")[0].getText(), data);
	}
}

function popupPost(id) {
	//alert(createQueryString(id));
	//ajaxUpdate.ajax_doPostXML(document.getElementById(id).action, createQueryString(id));
	$("#"+id).ajaxSubmit({success:popupformResponse,
            error:function(){
                alert("VirtuallyShow.com was not able to process your request\n\nPlease retry.");
                window.location.reload();
            },
            dataType: "text"});
	
}

function popupformResponse(responseText, statusText) {
	hideUpdater();

	if(statusText=='success') {
		var objDom = new XMLDoc(responseText, popupError); // Requires xmldom.js (XML for <script> Parser)
		var data = objDom.docNode;
		//that.onSuccess(objDomTree);
		
		if(disableFade) {
			if(!dontConfirm) 
			{
				alert(data.getElements("msg")[0].getText()); 
			} 
			dontConfirm=false;
		}
		else {
			var fade = new fadeObject(document.getElementById("popup"), function() { if(!dontConfirm) { alert(data.getElements("msg")[0].getText()); } dontConfirm=false; }  );
			fade.fadeIn(10, 3);
		}
	
		if(data.getElements("data").length==1) {
			callBackData(data.getElements("data")[0].getText(), data);
		}
	}
	else {
		popupError();
	}
}

function popupGet(id) {
	
	//ajaxUpdate.ajax_doGetXML(document.getElementById(id).action, createQueryString(id));
	$("#"+id).ajaxSubmit({success:popupformResponse,
		error:function(){
			alert("VirtuallyShow.com was not able to process your request\n\nPlease retry.");
			window.location.reload();
		},
		dataType: "text"});
	
}

function createQueryString(id) {
	var inputs=document.getElementById(id).getElementsByTagName("input");
	var queryString="";
	for(var index=0;index<inputs.length;index++) {
		if(inputs[index].type=='text' || inputs[index].type=='password' || inputs[index].type=='hidden') {
			if(trim(queryString).length==0) {
				queryString=inputs[index].name + '=' + checkspecialchar(inputs[index].value);
				//queryString=inputs[index].name + '=' + checkspecialchar(htmlentities(inputs[index].value));
			}
			else {
				queryString+='&' + inputs[index].name + '=' + checkspecialchar(inputs[index].value);
				//queryString+='&' + inputs[index].name + '=' + checkspecialchar(htmlentities(inputs[index].value));
			}
		}
		else if(inputs[index].type=='checkbox' || inputs[index].type=='radio') {
			if(inputs[index].checked) {
				if(trim(queryString).length==0) {
					queryString=inputs[index].name + '=' + inputs[index].value;
				}
				else {
					queryString+='&' + inputs[index].name + '=' + inputs[index].value;
				}		
			}
		}
				
	}

	inputs=document.getElementById(id).getElementsByTagName("textarea");
	for(var index=0;index<inputs.length;index++) {
		if(trim(queryString).length==0) {
			queryString=inputs[index].name + '=' + checkspecialchar(inputs[index].value);
			//queryString=inputs[index].name + '=' + checkspecialchar(htmlentities(inputs[index].value));
		}
		else {
			queryString+='&' + inputs[index].name + '=' + checkspecialchar(inputs[index].value);
			//queryString+='&' + inputs[index].name + '=' + checkspecialchar(htmlentities(inputs[index].value));
		}		
	}
	
	inputs=document.getElementById(id).getElementsByTagName("select");
	for(var index=0;index<inputs.length;index++) {
		if(trim(queryString).length==0) {
			queryString=inputs[index].name + '=' + inputs[index].options[inputs[index].selectedIndex].value;
		}
		else {
			queryString+='&' + inputs[index].name + '=' + inputs[index].options[inputs[index].selectedIndex].value;
		}
	}

	return queryString;
}


function checkspecialchar(str) {
	var newstr=str;
	newstr=newstr.replace(/&/g,'%26');
	return newstr;
}

function popupStartMove(e) {
	if(window.event) e=window.event;

	hideHelper();

	var area=getobject("popup");

	popupX=(e.clientX - area[0]);
	popupY=(e.clientY - area[1]);

	document.onmousemove=movePopup;
	document.onmouseup=popupStopMove;

	return false;
}

function movePopup(e) {
	if(window.event) e=window.event;

	var popup = document.getElementById("popup");

	popup.style.left=(e.clientX - popupX) + "px";
	popup.style.top=(e.clientY - popupY) + "px";
		
	return false;
}

function popupStopMove(e) {
	if(window.event) e=window.event;

	document.onmousemove="";	
	document.onmouseup="";	
}

function showLoader() {
	var top=document.documentElement.scrollTop;
	if(top==0) {
		top=document.body.scrollTop;	
	}
	if(!document.getElementById("loader"))
		return;
	var loader=document.getElementById("loader");
	loader.style.left=((document.body.clientWidth / 2) - (loader.offsetWidth / 2)) + "px";
	loader.style.top=(top + 10) + "px";
	loader.style.visibility="visible";

	//var fade = new fadeObject(loader);
	//fade.fadeIn();
}

function hideLoader() {
	if(document.getElementById("loader")) {
		var loader=document.getElementById("loader");
		loader.style.visibility="hidden";
	}
}

function showUpdater() {
	var top=document.documentElement.scrollTop;
	if(top==0) {
		top=document.body.scrollTop;	
	}
	var updater=document.getElementById("updater");
	updater.style.left=((document.body.clientWidth /2 ) - (updater.offsetWidth /2)) + "px";
	updater.style.top=(top + 10) + "px";
	updater.style.visibility="visible";

	//var fade = new fadeObject(loader);
	//fade.fadeIn();
}

function hideUpdater() {
	if(document.getElementById("updater")) {
		var updater=document.getElementById("updater");
		updater.style.visibility="hidden";
	}
}



function showProcessing() {
	
	var processingmodel=document.getElementById("processingmodel");
	processingmodel.style.display="";
	processingmodel.style.visibility="visible";
	processingmodel.style.left="0px";
	processingmodel.style.top="0px";
	processingmodel.style.height=document.body.clientHeight + "px";
	processingmodel.style.width=document.body.clientWidth + "px";
	
	
	var top=document.documentElement.scrollTop;
	if(top==0) {
		top=document.body.scrollTop;	
	}
	var updater=document.getElementById("processing");
	updater.style.left=((document.body.clientWidth /2 ) - (updater.offsetWidth /2)) + "px";
	updater.style.top=(top + 10) + "px";
	updater.style.visibility="visible";
}

function hideProcessing() {
	
	var processingmodel=document.getElementById("processingmodel");
	processingmodel.style.visibility="hidden";
	
	if(document.getElementById("processing")) {
		var updater=document.getElementById("processing");
		updater.style.visibility="hidden";
	}
}

function showHelper(disableFade) {
	var helper=document.getElementById("helper");
	if(helper) {
		var popup_area=getobject("popup");
		var helper_area=getobj(helper);

		//helper.style.left = ((popup_area[0] + popup_area[2]) - (helper_area[2] + 20)) + "px";
		helper.style.left = popup_area[0] + ((popup_area[2] / 2) - (helper_area[2] / 2)) + "px";
		helper.style.top = (popup_area[1] + 37) + "px";
		helper.style.visibility="visible";

		if(!disableFade) {
			var fade = new fadeObject(helper);
			fade.fadeIn();
		}
	}
}

function hideHelper() {
	if(document.getElementById("helper")) {
		var helper=document.getElementById("helper");
		if(helper) {
			helper.style.visibility="hidden";
		}
	}
}



function showPopup() {
	if(navigator.appName=="Microsoft Internet Explorer") {
		 $('img[src$=".png"]').each(function() {
					$(this).css('vertical-align','bottom');
					var s = this.src;
					if (!s.match(/white_topright.*/) && !s.match(/white_topleft.*/) && !s.match(/white_bottomleft.*/) && !s.match(/white_bottomright.*/) && !s.match(/top_left.*/) && !s.match(/top_right.*/) && !s.match(/bottom_left.*/) && !s.match(/bottom_right.*/)&& !s.match(/Facebook.*/)&& !s.match(/Linkin.*/)&& !s.match(/twitter.*/) ) {
					fixPng(this)
					}

					
					
		 });
	}
	var popupmodel=document.getElementById("popupmodel");
	popupmodel.style.display="";
	popupmodel.style.visibility="visible";
	
	var popup=document.getElementById("popup");

	if(!disableFade) {
		popup.style.opacity = 0;
		popup.style.filter = 'alpha(opacity=' + 0 + ')';
	}
	popup.style.display="";
	popup.style.visibility="visible";

	/*var popupshadow=document.getElementById("popupshadow");
	popupshadow.style.display="";
	popupshadow.style.visibility="visible";*/

	relocatePopup();

	//var fade = new fadeObject(popup, showObjects);
	if(!disableFade) {
		var fade = new fadeObject(popup);
		fade.fadeIn();
	}
}


  function fixPng(png) {
           // get src
        
           var src = png.src;
           // set width and height
           if (!png.style.width) { png.style.width = $(png).width(); }
           if (!png.style.height) { png.style.height = $(png).height(); }
           // replace by blank image
           png.src = blank.src;
           // set filter (display original image)
           png.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='scale')";
 }
 


function relocatePopup() {
	var top=document.documentElement.scrollTop;
	if(top==0) {
		top=document.body.scrollTop;	
	}

	if(!document.getElementById("popupmodel"))
		return;
	var popupmodel=document.getElementById("popupmodel");
	popupmodel.style.left="0px";
	popupmodel.style.top="0px";
	popupmodel.style.height=document.body.clientHeight + "px";
	popupmodel.style.width=document.body.clientWidth + "px";

	if(!document.getElementById("popup"))
		return;
	var popup=document.getElementById("popup");

	if(document.getElementById("flashInterface")) 
	{
		var area = getobject("flashInterface");

		//alert(area[0] + " " + area[1] + " " + area[2] + " " + area[3]);

		/*var offset=0;
		if(navigator.userAgent.indexOf("Chrome")>-1) {
			offset=4;	
		}
		else if(navigator.userAgent.indexOf("Safari")>-1) {
			offset=5;	
		}*/

		popup.style.left = (area[0] + 35) + "px";
		popup.style.top = (area[1] + 35) + "px";
	}
	else {
		popup.style.left=((document.body.clientWidth/2) - (popup.offsetWidth/2)) + "px";
		//popup.style.top=(top + ((screen.height/2) - (popup.offsetHeight/2))) + "px";
		popup.style.top=(top + 100) + 'px';
	}

	/*var popupshadow=document.getElementById("popupshadow");
	popupshadow.style.left=(((document.body.clientWidth/2) - (popup.offsetWidth/2)) + 5) + "px";
	//popupshadow.style.top=((document.documentElement.scrollTop + ((screen.height/2) - (popup.offsetHeight/2))) + 5) + "px";
	popupshadow.style.top=((document.documentElement.scrollTop + ((screen.height/2) - (popup.offsetHeight/2))) + 5) + "px";
	popupshadow.style.width=popup.offsetWidth + "px";*/
//	popupshadow.style.height=popup.offsetHeight + "px";


}

function hidePopup(disableFade) {
	
	disableFade=(disableFade==undefined) ? false : disableFade;

	hideHelper();

	var popupmodel=document.getElementById("popupmodel");
	popupmodel.style.visibility="hidden";

	var popup=document.getElementById("popup");
	popup.style.visibility="hidden";

	//hideObjects();

	/*var popupshadow=document.getElementById("popupshadow");
	popupshadow.style.visibility="hidden";*/
	if(disableFade) {
		showCombos(); 

		document.getElementById("popup").innerHTML="";
		if(document.getElementById("helpcontent"))
			document.getElementById("helpcontent").innerHTML="";
	}
	else {
		var fade = new fadeObject(document.getElementById("interface"), function() { showCombos(); 
		document.getElementById("popup").innerHTML=""; if(document.getElementById("helpcontent")) document.getElementById("helpcontent").innerHTML=""; });
		fade.fadeIn(10, 3);
	}

	/*var scripts=document.getElementsByTagName('script');
	for(var script=0;script<scripts.length;script++) {
		if(scripts[script].text.length>0 && scripts[script].id=='scripts') {
			alert(scripts[script].text);
		}
	}*/

	// IMPORTANT - remove the added script which came along in XML of the respective POPUP.
	var head = document.getElementsByTagName("head")[0];
	var script=document.getElementById("scripts");
	if(script) {
		head.removeChild(script);
	}

}

function showhelp() {
	mode="help";
	var help=document.getElementById("helparea");

	help.style.display="";
	relocatePopup();
}

function hideObjects() {
	var objects=document.getElementsByTagName("object");
	for(var index=0;index<objects.length;index++) {
		objects[index].style.visibility="hidden";
	}
}

function showObjects() {
	var objects=document.getElementsByTagName("object");
	for(var index=0;index<objects.length;index++) {
		objects[index].style.visibility="visible";	
	}
}

function hideCombos() {
	
	if(navigator.userAgent.indexOf("MSIE")>-1) {
		objects=document.getElementsByTagName("select");
		for(var index=0;index<objects.length;index++) {
			objects[index].style.visibility="hidden";
		}
		
		// Get all the combos from ifrm document
		frmscontainers=document.getElementsByTagName("iframe");
		for(var frmcount=0;frmcount<frmscontainers.length;frmcount++) {
			//var ifrmdoc=frmscontainers[frmcount].contentDocument || frmscontainers[frmcount].contentWindow;
			try{
				var ifrmdoc=frmscontainers[frmcount].contentDocument || frmscontainers[frmcount].contentWindow.document;
				if(ifrmdoc) {
					//objects=ifrmdoc.document.getElementsByTagName("select");
					objects=ifrmdoc.getElementsByTagName("select");
					for(var index=0;index<objects.length;index++) {
						objects[index].style.visibility="hidden";
					}
				}
			}
			catch(err){}
		}
	}	
}

function showCombos() {
	if(navigator.userAgent.indexOf("MSIE")>-1) {
		objects=document.getElementsByTagName("select");
		for(var index=0;index<objects.length;index++) {
			objects[index].style.visibility="visible";	
		}

		// Get all the combos from ifrm document
		frmscontainers=document.getElementsByTagName("iframe");
		for(var frmcount=0;frmcount<frmscontainers.length;frmcount++) {
			//var ifrmdoc=frmscontainers[frmcount].contentDocument || frmscontainers[frmcount].contentWindow;
			try{
				var ifrmdoc=frmscontainers[frmcount].contentDocument || frmscontainers[frmcount].contentWindow.document;
				if(ifrmdoc) {
					//objects=ifrmdoc.document.getElementsByTagName("select");
					objects=ifrmdoc.getElementsByTagName("select");
					for(var index=0;index<objects.length;index++) {
						objects[index].style.visibility="visible";
					}
				}
			}
			catch(err){}
		}
	}	
}


//window.onscroll=relocatepopup;
//window.onscroll=function(){ relocatepopup(); }
//document.body.onscroll=relocatepopup;

window.onresize=relocatePopup;

