
function load_contact_modal(property_id, property_name) {
    var modal_width = 465;
    var modal_height = 490;
    createPopUp('contact_modal_div', modal_width, modal_height);
    if ($("#NSPopUP")) $("#NSPopUP").html(window.contact_modal);
    /*$('.send_btn').attr('onclick', window.contact_modal_onclick);*/
    if (property_id != undefined) {
        $('#contact_modal_form .property_form_div').show();
        $('#contact_modal_form .property_id').attr('value', property_id);
        $('#contact_modal_form .property_name').attr('value', property_name);
    }
}

function unload_contact_modal () {
    closePopUp();
}

function submit_contact_modal() {
    
    var success_message = window.contact_modal_success_message;
    var fail_message = window.contact_modal_error_message;

    if (check_fields('contact_modal_form')) {

        var contact_modal_form = document.getElementById('contact_modal_form');

        $.post(window.REL_PHP_CODE_FOLDER + "/run_function.php", {
            function_to_call: "send_contact_modal_form_email",

            first_name: contact_modal_form.first_name.value,
            last_name: contact_modal_form.last_name.value,
            email: contact_modal_form.email.value,
            phone: contact_modal_form.phone.value,
            property_id: contact_modal_form.property_id.value,
            property_name: contact_modal_form.property_name.value,
            find_about_oasis: contact_modal_form.find_about_oasis.value,
            comments: contact_modal_form.comments.value
        }, function(){});
        unload_contact_modal();
        alert (success_message);
    } else alert (fail_message);
}

function check_fields(form_to_check) {
    var form = document.getElementById(form_to_check);
    var email = form.email.value;
    if ( !email_check(email) ) {
        return false;
    } else {
        return true;
    }
}

function email_check(str) {

    var at="@";
    var dot=".";
    var lat=str.indexOf(at);
    var lstr=str.length;

    //no @, @ at the beggining or @ at the end
    if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
        return false;
    }
    //no ., . at the beggining or . at the end
    if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
        return false;
    }
    //just one @
    if (str.indexOf(at,(lat+1))!=-1){
        return false;
    }
    //. just before @ or ????
    if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
        return false;
    }
    //dot after the @
    if (str.indexOf(dot,(lat+2))==-1){
        return false;
    }
    //no spaces
    if (str.indexOf(" ")!=-1){
        return false;
    }
    return true;
}

/* Contact modal plugin */
/* Get document height */
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 createPopUp (id, xwidth, xheight) {
    width = xwidth + "px";
    height = xheight + "px";
    // ensure the given ID exists
    obj = document.getElementById(id);
    if ( obj == null ) return;
    width = width.replace(/\s/g,'')  // remove spaces from width parameter
    height = height.replace(/\s/g,'')  // remove spaces from height parameter
    // Create semi-transparent div to cover the screen
    e = document.createElement("div");
    e.id = "NSPopTransparent";
    e.style.opacity = ".5";
    e.style.MozOpacity = ".5";
    e.style.KhtmlOpacity = ".5";  // older konqueror and safari
    e.style.filter = "alpha(opacity=50)";
    e.style.height = /*"100%"*/getDocHeight() + "px";
    e.style.width = "100%";
    oContainer = document.body.appendChild(e);
    // if the document is longer than the screen height (it scrolls)
    if (document.body.offsetHeight + 24 > oContainer.offsetHeight)
        e.style.height = (document.body.offsetHeight + 24) + "px";

    // create outside div
    e = document.createElement("div");
    e.id = "NSPopUP";
    // center the outside div within the viewable section of the document
    var left;
    var top = (getWindowSize()[1] / 2) - (getNumericPortion(height) );

    if (width.substr(width.length-1) == "%")
        left = (getWindowSize()[0] / 2) + getScrollXY()[0] - (getNumericPortion(width)/100 * getWindowSize()[0] /2 );
    else
        left = (getWindowSize()[0] / 2) + getScrollXY()[0] - (getNumericPortion(width) / 2 );
    if (height.substr(width.length-1) == "%")
        top = (getWindowSize()[1] / 2) + getScrollXY()[1] - (getNumericPortion(height)/100 * getWindowSize()[1] /2 );
    else
        top = (getWindowSize()[1] / 2) + getScrollXY()[1]- (getNumericPortion(height) / 2 );
    if (top < 10) top = 10;

    e.style.left = left + "px";
    e.style.top = top + "px";
    e.style.height = height;
    e.style.width = width;
    oPop=document.body.appendChild(e);

    // create body of popup
    e = document.createElement("div");
    e.id = "NSPopBodyFrame";
    e.style.height = height;//(oPop.offsetHeight - oTitle.offsetHeight - 2) + "px";
    oBody = oPop.appendChild(e);
    e = document.createElement("div");
    e.id="NSPopBodyText";
    e.innerHTML = obj.innerHTML;
    oText= oBody.appendChild(e);

}

function closePopUp(){
    oDiv = document.getElementById("NSPopTransparent");
    document.body.removeChild(oDiv);
    oDiv = document.getElementById("NSPopUP");
    document.body.removeChild(oDiv);
    enableAll(document.body,"input");
    enableAll(document.body,"select");
    enableAll(document.body,"textarea");
}

function disableAll(obj,type) {
    oInput = obj.getElementsByTagName(type);
    for (i=0;i<oInput.length ;i++) {
        oInput[i].disabled = true;
    }
}

function enableAll(obj,type) {
    oInput = obj.getElementsByTagName(type);
    for (i=0;i<oInput.length ;i++) {
        oInput[i].disabled = false;
    }
}

function getWindowSize() {
    var myWidth = 0;
    var myHeight = 0;
    if( typeof( window.innerWidth ) == 'number' ) {
        //Non-IE
        myWidth = window.innerWidth;
        myHeight = window.innerHeight;
    }
    else if(document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
        //IE 6+ in 'standards compliant mode'
        myWidth = document.documentElement.clientWidth;
        myHeight = document.documentElement.clientHeight;
    }
    else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
        //IE 4 compatible
        myWidth = document.body.clientWidth;
        myHeight = document.body.clientHeight;
    }
    return [myWidth, myHeight] ;
}

function getScrollXY() {
    var scrOfX = 0;
    var scrOfY = 0;
    if( typeof( window.pageYOffset ) == 'number' ) {
        //Netscape compliant
        scrOfY = window.pageYOffset;
        scrOfX = window.pageXOffset;
    }
    else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
        //DOM compliant
        scrOfY = document.body.scrollTop;
        scrOfX = document.body.scrollLeft;
    }
    else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
        //IE6 standards compliant mode
        scrOfY = document.documentElement.scrollTop;
        scrOfX = document.documentElement.scrollLeft;
    }
    return [ scrOfX, scrOfY ];
}

function getNumericPortion(str) {
    return str.match(/\d*/);
}
