
jQuery(document).ready(function() {

	pathArray = window.location.pathname.split( '/' );
	pathProcess = "";
	for ( i = 0; i<=pathArray.length; i++ ) {
	pathProcess += "../";
	}
	urlProcess = pathProcess + "process.php";
	
	urlRedir = window.location.protocol + "//" + window.location.host + "/"

	function TrimString(sInString) {	   
	   if ( sInString ) {
		  sInString = sInString.replace( /^\s+/g, "" );// strip leading
		  return sInString.replace( /\s+$/g, "" );// strip trailing
	   }
	}

// Errors Table
// 
// This table detects the error message and returns the appropiate message fo the customer.
//
	var msglist = '\
	1~login~Your username is invalid~Ο κωδικός δεν αναγνωρίστηκε~error|\
	2~login~Your username and/or password is invalid~Ο κωδικός δεν αναγνωρίστηκε~error|\
	3~login~Success~Έχετε συνδεθεί~ok|\
	4~logout~Logout~Έχετε αποσυνδεθεί~ok|\
	1~loginmain~Your username is invalid~Please try again~errordeposit|\
	2~loginmain~Your username and/or password is invalid~Please try again~errordeposit|\
	3~loginmain~Success~You are logged in~verify|\
	5~register~empty vars~<img style="float:left; padding:0 10px 10px 0" src="images/invalid.png" alt="" /> Some fields are not completed correctly. Please review the messages above.~errordeposit|\
	5~register~Success~<img style="float:left; padding:0 10px 10px 0" src="images/passed.png" alt="" /> Έχετε Εγγραφεί με επιτυχία~verify|\
	5~register~Email already exists~ This Email already exists~errordeposit|\
	5~register~Login name already exists~ Αυτό το όνομα χρήστη υπάρχει ήδη~errordeposit|\
	6~register~Screen alias already exists~Αυτό το παρωνύμιο υπάρχει ήδη~errordeposit|\
	7~register~Invalid referral_method~Μη έγκυρη μέθοδος σύστασης~errordeposit|\
	8~register~Unsupported Country Code~Το πεδίο Χώρα είναι κενό, παρακαλώ ελέγξτε το~errordeposit|\
	9~register~Invalid currency type~Μη έγκυρο συνάλλαγμα~errordeposit|\
	10~register~Invalid account type~Μη έγκυρος τύπος λογαριασμού~errordeposit|\
	11~register~Invalid Promo Code~Invalid Promo Code~errordeposit|\
	12~register~Posted balance transaction type is missing~Posted balance transaction type is missing~errordeposit|\
	13~register~Deposit transaction type is missing~Deposit transaction type is missing~errordeposit|\
	14~register~Could not insert account record~Could not insert account record~errordeposit|\
	15~register~Could not insert account_transaction record~Could not insert account_transaction record~errordeposit|\
	16~register~Could not create archive record~Could not create archive record~errordeposit|\
	17~register~Could not remove archive record~Could not remove archive record~errordeposit|\
	18~register~Could not insert posted balance record~Could not insert posted balance record~errordeposit|\
	19~register~The entered date is not valid~<img style="float:left; padding:0 10px 10px 0" src="images/invalid.png" alt="" /> Η ημερομηνία που γράψατε δεν είναι έγκυρη~errordeposit|\
	19~register~Passwords do not match~<img style="float:left; padding:0 10px 10px 0" src="images/invalid.png" alt="" /> Passwords do not match~errordeposit|\
	19~register~You must type only digits and letters~<img style="float:left; padding:0 10px 10px 0" src="images/invalid.png" alt="" /> Some fields are not completed correctly. Please review the messages above.~errordeposit|\
	19~register~wrong security code~<img style="float:left; padding:0 10px 10px 0" src="' + pathProcess + 'images/invalid.png" alt="" /> The security code is not correct~errordeposit|\
	19~register~You must type a valid Email. Must have the format xxxxx@domain.com~<img style="float:left; padding:0 10px 10px 0" src="' + pathProcess + 'images/invalid.png" alt="" /> You must type a valid Email. Must have the format xxxxx@domain.com~errordeposit|\
	20~loadfunmoney~Success~Επιτυχία~ok|\
	20~loadfunmoneybox~Success~Now you have more fun money~ok|\
	21~deposit~Redirected~Επιτυχία~verify|\
	22~deposit~Error~<img style="float:left; padding:0 10px 10px 0" src="images/invalid.png" alt="" /> Υπάρχουνορισμέναπεδίακενά ~errordeposit|\
	19~deposit~You must type only digits and letters~<img style="float:left; padding:0 10px 10px 0" src="images/invalid.png" alt="" /> Some fields are not completed correctly. Please review the messages above.~errordeposit|\
	21~updateinfo~Success~Ενημερώσατεμεεπιτυχίαταστοιχείασας~verify|\
	22~updateinfo~Error~<img style="float:left; padding:0 10px 10px 0" src="images/invalid.png" alt="" /> Υπάρχουνορισμέναπεδίακενά ~errordeposit|\
	19~updateinfo~You must type only digits and letters~<img style="float:left; padding:0 10px 10px 0" src="images/invalid.png" alt="" /> Some fields are not completed correctly. Please review the messages above.~errordeposit|\
	19~updateinfo~You must type a valid Email. Must have the format xxxxx@domain.com~<img style="float:left; padding:0 10px 10px 0" src="' + pathProcess + 'images/invalid.png" alt="" /> You must type a valid Email. Must have the format xxxxx@domain.com~errordeposit|\
	';

	function getMsg(msgString,option,style) {
		msgStr = TrimString(msgString)
		msgOpt = TrimString(option)
		msgSty = TrimString(style)
		var msgLArray = msglist.split('|');     								 // Split into lines
		for (var loop = 0; loop < msgLArray.length; loop++) {
			msglineArray = msgLArray[loop].split('~');
			msgCode  = TrimString(msglineArray[0]);
			msgType  = TrimString(msglineArray[1]);
			msgName  = TrimString(msglineArray[2]);
			msgInput = TrimString(msglineArray[3]);
			msgStatus = TrimString(msglineArray[4]);
			if ( (msgType ==  msgOpt) && (msgName ==  msgStr) ) {
				$("#"+ msgSty +"").removeClass();

				$("#"+ msgSty +"").fadeTo(600,0.1,function() 					//start fading the messagebox
				{ 
			  //add message and change the class of the box and start fading
				if ((msgStatus == "ok") || (msgStatus == "verify")) {
					$(this).html( '<div class="box-' + msgStatus + '"><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="tl"></td><td class="t"></td><td class="tr"></td></tr><tr><td class="l"></td><td class="c"><p>' + msgInput + '</p></td><td class="r"></td></tr><tr><td class="bl"></td><td class="b"></td><td class="br"></td></tr></tbody></table></div>' ).fadeTo(300,1,function()
					{ 
						if (msgOpt == "logout") {
						document.location='index.php';
						} else if (msgOpt == "register")   {
						document.location='index.php?option=com_content&view=article&id=59&Itemid=74';
						} else if (msgOpt == "deposit")   {
						document.location='index.php?option=com_content&view=article&id=49&Itemid=53';
						} else if (msgOpt == "updateinfo")   {
						document.location='index.php?option=com_content&view=article&id=60&Itemid=76';
						} else {
						document.location='index.php?option=com_content&view=article&id=4&Itemid=5';
						}					
					});
				} else{
					$(this).html( '<div class="box-' + msgStatus + '"><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="tl"></td><td class="t"></td><td class="tr"></td></tr><tr><td class="l"></td><td class="c"><p>' + msgInput + '</p></td><td class="r"></td></tr><tr><td class="bl"></td><td class="b"></td><td class="br"></td></tr></tbody></table></div>' ).fadeTo(300,1);					
				}
				});
				break;
				msgCode = "";
				msgType  = "";
				msgName  = "";
				msgInput = "";
				msgStatus = "";					
			}
		}
	}
	//Start Login

	$("#loginacct").submit(function()
	{
		//remove all the class add the messagebox classes and start fading
		$("#msgbox").removeClass().addClass('messagebox').text('Validating....').fadeIn(1000);
		//check the username exists or not from ajax
		$.post(urlProcess,{ login:$('#login').val(),ip:$('#ip').val(),user_name:$('#username').val(),password:$('#password').val(),rand:Math.random() } ,function(data)
        {		
			getMsg(data,'login','msgboxlogin');
		});
 		return false; //not to post the  form physically
	});
	//now call the ajax also focus move from 
	$("#password").blur(function()
	{
		$("#loginacct").trigger('submit');
		
	});

	$("#loginacctmain").submit(function()
	{
		//remove all the class add the messagebox classes and start fading
		$("#msgbox").removeClass().addClass('messagebox').text('Validating....').fadeIn(1000);
		//check the username exists or not from ajax
		$.post(urlProcess,{ login:$('#loginmain').val(),ip:$('#ip').val(),user_name:$('#usernamemain').val(),password:$('#passwordmain').val(),rand:Math.random() } ,function(data)
        {		
			getMsg(data,'login','msgboxmain');
		});
 		return false; //not to post the  form physically
	});
	//now call the ajax also focus move from 
	$("#password").blur(function()
	{
		$("#loginacctmain").trigger('submit');
		
	});


	$("#logoutacct").submit(function()
	{
		//remove all the class add the messagebox classes and start fading
		//check the username exists or not from ajax
		$.post(urlProcess,{ logout:$('#logout').val() } ,function(data)
        {		
			getMsg(data,'logout','msgboxlogout');
		});
 		return false; //not to post the  form physically
	});

	$("#registerForm").submit(function()
	{
		$.post(urlProcess,{ 
					register:$('#register').val(),
					fname:$('#fname').val(),
					lname:$('#lname').val(),
					month:$('#month').val(),
					day:$('#day').val(),
					year:$('#year').val(),
					remail:$('#remail').val(),
					rusername:$('#rusername').val(),
					rpassword:$('#rpassword').val(),
					rrpassword:$('#rrpassword').val(),
					ralias:$('#alias').val(),
					aboutus:$('#aboutus').val(),
					promocode:$('#promocode').val(),
					referred:$('#referred').val(),
					rcaptcha:$('#rcaptchanum').val(),
					rtermscond:$('#agree:checked').val(),
					rip:$('#rip').val(),
					lang:$('#lang').val(),
					urlsite:$('#urlsite').val(),
				rand:Math.random()},function(data)
		{		
			getMsg(data,'register','msgboxoption');
		});
 		return false; //not to post the  form physically
	});

	$("#loadfunmoney").submit(function()
	{
		$.post(urlProcess,{ loadfunmoney:$('#loadfunmoney').val(),user_name:$('#acctname').val(),rand:Math.random() } ,function(data)
        {		
			getMsg(data,'loadfunmoney','msgboxfunmoney');
		});
 		return false; //not to post the  form physically
	});

	$("#ldfunmoney").submit(function()
	{
		$.post(urlProcess,{ loadfunmoney:$('#loadfunmoney').val(),user_name:$('#acctname').val(),rand:Math.random() } ,function(data)
        {		
			getMsg(data,'loadfunmoney','msgboxldfunmoney');
		});
 		return false; //not to post the  form physically
	});


	$("#depositForm").submit(function()
	{
		$.post(urlProcess,{ 
					deposit:$('#deposit').val(),
					fname:$('#fname').val(),
					lname:$('#lname').val(),

					rip:$('#rip').val(),
					ds:$('#ds').val(),
					ft:$('#ft').val(),
					dbirth:$('#dbirth').val(),
					demail:$('#demail').val(),
					dcountry:$('#countrySelect').val(),
					dstate:$('#depprov').val(),
					dcity:$('#town').val(),
					daddress:$('#addr').val(),
					dzip:$('#pcode').val(),
					dphonecode:$('#phonecode').val(),
					dphone:$('#phone').val(),					
				rand:Math.random()},function(data)
		{		
		getMsg(data,'deposit','msgboxdeposit');
		});
 		return false; //not to post the  form physically
	});

	$("#updateForm").submit(function()
	{
		$.post(urlProcess,{ 
					updateinfo:$('#updateinfo').val(),
					fname:$('#fname').val(),
					lname:$('#lname').val(),
					dbirth:$('#datebirth').val(),
					demail:$('#demail').val(),
					dcountry:$('#countrySelect').val(),
					dstate:$('#depprov').val(),
					dcity:$('#town').val(),
					daddress:$('#addr').val(),
					dzip:$('#pcode').val(),
					dphonecode:$('#phonecode').val(),
					dphone:$('#phone').val(),					
				rand:Math.random()},function(data)
		{		
		getMsg(data,'updateinfo','msgboxupdateinfo');
		});
 		return false; //not to post the  form physically
	});
});