
/**
 *
 * 30/07/2008 Charles Bajomo	Fixed bug in downloadClient function where emails
 *								with upper-case characters in them failing the Regex
 *								test.
 *
 * $Id:$
 */

/**
 * gobals
 */
var newRegistration = false;
var yuuguuConfServer = "http://powwownow.yuuguu.com/";
var serverUrl = "http://yuuguu.powwownow.com/api/";
var clientDownlaodUrl = "http://yuuguu.powwownow.com/download/";
var siteUrl = "http://yuuguu.powwownow.com";
var ldpanel = null;

// http://partner1test.yuuguu.com:8080/download/	test client download
// http://www.yuuguu.com/downloads/powwownow/		live client download

/**
 *loadingPanel :  dhow a loading dialog box
 */
function loadingPanel(imgsrc) {
document.getElementById('submitBox').className = 'submitBox';
aPanel = new YAHOO.widget.Panel('loadpnl',{width:'200px',fixedcenter: true,constraintoviewport: true,underlay:'shadow',visible:false,close:false,modal:true,draggable:false});
		 aPanel.setHeader("<div font-weight:bold;'>Processing please wait.... </div>");
		 aPanel.setBody("<img src='"+imgsrc+"'width='100%'>");
		 aPanel.cfg.setProperty("underlay","matte");
		 aPanel.render(YAHOO.util.Dom.get('ldPanel'));
		 aPanel.show();

		 return aPanel;
}

/**
 * Redirect user to Yuuguu's web conferening front end
 */
function joinConference()
{
	var formObject = YAHOO.util.Dom.get('joinForm');
	pin = formObject.pin.value;
	name = formObject.nickname.value;
	callbacksite = formObject.callback.value;
	if ((pin.length > 0) && (name.length > 0))
	{
		// redirect User to the Yuuguu server
		window.location=yuuguuConfServer+"?pin="+escape(pin)+"&nickname="+escape(name)+"&callback="+escape(callbacksite);
	}
	else
	{
		MessageBox("Error","Please enter a screen name and pin.",null,[ { text:"Ok", handler:function(){this.hide();}, isDefault:true } ]);
	}
}

/**
 * Allow existing customers to download Yuuguu. If they are not registered with
 * Yuuguu it make a registration request to the yuuguu API
 */
var getContact = {

	handleSuccess:function(o){

		ldpanel.hide();
		result = eval('('+o.responseText+')');
		if (result.status == "SUCCESS")
		{
			if (result.hasYuuguu == true)
			{
				window.location=clientDownlaodUrl+"client.php"; // redirect to yuuguu client download URL
			}
			else
			{
				regYuuguuAccount(result.email,result.name,result.pin,result.password);
			}
		}
		else
		{
			MessageBox("Error",result.message,null,[ { text:"Ok", handler:function(){this.hide();document.getElementById('submitBox').className = '';}, isDefault:true } ]);
		}

	},

	handleFailure:function(o){
		ldpanel.hide();
		// Failure handler
		MessageBox("Connection Error","Sorry, we have not been able to process your request at this time. Please contact customer support",null,[ { text:"Ok", handler:function(){this.hide();document.getElementById('submitBox').className = '';}, isDefault:true } ]);
	},

	startRequest:function(password,email) {
	   YAHOO.util.Connect.asyncRequest('POST', serverUrl+'contact.php', contactCallback, "password="+escape(password)+"&email="+escape(email)+"&type=check");
	   ldpanel = loadingPanel('../images/general/loading.gif');
	}

};

var contactCallback =
{
	success:getContact.handleSuccess,
	faliure:getContact.handleFailure,
	scope:getContact
};


/**
 * class to handle changing contact yuuguu password
 */

var powChangeYuuGuuPasswd = {

	handleSuccess:function(o) {

		ldpanel.hide();
		result = eval('('+o.responseText+')');
		if (result.status == "SUCCESS")
		{
			if (result.url == "none")
			{
				MessageBox("Success",result.message,null,[ { text:"Ok", handler:function(){this.hide();document.getElementById('submitBox').className = '';}, isDefault:true } ]);
			}
			window.location = result.url;
		}
		else
		{
			MessageBox("Error",result.message,null,[ { text:"Ok", handler:function(){this.hide();document.getElementById('submitBox').className = '';}, isDefault:true } ]);
		}

	},

	handleFailure:function(o){
		// Failure handler
		ldpanel.hide();
		MessageBox("Connection Error","Sorry, we have not been able to process your request at this time. Please contact customer support",null,[ { text:"Ok", handler:function(){this.hide();document.getElementById('submitBox').className = '';}, isDefault:true } ]);

	},

	startRequest:function(email,password,apiURL) {
		YAHOO.util.Connect.asyncRequest('POST', apiURL, powChangeYuuGuuPasswdCallback, "&email="+escape(email)+"&password="+escape(password)+"&type=yuuguuUpdate"+"&change=password");
	}

};

var powChangeYuuGuuPasswdCallback =
{
	success:powChangeYuuGuuPasswd.handleSuccess,
	faliure:powChangeYuuGuuPasswd.handleFailure,
	scope:powChangeYuuGuuPasswd
};

/**
 * class to handle changing contact yuuguu details
 */

var powUpdateYuuGuu = {

	handleSuccess:function(o) {

		ldpanel.hide();
		result = eval('('+o.responseText+')');
		if (result.status == "SUCCESS")
		{
			window.location = result.url;
		}
		else
		{
			MessageBox("Error",result.message,null,[ { text:"Ok", handler:function(){this.hide();document.getElementById('submitBox').className = '';}, isDefault:true } ]);
		}

	},

	handleFailure:function(o){
		// Failure handler
		ldpanel.hide();
		MessageBox("Connection Error","Sorry, we have not been able to process your request at this time. Please contact customer support",null,[ { text:"Ok", handler:function(){this.hide();document.getElementById('submitBox').className = '';}, isDefault:true } ]);

	},

	startRequest:function(postKeys,postValues,email,apiURL) {
		postQuery = "email="+escape(email);
		changeList = "&change=";
		for(i=0; i < postKeys.length; i++)
		{
			postQuery = postQuery + "&"+postKeys[i]+"="+escape(postValues[i]);
			changeList = changeList + postKeys[i]+"|";
		}
		YAHOO.util.Connect.asyncRequest('POST', apiURL, powChangeYuuGuuPasswdCallback, postQuery+"&type=yuuguuUpdate"+changeList);
	}

};

var powUpdateYuuGuuCallback =
{
	success:powUpdateYuuGuu.handleSuccess,
	faliure:powUpdateYuuGuu.handleFailure,
	scope:powUpdateYuuGuu
};

function downloadClient()
{
	newRegistration = false;
	// get contact pin/email address
	formObject = document.getElementById('yuuguuDwn');
	email  = formObject.email.value;
	passwd  = formObject.password.value;
	if (isValidEmail(email) && passwd.length > 0) {
		// check for yuuguu account
		getContact.startRequest(passwd,email);
	} else {
		MessageBox("Input Error","Please enter a vaild email address and password",null,[ { text:"Ok", handler:function(){this.hide();document.getElementById('submitBox').className = '';}, isDefault:true } ]);
	}
}

/**
 * Make a request to the Yuuguu server to register a contact
 */

 var yuuguuConn = {

	handleSuccess:function(o){
		ldpanel.hide();
		result = eval('('+o.responseText+')');
		if ( (result.status == 201) || (result.status == 209) )
		{
			// account has been created successfully
			if (newRegistration == true)
			{
				// show registration complete dialog box
				MessageBox("Registration Complete",result.message,null,[ { text:"Ok", handler:function(){this.hide();document.getElementById('submitBox').className = '';window.location=result.url;}, isDefault:true } ]);
			}
			else
			{
				window.location=clientDownlaodUrl+"client.php"; // redirect to yuuguu client download URL
			}
		}
		else
		{
			MessageBox("Registration Error",result.message,null,[ { text:"Ok", handler:function(){this.hide();document.getElementById('submitBox').className = '';}, isDefault:true } ]);
		}
	},

	handleFailure:function(o){
		ldpanel.hide();
		if (o.status == 409)
		{
			MessageBox("Registration Error","Your email address/pin is already registered.<br /> Please download the yuuguu client.",null,[ { text:"Ok", handler:function(){this.hide();document.getElementById('submitBox').className = '';}, isDefault:true } ]);
		}
		else
		{
			MessageBox("Connection Error","Sorry, we have not been able to process your request at this time. Please contact customer support",null,[ { text:"Ok", handler:function(){this.hide();document.getElementById('submitBox').className = '';}, isDefault:true } ]);
		}
	},

	startRequest:function(pin,email,name,password) {
		// YAHOO.util.Connect.setForm(document.getElementById('empty'));
		YAHOO.util.Connect.asyncRequest('POST', serverUrl+'contact.php', yuuguuCallback, "password="+escape(password)+"&email="+escape(email)+"&name="+escape(name)+"&pin="+escape(pin)+"&type=yuuguu");
		//ldpanel = loadingPanel('../images/general/loading.gif');
	}

};

var yuuguuCallback =
{
	success:yuuguuConn.handleSuccess,
	faliure:yuuguuConn.handleFailure,
	scope:yuuguuConn
};

function regYuuguuAccount(email,name,pin,password)
{
	yuuguuConn.startRequest(pin,email,name,password);
}

function updateYuuguuAccount(item2Update,updateValues,email)
{
	powUpdateYuuGuu.startRequest(item2Update,updateValues,email,serverUrl+'contact.php');
}

function showError()
{
	formObject = document.getElementById('joinForm');
	if(formObject.error_msg.value.length > 0)
	{
		MessageBox("Error","<br />"+formObject.error_msg.value+"<br />",null,[ { text:"Ok", handler:function(){this.hide();document.getElementById('submitBox').className = '';}, isDefault:true } ]);
	}

}


/*function isNumeric(val)
{
	var RegExp = /^[9-0]+$/;
	if(RegExp.test(val)){
        return true;
    }else{
        return false;
   }
}*/

function isValidEmail(email)
{
    var RegExp = /^((([a-z]|[0-9]|!|#|$|%|&|'|\*|\+|\-|\/|=|\?|\^|_|`|\{|\||\}|~)+(\.([a-z]|[0-9]|!|#|$|%|&|'|\*|\+|\-|\/|=|\?|\^|_|`|\{|\||\}|~)+)*)@((((([a-z]|[0-9])([a-z]|[0-9]|\-){0,61}([a-z]|[0-9])\.))*([a-z]|[0-9])([a-z]|[0-9]|\-){0,61}([a-z]|[0-9])\.)[\w]{2,4}|(((([0-9]){1,3}\.){3}([0-9]){1,3}))|(\[((([0-9]){1,3}\.){3}([0-9]){1,3})\])))$/;
    if(RegExp.test(email.toLowerCase())){
        return true;
    }else{
        return false;
    }
}

function MessageBox(heading,body,icon,Buttons) {
	document.getElementById('submitBox').className = 'submitBox';
	if (icon == null)
	{
		icon = YAHOO.widget.SimpleDialog.ICON_WARN;
	}
	aMsgDlg = new YAHOO.widget.SimpleDialog('msgPanel', {
	width: "33em",close:false,
	effect:{effect:YAHOO.widget.ContainerEffect.FADE,
	        duration:0.25},
	fixedcenter:true,
	modal:true,
	draggable:false });
	aMsgDlg.setHeader("<span style='font-weight:bold;'>"+heading+"</span>");
	aMsgDlg.setBody("<span style='margin:0;padding:0;'><p style='margin-left:30px'>"+body+"</p></span>");
	aMsgDlg.cfg.setProperty("icon",icon);
	aMsgDlg.cfg.queueProperty("buttons", Buttons);
	aMsgDlg.render(YAHOO.util.Dom.get('myPanel'));
	aMsgDlg.show();
}