var _isloggedin = false;
YAHOO.namespace("example.container");
var navigateURL=null;

function generateGuid()
{
    var result, i, j;
    result = '';
    for(j=0; j<32; j++)
    {
	if( j == 8 || j == 12|| j == 16|| j == 20)
	result = result + '-';
	i = Math.floor(Math.random()*16).toString(16).toUpperCase();
	result = result + i;
    }
    return result
}

function SetUserName(response)
{
    var el2 = document.getElementById('l2');
    if (el2!=null)
	el2.parentNode.removeChild(el2);
    var el1 = document.getElementById('l1');
    if (el1!=null)
	el1.parentNode.removeChild(el1);
    var l1 = document.createElement('div');
    l1.setAttribute('id','l1');
    l1.innerHTML = ' Welcome, <span id="loginname">' + response + '</span> <a href="/customers/logout">Logout</a>';
    var login = document.getElementById('login');
    login.appendChild(l1);
}

function init() {

	// Define various event handlers for Dialog
	var handleSubmit = function() {
		this.submit();
	};
	var handleCancel = function() {
		this.cancel();
	};
	var handleSuccess = function(o) {
		var r = YAHOO.lang.JSON.parse(o.responseText);
		if (r[0]!=true)
		{
		    alert(r[1]);
		}else
		{
		_isloggedin = true;
		SetUserName(r[1]);
		if (navigateURL!=null)
		    {
			var n = navigateURL;
			navigateURL=null;
			window.location.href = n;
		    }
		    }
	};
	var handleFailure = function(o) {
		alert("Submission failed: " + o.status);
	};

	// Instantiate the Dialog
	YAHOO.example.container.logindialog = new YAHOO.widget.Dialog("logindialog",
							{ width : "30em",
							  fixedcenter : true,
							  modal:true,
							  visible : false,
							  constraintoviewport : true
							});
	// Validate the entries in the form to require that both first and last name are entered
	YAHOO.example.container.logindialog.validate = function() {
		var data = this.getData();
		if (data.username == "" || data.password == "") {
			alert("Please enter your username and password.");
			return false;
		} else {
			return true;
		}
	};

	// Wire up the success and failure handlers
	YAHOO.example.container.logindialog.callback = { success: handleSuccess,
						     failure: handleFailure };

	// Render the Dialog
	YAHOO.example.container.logindialog.render();
	YAHOO.example.container.logindialog.show();
	YAHOO.example.container.logindialog.hide();

	YAHOO.util.Event.addListener("show", "click", YAHOO.example.container.logindialog.show, YAHOO.example.container.logindialog, true);
}
YAHOO.util.Event.onDOMReady(init);

function initRegistrationDialog() {

	// Define various event handlers for Dialog
	var handleSubmit = function() {
		this.submit();
	};
	var handleCancel = function() {
		this.cancel();
	};
	var handleSuccess = function(o) {
		var r = YAHOO.lang.JSON.parse(o.responseText);
		if (r[0]!=true)
		{
		    alert(r[1]);
		}else
		{
		    _isloggedin = true;
		    SetUserName(r[1]);
		    }
	};
	var handleFailure = function(o) {
		alert("Submission failed: " + o.status);
	};

	// Instantiate the Dialog
	YAHOO.example.container.registrationdialog = new YAHOO.widget.Dialog("registrationdialog",
							{ width : "37em",
							  fixedcenter : true,
							  visible : false,
							  modal : true,
							  constraintoviewport : true
							});

	// Validate the entries in the form to require that both first and last name are entered
	YAHOO.example.container.registrationdialog.validate = function() {
		var data = this.getData();
		confirm_password = document.getElementById("confirm_password").value;
		if (data.username == "" || data.password=="") {
			alert("Please enter your username and password.");
			return false;
		}
		if (data.password != confirm_password) {
			alert("password and confirm don't match.");
			return false;
		}
		var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
		if (!emailPattern.test(data.email))
		  {
		    	alert("Your entry is not a valid e-mail address.");
			return false
		  }

		return true;
	};

	// Wire up the success and failure handlers
	YAHOO.example.container.registrationdialog.callback = { success: handleSuccess,
						     failure: handleFailure };

	// Render the Dialog
	YAHOO.example.container.registrationdialog.render();
	YAHOO.example.container.registrationdialog.show();
	YAHOO.example.container.registrationdialog.hide();

	YAHOO.util.Event.addListener("reg_show", "click", YAHOO.example.container.registrationdialog.show, YAHOO.example.container.registrationdialog, true);
}

YAHOO.util.Event.onDOMReady(initRegistrationDialog);

var CheckLoginCallBacks = {
    success: function (o) {
    var response = o.responseText;
    if (response=='True')
      {
	_isloggedin = true;
      }
    else
    {
        _isloggedin = false;
    }
},
failure: function (o) {
//    alert("Async call failed!");
    //document.title ="*";
},
timeout : 3000
}


function SecurePage(url)
{
    if (_isloggedin==true)
    {
	window.location.href = url;
    }
    else
    {
	YAHOO.example.container.logindialog.show();
    }
}

YAHOO.util.Connect.asyncRequest("POST", "/myaccount/isloggedin", CheckLoginCallBacks);
