/**
 * Javascript for window pop-ups (lightbox like)
 * 
 * @author Aleksey Korzun <al.ko@webfoundation.net>
 */

function controlPopUp(divId) {
		$(divId).style.display = 'none';
		$('popUpOverlay').hide();	
}		

function popUpNew(divId, listingId, fileName, divWidth, divHeight, divReload) {

	var popUp = document.getElementById("popUp"+divId);
	
	popUp.innerHTML = '<img style="margin:100px auto;" src="img/loader.gif" alt="Loading...">';
	
	if(popUp.style.display == 'none' || divReload == '1') {
		
		objPopUpOverlay.onclick 	= 	function () {popUpNew(divId); return false;}
		popUp.style.position 	= 	'absolute';
		popUp.style.zIndex 		= 	'100';	
		if(divWidth) {
			popUp.style.width	= 	divWidth+'px';
		} else {
			popUp.style.width	= 	'400px';			
		}

		objPopUpBody.insertBefore(popUp, objPopUpOverlay.nextSibling);
	
		new Ajax.Updater(
			popUp, 
			'/new_buy/include/popups/'+fileName+'.php?divId=popUp'+divId+'&listingId='+listingId,
			{
				onComplete:function(){ 
				
					popUp.style.display = 	'block'; 
				
					if(divHeight) {
						var popUpHeight 	= 	parseInt(divHeight);
					} else {
						var popUpHeight 	= 	popUp.offsetHeight;			
					}

					var popUpWidth 		= 	popUp.offsetWidth;						
					var arrayPageSize 	= 	getPageSize();
					var arrayPageScroll = 	getPageScroll();
							
					objPopUpOverlay.style.display 	= 'block';
					objPopUpOverlay.style.height 	= (arrayPageSize[1] + 'px');
					
					var popUpTop 		= 	arrayPageScroll[1] + ((arrayPageSize[3] - 35 - popUpHeight) / 2);
					var popUpLeft 		= 	((arrayPageSize[0] - 20 - popUpWidth) / 2);
						
					popUp.style.top 	= 	(popUpTop < 0) ? "0px" : popUpTop + "px";
					popUp.style.left 	= 	(popUpLeft < 0) ? "0px" : popUpLeft + "px";
					
				;},	
				asynchronous: 	true,
				evalScripts:	true
			} 
		);
	} else {
		popUp.style.display = 'none';
		$('popUpOverlay').hide();	
	}
}




function submitContactUs(form, buildingId) {
	
	var checkval;
	var building_id = buildingId;
	
	checkval = true;

	if(checkval) {
		if(form.subject.value.length == 0) {
			alert("Please enter your subject.");
			checkval= false;
		}
	}
	
	if(checkval) {
		if(form.msg.value.length == 0) {
			alert("Please enter your message.");
			checkval= false;
		}
	}

	if(checkval) {
		
		document.getElementById("submitContactUs").innerHTML = '<div style="text-align:center;"><img style="margin:100px auto;" src="img/loader.gif" alt="Loading..."></div>';
		
		new Ajax.Updater(
			document.getElementById("submitContactUs"), 
			'/new_buy/include/popups/submit/processForm.php?type=contact&buildingId='+buildingId,
			{
				parameters:Form.serialize(form),
				asynchronous: true,
				evalScripts:true
			}
		)
	}	
}

function submitScheduleAppointment(form, listingId) {
	var checkval;
	var size_phone = 32;
	var listingId = listingId;
	checkval = true;
	
	if(checkval) {
		checkval = (checkphonefunction(form.day_phone.value)) ? true : false;
		if (!checkval) alert("Your daytime phone number is invalid.  Please re-enter.");
	}
	if(checkval) {
		checkval = (checkphonefunction(form.cell_phone.value)) ? true : false;
		if (!checkval) alert("Your cell phone number is invalid.  Please re-enter.");
	}
	if(checkval) {
		checkval = (checkphonefunction(form.evening_phone.value)) ? true : false;
		if (!checkval) alert("Your home phone number is invalid.  Please re-enter.");
	}
	if(checkval) {
		if(form.day_phone.value.length > size_phone)        {
			alert("Length of Day Phone Number Exceeds Limits.  Please Re-Enter.");
			checkval= false;
		}
	}
	if(checkval) {
		if(form.cell_phone.value.length > size_phone)        {
			alert("Length of Cell Phone Number Exceeds Limits.  Please Re-Enter.");
			checkval= false;
		}
	}
	if(checkval) {
		if(form.evening_phone.value.length > size_phone)        {
			alert("Length of Evening Phone Number Exceeds Limits.  Please Re-Enter.");
			checkval= false;
		}
	}
	
	if(checkval) {			
	
		//I'm not proud of this but this works
		var day_phone= escape(form.day_phone.value);
		var cell_phone = escape(form.cell_phone.value);
		var evening_phone = escape(form.evening_phone.value);	
									
		var alternative = escape(form.alternative.value);
		
		document.getElementById("submitSheduleAppointment").innerHTML = '<div style="text-align:center;"><img style="margin:100px auto;" src="img/loader.gif" alt="Loading..."></div>';
	
		new Ajax.Updater(
			document.getElementById("submitSheduleAppointment"), 
			'/new_buy/include/popups/submit/processForm.php?type=appointment&listingId='+listingId,
			{
				parameters:Form.serialize(form),
				asynchronous: true,
				evalScripts:true
			}
		)
	}
}