

function showFormSubmitError() {
	var content = $('#formError span').clone();
	
	
	// get the form legend
	$('#editLegend span').append(content);
	

	setTimeout("$('#editLegend span').html('')", 5000);
	
}

function showSavingData() {
	var content = $('#savingData span').clone();
	
	
	// get the form legend
	$('#editLegend span').append(content);
	

	

	// show our waiting graphic
	//$('#editLegend span').show();
	
}


function hideSavingData() {
	// get the form legend
	$('#editLegend span span').remove();
}

function hideSavingData2(id) {
	// get the form legend
	$('#' + id + ' span span').remove();
}


function showDataSaved() {
	var content = $('#dataSaved span').clone();
	
	
	// get the form legend
	$('#editLegend span').append(content);
	
	setTimeout("hideSavingData()", 5000);
}



function disableEditForm() {
	
	//$('#editorSubmitButton').attr("disabled", true); 
	$('#editorSubmitButton').css('visibility', 'hidden');

	
	$('#editRecordForm').fadeTo('normal', .3);
}


function enableEditForm() {
	
	var show_form = true;
	for (data in edit_content_loaded) {
		//if (edit_content_loaded.data != true) {
		if (edit_content_loaded[data] != true) {
			show_form = false;
		}
		
		//alert('edit_content_loaded[' + data + '] = ' + edit_content_loaded[data]);
	}
	
	//alert('show_form = ' + show_form);
	
	if (show_form) {
		//$('#editorSubmitButton').removeAttr("disabled");
		$('#editorSubmitButton').css('visibility', 'visible');

		$('#editRecordForm').fadeTo('normal', 1);	
	}
	
}





function GeneratePassword(node_id) {

	var length = 8;
	var sPassword = "";
	
	var salt = "abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ123456789-_";  // salt to select chars from

	for (i=0; i < length; i++) {

		/*numI = getRandomNum();

		sPassword = sPassword + String.fromCharCode(numI);*/
		
		var character = salt.substr( (getRandomNum() % salt.length) , 1);
		
		sPassword = sPassword + character;
	}
	
	var my_node = document.getElementById(node_id);
	my_node.value = sPassword;

	return true;
}



 function getRandomNum() {

 	// between 0 - 1
 	var rndNum = Math.random()

 	// rndNum from 0 - 1000
 	rndNum = parseInt(rndNum * 1000);

 	// rndNum from 33 - 127
 	rndNum = (rndNum % 94) + 33;

 	return rndNum;
}







// Script by hscripts.com
function validate_urlValue(str) {

	var is_valid = false;
	
	// loop the chars in the string
	for(var j=0; j<str.length; j++) {
		
		is_valid = true;	// we got here so the string is not empty
		
		var the_char = str.charAt(j);	// get the next char in the string array
		var char_code = the_char.charCodeAt(0);	// get the code for this char
		// check the value of the char code
		// we allow alpha, numbers, hyphen
		if ((char_code > 47 && char_code < 58) || (char_code > 64 && char_code < 91) || (char_code > 96 && char_code < 123) || (char_code == 45)) {
			// nothing		
		} else {
			//alert('char_code = ' + char_code);
			is_valid = false;
		}
	}
	
	
	return is_valid;
}


//------------------------------------------------------------------------------------------------------------------------------------------------------

function displayFormElementError(node_id, error_str) {
	//alert('displayFormElementError node_id = ' + node_id);
	
	//return;
	
	var node_parent = $('#' + node_id).parent();
	//alert('displayFormElementError node_parent = ' + node_parent);
	var info_node = false;
	
	
	// get all the child nodes for the parent
	var kids = $(node_parent).children();
	
	// loop the kids to see if one has the class we are looking for
	$(kids).each(
		function (i) {
			if ($(this).hasClass('formElementInfoDiv')) {
				info_node = this;
			}
		}
	);
	
	// if we did not find a kid node with the class we want, make one
	if (! info_node) {
		$(node_parent).append('<div class="formElementInfoDiv">This field did not validate.</div>');
		
		// we can get the last child of the parent because we just appended a new div, so we know its the right one
		/**
		 * 2009-08-27
		 * ok, i was wrong
		 * this did not work when the form element is a select menu
		 * the div get appended correctly
		 * however, the following select seems to grab an option from the select
		 * the result is that the error str passed becomes an option
		 * the user sees the default message added above
		 * i can live with for now
		 *
		 * we need to fugure out how to get a reference to the div just appended
		 */
		
		info_node = $(":last-child", node_parent)[0];
	}
	
	//alert('info_node = ' + info_node);
	
	if (info_node !== false) {
		$(info_node).html(error_str);
		
		// if the string is empty
		if (error_str == '') {
			$(info_node).hide();
		}
	}
}

//------------------------------------------------------------------------------------------------------------------------------------------------------

function limitChars(node, limit) {
	var text = $(node).val();
	var textlength = text.length;
	var node_parent = $(node).parent();
	var info_node = false;
	
	// get all the child nodes for the parent
	var kids = $(node_parent).children();
	
	// loop the kids to see if one has the class we are looking for
	$(kids).each(
		function (i) {
			if ($(this).hasClass('formElementInfoDiv')) {
				info_node = this;
			}
		}
	);
	
	// if we did not find a kid node with the class we want, make one
	if (! info_node) {
		$(node_parent).append('<div class="formElementInfoDiv">ss</div>');
		
		// we can get the last child of the parent because we just appended a new div, so we know its the right one
		info_node = $(":last-child", node_parent)[0];
	}
	 

	if (info_node !== false) {
		if (textlength > limit) {
			$(info_node).html('You cannot write more then '+limit+' characters!');
			
			// this truncates the contents of the element to the length
			$(node).val(text.substr(0,limit));
			return false;
		} else {
			// this line will show them a running total. i do not care for it
			//$(info_node).html('You have '+ (limit - textlength) +' characters left.');
			$(info_node).html('');
			return true;
		}
	}
}

//------------------------------------------------------------------------------------------------------------------------------------------------------


// from the man
// http://www.quirksmode.org/js/cookies.html

function createCookie(name,value,days) {

	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	} else {
		var expires = "";
	}
	
	document.cookie = name+"="+value+expires+"; path=/";
}

//------------------------------------------------------------------------------------------------------------------------------------------------------

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	
	return null;
}

//------------------------------------------------------------------------------------------------------------------------------------------------------

function eraseCookie(name) {
	createCookie(name,"",-1);
}

//------------------------------------------------------------------------------------------------------------------------------------------------------

var file_browser_callback_action = false;

function loadFckFileBrowser_articleFiles(){
	
	var sub_dir = 'article_files/' + selected_aID;
	
	file_browser_callback_action = 'download';
	
	loadFckFileBrowser(sub_dir);
}

//------------------------------------------------------------------------------------------------------------------------------------------------------

function loadFckFileBrowser(sub_dir){
	
	var domain = 'content';
	switch (domain_suffix) {
		case 'dev':
		case 'test':
			domain += domain_suffix;
			break;
	}
	
		//var url = 'http://' + domain + '.primecp.com/javascript/fckeditor/editor/filemanager/browser/default/browser.html?Connector=http%3A%2F%2F' + domain + '.primecp.com%2Fjavascript%2Ffckeditor%2Feditor%2Ffilemanager%2Fconnectors%2Fphp%2Fconnector.php&mis_dir=Felt';
		var url = 'http://' + domain + '.primecp.com/javascript/fckeditor/editor/filemanager/browser/default/browser.html?Connector=http%3A%2F%2F' + domain + '.primecp.com%2Fjavascript%2Ffckeditor%2Feditor%2Ffilemanager%2Fconnectors%2Fphp%2Fconnector.php';
		
		if(sub_dir !== undefined){
			url += '&mis_dir=' + sub_dir;
		}
		//alert(url);
		
		var sOptions = "toolbar=no,status=no,resizable=yes,dependent=yes,scrollbars=yes" ;
		sOptions += ",width=700px";
		sOptions += ",height=500px";
		sOptions += ",left=100px";
		sOptions += ",top=50px";
		
		window.open( url, 'FCKBrowseWindow', sOptions ) ;
	}

//------------------------------------------------------------------------------------------------------------------------------------------------------

/*
this function is used when opening fck file browser in standalone mode
when you click on a file on the server the path to the file is passed to this function
*/

function SetUrl(str){
	//alert('SetUrl str = ' + str);
	//alert('attr_image_upload_index = ' + attr_image_upload_index);
	
	// we might do one of a few things here
	// look for some vars to indicate what to do
	
	if(file_browser_callback_action){
		//alert('file_browser_callback_action: ' + file_browser_callback_action);
		
		switch(file_browser_callback_action){
			case 'download':
				window.open(str,'Download');
				break;
		}
		
		
		
		
		
	}else if (typeof attr_image_upload_index != 'undefined') {
		// this is used in the article admin
		// the user is browsing the server and adding an image to one of the article's type attributes
		//alert( 'typeof attr_image_upload_index: ' + typeof attr_image_upload_index );
		if (typeof attr_image_upload_index == 'number') {
			$('#value_' + attr_image_upload_index).val(str);
		}
	}else if(typeof image_upload_field_id != 'undefined'){
		
		//alert( 'typeof image_upload_field_id: ' + typeof image_upload_field_id );
		/**
		 *this is for the featured section admin
		 */
		$('#' + image_upload_field_id).val(str);
		updateImagePreview();
	}else{
		//alert('site_functions.js::SetUrl() does not know what to do with: ' + str);
	}
}

//------------------------------------------------------------------------------------------------------------------------------------------------------

function loadExternalFile(filename, filetype) {
    
    var fileref = false;
    
    switch(filetype) {
        case 'php':
        case 'js':
            fileref = document.createElement('script');
            fileref.setAttribute("type","text/javascript");
            fileref.setAttribute("src", filename);
            break;
        case 'css':
            fileref = document.createElement("link");
            fileref.setAttribute("rel", "stylesheet");
            fileref.setAttribute("type", "text/css");
            fileref.setAttribute("href", filename);
            break;
    }
    
    if (typeof fileref != false) {
        document.getElementsByTagName("head")[0].appendChild(fileref);
        alert('new js file loaded');
    }
}

//------------------------------------------------------------------------------------------------------------------------------------------------------








