/******************************************************************************
 *
 * Purpose: Secureopeningdoc plugin
 * Author:  Jaouad Bennasser, SIRAP
 *
 ******************************************************************************
 *
 * Copyright (c) 2009 SIRAP
 *
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version. See the COPYING file.
 *
 * The software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with p.mapper; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 ******************************************************************************/

$.extend(PM.Plugin,
	{
		Secureopeningdoc:
	    {

			openDocument: function(layerName, fldName, fldValue) {

				var key = SID.replace(/=.*/, "");
				var val = SID.replace(/.*=/, "");
				
				var url = "";
			    var baseLoc = location.href.split(/\?/)[0];
			    baseLoc = baseLoc.split(/\/\//);
			    url = location.protocol + '//';
			    var patharray = baseLoc[1].split(/\//);
				var pathlen = patharray.length;
				//test if last element of patharray is empty.	
				if (patharray[patharray.length - 1] == '') {
					pathlen--;
				}
				//test if last element of patharray is a ".phtml" file (something like "index.phtml" or "map.phtml" ... )	    
				if (patharray[patharray.length - 1].lastIndexOf(".phtml") != -1) {
					pathlen--;
				}
				for (var i = 0 ; i < pathlen ; i++) {
					url += patharray[i] + '/';
				}
				
			    url += 'plugins/sirap_secureopeningdoc/secureopeningdoc.php';
				
			    var formstr = "";
				formstr += "<form action='" + url + "' id='sirap-secureopeningdocform' target='sirap-secureopeningdocform' onsubmit='window.open(\"\",this.target);return true;' method='post'>";
			    formstr += "<input name='layerName' type='hidden' value='" + layerName + "'/>";
				formstr += "<input name='fldName' type='hidden' value='" + fldName + "'/>";
				formstr += "<input name='fldValue' type='hidden' value='" + fldValue + "'/>";
				formstr += "<input name='" + key + "' type='hidden' value='" + val + "'/>";
				formstr += "<input name='preparemode' type='hidden' value='1' />";
				formstr += "</form>";

				$('#sirap-secureopeningdocform').remove();
				$('body').append(formstr);
				$('#sirap-secureopeningdocform').submit();
			}

	    }
	}
);
			
