Expenews.Accounts = {
    login: function(username, password, responseSel,testing){
        var tok = "Basic "+$.base64Encode(username+":"+password);
        var url = Expenews.Config.Url.login;
        return Etiqs.restfull.get(url,{
            success: function(x, t){
        		var sessionId = Etiqs.Utils.getLocationIndex(x.getResponseHeader('Location'), 1);
                var accountId = Etiqs.Utils.getLocationIndex(x.getResponseHeader('Location'), 3);
                var userName = x.getResponseHeader('Expenews-User');
		var dom = x.getResponseHeader('domains').split('|');
		var doma="";
		/*
		for (i=0;i<dom.length;i++){
			doma=dom[i];
		*/
		doma="."+dom[0];
		if (doma!='.localhost'){
			$.cookie('session_id', sessionId, {path: '/', domain: doma});
			$.cookie('account_id', accountId, {path: '/', domain: doma});
			$.cookie('user_name', userName, {path: '/', domain: doma});
			$.cookie('expedition_editor', null, {path: '/', domain: doma});
			$.cookie('created_expedition_id', null, {path: '/', domain: doma});
		}else{
			$.cookie('session_id', sessionId, {path: '/'});
			$.cookie('account_id', accountId, {path: '/'});
			$.cookie('user_name', userName, {path: '/'});
			$.cookie('expedition_editor', null, {path: '/'});
			$.cookie('created_expedition_id', null, {path: '/'});
		}
                //}
		$.fancybox.close();
		loginOk();

                return true;
            }
            ,fail: function(x, t){
            	if(testing){
    				return x.responseXML;
    			}
            	return false;}
            ,before: function(x){x.setRequestHeader("Authorization", tok);}
            ,responseSel: responseSel
            ,temporal: true
            ,failText: text['loginFail']
            ,okText: text['loginOk']
            ,omitRedirect: true
        });
    },
    logout: function(testing){
        return Etiqs.restfull.get(Expenews.Config.Url.logout,{
            success: function(x, t){
		var dom = x.getResponseHeader('domains').split('|');
		var doma="";
		/*
		for (i=0;i<dom.length;i++){
			doma=dom[i];
		*/
		doma="."+dom[0];
		if (doma!='.localhost'){
			$.cookie('session_id', null, {path: '/', domain: doma});
			$.cookie('account_id', null, {path: '/', domain: doma});
			$.cookie('user_name', null, {path: '/', domain: doma});
			$.cookie('expedition_editor', null, {path: '/', domain: doma});
			$.cookie('created_expedition_id', null, {path: '/', domain: doma});
		}else{
			$.cookie('session_id', null, {path: '/'});
			$.cookie('account_id', null, {path: '/'});
			$.cookie('user_name', null, {path: '/'});
			$.cookie('expedition_editor', null, {path: '/'});
			$.cookie('created_expedition_id', null, {path: '/'});
		}
		//}
		logoutOk();
		return true;
            }
            ,fail: function(x, t){
            	if(testing){
    				return x.responseXML;
    			}
            }
        });   
    },
    resetPassword: function(email, responseSel){
        var url = Expenews.Config.Url.resetPassword;
        return Etiqs.restfull.get(url+'?email='+email+'&lang='+lang,{
            success: function(x, t){
                return true;
            }
            ,fail: function(x, t){
            	return false;
            }
            ,responseSel: responseSel
            ,temporal: true
            ,failText: text['resetPasswordFail']
            ,okText: text['resetPasswordOk']
            ,omitRedirect: true
        });
    },
    create: function(first_name, last_name, description, email, password,testing){
	 	var passwordText = "";
		if(password){
			passwordText="password = '"+password+"'";
		}
        xml = "<account email=\""+email+"\" first_name=\""+first_name+"\" last_name=\""+last_name+"\" "+passwordText+"><description><![CDATA["+description+"]]></description></account>";
        var createdId = null;
        Etiqs.restfull.post(
            Expenews.Config.Url.accounts, 
            xml, 
            {
                success: function(x, t){
                    createdId = Etiqs.Utils.getLocationIndex(x.getResponseHeader('Location'),1);
                }
                ,fail: function(x, t){
                	if(testing){
        				return x.responseXML;
        			}
		}
                ,contentType: 'text/xml'
                ,responseSel: "#responseStatus"
                ,okText: "OK"
                ,failText: "Error"
            }
        );
        return createdId;
    },
    updateAccount: function(account_id, first_name, last_name, description, email, password,testing){
        xml = "<account id='"+account_id+"' email=\""+email+"\" first_name=\""+first_name+"\" last_name=\""+last_name+"\" password=\""+password+"\"><description><![CDATA["+description+"]]></description></account>";
        return Etiqs.restfull.put(
            Expenews.Config.Url.accountUpdate(account_id),
            xml, 
            {
                success: function(x, t){
			return true;
                }
                ,fail: function(x, t){
                	if(testing){
        				return x.responseXML;
        			}
			return false;
		}
                ,contentType: 'text/xml'
                ,responseSel: "#responseStatus"
                ,okText: "OK"
                ,failText: "Error"
            }
        );
    },
    addImage: function(account_id,file,content){
        xml = "<image><type>"+content+"</type><location>"+file+"</location></image>";
        return Etiqs.restfull.post(
		Expenews.Config.Url.accountImage(account_id),
		xml,
            {
                success: function(x, t){
                    return $("location", x.responseXML).text();
                }
                ,fail: function(x, t){
			return false;
		}
            }
        );
    },

    list: function(orderBy, limit, offset, desc){
        alert("don't use me");
    	/*return Etiqs.restfull.get(
            Expenews.Config.Url.accounts+"?order="+orderBy+"&limit="+limit+"&offset="+offset+"&desc="+desc,
            {
                success: function(x, t){
                    return x.responseXML;
                }
                ,fail: function(x, t){}
                ,responseSel: "#status-response"
                ,okText: "Loaded"
                ,failText: "Error"
            }
        );*/
    },
	getAccountId: function(email){
		return Etiqs.restfull.get(
            Expenews.Config.Url.getAccountId(email),
            {
                success: function(x, t){
                    return x.responseText;
                }
                ,fail: function(x, t){
					return null;
				}
            }
        );
	},
	getPublicData: function(email,testing){
		return Etiqs.restfull.get(
            Expenews.Config.Url.accountGetPublicData(email),
            {
                success: function(x, t){
            	if(testing){
            		return x;
            	}
			id=$("account", x.responseXML).attr("id");
			first_name=$("account", x.responseXML).attr("first_name");
			last_name=$("account", x.responseXML).attr("last_name");
			image=$("account", x.responseXML).attr("image");
			description=$("description", x.responseXML).text();
			return Array(id, first_name, last_name, image, description);
                }
                ,fail: function(x, t){
					return null;
		}
            }
        );
	},
	removeSocial: function(account, linktype){
		return Etiqs.restfull.del(Expenews.Config.Url.accountSocial(account, linktype),{
		success: function(x, t){
			return true;
		}
		,fail: function(x, t){
			return false;
		}
		});
	},
	getContacts: function(accountId){
		return Etiqs.restfull.get(
		Expenews.Config.Url.getContacts(accountId), 
		{
			success: function(x, t){
				var returnContacts = {"total": 0, "list": Array()};
				var totalContacts = 0;
				$("account", x.responseXML).each(
					function(){
						returnContacts["list"].push([$(this).attr("email"), $(this).attr("id")]);
						totalContacts++;
					}
				);
				returnContacts["total"] = totalContacts;
			return returnContacts;
			}
			,fail: function(x, t){}
		}
		);
	},
	deleteContacts: function(accountId, code){
		return Etiqs.restfull.del(Expenews.Config.Url.deleteContacts(accountId, code),{
		success: function(x, t){
			return true;
		}
		,fail: function(x, t){
			return false;
		}
		});
		
	},

	notifyNewAccount: function(lang,first_name, last_name, email, pwd){
        xml = "<accounts><account lang='"+lang+"' first_name=\""+first_name+"\" last_name=\""+last_name+"\" email=\""+email+"\" pass='"+pwd+"' /></accounts>";
	
        return Etiqs.restfull.post(
            Expenews.Config.Url.notifyNewAccount(lang), 
            xml, 
            {
                success: function(x, t){
			return $("id", x.responseXML).text();
                }
                ,fail: function(x, t){
			return false;
		}
                ,contentType: 'text/xml'
                ,responseSel: "#responseStatus"
                ,okText: "OK"
                ,failText: "Error"
            }
        );
    },
	getExpeditionsfromVoicemailCode: function(voicemail_code, mail)	{
		
		return Etiqs.restfull.get(
		Expenews.Config.Url.ExpeditionsfromVoicemailCode(voicemail_code, mail), 
		{
			success: function(x, t){
				
				var returnExpeditions = {"total": 0, "active":0, "list": Array()};
				var totalExpeditions = 0;
				$("expedition", x.responseXML).each(
					function(){
						returnExpeditions["list"].push([$(this).attr("account_name"),
							$(this).attr("account_email"),
							$(this).attr("expedition_name"),
							$(this).attr("expedition_status"),
							$(this).attr("expedition_voicemail_code"),
							$(this).attr("expedition_start_date"),
							$(this).attr("expedition_end_date"),
							$(this).attr("expedition_service")]);
						totalExpeditions++;
					}
				);
				returnExpeditions["total"] = totalExpeditions;
			return returnExpeditions;
			}
			,fail: function(x, t){}
		}
		);
	},
	addToken: function(account_id, token){
        xml = "<tokens><token><![CDATA["+token+"]]></token></tokens>";
	
        return Etiqs.restfull.post(
            Expenews.Config.Url.addToken(account_id), 
            xml, 
            {
                success: function(x, t){
			return true;
                }
                ,fail: function(x, t){
			return false;
		}
            }
        );
	},	
	delToken: function(account_id, token){
        xml = "<tokens><token><![CDATA["+token+"]]></token></tokens>";
	
        return Etiqs.restfull.post(
            Expenews.Config.Url.delToken(account_id), 
            xml, 
            {
                success: function(x, t){
			return true;
                }
                ,fail: function(x, t){
			return false;
		}
            }
        );
    },

};
