/******************************************************************************
 *
 * Purpose: common js function for pmapper plugins
 * Author:  Thomas Raffin, SIRAP
 *
 ******************************************************************************
 *
 * Copyright (c) 2008 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.
 *
 * This 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
 *
 ******************************************************************************/

function redirectPage(urlin) {
	var url = urlin.replace(SID, "");
	url = url.replace("&&", "&");
	url = url.replace(/&$/, "");
	var key = SID.replace(/=.*/, "");
	var val = SID.replace(/.*=/, "");
	var redirectformstr = "<form id='redirectform' action='" + url + "' method='post'><input name='" + key + "' type='hidden' value='" + val + "'/></form>"
	document.body.innerHTML += redirectformstr;
	document.getElementById("redirectform").submit(); 
}

// typewin can be the id of the element like "frame" (with "#")
function openAjaxQueryIn(typewin, url, dlgTitle, dlgwidth, dlgheight) {
	if (typewin == 'window') {
		if (url.pos('?') == 0) {
			url += '?';
		} else {
			url += '&';
		}
		url += 'addjsandcss=true';
		openResultwin(url);
	} else if (typewin == 'dynwin') {
		var myheight = dlgheight ? dlgheight : 400;
		var mywidth = dlgwidth ? dlgwidth : 600;
		createDnRDlg({w:mywidth, h:myheight, l:80, t:100}, {resizeable:true, newsize:true}, 'pmDlgContainer', dlgTitle, url);
	} else {
	    $.ajax({
	        url: url,
	        dataType: "html",
	        success: function(response){
		        if (typewin == 'frame') {
					$("#infoFrame").html(response);
				} else if (typewin[0] == '#') { 
					$(typewin).html(response);
				}
			}
		});  
	} 
}

function pjs(str) {
	return localeList[str] ? localeList[str] : str; 
}

/**
 * Convert HEXA color to RGB 
 */
function convertHexToRGB(hexColor){
    var r = HexToR(hexColor);
    var g = HexToG(hexColor);
    var b = HexToB(hexColor);
    var rgb = r.toString() + ", " + g.toString() + ", " + b.toString(); 

    return rgb;
}
function HexToR(h) {
	return parseInt((cutHex(h)).substring(0,2),16)
} 
function HexToG(h) {
	return parseInt((cutHex(h)).substring(2,4),16)
} 
function HexToB(h) {
	return parseInt((cutHex(h)).substring(4,6),16)
} 
function cutHex(h) {
	return (h.charAt(0)=="#") ? h.substring(1,7):h
}  

/**
 * Convert RGB color to HEXA 
 */
function convertRgbToHex(num) {
	var decToHex="";
	var arr = new Array();
	var numStr = new String();
	numStr = num;

	arr = numStr.split(",");

	for(var i=0;i<3;i++){
		var hexArray = new Array( "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F" );
		var code1 = Math.floor(arr[i] / 16);
		var code2 = arr[i] - code1 * 16;
		decToHex += hexArray[code1];
		decToHex += hexArray[code2];
	}
	return (decToHex);
}	

function generateColor(iClass, nbClass, hexColor1, hexColor2) {
/*	
	var r = Math.round(Math.random()*255);
	var g = Math.round(Math.random()*255);
	var b = Math.round(Math.random()*255);
    
	var colorRGB = r + "," + g + "," + b;
*/
	var r1 = HexToR(hexColor1);
    var g1 = HexToG(hexColor1);
    var b1 = HexToB(hexColor1);
	var r2 = HexToR(hexColor2);
    var g2 = HexToG(hexColor2);
    var b2 = HexToB(hexColor2);
	var nb = (nbClass > 1) ? nbClass - 1 : 1;
	var rOffset = Math.round((r2 - r1)/nb);
	var gOffset = Math.round((g2 - g1)/nb);
	var bOffset = Math.round((b2 - b1)/nb);
	var r = Math.max(Math.min(255, r1 + iClass * rOffset),0);
	var g = Math.max(Math.min(255, g1 + iClass * gOffset),0);
	var b = Math.max(Math.min(255, b1 + iClass * bOffset),0);

	var hexaColor = convertRgbToHex(r + ',' + g + ',' + b);
    
	return hexaColor;
}
