/*
* This file contains all the functions related to the moodal boxes popup
* Paulino Calderon <paulino@calder0n.com>
*/

var returnURL='';//will store the return url

//checks if the cookie already exists
function checkCookie() {
    if(document.cookie.indexOf('name')!='-1') {
        return true;
    } else {
        return false;
    }
}
//sets the cookie
function SetCookie(cookieName,cookieValue,nDays) {
 var today = new Date();
 var expire = new Date();
 if (nDays==null || nDays==0) nDays=1;
 expire.setTime(today.getTime() + 3600000*24*nDays);
 document.cookie = cookieName+"="+escape(cookieValue)
                 + ";expires="+expire.toGMTString();
}

//main function that handles the cookie check and launches the moodalbox
function link(href) {
    if(checkCookie()) {
        setReturnURL(href);
        openURL();
    } else {
        $('utube').toggle(); 
        Modalbox.show($('contactInformation').innerHTML, {title: 'Milwaukee Condo Man Virtual Tour Sign-in', width: 450, height: 540});
        setReturnURL(href);
        
    }
}
//Opens the destination url
function openURL() {
        var extWindow=window.open(returnURL,'','width=990,height=700,location=0,top=0');
}
//Sets the destination url
function setReturnURL(href) {
    returnURL=href;
}
//validates the form and process the request
function validator() {
	if($F('name').length < 5  || $F('email').length < 9 || $F('phone').length < 7) {
		alert("You need to enter your full name, valid email and telephone number. Condo Buyers are invited to use this resource with no fees while shopping for a condo in Milwaukee. You are invited to use this resource under the condition that you consider using Milwaukee Realty Inc in your next Real Estate transaction. If you would rather speak with a broker directly on the phone before you register, call David Price of Milwaukee Realty Inc.: 414-224-7200.");
	} else {

		if ($F('consider')=='y') {
		

		sendContact();
		Modalbox.hide();
		openURL();
		return false;
		} else {
			alert("Condo Buyers are invited to use this resource with no fees while shopping for a condo in Milwaukee. You are invited to use this resource under the condition that you consider using Milwaukee Realty Inc in your next Real Estate transaction. If you would rather speak with a broker directly on the phone before you register, call David Price of Milwaukee Realty Inc.: 414-224-7200.");
		}
	}
}
//Sends the request to the contact form
function sendContact() {
  new Ajax.Request('proxy2.asp',
  {
    method:'get', 
    parameters: $('contactForm').serialize(true), 
    onSuccess: function(transport){
      var response = transport.responseText || "no response text";
      SetCookie("name", response, 1);
    },
    onFailure: function(){ alert('Something went wrong...') }
  });
}

