function popupClose() {
    if (document.getElementById("loginpopup") != null) {
        document.getElementById("loginpopup").style.display = "none";
    }
    if (document.getElementById("registerpopup") != null) {
        document.getElementById("registerpopup").style.display = "none";
    }
	if (document.getElementById("schedulepopup") != null) {
	    document.getElementById("schedulepopup").style.display = "none";
	}
	if (document.getElementById("moreinfopopup") != null) {
	    document.getElementById("moreinfopopup").style.display = "none";
	}
	if (document.getElementById("searchespopup") != null) {
	    document.getElementById("searchespopup").style.display = "none";
	}
	if (document.getElementById("savesearchpopup") != null) {
	    document.getElementById("savesearchpopup").style.display = "none";
	}
	return false;
}

function showLoginPopup()
{
    if (document.getElementById("loginpopup") != null) {
        document.getElementById("loginpopup").style.display = "block";
    }
    if (document.getElementById("registerpopup") != null) {
        document.getElementById("registerpopup").style.display = "none";
    }
	if (document.getElementById("schedulepopup") != null) {
	    document.getElementById("schedulepopup").style.display = "none";
	}
	if (document.getElementById("moreinfopopup") != null) {
	    document.getElementById("moreinfopopup").style.display = "none";
	}
	if (document.getElementById("searchespopup") != null) {
	    document.getElementById("searchespopup").style.display = "none";
	}
	if (document.getElementById("savesearchpopup") != null) {
	    document.getElementById("savesearchpopup").style.display = "none";
	}
}

function showRegisterPopup()
{
    if (document.getElementById("loginpopup") != null) {
        document.getElementById("loginpopup").style.display = "none";
    }
    if (document.getElementById("registerpopup") != null) {
        document.getElementById("registerpopup").style.display = "block";
    }
	if (document.getElementById("schedulepopup") != null) {
	    document.getElementById("schedulepopup").style.display = "none";
	}
	if (document.getElementById("moreinfopopup") != null) {
	    document.getElementById("moreinfopopup").style.display = "none";
	}
	if (document.getElementById("searchespopup") != null) {
	    document.getElementById("searchespopup").style.display = "none";
	}
	if (document.getElementById("savesearchpopup") != null) {
	    document.getElementById("savesearchpopup").style.display = "none";
	}
}

function showSchedulePopup(address)
{
    if (document.getElementById("loginpopup") != null) {
        document.getElementById("loginpopup").style.display = "none";
    }
    if (document.getElementById("registerpopup") != null) {
        document.getElementById("registerpopup").style.display = "none";
    }
	if (document.getElementById("schedulepopup") != null) {
	    document.getElementById("schedulepopup").style.display = "block";
	    document.getElementById("popup_schedule_propertyBox").value = document.getElementById("popup_schedule_propertyBox").value + address;
	}
	if (document.getElementById("moreinfopopup") != null) {
	    document.getElementById("moreinfopopup").style.display = "none";
	}
	if (document.getElementById("searchespopup") != null) {
	    document.getElementById("searchespopup").style.display = "none";
	}
	if (document.getElementById("savesearchpopup") != null) {
	    document.getElementById("savesearchpopup").style.display = "none";
	}
}

function showMoreInfoPopup(ar_infoType, subs_infoType)
{
    if (document.getElementById("loginpopup") != null) {
        document.getElementById("loginpopup").style.display = "none";
    }
    if (document.getElementById("registerpopup") != null) {
        document.getElementById("registerpopup").style.display = "none";
    }
	if (document.getElementById("schedulepopup") != null) {
	    document.getElementById("schedulepopup").style.display = "none";
	}
	if (document.getElementById("moreinfopopup") != null) {
	    document.getElementById("moreinfopopup").style.display = "block";
	}
	if (document.getElementById("searchespopup") != null) {
	    document.getElementById("searchespopup").style.display = "none";
	}
	if (document.getElementById("savesearchpopup") != null) {
	    document.getElementById("savesearchpopup").style.display = "none";
	}
	if (document.getElementById("MoreInfoMain_pnlDetails") != null) {
	    $find('MoreInfoMain_ModalPopupExtenderMoreInfo').show();
	}
}

function closeMoreInfoPopup() {
    $find('MoreInfoMain_ModalPopupExtenderMoreInfo').hide();	
}

function showSearchesPopup() {
    if (document.getElementById("loginpopup") != null) {
        document.getElementById("loginpopup").style.display = "none";
    }
    if (document.getElementById("registerpopup") != null) {
        document.getElementById("registerpopup").style.display = "none";
    }
    if (document.getElementById("schedulepopup") != null) {
        document.getElementById("schedulepopup").style.display = "none";
    }
    if (document.getElementById("moreinfopopup") != null) {
        document.getElementById("moreinfopopup").style.display = "none";
    }
    if (document.getElementById("searchespopup") != null) {
        document.getElementById("searchespopup").style.display = "none";
    }
    if (document.getElementById("searchespopup") != null) {
        document.getElementById("searchespopup").style.display = "block";
    }
    if (document.getElementById("savesearchpopup") != null) {
        document.getElementById("savesearchpopup").style.display = "none";
    }
}

function showSaveSearchPopup() {
    if (document.getElementById("loginpopup") != null) {
        document.getElementById("loginpopup").style.display = "none";
    }
    if (document.getElementById("registerpopup") != null) {
        document.getElementById("registerpopup").style.display = "none";
    }
    if (document.getElementById("schedulepopup") != null) {
        document.getElementById("schedulepopup").style.display = "none";
    }
    if (document.getElementById("moreinfopopup") != null) {
        document.getElementById("moreinfopopup").style.display = "none";
    }
    if (document.getElementById("searchespopup") != null) {
        document.getElementById("searchespopup").style.display = "none";
    }
    if (document.getElementById("searchespopup") != null) {
        document.getElementById("searchespopup").style.display = "none";
    }
    if (document.getElementById("savesearchpopup") != null) {
        document.getElementById("savesearchpopup").style.display = "block";
    }
}

function showEmailAgentPopup(ar_infoType)
{
    if (document.getElementById("loginpopup") != null) {
        document.getElementById("loginpopup").style.display = "none";
    }
    if (document.getElementById("registerpopup") != null) {
        document.getElementById("registerpopup").style.display = "none";
    }
	if (document.getElementById("schedulepopup") != null) {
	    document.getElementById("schedulepopup").style.display = "none";
	}
	if (document.getElementById("moreinfopopup") != null) {
	    document.getElementById("moreinfopopup").style.display = "none";
	}
	if (document.getElementById("searchespopup") != null) {
	    document.getElementById("searchespopup").style.display = "none";
	}
	if (document.getElementById("savesearchpopup") != null) {
	    document.getElementById("savesearchpopup").style.display = "none";
	}
	if (document.getElementById("emailagentpopup") != null) {
        document.getElementById("emailagentpopup").style.display = "block";
    }
}

function closePopups(){
	var popups = Array('loginpopup', 'registerpopup', 'schedulepopup', 'moreinfopopup', 'searchespopup', 'savesearchpopup', 'emailagentpopup');
	for(var i = 0; i < popups.length; i++){
		if (document.getElementById(popups[i]) != null) {
	        document.getElementById(popups[i]).style.display = 'none';
	    }
	}
	
}

function validateNotEmpty(ar_str) {
    var str_check = ar_str;
    str_check = str_check.replace(/^\s+|(\s+(?!\S))/mg, "");
    if (str_check.length > 0) return true; return false;
}

function validateIsEmail(ar_email) {
    var re_email = /^[a-zA-Z]+([_\.-]?[a-zA-Z0-9]+)*@[a-zA-Z0-9]+([\.-]?[a-zA-Z0-9]+)*(\.[a-zA-Z]{2,4})+$/;
    if (ar_email.search(re_email) == -1) return false; else return true;
}

function validationFailed(ar_errorBoxId, ar_errorMsg, ar_focusElement) {
    setErrorMessage(ar_errorBoxId, ar_errorMsg);
    if(ar_focusElement != null)
    	ar_focusElement.focus();

    return false;
}

function setErrorMessage(ar_id, ar_message) {
    document.getElementById(ar_id).innerHTML = ar_message;
}

function validateRegisterPopup(ar_errorBoxId){
	var name = document.getElementById('popup_register_nameBox');
	if(!validateNotEmpty(name.value)){
		return validationFailed(ar_errorBoxId, 'Please enter your name', name);
	}
	
	var email = document.getElementById('popup_register_emailBox');
	if(!validateNotEmpty(email.value)){
	    return validationFailed(ar_errorBoxId, 'Please enter your email', email);
	}
	else if(!validateIsEmail(email.value)){
		email.value = '';
		return validationFailed(ar_errorBoxId, 'Please enter correct email address', email);
	}
	
	var password = document.getElementById('popup_register_passwordBox');
	if(!validateNotEmpty(password.value)){
		return validationFailed(ar_errorBoxId, 'Please enter a password', password);
	}
	else if(password.value.length < 4){
		password.value = '';
		return validationFailed(ar_errorBoxId, 'The password length have to be at least 4 symbols', password);
	}
	
	var passwordConfirm = document.getElementById('popup_register_passwordConfirmBox');
	if(!validateNotEmpty(passwordConfirm.value)){
		return validationFailed(ar_errorBoxId, 'Please retype your password', passwordConfirm);
	}
	else if(password.value != passwordConfirm.value){
		passwordConfirm.value = '';
		return validationFailed(ar_errorBoxId, 'Enter the same password', passwordConfirm);
	}
	
	return true;
}

function validateLoginPopup(ar_errorBoxId) {
	var username = document.getElementById('popup_login_emailBox');
	if(!validateNotEmpty(username.value)){
		return validationFailed(ar_errorBoxId, 'Please enter an username', username);
	}
	else if(!validateIsEmail(username.value)){
		username.value = '';
		return validationFailed(ar_errorBoxId, 'Please enter for username the email you registered with', username);
	}

	var password = document.getElementById('popup_login_passwordBox');
	if(!validateNotEmpty(password.value)){
		return validationFailed(ar_errorBoxId, 'Please enter a password', password);
	}
	
	return true;
}

function validateSchedulePopup(ar_errorBoxId) {
    var name = document.getElementById('popup_schedule_nameBox');
    if (!validateNotEmpty(name.value)) {
        return validationFailed(ar_errorBoxId, 'Please enter your name', name);
    }

    var email = document.getElementById('popup_schedule_emailBox');
    if (!validateNotEmpty(email.value)) {
        return validationFailed(ar_errorBoxId, 'Please enter your email', email);
    }
    else if (!validateIsEmail(email.value)) {
        email.value = '';
        return validationFailed(ar_errorBoxId, 'Please enter correct email address', email);
    }

    var msg = document.getElementById('popup_schedule_propertyBox');
    if (!validateNotEmpty(msg.value)) {
        return validationFailed(ar_errorBoxId, 'Please enter a property for scheduling', msg);
    }

    return true;
}

function validateMoreInfoPopup(ar_errorBoxId) {
    var name = document.getElementById('popup_moreinfo_nameBox');
    
    if (!validateNotEmpty(name.value)) {
        return validationFailed(ar_errorBoxId, 'Please enter your name', name);
    }
    
    var email = document.getElementById('popup_moreinfo_emailBox');
    if (!validateNotEmpty(email.value)) {
        return validationFailed(ar_errorBoxId, 'Please enter your email', email);
    }
    else if (!validateIsEmail(email.value)) {
        email.value = '';
        return validationFailed(ar_errorBoxId, 'Please enter correct email address', email);
    }
    
    if(document.getElementById('popup_moreinfo_askquestion').value != 'true' &&
    	document.getElementById('popup_moreinfo_seehome').value != 'true' &&
    	document.getElementById('popup_moreinfo_preapproving').value != 'true' &&
    	document.getElementById('popup_moreinfo_startoffer').value != 'true')
    	return validationFailed(ar_errorBoxId, 'Please enter a reason for Inquiry.', null);
    		
    
    var msg = document.getElementById('popup_moreinfo_message');
    if (!validateNotEmpty(msg.value)) {
        return validationFailed(ar_errorBoxId, 'Please enter a property for scheduling', msg);
    }
    
    var checkIn = document.getElementById('popup_moreinfo_checkFrom');
    if (!validateNotEmpty(checkIn.value)) {
        return validationFailed(ar_errorBoxId, 'Please enter Check In date', checkIn);
    }
    
    var checkOut = document.getElementById('popup_moreinfo_checkTo');
    if (!validateNotEmpty(checkOut.value)) {
        return validationFailed(ar_errorBoxId, 'Please enter Check Out date', checkOut);
    }

    document.getElementById(ar_errorBoxId).value = '';

    return true;
}

function validateEmailAgentPopup(ar_errorBoxId) {
    var name = document.getElementById('popup_emailAgent_nameBox');
    
    if (!validateNotEmpty(name.value)) {
        return validationFailed(ar_errorBoxId, 'Please enter your name', name);
    }
    
    var email = document.getElementById('popup_emailAgent_emailBox');
    if (!validateNotEmpty(email.value)) {
        return validationFailed(ar_errorBoxId, 'Please enter your email', email);
    }
    else if (!validateIsEmail(email.value)) {
        email.value = '';
        return validationFailed(ar_errorBoxId, 'Please enter correct email address', email);
    }
       
    var msg = document.getElementById('popup_emailAgent_message');
    if (!validateNotEmpty(msg.value)) {
        return validationFailed(ar_errorBoxId, 'Please enter a property for scheduling', msg);
    }

    document.getElementById(ar_errorBoxId).value = '';

    return true;
}

// SaveSearch Popup
function validateSaveSearchPopup(ar_errorBoxId) {
    var name = document.getElementById('popup_savesearch_nameBox');
    if (!validateNotEmpty(name.value)) {
        return validationFailed(ar_errorBoxId, 'Your Search Name cannot be empty.', name);
    }

    return true;
}

//Positioning for elements of calendar.
 function showCelendarUnderEl(element ,testelem)
 {
   var pos = $(element).cumulativeOffset();
  ofsL = pos[0] +"px";
  ofsT = (pos[1] + 25 ) +"px";
  var domelements = $$('div.'+ testelem);
  domelements[0].setStyle({left: ofsL , top: ofsT  });

 }
 //The next function works only with js calendar. It uses his API
 function setMinimalCalendarDate(checkIn)
 {
	 var datesIn = $(checkIn).value;
	 $('popup_moreinfo_checkTo').value = datesIn;
	 return datesIn;
	 
 }


