function popupTickSelection(ar_element, ar_subject){
	if(ar_element.className == 'checked'){
		ar_element.className = '';
		document.getElementById(ar_subject).value = 'false';
	}
	else if(ar_element.className == ''){
		ar_element.className = 'checked';
		document.getElementById(ar_subject).value = 'true';
	}
}

function popupClose() {
	var popups = Array('loginpopup', 'registerpopup', 'schedulepopup', 'moreinfopopup', 'searchespopup', 'savesearchpopup', 'emailagentpopup', 'forgotpassword', 'changepassword');
	for (var i = 0; i < popups.length; i++) {
	    if (document.getElementById(popups[i]) != null) {
	        document.getElementById(popups[i]).style.display = 'none';
	    }
	}
}

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";
	}
	if (document.getElementById("confrimloadsearch") != null) {
	    document.getElementById("confrimloadsearch").style.display = "none";
	}
	if (document.getElementById("forgotpassword") != null) {
	    document.getElementById("forgotpassword").style.display = "none";
	}
	if (document.getElementById("changepassword") != null) {
	    document.getElementById("changepassword").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";
	}
	if (document.getElementById("confrimloadsearch") != null) {
	    document.getElementById("confrimloadsearch").style.display = "none";
	}
	if (document.getElementById("forgotpassword") != null) {
	    document.getElementById("forgotpassword").style.display = "none";
	}
	if (document.getElementById("changepassword") != null) {
	    document.getElementById("changepassword").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("confrimloadsearch") != null) {
	    document.getElementById("confrimloadsearch").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("forgotpassword") != null) {
	    document.getElementById("forgotpassword").style.display = "none";
	}
	if (document.getElementById("changepassword") != null) {
	    document.getElementById("changepassword").style.display = "none";
	}
}

function showMoreInfoPopup(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 = "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("confrimloadsearch") != null) {
	    document.getElementById("confrimloadsearch").style.display = "none";
	}
	if (document.getElementById("MoreInfoMain_pnlDetails") != null) {
	    $find('MoreInfoMain_ModalPopupExtenderMoreInfo').show();
	}
	if (document.getElementById("forgotpassword") != null) {
	    document.getElementById("forgotpassword").style.display = "none";
	}
	if (document.getElementById("changepassword") != null) {
	    document.getElementById("changepassword").style.display = "none";
	}
	
	var ticks = Array('popup_moreinfo_askquestion', 'popup_moreinfo_seehome', 'popup_moreinfo_preapproving');
	for(var i = 0; i < ticks.length; i++){
		if(document.getElementById(ticks[i]) != null){
			document.getElementById(ticks[i] + 'Tick').className = '';
			document.getElementById(ticks[i]).value = 'false';
		}
	}
	
	if(ar_infoType != null){
		popupTickSelection(document.getElementById(ar_infoType + 'Tick'), ar_infoType);
	}
}

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("confrimloadsearch") != null) {
        document.getElementById("confrimloadsearch").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";
    }
    if (document.getElementById("forgotpassword") != null) {
        document.getElementById("forgotpassword").style.display = "none";
    }
    if (document.getElementById("changepassword") != null) {
        document.getElementById("changepassword").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("confrimloadsearch") != null) {
        document.getElementById("confrimloadsearch").style.display = "none";
    }
    if (document.getElementById("savesearchpopup") != null) {
        document.getElementById("savesearchpopup").style.display = "block";
    }
    if (document.getElementById("forgotpassword") != null) {
        document.getElementById("forgotpassword").style.display = "none";
    }
    if (document.getElementById("changepassword") != null) {
        document.getElementById("changepassword").style.display = "none";
    }
}

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("confrimloadsearch") != null) {
	    document.getElementById("confrimloadsearch").style.display = "none";
	}
	if (document.getElementById("emailagentpopup") != null) {
        document.getElementById("emailagentpopup").style.display = "block";
    }
    if (document.getElementById("forgotpassword") != null) {
        document.getElementById("forgotpassword").style.display = "none";
    }
    if (document.getElementById("changepassword") != null) {
        document.getElementById("changepassword").style.display = "none";
    }
}

function showConfirmLoadSearch(elementID) {
    closePopups();
    document.getElementById('confrimloadsearch').style.display = "block";
    document.getElementById('ConfirmLoadSearchMain_hdnRequestedSearchId').value = elementID;
}

function showForgotPasswordPopup() {
    closePopups();
    if(document.getElementById("forgotpassword") != null){
    	document.getElementById('forgotpassword').style.display = "block";
    }
}

function showChangePasswordPopup() {
    closePopups();
    if(document.getElementById('changepassword') != null){
    	document.getElementById('changepassword').style.display = "block";
    }
}

function showChangePasswordPopupOnSuccess() {
    closePopups();
    document.getElementById('changepassword').style.display = "block";
	document.getElementById('popup_changepassword_success').style.display = 'block';
}

function closePopups(){
    var popups = Array('loginpopup', 'registerpopup', 'schedulepopup', 'moreinfopopup', 'searchespopup', 'savesearchpopup', 'emailagentpopup', 'forgotpassword');
	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);
    }

    var agentConnection = document.getElementById('popup_register_agentList');
    var agentConnectionYes = document.getElementById('agent_conection_yes');
    var agentListSelector = document.getElementById('RegisterMain_ddlAgents');
   
    if (agentConnectionYes.checked == true) {
    	if (agentListSelector.value == "-1") {
            return validationFailed(ar_errorBoxId, 'Please select an agent ', agentConnection);
        }
    }
	
	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);
    }

    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;
}

// Change Password Popup
function validateChangePassPopup(ar_errorBoxId) {
    var password = document.getElementById('popup_changePassword');
    var confPassword = document.getElementById('popup_confChangePassword');
    if (!validateNotEmpty(password.value)) {
        return validationFailed(ar_errorBoxId, 'Your Password cannot be empty.', password);
    }
    else if(password.value.length < 4){
		password.value = '';
		return validationFailed(ar_errorBoxId, 'The password length have to be at least 4 symbols', password);
	}
    
    if (password.value != confPassword.value) {
        return validationFailed(ar_errorBoxId, 'Your Passwords do not match.', confPassword);
    }

    return true;
}

//validate forgot password
function validateForgotPassword(ar_errorBoxIdEmp, ar_errorBoxIdWrong){
	var forgotEmail =  document.getElementById('popup-your-email-adr');
	 document.getElementById(ar_errorBoxIdEmp).innerHTML = '';
	 document.getElementById(ar_errorBoxIdWrong).innerHTML = '';
	
	if (!validateNotEmpty(forgotEmail.value)) {
        return validationFailed(ar_errorBoxIdEmp, '!', forgotEmail);
    }
	else if(!validateIsEmail(forgotEmail.value)){
		forgotEmail.value = '';
		
		return validationFailed(ar_errorBoxIdWrong, 'Wrong Email Format!', forgotEmail);
	}
	else {

		 return true;
	}
}

