/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is DotClear Weblog.
 *
 * The Initial Developer of the Original Code is
 * Olivier Meunier.
 * Portions created by the Initial Developer are Copyright (C) 2003
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */
//alert('test');
function catChangePath(idArray)
{
    if (document.formPost.c_path.value == '') {
        document.formPost.c_path.value = idArray[document.formPost.c_parentid.value];
    }
}

function setUrl(titleField, urlField, param, param2)
{
    url   = document.getElementById(urlField);
    title = document.getElementById(titleField);
    if (param == 'cat') {
        if (document.getElementById('quirk_category_path').value == 0) {
            urlFromTitle = titleToUrl(title.value,'cat');
            url.value = param2[document.formPost.c_parentid.value] + urlFromTitle + '/';
        }        
    } else {
        if (param2 == 0) {
            url.value = titleToUrl(title.value,'art');
        }                            
    }
}

function titleToUrl(string, type)
{
    if (type == 'art') {
        return string.replace(/([^a-z0-9 ])/ig, replaceFunc).replace(/[^a-z0-9 ]/ig, 
        '').replace(/ /g, '-').replace(/^([0-9]+)/ig, 
        '').replace(/([\-]+)$/ig, '').replace(/([\-]+)/ig, '-').replace(/([0-9\-]+)$/ig, '').replace(/([\-]+)$/ig, '').replace(/^([\-]+)/ig, '');
    } else {
        return string.replace(/([^a-z0-9 ])/ig, replaceFunc).replace(/[^a-z0-9 ]/ig,
        '').replace(/ /g, '-').replace(/([\-]+)/ig, '-').replace(/([\-]+)$/ig, '').replace(/^([\-]+)/ig, '');
    }
}

/** str: matched substring
    p1: parenthetical match
    offset: offset within the string
    s: string itself
    */
function replaceFunc(str, p1, offset, s)
{
    notclean = new String(unescape("%C0%C1%C2%C3%C4%C5%E0%E1%E2%E3%E4%E5%D2%D3%D4%D5%D6%D8%F2%F3%F4%F5%F6%F8%C8%C9%CA%CB%E8%E9%EA%EB%CC%CD%CE%CF%EC%ED%EE%EF%D9%DA%DB%DC%F9%FA%FB%FC%FF%D1%F1%E7"));
    clean    = new String("AAAAAAaaaaaaOOOOOOooooooEEEEeeeeIIIIiiiiUUUUuuuuyNnc");
    idx = notclean.indexOf(str);
    if (idx != -1) {
        return clean.charAt(idx);
    } else {    
        return ' ';
    }
}

function openClose(id,mode)
{
//alert(id);
	if(document.getElementById) {
		element = document.getElementById(id);
		img = document.getElementById('img_' + id);
		//img="img_content1";
	} else if(document.all) {
		element = document.all[id];
		img = document.all['img_' + id];
		//img="img_content1";
	} else return;

	if(element.style) {
		if(mode == 0) {
			if(element.style.display == 'block' ) {
				element.style.display = 'none';
				img.src = '/xmedia/theme/default/plus.png';
			} else {
				element.style.display = 'block';
				img.src = '/xmedia/theme/default/minus.png';
			}
		} else if(mode == 1) {
			element.style.display = 'block';
			img.src = '/xmedia/theme/default/minus.png';
		} else if(mode == -1) {
			element.style.display = 'none';
			img.src = '/xmedia/theme/default/plus.png';
		}
	}
}

function openCloseSpan(id,mode)
{
	if(document.getElementById) {
		element = document.getElementById(id);
	} else if(document.all) {
		element = document.all[id];
	} else return;

	if(element.style) {
		if(mode == 0) {
			if(element.style.display == 'inline' ) {
				element.style.display = 'none';
			} else {
				element.style.display = 'inline';
			}
		} else if(mode == 1) {
			element.style.display = 'inline';
		} else if(mode == -1) {
			element.style.display = 'none';
		}
	}
}

function openCloseBlockIf(id, idsource, cond, mode, modeelse)
{
	if(document.getElementById) {
		element = document.getElementById(id);
        sourceel = document.getElementById(idsource);
	} else if(document.all) {
		element = document.all[id];
        sourceel = document.all[idsource];
	} else return;
    
    if (sourceel.value == cond) {
        openCloseBlock(id,mode);
    } else {
        openCloseBlock(id,modeelse);
    }
}

// function in_array(needle,haystack) 
// craig heydenburg 4/8/02 
// this function is similar to the PHP function of the same name 
function in_array(value, array) 
{ 
    var bool = false; 
    for (var i=0; i<array.length; i++) { 
        if (array[i]== value) { bool=true; } 
    } 
    return bool; 
}

function openCloseBlockIfArray(id, idsource, cond, mode, modeelse)
{
	if(document.getElementById) {
		element = document.getElementById(id);
        sourceel = document.getElementById(idsource);
	} else if(document.all) {
		element = document.all[id];
        sourceel = document.all[idsource];
	} else return;
    
    if (in_array(sourceel.value,cond)) {
        openCloseBlock(id,mode);
    } else {
        openCloseBlock(id,modeelse);
    }

}

function openCloseBlock(id,mode)
{
	if(document.getElementById) {
		element = document.getElementById(id);
	} else if(document.all) {
		element = document.all[id];
	} else return;

	if(element.style) {
		if(mode == 0) {
			if(element.style.display == 'block' ) {
				element.style.display = 'none';
			} else {
				element.style.display = 'block';
			}
		} else if(mode == 1) {
			element.style.display = 'block';
		} else if(mode == -1) {
			element.style.display = 'none';
		}
	}
}

function mOpenClose(idArray,mode)
{
	for(var i=0;i<idArray.length;i++)
	{
		openClose(idArray[i],mode);
	}
}

function popup(url)
{
	top.win=window.open(url,'dc_popup',
	'alwaysRaised=yes,toolbar=no,height=450,width=480,menubar=no,resizable=yes,scrollbars=yes,status=no');
}

function insertTextIn(formObj,text)
{
	formObj.value += text;
}

function insertImage(origine,url,act,text)
{
	form = origine.forms['formPost'];
    
    if (form.n_content_format)           format = form.n_content_format.value;
    else if (form.a_description_format)  format = form.a_description_format.value;
    else if (form.a_page_content_format) format = form.a_page_content_format.value;
    else if (form.c_format)              format = form.c_format.value;

	title = window.prompt(text);
	
	if (act == 'img') {
    	if (format == 'wiki')
    	{
    		if (title != '') {
    			image = '(('+url+'|'+title+'))';
    		} else {
    			image = '(('+url+'))';
    		}
    	}
    	else
    	{
    		image = '<p><img src="'+url+'" alt="'+title+'" /></p>';
    	}
	} else {
    	if (format == 'wiki')
    	{
    		if (title != '') {
    			image = '['+title+'|'+url+']';
    		} else {
    			image = '['+url+']';
    		}
    	}
    	else
    	{
    		image = '<p><a href="'+url+'">'+title+'</a></p>';
    	}	
	
	}
	
	image = "\n\n"+image;

    if (form.n_content)           form.n_content.value += image;
    else if (form.a_description)  form.a_description.value += image;
    else if (form.a_page_content) form.a_page_content.value += image;
    else if (form.c_description)  form.c_description.value += image;

}

function isFilled(elm) {
    if (elm.value == "" ||
        elm.value == null) 
    return false;
    else return true;
}
function isReady(name, text) {
	arr = document.getElementsByName(name);
	inpt = arr.item(0);
    if (isFilled(inpt) == false) {
	    alert (text);
		inpt.focus();
    return false;
    }
return true;	
}	
