var CSRF_Cookie_Name="softgis_csrf";var api_full_url="http://pehmo.tkk.fi";function getCookie(c_name){var i,x,y,ARRcookies=document.cookie.split(";");for(i=0;i<ARRcookies.length;i++){x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);x=x.replace(/^\s+|\s+$/g,"");if(x===c_name){return unescape(y);}}}
function register(username,password,callback_function){var data={};data['username']=(username!==undefined)?username:null;data['password']=(password!==undefined)?password:null;dojo.xhrPost({"url":api_full_url+'/e-pehmo/user/register/',"handleAs":"json","postData":encodeURIComponent(dojo.toJson(data)),"failOk":true,"headers":{"Content-Type":"application/json","X-CSRFToken":getCookie(CSRF_Cookie_Name)},"handle":function(response,ioArgs){if(callback_function!==undefined){callback_function({"status_code":ioArgs.xhr.status,"message":ioArgs.xhr.responseText});}}});}
function login(username,password,callback_function){var data={};data['username']=(username!==undefined)?username:null;data['password']=(password!==undefined)?password:null;dojo.xhrPost({"url":api_full_url+'/e-pehmo/user/login/',"handleAs":"text","postData":encodeURIComponent(dojo.toJson(data)),"failOk":true,"headers":{"Content-Type":"application/json","X-CSRFToken":getCookie(CSRF_Cookie_Name)},"handle":function(response,ioArgs){if(callback_function!==undefined){callback_function({"status_code":ioArgs.xhr.status,"message":ioArgs.xhr.responseText});}}});}
function logout(callback_function){dojo.xhrGet({"url":api_full_url+'/e-pehmo/user/logout/',"handle":function(response,ioArgs){if(callback_function!==undefined){callback_function({"status_code":ioArgs.xhr.status,"message":ioArgs.xhr.responseText});}}});}
function create_session(callback_function){dojo.xhrPost({"url":api_full_url+'/e-pehmo/user/session/',"handleAs":"text","failOk":true,"headers":{"Content-Type":"application/json","X-CSRFToken":getCookie(CSRF_Cookie_Name)},"handle":function(response,ioArgs){if(callback_function!==undefined){callback_function({"response":response,"ioArgs":ioArgs});}}});}
function delete_session(callback_function){dojo.xhrDelete({"url":api_full_url+'/e-pehmo/user/session/',"handleAs":"text","failOk":true,"headers":{"Content-Type":"application/json","X-CSRFToken":getCookie(CSRF_Cookie_Name)},"handle":function(response,ioArgs){if(callback_function!==undefined){callback_function({"response":response,"ioArgs":ioArgs});}}});}
function get_session(callback_function){dojo.xhrGet({"url":api_full_url+'/e-pehmo/user/session/',"handleAs":"text","failOk":true,"headers":{"Content-Type":"application/json","X-CSRFToken":getCookie(CSRF_Cookie_Name)},"handle":function(response,ioArgs){if(callback_function!==undefined){callback_function({"response":response,"ioArgs":ioArgs});}}});}
function new_password(email,callback_function){var data={};data.email=(email!==undefined)?email:null;dojo.xhrPost({"url":api_full_url+'/e-pehmo/user/newpassword/',"handleAs":"text","postData":encodeURIComponent(dojo.toJson(data)),"failOk":true,"headers":{"Content-Type":"application/json","X-CSRFToken":getCookie(CSRF_Cookie_Name)},"handle":function(response,ioArgs){if(callback_function!==undefined){callback_function({"status_code":ioArgs.xhr.status,"message":ioArgs.xhr.responseText});}}});}
function change_password(old_password,new_password,callback_function){var data={};data.old_password=(old_password!==undefined)?old_password:null;data.new_password=(new_password!==undefined)?new_password:null;dojo.xhrPost({"url":api_full_url+'/e-pehmo/user/changepassword/',"handleAs":"text","postData":encodeURIComponent(dojo.toJson(data)),"failOk":true,"headers":{"Content-Type":"application/json","X-CSRFToken":getCookie(CSRF_Cookie_Name)},"handle":function(response,ioArgs){if(callback_function!==undefined){callback_function({"status_code":ioArgs.xhr.status,"message":ioArgs.xhr.responseText});}}});}
function save_profile_values(profile_value_pairs,callback_function){dojo.xhrPost({"url":api_full_url+"/e-pehmo/profile/","handleAs":"json","postData":encodeURIComponent(dojo.toJson(profile_value_pairs)),"headers":{"Content-Type":"application/json","X-CSRFToken":getCookie(CSRF_Cookie_Name)},"failOk":true,"handle":function(response,ioArgs){if(callback_function!==undefined){callback_function({"status_code":ioArgs.xhr.status,"message":ioArgs.xhr.responseText});}}});}
var profile_values={};function get_profiles(limiter_param,callback_function){if(limiter_param===undefined||limiter_param===null){limiter_param='';}
if(profile_values[limiter_param]===undefined){dojo.xhrGet({"url":api_full_url+'/e-pehmo/profile/'+limiter_param,"handleAs":"json","failOk":true,"headers":{"Content-Type":"application/json","X-CSRFToken":getCookie(CSRF_Cookie_Name)},"load":function(response,ioArgs){if(callback_function!==undefined){callback_function(response,ioArgs);}
profile_values[limiter_param]=response;return response;},"error":function(response,ioArgs){if(callback_function!==undefined){callback_function(response,ioArgs);}
if(djConfig.debug){console.error("HTTP status code: ",ioArgs.xhr.status);}
return response;}});}else if(callback_function!==undefined){callback_function(profile_values[limiter_param]);}
return[];}
function get_features(limit_params,callback_function){if(limit_params===undefined){limit_params="";}
dojo.xhrGet({"url":api_full_url+'/e-pehmo/feature/'+limit_params,"handleAs":"json","headers":{"Content-Type":"application/json","X-CSRFToken":getCookie(CSRF_Cookie_Name)},"handle":function(response,ioArgs){if(callback_function!==undefined){callback_function({"response":response,"ioArgs":ioArgs});}}});}
function create_feature(feature_or_feature_collection,callback_function){dojo.xhrPost({"url":api_full_url+"/e-pehmo/feature/","handleAs":"json","postData":encodeURIComponent(dojo.toJson(feature_or_feature_collection)),"headers":{"Content-Type":"application/json","X-CSRFToken":getCookie(CSRF_Cookie_Name)},"handle":function(response,ioArgs){if(callback_function!==undefined){callback_function({"response":response,"ioArgs":ioArgs});}}});}
function update_feature(feature_or_feature_collection,callback_function){dojo.xhrPut({"url":api_full_url+"/e-pehmo/feature/","handleAs":"text","postData":encodeURIComponent(dojo.toJson(feature_or_feature_collection)),"headers":{"Content-Type":"application/json","X-CSRFToken":getCookie(CSRF_Cookie_Name)},"handle":function(response,ioArgs){if(callback_function!==undefined){callback_function({"response":response,"ioArgs":ioArgs});}}});}
function delete_feature(feature_or_feature_collection,callback_function){var feature_ids_array=[];var type=feature_or_feature_collection.type;var i=0;if(type==="Feature"){feature_ids_array[0]=feature_or_feature_collection.id;}
else if(type==="FeatureCollection"){for(i=0;i<feature_or_feature_collection.features.length;i++){if(feature_or_feature_collection.features[i].id!==undefined){feature_ids_array.push(feature_or_feature_collection.features[i].id);}}}
dojo.xhrDelete({"url":api_full_url+'/e-pehmo/feature/?ids='+dojo.toJson(feature_ids_array),"handleAs":"text","headers":{"Content-Type":"application/json","X-CSRFToken":getCookie(CSRF_Cookie_Name)},"handle":function(response,ioArgs){if(callback_function!==undefined){callback_function({"response":response,"ioArgs":ioArgs});}}});}
function save_graphic(graphic,callback_function){console.log("DEPRACATED save_graphic");var properties=graphic.attributes; var geojson_feature={};geojson_feature.type="Feature";geojson_feature.properties=properties;geojson_feature.geometry={};if(graphic.geometry.type==="polyline"){geojson_feature.geometry.type="LineString";geojson_feature.geometry.coordinates=graphic.geometry.paths[0];}else if(graphic.geometry.type==="point"){geojson_feature.geometry.type="Point";geojson_feature.geometry.coordinates=[graphic.geometry.x,graphic.geometry.y];}else if(graphic.geometry.type==="polygon"){geojson_feature.geometry.type="Polygon";geojson_feature.geometry.coordinates=graphic.geometry.rings;}
if(graphic.id!==undefined&&graphic.id!==null){geojson_feature.id=graphic.id;} 
geojson_feature.geometry.crs={"type":"EPSG","properties":{"code":graphic.geometry.spatialReference.wkid}};if(graphic.id!==undefined&&graphic.id!==null){update_feature(geojson_feature,callback_function);}else{create_feature(geojson_feature,callback_function);}}
function remove_graphic(feature_id,callback_function){console.log("DEPRACATED remove_graphic");var feature={'type':'Feature','id':feature_id};delete_feature(feature,callback_function);}
var LAYER_ADD_SEMAPHORES={};var QUERIED_PARAM={};function get_graphics(limiter_param,map_layer,infotemplate,callback_function){console.log("DEPRACATED get_graphics");if(limiter_param===undefined||limiter_param===null){limiter_param='';} 
if(QUERIED_PARAM[limiter_param]){return;}else{QUERIED_PARAM[limiter_param]=true;}
dojo.xhrGet({"url":api_full_url+'/e-pehmo/feature/'+limiter_param,"handleAs":"json","sync":false,"headers":{"Content-Type":"application/json","X-CSRFToken":getCookie(CSRF_Cookie_Name)},"load":function(response,ioArgs){var return_graphics=[];var i=0;LAYER_ADD_SEMAPHORES[limiter_param]=true;var spatialReference=new esri.SpatialReference({"wkid":response.crs.properties.code});for(i=0;i<response.features.length;i++){var geometry=response.features[i].geometry;var properties=response.features[i].properties;var id=response.features[i].id;var graphic=new esri.Graphic({});if(geometry.type==="Point"){graphic.setGeometry(new esri.geometry.Point(geometry.coordinates).setSpatialReference(spatialReference));}else if(geometry.type==="LineString"){graphic.setGeometry(new esri.geometry.Polyline({"paths":[geometry.coordinates]}).setSpatialReference(spatialReference));}else if(geometry.type==="Polygon"){graphic.setGeometry(new esri.geometry.Polygon({"rings":geometry.coordinates}).setSpatialReference(spatialReference));}
graphic.setAttributes(properties);graphic.id=id;if(infotemplate!==undefined){graphic.setInfoTemplate(infotemplate);}
if(map_layer!==undefined){map_layer.add(graphic);}
return_graphics.push(graphic);}
LAYER_ADD_SEMAPHORES[limiter_param]=false;return return_graphics;},"error":function(response,ioArgs){if(djConfig.debug){console.error("HTTP status code: ",ioArgs.xhr.status);}
return response;},"handle":function(response,ioArgs){if(callback_function!==undefined){callback_function({"response":response,"ioArgs":ioArgs});}}});}
function get_email(callback_function)
{dojo.xhrGet({"url":api_full_url+"/e-pehmo/email/","handleAs":"json","headers":{"Content-Type":"application/json","X-CSRFToken":"d883ffcec8da4ff7972a3c779aef30b7"},"load":function(response,ioArgs){if(callback_function!==undefined){callback_function(response);}
return response;},"error":function(response,ioArgs){if(djconfig.debug){console.error("HTTP status code: ",ioArgs.xhr.status);}
return response;}});}
function checkRegexp(o,regexp)
{}
function validate_email(email_address)
{}
function save_email(email_address,callback_function)
{dojo.xhrPost({"url":api_full_url+"/e-pehmo/email/","postData":encodeURIComponent(dojo.toJson(email_address)),"headers":{"Content-Type":"application/json","X-CSRFToken":"d883ffcec8da4ff7972a3c779aef30b7"},"failOk":true,"handle":function(response,ioArgs){if(callback_function!==undefined){callback_function({"status_code":ioArgs.xhr.status,"message":ioArgs.xhr.responseText});}}});}
function delete_email(callback)
{}
