// Preloads pages. Pleaces new page source in lightbox then centers and displays.
function showPopup(reg_Login) {
	var objsrcHTML = document.getElementById('srcHTML');
	if(objsrcHTML){
		objsrcHTML.innerHTML="";
	}
	hideAllByTag('select');

	var allSelect = document.getElementsByTagName("select");
	
	var num = allSelect.length;

	if(reg_Login=="Reg"){
		showRegistration();
	}
	if(reg_Login=="login"){
		showLogin();
	}
	if(reg_Login=="share"){
		displayNewPopup();
	}
	if(reg_Login=="submit"){
		ValidateForm();
	}
	if(reg_Login=="add_feature"){
		displayAdditionalFeaturePopup();
	}
	
	var objOverlay = document.getElementById('overlay');
	var objLightbox = document.getElementById('lightbox');
	var objLoadingImage = document.getElementById('loadingImage');
	
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();

	// center loadingImage if it exists
	if (objLoadingImage) {
		objLoadingImage.style.top = (arrayPageScroll[1] + ((arrayPageSize[3] - 35 - objLoadingImage.height) / 2) + 'px');
		objLoadingImage.style.left = (((arrayPageSize[0] - 20 - objLoadingImage.width) / 2) + 'px');
		objLoadingImage.style.display = 'block';
	}

	// set height of Overlay to take up whole page and show
	objOverlay.style.height = (arrayPageSize[1] + 'px');
	objOverlay.style.display = 'block';

	// center lightbox and make sure that the top and left values are not negative
	// and the page source placed outside the viewport
	var lightboxWidth = objLightbox.style.width;
	var lightboxHeight = objLightbox.style.height;

	var index = lightboxWidth.indexOf("px");
	if (index != -1) {
		lightboxWidth = lightboxWidth.substring(0, index);
	}
	
	index = lightboxHeight.indexOf("px");
	if (index != -1) {
		lightboxHeight = lightboxHeight.substring(0, index);
	}
	
	var lightboxTop = arrayPageScroll[1] + ((arrayPageSize[3] - 35 - lightboxHeight) / 2);
	var lightboxLeft = ((arrayPageSize[0] - 20 - lightboxWidth) / 2);
	
	objLightbox.style.top = (lightboxTop < 0) ? "0px" : lightboxTop + "px";
	objLightbox.style.left = (lightboxLeft < 0) ? "0px" : lightboxLeft + "px";

	// A small pause between the page loading and displaying is required with IE,
	// this prevents the previous image displaying for a short burst causing flicker.
	if (navigator.appVersion.indexOf("MSIE") != -1) {
		pause(250);
	} 

	if (objLoadingImage) {	
		objLoadingImage.style.display = 'none';
	}
	objLightbox.style.display = 'block';

	// After page is loaded, update the overlay height as the new image might have
	// increased the overall page height.
	arrayPageSize = getPageSize();
	objOverlay.style.height = (arrayPageSize[1] + 'px');
	
	// return false;
}

function showContactMailPopup(agentId,dealId,languageCode,countryCode) {
	var objsrcHTML = document.getElementById('srcHTML');
	if(objsrcHTML) {
		objsrcHTML.innerHTML = "";
	}

	hideAllByTag('select');

	var allSelect = document.getElementsByTagName("select");
	
	var num = allSelect.length;

	var objOverlay = document.getElementById('overlay');
	var objLightbox = document.getElementById('lightbox');
	var objLoadingImage = document.getElementById('loadingImage');
	
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();

	// center loadingImage if it exists
	if (objLoadingImage) {
		objLoadingImage.style.top = (arrayPageScroll[1] + ((arrayPageSize[3] - 35 - objLoadingImage.height) / 2) + 'px');
		objLoadingImage.style.left = (((arrayPageSize[0] - 20 - objLoadingImage.width) / 2) + 'px');
		objLoadingImage.style.display = 'block';
	}

	// set height of Overlay to take up whole page and show
	objOverlay.style.height = (arrayPageSize[1] + 'px');
	objOverlay.style.display = 'block';

	// center lightbox and make sure that the top and left values are not negative
	// and the page source placed outside the viewport
	if(objLightbox != null) {
		var lightboxWidth = objLightbox.style.width;
		var lightboxHeight = objLightbox.style.height;
		var index = lightboxWidth.indexOf("px");

		if (index != -1) {
			lightboxWidth = lightboxWidth.substring(0, index);
		}
		
		index = lightboxHeight.indexOf("px");
		if (index != -1) {
			lightboxHeight = lightboxHeight.substring(0, index);
		}
		
		var lightboxTop = arrayPageScroll[1] + ((arrayPageSize[3] - 35 - lightboxHeight) / 2);
		var lightboxLeft = ((arrayPageSize[0] - 20 - lightboxWidth) / 2);
		
		objLightbox.style.top = (lightboxTop < 0) ? "0px" : lightboxTop + "px";
		objLightbox.style.left = (lightboxLeft < 0) ? "0px" : lightboxLeft + "px";
	}
	
	/* Code for new window for agent details - #2939 */
	var agentLightBox = document.getElementById('agentLightbox');
	
	if(agentLightBox != null) {
		var agentLightboxWidth = agentLightBox.style.width;
		var agentLightboxHeight = agentLightBox.style.height;
		var index = agentLightboxWidth.indexOf("px");

		if (index != -1) {
			agentLightboxWidth = agentLightboxWidth.substring(0, index);
		}
		
		index = agentLightboxHeight.indexOf("px");
		
		if (index != -1) {
			agentLightboxHeight = agentLightboxHeight.substring(0, index);
		}
		
		var agentLightboxTop = arrayPageScroll[1] + ((arrayPageSize[3] - 150 - agentLightboxHeight) / 2);
		var agentLightboxLeft = ((arrayPageSize[0] + 25 - agentLightboxWidth) / 2);
		
		agentLightBox.style.top = (agentLightboxTop < 0) ? "0px" : agentLightboxTop + "px";
		agentLightBox.style.left = (agentLightboxLeft < 0) ? "0px" : agentLightboxLeft + "px";
	}
	/* Code for #2939 ends here */

	// A small pause between the page loading and displaying is required with IE,
	// this prevents the previous image displaying for a short burst causing flicker.
	if (navigator.appVersion.indexOf("MSIE") != -1) {
		pause(250);
	} 

	if (objLoadingImage) {	
		objLoadingImage.style.display = 'none';
	}
	if(objLightbox != null) {
		objLightbox.style.display = 'block';
	}
	
	/* Code for new window for agent details - #2939 */
	if(agentLightBox != null) {
		agentLightBox.style.display = 'block';
	}
	/* Code for #2939 ends here */
	
	// After page is loaded, update the overlay height as the new image might have
	// increased the overall page height.
	arrayPageSize = getPageSize();
	objOverlay.style.height = (arrayPageSize[1] + 'px');
	
	contactAgent(agentId,dealId,languageCode,countryCode);
	
	//return false;
}

// Returns array with x,y page scroll values.
function getPageScroll() {
	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}

// Returns array with page width, height and window width, height
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}


// Pauses code execution for specified time. Uses busy code, not good.
function pause(numberMillis) {
	var now = new Date();
	var exitTime = now.getTime() + numberMillis;
	while (true) {
		now = new Date();
		if (now.getTime() > exitTime)
			return;
	}
}

function hideLightbox() {
	document.getElementById("srcHTMLNew").style.display = "block";
	objOverlay = document.getElementById('overlay');
	objLightbox = document.getElementById('lightbox');

	if(document.getElementById('agentLightbox') != null) {
		document.getElementById('agentLightbox').style.display = 'none';
	}

	objOverlay.style.display = 'none';
	if(objLightbox != null) {
		objLightbox.style.display = 'none';
	}

	showAllByTag('select','visible');

	checkLoginStatus();
	var objEmailBox=document.getElementById("myemail");
	if(objEmailBox)
		objEmailBox.value=enterEmail;
}

function hideAllByTag(tagName) {
	var elements = document.getElementsByTagName(tagName);
    var i = 0;
    while (i < elements.length) {
    	//elements[i].style.display = "none";
		elements[i].style.visibility = "hidden";
        i++;
	}
	if(document.getElementById("advertisement-div") != null)
		document.getElementById("advertisement-div").style.visibility = "hidden";
}

function showAllByTag(tagName,dispType) {
	var elements = document.getElementsByTagName(tagName);
    var i = 0;
    if (dispType == "") {
    	dispType = "inline";
	}
    while (i < elements.length) {
    	//elements[i].style.display = dispType;
		elements[i].style.visibility = dispType;
        i++;
	}
	if(document.getElementById("advertisement-div") != null)
		document.getElementById("advertisement-div").style.visibility = dispType;
}

function showLogin(){
	document.getElementById("normalpopup").style.display="none";
	document.getElementById("srcHTML").style.display="none";
	gSLAuthProxy.ShowLoginFull();
}
function showRegistration(){
	document.getElementById("normalpopup").style.display="none";
	document.getElementById("srcHTML").style.display="none";
	gSLAuthProxy.ShowRegistration();
}

/* To display LightBox with Share page form on 'Share' Link */
function sharePopUp(lightBoxName) {
	var objsrcHTML = document.getElementById('srcHTML');
	
	if(objsrcHTML) {
		objsrcHTML.innerHTML = "";
	}
	
	displayNewPopup();
	var topGap = '';
	var leftGap = '';
	var objLightbox = document.getElementById(lightBoxName);
	var objOverlay = document.getElementById('overlay');
	
	if(lightBoxName == 'agentLightbox') {
		topGap = 150;
		leftGap = 30;
	}
	lightBoxSettings(objOverlay, objLightbox, topGap, leftGap);
}

function lightBoxSettings(objOverlay, objLightbox, topGap, leftGap) {
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();
	
	// set height of Overlay to take up whole page and show
	objOverlay.style.height = (arrayPageSize[1] + 'px');
	objOverlay.style.display = 'block';

	// center lightbox and make sure that the top and left values are not negative
	// and the page source placed outside the viewport
	var lightboxWidth = objLightbox.style.width;
	var lightboxHeight = objLightbox.style.height;

	var index = lightboxWidth.indexOf("px");
	if (index != -1) {
		lightboxWidth = lightboxWidth.substring(0, index);
	}
	
	index = lightboxHeight.indexOf("px");
	if (index != -1) {
		lightboxHeight = lightboxHeight.substring(0, index);
	}
	
	var lightboxTop = arrayPageScroll[1] + ((arrayPageSize[3] - topGap - lightboxHeight) / 2);
	var lightboxLeft = ((arrayPageSize[0] - leftGap - lightboxWidth) / 2);
	
	objLightbox.style.top = (lightboxTop < 0) ? "0px" : lightboxTop + "px";
	objLightbox.style.left = (lightboxLeft < 0) ? "0px" : lightboxLeft + "px";

	// A small pause between the page loading and displaying is required with IE,
	// this prevents the previous image displaying for a short burst causing flicker.
	if (navigator.appVersion.indexOf("MSIE") != -1) {
		pause(250);
	}

	objLightbox.style.display = 'block';
	
	// After page is loaded, update the overlay height as the new image might have
	// increased the overall page height.
	arrayPageSize = getPageSize();
	objOverlay.style.height = (arrayPageSize[1] + 'px');
}