// Environment ---------------------------------------------------------------------------
var siteHost = window.location.hostname
if(siteHost=='lsnetbook' || siteHost=='devbox.pg54.com' || siteHost=='devbox')siteHost=siteHost+'/FBv4'
siteHost = 'http://'+siteHost+'/'

var outerWrapper = 940
var scrollOffset
var boxWidth = 0
var scrollOffset = 0
var scrollTimerId
var scrollPause = 40
var scrollStep = 1


// Ajax Common ---------------------------------------------------------------------------
var xmlHttp

function nowTime(){
	var date = new Date();
	var timestamp = date.getTime();
	return timestamp
}

function GetXmlHttpObject(){
	var objXMLHttp=null
	if (window.XMLHttpRequest){
		objXMLHttp=new XMLHttpRequest()
	}else if (window.ActiveXObject){
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
	}
	return objXMLHttp
}



// The PopUps ---------------------------------------------------------------

var popupId
var popupWidth
var popupHeight

function popUp(newpopupId,newpopupWidth,newpopupHeight,data){
	popupId = newpopupId
	popupWidth = newpopupWidth
	popupHeight = newpopupHeight
	var url=siteHost+'PopUps/'+newpopupId+'.php?host='+siteHost+'&data='+data+'&nochache='+nowTime()
	xmlHttpPopUpContent=GetXmlHttpObject()
	xmlHttpPopUpContent.open("GET",url,true)
	xmlHttpPopUpContent.onreadystatechange = function() {
		if (xmlHttpPopUpContent.readyState==4 || xmlHttpPopUpContent.readyState=="complete"){
			//alert(xmlHttpPopUpContent.responseText)
			bits=xmlHttpPopUpContent.responseText.split('[[fblcm]]')
			if(bits[1]){
				//alert('OK')
				window.location=siteHost+'Home/Loyalty_Club?fblcm='+bits[1]
			}else{
				document.getElementById('PopUpContent').innerHTML=xmlHttpPopUpContent.responseText
				openPopUp()
			}
		}
	}
	xmlHttpPopUpContent.send(null);
}


function openPopUp(){
	if(window.pageYOffset){
		//newTopPos1 = window.pageYOffset
		newTopPos2 = 50+window.pageYOffset
	}else{
		//newTopPos1 = document.documentElement.scrollTop
		newTopPos2 = 50+document.documentElement.scrollTop
	}

	var windowWidth = document.documentElement.clientWidth
	var leftMargin = parseInt((windowWidth-popupWidth)/2)
	var popupDiv = document.getElementById('PopUpControl').style
	var popupWrapper = document.getElementById('PopUpWrapper').style
	var newWrapperHeight = getDocHeight()

	popupWrapper.height= newWrapperHeight+'px'
	popupWrapper.display='block'


	popupDiv.width= popupWidth+'px'
	popupDiv.height= popupHeight+'px'
	popupDiv.left=leftMargin+'px'
	popupDiv.top=newTopPos2+'px'
	popupDiv.display='block'

}


function closePopUp(){
	var popupDiv = document.getElementById('PopUpControl').style
	var popupWrapper = document.getElementById('PopUpWrapper').style
	popupDiv.display='none'
	popupWrapper.display='none'
	document.getElementById('PopUpContent').innerHTML=''
}

function getDocHeight() {
	var D = document;
	return Math.max(
	Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
	Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
	Math.max(D.body.clientHeight, D.documentElement.clientHeight)
	);
}

function showCatLinks(catLinksId){
	document.getElementById('CatLinks'+catLinksId).style.display='block'
}

function hideCatLinks(catLinksId){
	document.getElementById('CatLinks'+catLinksId).style.display='none'
}


// LOYALTY CLUB ======================================================================
function confirmLoyaltyClubSignUp(){
	newEmailAddress = document.getElementById('EmailBox').value
	if(newEmailAddress.length>5 && newEmailAddress!='enter your email here'){
		popUp('LoyaltyClubRegistration',380,200,newEmailAddress)
	}else{
		alert('Please enter your email address')
	}
}

function matchEmails(){
	email1 = document.getElementById('EmailAddress1').value
	email2 = document.getElementById('EmailAddress2').value
	if(email1==email2){
		document.getElementById('ConfirmLoyaltyClubEmail').disabled=false
	}else{
		document.getElementById('ConfirmLoyaltyClubEmail').disabled=true
	}
}

function submitNewLoyaltyClubEmail(){
	data = document.getElementById('EmailAddress1').value
	var url=siteHost+'/Modules/AddLoyaltyClubRegistration.php?host='+siteHost+'&data='+data+'&nochache='+nowTime()
	//alert(url)
	xmlHttp=GetXmlHttpObject()
	xmlHttp.open("GET",url,true)
	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
			document.getElementById('PopUpContent').innerHTML=xmlHttp.responseText
			setTimeout(closePopUp,10000);
		}
	}
	xmlHttp.send(null);
}

function confirmLoyaltyClubRegistration(){
	popUp('RegistrationConfirmation',380,200)
	setTimeout(closePopUp,5000);
}

// Search =========================================================================

var mouseOverLinks = false
var searchDelayTimerId

function hideSearch(){
	document.getElementById('PredictiveSearchResults').style.display='none'
}

function resetSearchText(){
	if(!mouseOverLinks)document.getElementById('PredictiveSearchResults').style.display='none'
	var searchText = document.getElementById('SearchText').value
	if(searchText==''){
		document.getElementById('SearchText').value='Search'
	}else{
		if(searchText=='Search'){
			document.getElementById('SearchText').value=''
		}
	}
}

function startSearchTimer(){
	clearTimeout(searchDelayTimerId)
	searchDelayTimerId = setTimeout('doSearch()',100)
}

function doSearch(){
	var searchText = document.getElementById('SearchText').value
	if(searchText!=''){
		var url=siteHost+'/Modules/FBv4_PredictiveSearch.php?host='+siteHost+'&searchText='+searchText+'&nochache='+nowTime()
		xmlHttp=GetXmlHttpObject()
		xmlHttp.open("GET",url,true)
		xmlHttp.onreadystatechange = function() {
			if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){

				if(xmlHttp.responseText!=''){
					//alert(xmlHttp.responseText)
					document.getElementById('PredictiveSearchResults').innerHTML=xmlHttp.responseText
					document.getElementById('PredictiveSearchResults').style.display='block'

				}else{
					document.getElementById('PredictiveSearchResults').style.display='none'
				}
			}
		}
		xmlHttp.send(null);
	}else{
		document.getElementById('PredictiveSearchResults').style.display='none'
	}
}

// Scroller ===================================================


function initScroller(){
	scrolltext = document.getElementById('ScrollText').value
	if(scrolltext!=''){
		document.getElementById('RepeadtedScrollerContentHolder').innerHTML='<div id="TextBlock1" class="TextBlockWrapper">'+scrolltext+'</div>'
	}
	boxWidth = document.getElementById('TextBlock1').offsetWidth
	//document.getElementById('BoxWidth').value = boxWidth

	var requiredRepeats = parseInt(outerWrapper/boxWidth)+2
	//document.getElementById('Repetitions').value = requiredRepeats
	
	var sliderWidth = (requiredRepeats*boxWidth)+100
	document.getElementById('RepeadtedScrollerContentHolder').style.width=sliderWidth+'px'

	repeatedText = ''
	for(x=0;x<requiredRepeats;x++){
		repeatedText = repeatedText+'<div id="TextBlock1" class="TextBlockWrapper">'+scrolltext+'</div>'

	}
	document.getElementById('RepeadtedScrollerContentHolder').innerHTML=repeatedText
	
	scrollTimerId = setInterval('moveScroller()',scrollPause)
}

function moveScroller(){
	scrollOffset = scrollOffset - scrollStep
	if((0-scrollOffset)>boxWidth) scrollOffset = 0
	document.getElementById('RepeadtedScrollerContentHolder').style.left = scrollOffset+'px'
}

function showConfirmation(){
	document.getElementById('PasswordConfirmation').style.display = 'block'
}

function hideConfirmation(){
	document.getElementById('PasswordConfirmation').style.display = 'none'
}
