YAHOO.namespace("contact");

function init() {
	YAHOO.contact.tyPanel = new YAHOO.widget.Panel("thankyouPanel", {fixedcenter:true, 
														   visible:false, 
														   width:"500px",
														   close:true,
														   draggable:false,
														   zindex:4,
														   modal:true,
														   effect:{effect:YAHOO.widget.ContainerEffect.FADE, duration:0.25}});

	YAHOO.contact.tyPanel.setHeader("close"); 
	YAHOO.contact.tyPanel.setBody("<h5>Thank you for contacting Schumacher Properties.</h5><p></p><p>We'll be back in touch with you within one business day. However, if this is an urgent matter, please contact Rebecca at <span class=\"emph\">415.928.3800</span>.</p><p>You will receive one follow-up email from us that thanks you for your inquiry, and invites you to subscribe to our property updates and neighborhood pricing analyses.</p><p>Many thanks again, and we look forward to talking with you.</p>"); 
	YAHOO.contact.tyPanel.setFooter(""); 

	YAHOO.contact.tyPanel.render(document.body);
	
	YAHOO.contact.submitChanges = function() {
		var nameObj = YAHOO.util.Dom.get("ContactusName");
		var phoneObj = YAHOO.util.Dom.get("ContactusPhone")
		var emailObj = YAHOO.util.Dom.get("ContactusEmail");
		var nameErr = YAHOO.util.Dom.get("nameError");
		var emailErr = YAHOO.util.Dom.get("emailError");
		var phoneErr = YAHOO.util.Dom.get("phoneError");
		if(nameObj){
			if ((nameObj.value==null)||(nameObj.value=="")) {
				YAHOO.util.Dom.removeClass(nameErr, "not-displayed");
				nameObj.focus();
				return false;
			} else {
				if(!YAHOO.util.Dom.hasClass(nameErr, "not-displayed")) {
					YAHOO.util.Dom.addClass(nameErr, "not-displayed");
				}
			}
			if (!ValidateForm(emailObj)) {
				if ((phoneObj.value==null) || (phoneObj.value=="")) {
					YAHOO.util.Dom.removeClass(emailErr, "not-displayed");
					YAHOO.util.Dom.removeClass(phoneErr, "not-displayed");
					return false;
				}
			}
			if (!YAHOO.util.Dom.hasClass(emailErr, "not-displayed")) {
				YAHOO.util.Dom.addClass(emailErr, "not-displayed");
			}
			if (!YAHOO.util.Dom.hasClass(phoneErr, "not-displayed")) {
				YAHOO.util.Dom.addClass(phoneErr, "not-displayed");
			}
		} else {
			if(!ValidateForm(emailObj)) {
				YAHOO.util.Dom.removeClass(emailErr, "not-displayed");
				emailObj.focus();
				return false;
			}
			if(!YAHOO.util.Dom.hasClass(emailErr,"not-displayed")) {
				YAHOO.util.Dom.addClass(emailErr,"not-displayed");
			}
		}
		var formObj = YAHOO.util.Dom.get("ContactusRecordForm");
		YAHOO.util.Connect.setForm(formObj);
		var callback =
		{
			success:YAHOO.contact.handleSuccess,
			failure:YAHOO.contact.handleFailure
		}
	
		var cObj = YAHOO.util.Connect.asyncRequest('POST', formObj.action, callback);
	}
	
	YAHOO.contact.handleSuccess = function(o) {
		var mess = o.responseText;
		if (mess.indexOf("Success")>=0) {
			YAHOO.contact.tyPanel.show();
		} else {
			alert(o.responseText);
		}
	}
	
	YAHOO.contact.handleFailure = function(o) {
		alert("Could not send message. -na");
	}
		
	YAHOO.util.Event.addListener("contactSubmit", "click", YAHOO.contact.submitChanges);
	YAHOO.util.Event.addListener("tyHide", "click", YAHOO.contact.tyPanel.hide, YAHOO.contact.tyPanel, true);

}

YAHOO.util.Event.addListener(window,"load", init);

var WindowObjectReference = null; // global variable

function openSPPopup(url)
{
  if(WindowObjectReference == null || WindowObjectReference.closed)
  /* if the pointer to the window object in memory does not exist
     or if such pointer exists but the window was closed */

  {
    WindowObjectReference = window.open(url,
   "sppopup", "resizable=yes,scrollbars=yes,status=yes,outerwidth=1000, height=600, top=250");
    /* then create it. The new window will be created and
       will be brought on top of any other window. */
  }
  else
  {
    WindowObjectReference.focus();
    /* else the window reference must exist and the window
       is not closed; therefore, we can bring it back on top of any other
       window with the focus() method. There would be no need to re-create
       the window or to reload the referenced resource. */
  };
}

function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    return false
		 }

 		 return true					
	}

function ValidateForm(emailID){
	
	if ((emailID.value==null)||(emailID.value=="")){
		return false
	}
	if (echeck(emailID.value)==false){
		return false
	}
	return true
 }