﻿var bluemt = {}

/**
 *
 */
bluemt.init = function() {
   bluemt.initNotificationDlg();
   bluemt.initDlg(); 
}


bluemt.initNotificationDlg = function() {
    bluemt.notiDlg = new YAHOO.widget.Dialog("notificationDlg", 
		{ modal:true, visible:false, fixedcenter:true, 
		  constraintoviewport:true, draggable:false, 
		  close:true, iframe:true, underlay:"shadow"
		});
		
	try {
		bluemt.notiDlg.render(document.body);	
	} catch(e) {alert("Error in initWaitDlg: "+e.message);}
}

/**
 *
 */
bluemt.initDlg = function() {
    var handleSubmit = function() { 
        bluemt.contactDlg.submit();
	};
	
	var handleCancel = function() { 
	    bluemt.contactDlg.hide();
	    bluemt.clearData();
	}; 

	bluemt.contactDlg = new YAHOO.widget.Dialog("contactDlg", 
		{ modal:true, visible:false, fixedcenter:true, 
		  constraintoviewport:true, draggable:false, 
		  close:true, iframe:true, underlay:"shadow",
		  effect:{effect:YAHOO.widget.ContainerEffect.FADE,duration:0.25},
		  buttons:[{text:"Submit", handler:handleSubmit, isDefault:true},
			       { text:"Cancel", handler:handleCancel } ]	
		});
		
    bluemt.contactDlg.callback = {
        success: bluemt.questionSubmitted,
        failure: bluemt.questionFailed
    };
    
    bluemt.contactDlg.validate = bluemt.validateQuestion;
		
	try {
		bluemt.contactDlg.render(document.body);	
	} catch(e) {alert("Error in initWaitDlg: "+e.message);}
}

/**
 *
 */
bluemt.questionSubmitted = function(o) {
    bluemt.contactDlg.hide();
	document.getElementById("pNotification").innerHTML = 
       "Thanks, your question/comment has been sent.";
    bluemt.clearData();
  	bluemt.notiDlg.show();
}

/**
 *
 */
bluemt.clearData = function() {
    document.getElementById("txtName").value = "";
    document.getElementById("txtPhone").value = "";
    document.getElementById("txtEmail").value = "";
    document.getElementById("txtQuestion").value = "";
}

/**
 *
 */
bluemt.questionFailed = function(o) {
    bluemt.contactDlg.hide();
    document.getElementById("pNotification").innerHTML = 
  	    "An error has occurred, please try again later";
  	bluemt.notiDlg.show();
}

/** 
 *
 */
bluemt.validateQuestion = function() {
    var data = bluemt.contactDlg.getData();
    
    if(bluemt.trim(data.txtName) === "" || 
       bluemt.trim(data.txtQuestion) === "" || 
       (bluemt.trim(data.txtEmail) === "" && 
        bluemt.trim(data.txtPhone) === "")) {
        alert("Name, Email, and Question are required.");
        return false;
    }
    
    return true;
}

/**
 *
 */
bluemt.showContactDlg = function(e) {
    bluemt.contactDlg.show();   
}

/**
 * Removes beginning and trailing white space.
 * 
 * @param data {string}
 */
bluemt.trim = function(data) { 
	if(data !== null && typeof(data) == "string")
		return data.replace(/^\s*|\s*$/g,'')
		
	return data;
}

/**
 * Restricts entry inside a text field to non-negative whole numbers.
 */
bluemt.numericFieldKeyPress = function(e) {
    var keyId = (window.event) ? event.keyCode : (e.keyCode == 0 ? e.charCode : e.keyCode);

    //allow enter/tab keys and numbers
    if(keyId != 13 && keyId != 9 && (keyId < 48 || keyId > 57)) {
        if(window.event) {
            event.returnValue = false;
        }
        else {
            e.returnValue = false;
            e.preventDefault();
        }
    }
}

YAHOO.util.Event.addListener(window, "load", bluemt.init);
YAHOO.util.Event.addListener("aContact", "click", bluemt.showContactDlg);
YAHOO.util.Event.addListener("aContact2", "click", bluemt.showContactDlg);
YAHOO.util.Event.addListener("aContact3", "click", bluemt.showContactDlg);
YAHOO.util.Event.addListener("txtPhone", "keypress", bluemt.numericFieldKeyPress);
