/*
 * FCKeditor - The text editor for internet
 * Copyright (C) 2003-2005 Frederico Caldeira Knabben
 *
 * Licensed under the terms of the GNU Lesser General Public License:
 * 		http://www.opensource.org/licenses/lgpl-license.php
 *
 * For further information visit:
 * 		http://www.fckeditor.net/
 *
 * "Support Open Source software. What about a donation today?"
 *
 * File Name: fck_image.js
 * 	Scripts related to the Image dialog window (see fck_image.html).
 *
 * File Authors:
 * 		Frederico Caldeira Knabben (fredck@fckeditor.net)
 *
 * Modified by SWsoft Inc.:
 * 	has been removed some functionality: resize, URL and etc.
 */

var oEditor		= window.parent.InnerDialogLoaded() ;
var FCK			= oEditor.FCK ;
var FCKLang		= oEditor.FCKLang ;
var FCKConfig	= oEditor.FCKConfig ;

var oImage = FCK.Selection.GetSelectedElement();

function ImageUrlSetter () {
	this.set = function(urlStr) {
		GetE('txtUrl').value = urlStr;
		// GetE('txtUploadFile').value = '';
	}
}
var ImageUrl = new ImageUrlSetter();

window.onload = function() {
	oEditor.FCKLanguageManager.TranslatePage(document);
	LoadSelection();
	if (FCKConfig.ImageUpload) {
		GetE('frmUpload').action = FCKConfig.ImageUploadURL;
	}
	window.parent.SetOkButton(true);
}

function LoadSelection() {
	if (!oImage) {
		return ;
	}
	var sUrl = GetAttribute(oImage, '_fcksavedurl', '');
	if (sUrl.length==0) {
		sUrl = GetAttribute(oImage, 'src', '');
	}
	GetE('txtUrl').value = sUrl;
	GetE('txtAlt').value = GetAttribute(oImage, 'alt', '');
}

//#### The OK button was hit.
function Ok() {
	if (GetE('txtUrl').value.length == 0) {
		alert('Please, upload image to server.');
		return false;
	}
	var bHasImage = (oImage!=null);
	if (!bHasImage) {
		oImage = FCK.CreateElement('IMG') ;
	} else {
		oEditor.FCKUndo.SaveUndoStep() ;
	}
	UpdateImage(oImage);
	oEditor.FCKUndo.SaveUndoStep();
	return true;
}

function UpdateImage(e, skipId) {
	e.src = GetE('txtUrl').value;
	SetAttribute( e, "_fcksavedurl", GetE('txtUrl').value);
	SetAttribute( e, "alt" , GetE('txtAlt').value);
}

function OnUploadCompleted(errorNumber, fileUrl, fileName, customMsg) {
	switch (errorNumber) {
		case 0:	// No errors
			alert('Your file has been successfully uploaded');
			break;
		case 1:	// Custom error
			alert(customMsg);
			return;
		case 101:	// Custom warning
			alert(customMsg);
			break;
		case 201:
			alert('A file with the same name is already available. The uploaded file has been renamed to "' + fileName + '"');
			break;
		case 202:
			alert('Invalid file type');
			return;
		case 203:
			alert("Security error. You probably don't have enough permissions to upload. Please check your server.");
			return;
		default:
			alert('Error on file upload. Error number: ' + errorNumber);
			return;
	}
	GetE('frmUpload').reset();
}

var oUploadAllowedExtRegex	= new RegExp(FCKConfig.ImageUploadAllowedExtensions, 'i');
var oUploadDeniedExtRegex	= new RegExp(FCKConfig.ImageUploadDeniedExtensions, 'i');
function CheckUpload() {
	var sFile = GetE('txtUploadFile').value;
	if (sFile.length == 0) {
		alert('Please select a file to upload');
		return false;
	}
	if (	(FCKConfig.ImageUploadAllowedExtensions.length > 0 && !oUploadAllowedExtRegex.test(sFile))
		||	(FCKConfig.ImageUploadDeniedExtensions.length > 0 && oUploadDeniedExtRegex.test( sFile ))) {
		OnUploadCompleted(202);
		return false;
	}
	return true;
}
