// JavaScript Document
function switchDiv(div_id)
{
  var style_sheet = getStyleObject(div_id);
  if (style_sheet)
  {
    hideAll();
    //changeObjectVisibility(div_id, "visible");
    changeObjectVisibility(div_id, "block");
  }
  else 
  {
    alert("sorry, this only works in browsers that do Dynamic HTML");
  }
}

function getStyleObject(objectId) {
  // checkW3C DOM, then MSIE 4, then NN 4.
  //
  if(document.getElementById && document.getElementById(objectId)) {
	return document.getElementById(objectId).style;
   }
   else if (document.all && document.all(objectId)) {  
	return document.all(objectId).style;
   } 
   else if (document.layers && document.layers[objectId]) { 
	return document.layers[objectId];
   } else {
	return false;
   }
}

function changeObjectVisibility(objectId, newVisibility) {
    // first get the object's stylesheet
    var styleObject = getStyleObject(objectId);

    // then if we find a stylesheet, set its visibility
    // as requested
    //
    if (styleObject) {
	//styleObject.visibility = newVisibility;
	styleObject.display = newVisibility;
	return true;
    } else {
	return false;
    }
}

function changeObjectBackground(objectId, newBackground) {
    // first get the object's stylesheet
    var styleObject = getStyleObject(objectId);

    // then if we find a stylesheet, set its visibility
    // as requested
    //
    if (styleObject) {
	//styleObject.visibility = newVisibility;
	styleObject.backgroundImage = newBackground;
	return true;
    } else {
	return false;
    }
}

//param 2 (optional) is ID. Param 3 (optional) is tag name e.g., p or div, etc
function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	//alert(j);
	return classElements;
}

function setFocus(frm,element){
	document.forms[frm].elements[element].focus();
	}

/*
function hideAllFastFacts(){
   	changeObjectVisibility("Arabic","none");
   	changeObjectVisibility("Chinese","none");
   	changeObjectVisibility("Spanish","none");
	}
*/

function goHomePage(url){
	window.location = url;	
}

var redirectUrl='';
var uiWarningTimeout=60000;
var uiReSubmitTimeout=1000;

function login_ajax(pathOffset){
	$('frmLoginSubmit').disabled=true;
	$('loginMsg').update('please wait...');
	$("loginMsgWrapper").style.display="block";
	setTimeout('$("loginMsgWrapper").style.display="none";setFocusById("frmLoginUser");',uiWarningTimeout);
	setTimeout('$("frmLoginSubmit").disabled=false;',uiReSubmitTimeout);

	var serP=$('frmLogin').serialize();

	new Ajax.Request(pathOffset + "phplogininc/login_ajax_pr.php", {
		
		method:'post',
		
		parameters: serP,
		
		onComplete: function(transport){
			var response = transport.responseText;
			//check for logon - we'll close the lightbox and redirect the parent page to intended destination
			var res = response.search(/bingoRedirect/);
			if(res != -1){
				redirectUrl = response.replace('bingoRedirect:', '');
				$('loginMsg').update('You have been signed in - Welcome back!');
				setTimeout("goHomePage(redirectUrl)",2000);
			}
			//otherwise
			else{
				$('loginMsg').update(response);
			}
		},
		
		onFailure: function(){
			alert('ajax error');
		}
		
	});

}							


function register_ajax(pathOffset){
	$('frmRegisterSubmit').disabled=true;
	$('registerMsg').update('please wait...');
	$("registerMsgWrapper").style.display="block";
	setTimeout('$("registerMsgWrapper").style.display="none";',uiWarningTimeout);
	setTimeout('$("frmRegisterSubmit").disabled=false;',uiReSubmitTimeout);

	var serP=$('frmRegister').serialize();

	new Ajax.Request(pathOffset + "phplogininc/register_ajax_pr.php", {
		
		method:'post',
		
		parameters: serP,
		
		onComplete: function(transport){
			var response = transport.responseText;

			//look for embedded code to command stuff
			var res = response.search(/hideFormDiv:/);
			if(res != -1){
				var html = response.replace('hideFormDiv:','');
				$('hideUponRegistration').style.display="none";
				$('registrationAccepted').update(html);
			}
			else{
				$('registerMsg').update(response);
			}
		
		$('captcha').src = 'bestphpcms/open_src/securimage/securimage_show.php?sid=' + Math.random();
		},
		
		onFailure: function(){
			alert('ajax error');
		}
		
	});

}							


function contact_us_ajax(pathOffset){
	$('frmContactSubmit').disabled=true;
	$('msgAck').update('please wait...');
	$("msgAckWrapper").style.display="block";
	setTimeout('$("msgAckWrapper").style.display="none";',uiWarningTimeout);
	setTimeout('$("frmContactSubmit").disabled=false;',uiReSubmitTimeout);

	var serP=$('frmContact').serialize();

	new Ajax.Request(pathOffset + "phpcontactinc/contact_ajax_pr.php", {
		
		method:'post',
		
		parameters: serP,

		onComplete: function(transport){
			var response = transport.responseText;
			//check for logon - we'll close the lightbox and redirect the parent page to intended destination
			if(response.search(/tooManyMessagesSent:/) != -1){
				var warning = response.replace('tooManyMessagesSent:', '');
				$('msgAck').update(warning);
				$('hideUponMessageSent').style.display="none";
				//DEBUG!!! disable the submit button
			}
			else if(response.search(/secIssue:/) != -1){
				var warning = response.replace('secIssue:', '');
				$('msgAck').update(warning);
			}
			//otherwise
			else{
				$('msgAck').update(response);
				$('hideUponMessageSent').style.display="none";
			}
		},
		
		onFailure: function(){
			alert('ajax error');
		}
		
	});

}							


function new_pwd_ajax(pathOffset){
	$('faqForgotPwSubmit').disabled=true;
	$('forgotPwdAck').update('please wait...');
	$("forgotPwdAckWrapper").style.display="block";
	setTimeout('$("forgotPwdAckWrapper").style.display="none";',uiWarningTimeout);
	setTimeout('$("faqForgotPwSubmit").disabled=false;',uiReSubmitTimeout);

	var serP=$('frmForgotPw').serialize();

	new Ajax.Request(pathOffset + "phplogininc/newPwd_ajax_pr.php", {
		
		method:'post',
		
		parameters: serP,

		onComplete: function(transport){
			var response = transport.responseText;
			//check for logon - we'll close the lightbox and redirect the parent page to intended destination
			if(response.search(/newpwdsent:/) != -1){
				var confirmation = response.replace('newpwdsent:', '');
				$('msgAck').update(confirmation);
				$('faqForgotPwSubmit').style.visibility='hidden';
			}
			//otherwise
			else{
				$('forgotPwdAck').update(response);
			}
		},
		
		onFailure: function(){
			alert('ajax error');
		}
		
	});

}							


function setFocus(){
	document.forms[0].elements[0].focus();
	document.forms[0].elements[0].select();
	}

function setFocusById(targetId){
	$(targetId).focus();
	$(targetId).select();
	}

function getWindowHeight() { 
var windowHeight = 0; 
if (typeof(window.innerHeight) == 'number') { 
windowHeight = window.innerHeight; 
} 
else { 
if (document.documentElement && document.documentElement.clientHeight) { 
windowHeight = document.documentElement.clientHeight; 
} 
else { 
if (document.body && document.body.clientHeight) { 
windowHeight = document.body.clientHeight; 
} 
} 
} 
return windowHeight; 
} 

function getWindowWidth() { 
var windowWidth = 0; 
if (typeof(window.innerWidth) == 'number') { 
windowWidth = window.innerWidth; 
} 
else { 
if (document.documentElement && document.documentElement.clientWidth) { 
windowWidth = document.documentElement.clientWidth; 
} 
else { 
if (document.body && document.body.clientWidth) { 
windowWidth = document.body.clientWidth; 
} 
} 
} 
return windowWidth; 
} 

function CloseAllSecDivs(){
	var secdiv_arry = document.getElementsByClassName('secdiv');
	var secimg_arry = document.getElementsByClassName('secimg');
	for(i=0;i<secdiv_arry.length;i++){
		secdiv_arry[i].style.display = "none";
	}

	for(i=0;i<secimg_arry.length;i++){
		secimg_arry[i].src = "assets/arwr.png";
	}
}

function SetWidth(pixels){
	$('wrapper').style.width = pixels + 'px';
	$('content').style.width = pixels + 'px';
	$('owners_r1c1').style.width = pixels + 'px';
	$('owners_r2c1').style.width = (pixels-5) + 'px';
}

/*** the following are connected with ajax based location input ***/

// adds options to the country list box 
function ajax_addOptions(method,targetId,p1){

	new Ajax.Request("ajaxinc/cust_members_prototype_ajax_pr.php", {
		
		method:'get',
		
		parameters:'method=' + method + '&newValue1=' + p1,
		
		onSuccess: function(transport){
			var options_arry = transport.responseText.evalJSON();
			//alert(options_arry.inspect());
			$(targetId).length=1;

			//add the first element - dep on  region
			if(p1=='USA'){
			$(targetId).options[0]=new Option('--Select a State--',null, false, false);
			$(targetId).style.display='block';
			$('_worldRegion').value='North America';
			$('_country').value='USA';
			}
			else if(p1=='Canada'){
			$(targetId).style.display='none';
			$('_worldRegion').value='North America';
			onChangeCountry('Canada');
			}
			else{
			$(targetId).options[0]=new Option('--Select a Country--',null, false, false);
			$(targetId).style.display='block';
			$('_country').value='';
			}
			
			//add the options			
			for(var i=0;i<options_arry.length;i++){
				$(targetId).options[i+1]=new Option(options_arry[i],options_arry[i], false, false);
			}//for

			//enable the control
			$(targetId).disabled=false;
		},
		
		onFailure: function(){
			alert('ajax error');
		}
		
	});

}//fn ajax_addOptions()			


// adds Top Locations 
function ajax_addTopLocations(country,region){

	new Ajax.Request("ajaxinc/cust_members_prototype_ajax_pr.php", {
		
		method:'get',
		
		parameters:'method=addTopLocations' + '&newValue1=' + country + '&newValue2=' + region,
		
		onSuccess: function(transport){
			//alert(transport.responseText);					
			$('keyboard').update(transport.responseText);
			ajax_getCities('');
		},
		
		onFailure: function(){
			alert('ajax error');
		}
		
	});

}//fn ajax_addOptions()		


// adds Top Locations 
function ajax_getCities(firstLetters){
	$('ldinfoimgGeo').style.visibility='visible';

	new Ajax.Request("ajaxinc/cust_members_prototype_ajax_pr.php", {
		
		method:'get',
		
		parameters:'method=getCities' + '&newValue1=' + firstLetters,
		
		onSuccess: function(transport){
			//alert(transport.responseText);					
			$('keyboard').update(transport.responseText);		
			$('ldinfoimgGeo').style.visibility='hidden';
		},
		
		onFailure: function(){
			alert('ajax error');
		}
		
	});

}//fn ajax_addOptions()		


// adds gets GPS coords, adds marker and centers map 
function ajax_getCoords(p1,p2){

	new Ajax.Request("ajaxinc/cust_members_prototype_ajax_pr.php", {
		
		method:'get',
		
		parameters:'method=getCoords' + '&newValue1=' + p1 + '&newValue2=' + p2,
		
		onSuccess: function(transport){
			//alert(transport.responseText);					
			$('keyboard').update(transport.responseText);		
		},
		
		onFailure: function(){
			alert('ajax error');
		}
		
	});

}//fn ajax_getCoords()		


// adds gets GPS coords, adds marker and centers map 
function ajax_getCoordsFromAddress(p1,p2,p3,p4){

	//remove airport code suffix (if any) from city name
	var city=p2;
	if(p2.indexOf(':')>0)city=p2.substring(0,p2.indexOf(':'));

	//if country is USA then include zip
	if($('_country').value!='USA')p3='';

	new Ajax.Request("ajaxinc/cust_members_prototype_ajax_pr.php", {
		
		method:'get',
		
		parameters:'method=getCoordsFromAddress' + '&newValue1=' + p1 + '&newValue2=' + city + '&newValue3=' + p3 + '&newValue4=' + p4,
		
		onSuccess: function(transport){
			//alert(transport.responseText);					
			$('keyboard').update(transport.responseText);		
		},
		
		onFailure: function(){
			alert('ajax error');
		}
		
	});

}//fn ajax_getCoords()		


function onChangeCountry(country){
	resetAddress();
	$('_city_acomplete').value='';
	if($('_country').value=='USA'){
		$('_state').value=$('_countrySelect').value.substring($('_countrySelect').value.indexOf('(')+1,$('_countrySelect').value.indexOf(')'));
		$('_state').disabled=true;
	}
	else{
		$('_country').value=country;
	}
	ajax_addTopLocations(country,'NULL');
	$('_topAirports').style.display='block';
	$('keyboard').style.display ='block';
	$('gmapBackdrop').style.visibility='visible';
}//onChangeCountry


// resets the input form for entering a new location 
function resetLocation() {
	$('_city_acomplete').value='';
	$('city').value='';
	$('_region').selectedIndex=0;
	$('_countrySelect').selectedIndex=0;
	$('_countrySelect').disabled=true;
	$('_topAirports').selectedIndex=0;
	$('_topAirports').disabled=true;
	$('gmapBackdrop').style.visibility='hidden';
	$('keyboard').style.display='none';
	$('_topAirports').style.display='none';
	$('setMapMarkerAtAddress').style.visibility='hidden';
	$('_worldRegion').value='';
	$('_country').value='';
	resetAddress();
}//fn resetLocation

function resetAddress(){
	$('_lt').value='';
	$('_lg').value='';
	$('_street').value='';
	$('_state').value='';
	$('_state').disabled=false;
	$('_zip').value='';
	$('street').value='';
	$('state').value='';
	$('zip').value='';
	$('full_address').value=0;
}
	
function copyGeoFields(){
	$('worldRegion').value= $('_worldRegion').value;
	$('country').value= $('_country').value;
	$('city').value= $('_city_acomplete').value;
	$('street').value= $('_street').value;
	$('state').value= $('_state').value;
	$('zip').value= $('_zip').value;
	$('lt').value= $('_lt').value;
	$('lg').value= $('_lg').value;			
	$('lt_f').value= $('_lt').value;
	$('lg_f').value= $('_lg').value;			
}

/*
check if listing qualifies for promo
*/
function checkPromoQualification(p1,p2,p3,p4,p5){

	new Ajax.Request("ajaxinc/cust_members_prototype_ajax_pr.php", {
		
		method:'get',
		
		parameters:'method=doesListingQualifyForOffer' + '&newValue1=' + p1 + '&newValue2=' + p2 + '&newValue3=' + p3 + '&newValue4=' + p4 + '&newValue5=' + p5,
		
		onSuccess: function(transport){
			alert(transport.responseText);					
		},
		
		onFailure: function(){
			alert('ajax error');
		}
		
	});

}