/*
 COPYRIGHT 2009 ESRI

 TRADE SECRETS: ESRI PROPRIETARY AND CONFIDENTIAL
 Unpublished material - all rights reserved under the
 Copyright Laws of the United States and applicable international
 laws, treaties, and conventions.

 For additional information, contact:
 Environmental Systems Research Institute, Inc.
 Attn: Contracts and Legal Services Department
 380 New York Street
 Redlands, California, 92373
 USA

 email: contracts@esri.com
 */
//>>built
define("esri/arcgis/csv",["dojo/_base/lang","dojo/_base/array","dojo/_base/Deferred","dojo/sniff","dojo/number","dojox/data/CsvStore","esri/kernel","esri/config","esri/request","esri/SpatialReference","esri/geometry/Point","esri/geometry/jsonUtils","esri/geometry/webMercatorUtils"],function(_1,_2,_3,_4,_5,_6,_7,_8,_9,_a,_b,_c,_d){var _e=["lat","latitude","y","ycenter","latitude83","latdecdeg","POINT-Y"],_f=["lon","lng","long","longitude","x","xcenter","longitude83","longdecdeg","POINT-X"];function _10(_11){var _12=[","," ",";","|","\t"];var _13=0;var _14="";_2.forEach(_12,function(_15){var _16=_11.split(_15).length;if(_16>_13){_13=_16;_14=_15;}});return _14;};function _17(d,_18){if(!d||Object.prototype.toString.call(d)!=="[object Date]"||isNaN(d.getTime())){return false;}var _19=true;if(_4("chrome")&&/\d+\W*$/.test(_18)){var _1a=_18.match(/[a-zA-Z]{2,}/);if(_1a){var _1b=false,i=0,len=_1a.length,_1c=/^((jan(uary)?)|(feb(ruary)?)|(mar(ch)?)|(apr(il)?)|(may)|(jun(e)?)|(jul(y)?)|(aug(ust)?)|(sep(tember)?)|(oct(ober)?)|(nov(ember)?)|(dec(ember)?)|(am)|(pm)|(gmt)|(utc))$/i;while(!_1b&&(i<=len)&&!(_1b=!_1c.test(_1a[i]))){i++;}_19=!_1b;}}return _19;};function _1d(_1e,_1f,_20){var _21=_1e.indexOf("\n");var _22=_1.trim(_1e.substr(0,_21));var _23=_1f.columnDelimiter;if(!_23){_23=_10(_22);}var _24=new _6({data:_1e,separator:_23});var _25=(_4("ie")<9)?750:1001;_24.fetch({start:0,count:_25,onComplete:function(_26,_27){var _28=0;var _29={"layerDefinition":_1f.layerDefinition,"featureSet":{"features":[],"geometryType":"esriGeometryPoint"}};var _2a=_29.layerDefinition.objectIdField;if(!_2a){if(!_2.some(_29.layerDefinition.fields,function(_2b){if(_2b.type=="esriFieldTypeOID"){_2a=_2b.name;return true;}return false;})){_29.layerDefinition.fields.push({"name":"__OBJECTID","alias":"__OBJECTID","type":"esriFieldTypeOID","editable":false,"domain":null});_2a="__OBJECTID";}}var _2c,_2d;var _2e=_24._attributes;var _2f=[];var _30=[];_2.forEach(_29.layerDefinition.fields,function(_31,_32){if(_31.type==="esriFieldTypeDate"){_2f.push(_31.name);}else{if(_31.type==="esriFieldTypeDouble"||_31.type==="esriFieldTypeInteger"){_30.push(_31.name);}}});if(_1f.locationInfo&&_1f.locationInfo.locationType==="coordinates"){_2c=_1f.locationInfo.latitudeFieldName;_2d=_1f.locationInfo.longitudeFieldName;}else{_2.forEach(_2e,function(_33){var _34;_34=_2.indexOf(_e,_33.toLowerCase());if(_34!==-1){_2c=_33;}_34=_2.indexOf(_f,_33.toLowerCase());if(_34!==-1){_2d=_33;}},this);}if(!_2c||!_2d){setTimeout(function(){console.error("File does not seem to contain fields with point coordinates.");},1);return;}var i=0,il=_26.length;for(i;i<il;i++){if(_29.featureSet.features.length>=1000){setTimeout(function(){console.error("1000 feature limit reached. Unable to load any more data.");},1);break;}var _35=_26[i];var _36=_24.getAttributes(_35),_37={};_2.forEach(_36,function(_38,_39){if(_38){var _3a=_38;if(_38.length===0){_2.forEach(_29.layerDefinition.fields,function(_3b,idx){if(_3b.name==="attribute_"+(idx-1)){_38="attribute_"+(idx-1);}});}if(_2.some(_2f,function(a){return a===_38;})){var val=_24.getValue(_35,_3a),_3c=new Date(val);_37[_38]=_17(_3c,val)?_3c.getTime():null;}else{if(_2.some(_30,function(a){return a===_38;})){var _3d=_5.parse(_24.getValue(_35,_3a));if((_38==_2c||_38==_2d)&&(isNaN(_3d)||Math.abs(_3d)>181)){_3d=parseFloat(_24.getValue(_35,_3a));if(isNaN(_3d)){_37[_38]=null;}else{_37[_38]=_3d;}}else{if(isNaN(_3d)){_37[_38]=null;}else{_37[_38]=_3d;}}}else{_37[_38]=_24.getValue(_35,_3a);}}}});_37[_2a]=_28;_28++;var _3e=_37[_2c];var _3f=_37[_2d];if(_3f==null||_3e==null||isNaN(_3e)||isNaN(_3f)){continue;}var _40=new _b(_3f,_3e,new _a({wkid:4326}));var _41={"geometry":_40.toJson(),"attributes":_37};_29.featureSet.features.push(_41);}_29.layerDefinition.name="csv";if(_20){_20(_29);}},onError:function(_42){console.error("Error fetching items from CSV store: ",_42);}});return true;};function _43(_44,_45,_46,_47,_48,_49){if(_44.length===0){_48(null);}var _4a=_c.getGeometryType(_45);var _4b=[];_2.forEach(_44,function(_4c){var _4d=new _4a(_4c);_4d.spatialReference=_46;_4b.push(_4d);},this);var _4e=[102113,102100,3857];if(_46.wkid&&_46.wkid===4326&&_47.wkid&&_2.indexOf(_4e,_47.wkid)>-1){_2.forEach(_4b,function(_4f){if(_4f.xmin){_4f.xmin=Math.max(_4f.xmin,-180);_4f.xmax=Math.min(_4f.xmax,180);_4f.ymin=Math.max(_4f.ymin,-89.99);_4f.ymax=Math.min(_4f.ymax,89.99);}else{if(_4f.rings){_2.forEach(_4f.rings,function(_50){_2.forEach(_50,function(_51){_51[0]=Math.min(Math.max(_51[0],-180),180);_51[1]=Math.min(Math.max(_51[1],-89.99),89.99);},this);},this);}else{if(_4f.paths){_2.forEach(_4f.paths,function(_52){_2.forEach(_52,function(_53){_53[0]=Math.min(Math.max(_53[0],-180),180);_53[1]=Math.min(Math.max(_53[1],-89.99),89.99);},this);},this);}else{if(_4f.x){_4f.x=Math.min(Math.max(_4f.x,-180),180);_4f.y=Math.min(Math.max(_4f.y,-89.99),89.99);}}}}},this);_44=[];_2.forEach(_4b,function(_54){var _55=_d.geographicToWebMercator(_54);if(_47.wkid!==102100){_55.spatialReference=_47;}_44.push(_55.toJson());},this);_48(_44);}else{if(_46.wkid!==null&&_2.indexOf(_4e,_46.wkid)>-1&&_47.wkid!==null&&_47.wkid===4326){_44=[];_2.forEach(_4b,function(_56){_44.push(_d.webMercatorToGeographic(_56).toJson());},this);_48(_44);}else{var _57=function(_58,_59){if(_58&&_58.length===_44.length){_44=[];_2.forEach(_58,function(_5a){if(_5a&&((_5a.rings&&_5a.rings.length>0&&_5a.rings[0].length>0&&_5a.rings[0][0].length>0&&!isNaN(_5a.rings[0][0][0])&&!isNaN(_5a.rings[0][0][1]))||(_5a.paths&&_5a.paths.length>0&&_5a.paths[0].length>0&&_5a.paths[0][0].length>0&&!isNaN(_5a.paths[0][0][0])&&!isNaN(_5a.paths[0][0][1]))||(_5a.xmin&&!isNaN(_5a.xmin)&&_5a.ymin&&!isNaN(_5a.ymin))||(_5a.x&&!isNaN(_5a.x)&&_5a.y&&!isNaN(_5a.y)))){_44.push(_5a.toJson());}else{_44.push(null);}},this);_48(_44);}else{_49(_58,_59);}};if(_8.defaults.geometryService){_8.defaults.geometryService.project(_4b,_47,_1.hitch(this,_57),_49);}else{_48(null);}}}};function _5b(sp1,sp2){var _5c=[102113,102100,3857];if(sp1&&sp2&&sp1.wkid===sp2.wkid&&sp1.wkt===sp2.wkt){return true;}else{if(sp1&&sp2&&sp1.wkid&&sp2.wkid&&_2.indexOf(_5c,sp1.wkid)>-1&&_2.indexOf(_5c,sp2.wkid)>-1){return true;}}return false;};function _5d(_5e,_5f,_60,_61){if(!_5e.featureSet||_5e.featureSet.length===0){return;}if(_5b(_60,_5f)){_61(_5e);return;}var _62=function(_63){var _64=[];_2.forEach(_5e.featureSet.features,function(_65,i){if(_63[i]){_65.geometry=_63[i];_64.push(_65);}},this);_61(_5e);};var _66=function(_67,_68){console.error("error projecting featureSet ("+_5e.layerDefinition.name+"). Final try.");_61(_5e);};var _69=function(_6a,_6b){console.error("error projecting featureSet ("+_5e.layerDefinition.name+"). Try one more time.");_43(_6c,_5e.featureSet.geometryType,_5f,_60,_1.hitch(this,_62),_1.hitch(this,_66));};if(_5e.featureSet.features&&_5e.featureSet.features.length>0){var _6c=[];_2.forEach(_5e.featureSet.features,function(_6d){_6c.push(_6d.geometry);});_43(_6c,_5e.featureSet.geometryType,_5f,_60,_1.hitch(this,_62),_1.hitch(this,_69));}else{_61(_5e);}};function _6e(_6f){var _70=new _3();var _71=function(_72){_70.callback(_72);};var _73=_9({url:_6f.url,handleAs:"text",load:function(_74){_1d(_74,_6f,_1.hitch(this,_71));},error:function(_75){console.error("error: "+_75);}},{usePost:false});return _70;};function _76(_77,_78){var _79=new _3();var _7a=function(_7b){_79.callback(_7b);};_5d(_77,new _a({wkid:4326}),_78,_1.hitch(this,_7a));return _79;};function _7c(_7d){var _7e=_7d.layerDefinition.fields;var _7f={"esriFieldTypeDouble":1,"esriFieldTypeSingle":1};var _80={"esriFieldTypeInteger":1,"esriFieldTypeSmallInteger":1};var dt={"esriFieldTypeDate":1};var _81=null;var _82=_2.map(_7e,_1.hitch(this,function(_83){if(_83.name.toUpperCase()==="NAME"){_81=_83.name;}var _84=(_83.type!=="esriFieldTypeOID"&&_83.type!=="esriFieldTypeGlobalID"&&_83.type!=="esriFieldTypeGeometry");var _85=null;if(_84){var f=_83.name.toLowerCase();var _86=",stretched value,fnode_,tnode_,lpoly_,rpoly_,poly_,subclass,subclass_,rings_ok,rings_nok,";if(_86.indexOf(","+f+",")>-1||f.indexOf("area")>-1||f.indexOf("length")>-1||f.indexOf("shape")>-1||f.indexOf("perimeter")>-1||f.indexOf("objectid")>-1||f.indexOf("_")===f.length-1||(f.indexOf("_i")===f.length-2&&f.length>1)){_84=false;}if(_83.type in _80){_85={places:0,digitSeparator:true};}else{if(_83.type in _7f){_85={places:2,digitSeparator:true};}else{if(_83.type in dt){_85={dateFormat:"shortDateShortTime"};}}}}return _1.mixin({},{fieldName:_83.name,label:_83.alias,isEditable:true,tooltip:"",visible:_84,format:_85,stringFieldOption:"textbox"});}));var _87={title:_81?"{"+_81+"}":"",fieldInfos:_82,description:null,showAttachments:false,mediaInfos:[]};return _87;};var csv={latFieldStrings:_e,longFieldStrings:_f,buildCSVFeatureCollection:_6e,projectFeatureCollection:_76,generateDefaultPopupInfo:_7c,_getSeparator:_10,_isValidDate:_17,_processCsvData:_1d,_projectGeometries:_43,_sameSpatialReference:_5b,_projectFeatureSet:_5d};if(_4("extend-esri")){_1.setObject("arcgis.csv",csv,_7);}return csv;});