var invitationsClass = Class.create({
  init: function() {					
	this.inProvider = null;
	this.inErr = null;
	this.inCurrTab = null;
	this.inSess = null;
	this.inList = new Array();
	this.inT = null;
  },

  inInitTabs: function() {

	cInvites.inCurrTab = $(".tabbox .tab1");
	cInvites.inCurrTab.show();

	$(".tabbox .menu li").click(function () { 
	  cInvites.inTabClick(this);
    });
	
	$(".provider_icon_item").each(function(s){
		$(this).mouseover(function(){
			$(this).addClass("hover");
		})
		$(this).mouseout(function(){
			$(this).removeClass("hover");
		})
		$(this).click(function(){
			$(".provider_icon_item").removeClass("active");
			$(this).addClass("active");
			cInvites.inProvider = this.id.substring(3);
		})
	});
  }, 

 inTabClick: function(obj){
	$(".tabbox .menu li").removeClass("active");
	$(".tabbox .tab").hide();
	$(".tabbox .content ." + obj.id).show();
	$(obj).addClass("active");
	cInvites.inCurrTab=$(".tabbox .tab");
	cInvites.inClearProvider();
}, 

inClearProvider: function(){
	$(".provider_icon_item").removeClass("active");
	cInvites.inProvider = null;
	this.clearAll('email');
	this.clearAll('social');
	this.clearAll('manual');
	$(".tabbox .inner2").hide();
	$(".tabbox .inner2 .outset .scrollbox").html('');
	$(".tabbox .success").hide();
	$('#in3Msg').html('');
},

clearAll:function(t){
	switch(t){
		case 'email':
			$('#eMsg').html('');
			$('#eMsg').removeAttr("class");
			$('#eEmail').val('');
			$('#ePass').val('');
			$('#eErrMail').html('');
			$('#eErrPass').html('');
		break;
		case 'social':
			$('#sMsg').html('');
			$('#sMsg').removeAttr("class");
			$('#sLogin').val('');
			$('#sPass').val('');
			$('#sErrLogin').html('');
			$('#sErrPass').html('');
		break;
		case 'manual':
			$('#mMsg').html('');
			$('#mMsg').removeAttr("class");
			$('#mMails').val('');
			$('#mMessage').val('');
		break;
	}
	$('#lMsg').html('');
	$('#lMsg').removeAttr("class");
},

inSendManual: function(){
		$('#mMsg').html('');
		$('#mMsg').removeAttr("class");

		var mailsArray = new Array();
		var error=null;
		var mails=$('#mMails').val();
		var message=$('#mMessage').val();
		var contacts = new Array();
		var mailArr= new Array();
		if(mails==''){
			cInvites.printError('mMsg', 4, null);
			return false;
		}
		mails=mails.replace(/ /g, ',');
		mails=mails.replace(/;/g, ',');
		mailsArray = mails.split(',');

		for(var i=0; i<mailsArray.length; i++){
			if(mailsArray[i].length>3)
				mailArr.push(mailsArray[i]);
		}

		for(var i=0; i<mailArr.length; i++){
			mailArr[i] = mailArr[i].replace(/^\s+|\s+$/g, '') ;
			if(mailArr[i]){
				res=this.inCheckMail(mailArr[i]);
				if(res){
					contacts.push(mailArr[i]);
				}else{
					cInvites.printError('mMsg', 5, null);
					return false;
				}
			}
			if(contacts.length>=10) 
				break;
		}
		
		if(contacts.length<1){
			cInvites.printError('mMsg', 5, null);
			return false;
		}		
		if (0) return false;
		$('#mMsg').html('<img src="/img/icos/loading2.gif" /> Loading...');

		var url = '/sys-ajax/send-manual-invites/';

		$.post(url, 
			{contacts: [contacts], message:message},
			function(data){
				if(data.error){
					cInvites.printError('mMsg', 1, null);
				}else if(data.sent){
					cInvites.inManualSendRes(true, 'Sending success.');	
				}else if(data.errorMsg){
					cInvites.inManualSendRes(false, json.errorMsg);
				}else{
					cInvites.inManualSendRes(false, 'Sending failed.');
				}
			}, 
			"json"
		);
	}, 

	inCheckMail: function(mail){
		var find_result;
		var filter=/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		if (filter.test(mail))
			find_result=true;
		else{
			find_result=false;
		}
		return (find_result);
	}, 

	manualCountMails: function(){
		var mails=$('#mMails').val();
		var mailsArray=new Array();
		mails=mails.replace(/ /g, ',');
		mails=mails.replace(/;/g, ',');
		mailsArray = mails.split(',');
		var n=0;
		for(var i=0; i<mailsArray.length; i++){
			if(mailsArray[i].length>3)
				n++;					
		}
		if(n>=10) {
			cInvites.printError('mMsg', 6, null);
			return false;
		}else{
			$('#mMsg').html('');
			$('#mMsg').removeAttr('class');

		}
	}, 
	
	inManualSendRes: function(res, msg){
		if (res){
			$(".tabbox .tab3").hide();
			$(".tabbox .success").show();
			$('#in3Msg').html(msg);
			$('#mMails').val('');
			$('#mMessage').val('');

			//rodom successss
		}else{
			cInvites.printError('mMsg', null, msg);
		}
	}, 

	inPreLogin: function(t){
		var inLogin = null;
		var inPass = null;
		var err = false;
		var eMsg = '';

		if (!cInvites.inProvider){
			err=true;
			cInvites.printError(t, 7, null);
		}else{
			cInvites.printError(t, 0, null);
		}
		cInvites.inT=t;

		switch(t){
			case 'email':
				
				inLogin = $('#eEmail').val();
				inLogin =$.trim(inLogin);
				inPass = $('#ePass').val();
				inPass =$.trim(inPass);
				if (!inLogin){
					err=true;
					$('#eErrMail').html('Email is missing');
				}else{
					$('#eErrMail').html('');
				}

				if (!inPass){
					err=true;
					$('#eErrPass').html('Password is missing');
				}else{
					$('#eErrPass').html('');
				}
				if (err) break;
				else{
					$('#eMsg').html(eMsg);
					$('#eMsg').removeAttr('class');
				}
				//do login
				$('#eMsg').html('<img src="/img/icos/loading2.gif" /> Loading...');
				
				this.inGetList('email', inLogin, inPass, 1, null);
			break;
			case 'social':
				
				inLogin = $('#sLogin').val();
				inLogin=$.trim(inLogin);
				inPass = $('#sPass').val();
				inPass=$.trim(inPass);
				if (!inLogin){
					err=true;
					$('#sErrLogin').html('Login is missing');
				}else{
					$('#sErrLogin').html('');
				}
				if (!inPass){
					err=true;
					$('#sErrPass').html('Password is missing');
				}else{
					$('#sErrPass').html('');
				}
				if (err) break;
				else{
					$('#sMsg').html(eMsg);
					$('#sMsg').removeAttr('class');
				}
				//do login
				$('#sMsg').html('<img src="/img/icos/loading2.gif" /> Loading...');
				this.inGetList('social', inLogin, inPass, 1, null);
			break;
			default :
				break;
		}
	}, 

	inGetList: function(t, login, pass, last, sess){
		var url = '';
		if (t=='email') url = '/sys-ajax/get-email-contacts/';
		else if (t=='social') url = '/sys-ajax/get-social-contacts/';
		if (!url || !cInvites.inProvider) return false;
	
		$.post(url, 
			{type: t, provider: cInvites.inProvider, login: login, pass: pass, last: last, sess: sess},
			function(data){
				if(data.error){
					if (data.msg) cInvites.printError(t, null, data.msg);
					else cInvites.printError(t, 1, null); 
				}else if (data.list && data.sess){
					cInvites.inSess = data.sess;
					cInvites.inList =cInvites.inList.concat(data.list);
					cInvites.inMakeList(t, cInvites.inList);
					if (data.have_next){
						cInvites.inGetList(t, login, pass, data.last, data.sess);
					}else{ 
						cInvites.inList=new Array();
					}
				}else{
					cInvites.printError(t, 2, null);
				}
			}, 
			"json"
		);
	}, 

	inMakeList: function(t, list){
		
		switch(t){
			case 'email':
				if (cInvites.inErr){
					cInvites.printError(t, null, cInvites.inErr);
					break;
				}else if (!list){
					cInvites.printError(t, 8, null);
					break;
				}

				var rows='';

				$.each( list, function(i, s){
					 rows += '<tr '+((i%2)?' class="even"':'')+'><td class="first"><input type="checkbox" value="'+i+'" name="inCheck[]" class="check" /></td><td>'+s[1]+'</td><td>'+s[0]+'</td></tr>';
				});
				
				
				var tbl = '<table class="contacts"><tr><th class="first"><input type="checkbox" class="check" onclick="cInvites.toggleCheck(this);" /></th><th>Name</th><th>Email</th></tr>'+rows+'</table>';

				$(cInvites.inCurrTab).hide();
				this.clearAll('email');
				this.clearAll('manual');
				
				$(".tabbox .inner2 .outset .scrollbox").html(tbl);
				$(".tabbox .inner2").show();

			break;
			case 'social':
				if (cInvites.inErr){
					cInvites.printError(t, null, cInvites.inErr);
					break;
				}else if (!list){
					cInvites.printError(t, 8, null);
					break;
				}

				var rows='';
				$.each( list, function(i, s){
					 rows += '<tr '+((i%2)?' class="even"':'')+'><td class="first"><input type="checkbox" value="'+i+'" name="inCheck[]" class="check" /></td><td>'+s[1]+'</td></tr>';
				});

				var tbl = '<table class="contacts"><tr><th class="first"><input type="checkbox" class="check" onclick="cInvites.toggleCheck(this);" /></th><th>Name</th></tr>'+rows+'</table>';

				$(cInvites.inCurrTab).hide();

				this.clearAll('social');
				this.clearAll('manual');
				
				$(".tabbox .inner2 .outset .scrollbox").html(tbl);
				$(".tabbox .inner2").show();
			break;
		}
	}, 

	toggleCheck: function(obj){
		$("#inListF INPUT[type='checkbox']").attr('checked', obj.checked);
	}, 

	inPreSendInvites: function(){
		$('#lMsg').html('');
		$('#lMsg').removeAttr('class');
//		var items = $('inListF').getInputs();
		var contacts = new Array();
		
		$("INPUT[@name=inCheck][type='checkbox']").each(function(e){
			if(this.checked){
				contacts.push($(this).val());
			}
		});
			
		if (!contacts.length){
			cInvites.printError('lMsg', 3, null);
			return;
		}
		$('#lMsg').html('<img src="/img/icos/loading2.gif" /> Sending...');
		cInvites.inSendInvites(contacts);
	}, 

	inSendInvites: function(contacts){
		var url = '';
		if (cInvites.inT=='email') url = '/sys-ajax/send-email-invites/';
		else if (cInvites.inT=='social') url = '/sys-ajax/send-social-invites/';
		if (!url || !cInvites.inProvider) return false;
		var message = $('#inMessage').val();

		$.post(url, 
			{type: cInvites.inT, provider: cInvites.inProvider, sess: cInvites.inSess, contacts: contacts, message:message},
			function(data){
				if(data.error){
					cInvites.printError('lMsg', 1, null);
				}else if (data.sent == true){
					cInvites.inSendRes(true, 'Sending success.');
				}else if(data.errorMsg){
					cInvites.inSendRes(false, data.errorMsg);
				}else{
					cInvites.inSendRes(false, 'Sending failed.');
				}
			}, 
			"json"
		);
	}, 

	inSendRes: function(res, msg){
		if (res){
			$(".tabbox .inner2").hide();
			$(".tabbox .inner2 .outset .scrollbox").html('');
			$(".tabbox .success").show();
			$('#in3Msg').html('Sending success.');
			$('#inMessage').val('');
		}else{
			this.printError('lMsg', null, msg);
		}
	}, 

	printError: function(place, err, msg){

		errArr=new Array();
		errArr[0]='';
		errArr[1]='system error, try again later';
		errArr[2]='no contacts or error';
		errArr[3]='Choose contacts to send invitation.';
		errArr[4]='please insert e-mail';
		errArr[5]='there are not valid e-mail\'s';
		errArr[6]='You can type not more that 10 e-mail\'s';
		errArr[7]='Provider not selected';
		errArr[8]='Your contacts did not received. Try again later.';

		if(place=='email')
			place='eMsg';
		else if(place=='social')
			place='sMsg';
		else if(place=='manual')
			place='mMsg';
		
		if(err){
			msg=errArr[err];
			$('#'+place).addClass('error2');
		}else{
			$('#'+place).removeAttr("class");
		}

		$('#'+place).html(msg);
	} 


});
