/**
 * Features string for general use, with all the values set to "no".
 */
POPUP_FEATURES_DEFAULT = "toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no";

/**
 * TODO : for other needs, add here other feature strings. They should have the same "POPUP_FEATURES_" prefix,
 * and should be DOCUMENTED to explain the use we can do with it.
 * 
 */

/**
 * Opens a popup window, with optional features string. 
 * Caution : the features provided by the passed string must not contain width and height parameters,
 * 			 they are appended in the function.
 * 
 */
function OpenFeaturedPopup(url, name, width, height, features) {
	if (features == null) {
		features = "";
	}
	else {
		features += ","; 
	}
	features += "width=" + width + ",height=" + height;
	//alert("features : " + features)
    popupWindow = window.open(url, name, features);
}

/**
 * Closes the opened popup window, after putting the focus on the opener.
 * @param windowRef  WindowObject the reference of the popup to close.<br/>
 * 		Usually, the call of this function will be :
 * 		ClosePopup(window);
 */
function ClosePopup(windowRef) {
	var myOpener = windowRef.opener;
	if (myOpener != null) {
		myOpener.focus();
	}
	windowRef.close();
}

/*=======================================================================================*/
/*		Functions for prossessing form fields textual content. 							 */
/*=======================================================================================*/

/**
 * Replaces a set of characters in a string by another.
 * @param sString source string.
 * @param sReplaceThis sequence of chars to replace.
 * @param sWithThis replacing sequence.
 * @return a string with replaced occurences. 
 */
function ReplaceString(sString, sReplaceThis, sWithThis) { 
	if (sReplaceThis != "" && sReplaceThis != sWithThis) {
		var counter = 0;
		var start = 0;
		var before = "";
		var after = "";
		while (counter<sString.length) {
			start = sString.indexOf(sReplaceThis, counter);
			if (start == -1) {
			break;
			} else {
				before = sString.substr(0, start);
				after = sString.substr(start + sReplaceThis.length, sString.length);
				sString = before + sWithThis + after;
				counter = before.length + sWithThis.length;
			}
		}
	}
	return sString;
}

/**
 * Returns the collection of form fields that fit the
 * specified type.
 * @param formName the name of the form to iterate.
 * @param fieldType the type of the fields to extract.
 */
function GetFormElementByType(formName, fieldType) {
    var fields = new Array();
    var formToProcess = document.forms[formName];
    // Iterating over all form fields
    for ( var i = 0; i < formToProcess.elements.length; i++) {
        formField = formToProcess.elements[i];
        // Filtering by type
        if (formField.type == fieldType) {
            fields.push(formField);
        }
    }
    return fields;
}

/**
 * Iterates over all the textareas of the specified form, to replace 
 * the "\n" by the html "<br/>" carriage return.
 * @param formName the name of the form to iterate.
 * @param fieldList (optionnal). Collection of textareas to process. The elements
 			of the collection are the REFERENCES of the objects.
 *          If not provided, all the textareas will be processed.
 * @param callbackFunction (optionnal). Callback function to apply on the selected 
 *          form fields.
 * 			If not provided the default function is ReplaceCarriageReturn(sourceStr).
 * 
 * @see ReplaceCarriageReturn(sourceStr).
 */
function ProcessTextAreas(formName, fieldList, callbackFunction) {
   if (fieldList == null) { 
        // Processing all the textareas included in the form
        fieldList = GetFormElementByType(formName, "textarea");
    }    
    // Set the function to apply to the field
    if (callbackFunction == null) {
        callbackFunction = ReplaceCarriageReturn;
    }
    for ( var i = 0; i < fieldList.length; i++) {
        formField = fieldList[i];
        formField.value = callbackFunction(formField.value);
    }
}

function ProcessInputHidden(formName, fieldList, callbackFunction) {
   if (fieldList == null) { 
        // Processing all the textareas included in the form
        fieldList = GetFormElementByType(formName, "input");
    }    
    // Set the function to apply to the field
    if (callbackFunction == null) {
        callbackFunction = ReplaceCarriageReturn;
    }
    for ( var i = 0; i < fieldList.length; i++) {
        formField = fieldList[i];
        formField.value = callbackFunction(formField.value);
    }
}



/**
 * Replaces all the CRLF chars ("\n") by the HTML "<br/>" tag.
 * Uses the ReplaceString() function.
 * This is the default callback function for ProcessTextAreas function.
 * If you need to make some other text replacement, just write another 
 * function, that has the same signature, and pass it in the call : 
 * 			ProcessTextAreas(formName, fieldList, <<your-function-name>>)
 * @param sourceStr source string to parse and modify.
 * @see ReplaceString(sString, sReplaceThis, sWithThis).
 * @see ProcessTextAreas(formName, fieldList, callbackFunction)
 */
function ReplaceCarriageReturn(sourceStr) {
    return ReplaceString(sourceStr, "\n", "<br/>");
}






/**
 * Replaces all the CRLF chars ("\n") by the HTML "<br/>" tag.
 * Uses the ReplaceString() function.
 * This is the default callback function for ProcessTextAreas function.
 * If you need to make some other text replacement, just write another 
 * function, that has the same signature, and pass it in the call : 
 * 			ProcessTextAreas(formName, fieldList, <<your-function-name>>)
 * @param sourceStr source string to parse and modify.
 * @see ReplaceString(sString, sReplaceThis, sWithThis).
 * @see ProcessTextAreas(formName, fieldList, callbackFunction)
 */
function ReplaceForCrosslogSubmitCarriageReturn(sourceStr) {
    return ReplaceString(sourceStr, "\n", "\\n");
}





/**
 * Replaces all the CRLF chars ("\n") by the HTML "<br/>" tag.
 * Uses the ReplaceString() function.
 * This is the default callback function for ProcessTextAreas function.
 * If you need to make some other text replacement, just write another 
 * function, that has the same signature, and pass it in the call : 
 * 			ProcessTextAreas(formName, fieldList, <<your-function-name>>)
 * @param sourceStr source string to parse and modify.
 * @see ReplaceString(sString, sReplaceThis, sWithThis).
 * @see ProcessTextAreas(formName, fieldList, callbackFunction)
 */
function ReplaceForCrosslogLoadCarriageReturn(sourceStr) {
    return ReplaceString(sourceStr, "\\n", "\n");
}

/**
 *
 * Limite les textarea a une taille d?finie maxChars
 *
 */
function limiterTailleTextarea(textarea, maxChars)
{
	if(textarea.value.length <= maxChars) return;

	textarea.value = textarea.value.substr(0, maxChars)
}


/**
 * Fonction gérant le chargement et l'intégration des Flash en html (dans une balise div)
 *
 * Tous les paramètres sont de type : String
 * Paramètres obligatories :
 *  - sourceUrl: url du flash
 *  - objectName: name du flash
 *  - destId: id du div intégrant le flash
 *
 * Paramètres facultatifs :
 *  - width : largeur
 *  - height : hauteur
 *  - flashVersion (par défaut : "8")
 *  - backgroundColor (par défaut : "#ffffff")
 *  - alignObject 
 *
 * Exemple :
 * <div id="lamarqueFlash">
 * 	<script language="JavaScript" type="text/javascript">
 *		WriteFlashObject("swf/lamarque.swf", "lamarque", "lamarqueFlash", "723", "394", "8", "#ffffff", "middle");
 *	</script>
 * </div> 
 * Pour plus d'informations: http://blog.deconcept.com/swfobject/
 */
 
	function WriteFlashObject(sourceUrl, objectName, destId, width, height, flashVersion, backgroundColor, alignObject, flashvars) {

	// flashVersion (par défaut : "8")
	if (flashVersion == null){
		flashVersion = "8";
	}
	// backgroundColor (par défaut : "#ffffff")
	if (backgroundColor == null){
		backgroundColor = "#ffffff";
	}
	
		var so = new SWFObject(sourceUrl, objectName, width, height, flashVersion, backgroundColor);
		if(flashvars != null){
			so.addParam("FlashVars", flashvars);
		}
		if(alignObject != null){
			so.addParam("align", alignObject);
		}
		
		so.write(destId);
	}
