var imageShown = false;

function doEmailCheck(field, tdiv){
	//checks if contents of a field is a valid email - if it is will make text green, if not red
	tdiv = document.getElementById(tdiv);
	if(isValidEmail(field.value)){
		try{
		tdiv.innerHTML = "";		
		field.style.color = "Green";
		field.style.borderColor = "";
		}catch(err){
			//do nothing
		}
		return true;
	}else{
		try{
		tdiv.innerHTML = "Please enter a valid email";
		field.style.borderColor = "#FF0000";
		field.style.color = "Red";
		}catch(err){
			//do nothing
		}
		return false;
	}
}

function explodeArray(item,delimiter){
	tempArray=new Array(1);
	var Count=0;
	var tempString=new String(item);

	while (tempString.indexOf(delimiter)>0)
	{
	tempArray[Count]=tempString.substr(0,tempString.indexOf(delimiter));
	tempString=tempString.substr(tempString.indexOf(delimiter)+1,tempString.length-tempString.indexOf(delimiter)+1);
	Count=Count+1
	}
	
	tempArray[Count]=tempString;
	return tempArray;
}

function fnBeforePaste(){
	var swapCodes   = new Array(8211, 8212, 8216, 8217, 8220, 8221, 8226, 8230); // dec codes from char at
	var swapStrings = new Array("--", "--", "'",  "'",  '"',  '"',  "*",  "...");  
	// debug for new codes
    // for (i = 0; i < input.length; i++)  alert("'" + input.charAt(i) + "': " + input.charCodeAt(i));
    
    var output = window.clipboardData.getData("Text");
    for (i = 0; i < swapCodes.length; i++) {
        var swapper = new RegExp("\\u" + swapCodes[i].toString(16), "g"); // hex codes
        output = output.replace(swapper, swapStrings[i]);
    }
    window.clipboardData.setData("Text", output);
    return true;
}

function cleanWordClipboard(obj) {
	var swapCodes   = new Array(8211, 8212, 8216, 8217, 8220, 8221, 8226, 8230); // dec codes from char at
	var swapStrings = new Array("--", "--", "'",  "'",  '"',  '"',  "*",  "...");  
	// debug for new codes
    // for (i = 0; i < input.length; i++)  alert("'" + input.charAt(i) + "': " + input.charCodeAt(i));
    
    var output = obj.value;
    for (i = 0; i < swapCodes.length; i++) {
        var swapper = new RegExp("\\u" + swapCodes[i].toString(16), "g"); // hex codes
        output = output.replace(swapper, swapStrings[i]);
    }
    return obj.value = output;
}



function isPhoneNumber(phone){
	regphone = /^[0-9]{3}-[0-9]{3}-[0-9]{4}$/i;
	return regphone.test(phone);
}

function format_phone(field_id){
	// Attempt to auto-format basic US phone numbers.  This method supports
	// 7 and 10 digit numbers.  Example: (410) 555-1212
	// Get the field that fired the event
	var oField = document.getElementById(field_id);
	// If we have the field and all is well
	if (typeof(oField) != "undefined" && oField != null){
		// Remove any non-numeric characters
		var sTmp = oField.value.replace(/[^0-9]/g, "");
		// If the number is a length we expect and support, format the number
		if(sTmp.length<10){
			document.getElementById(field_id + "err").innerHTML = "Invalid Phone";
			return;
		}
		//strip any preceeding 1's
		if(sTmp.indexOf('1')==0){
			sTmp = sTmp.substr(1);
		}
		switch (sTmp.length){
			/*case "14105551212".length:
				oField.value = sTmp.substr(0,1) + "(" + sTmp.substr(1, 3) + ") " + sTmp.substr(4, 3) + "-" + sTmp.substr(7, 4);
				document.getElementById(field_id + "err").innerHTML = "";
				break;*/
			case "4105551212".length:
				oField.value = "(" + sTmp.substr(0, 3) + ") " + sTmp.substr(3, 3) + "-" + sTmp.substr(6, 4);
				document.getElementById(field_id + "err").innerHTML = "";				
				break;
			default:
				oField.value = "(" + sTmp.substr(0, 3) + ") " + sTmp.substr(3, 3) + "-" + sTmp.substr(6, 4) + " ext " + sTmp.substr(10, sTmp.length-10);
				document.getElementById(field_id + "err").innerHTML = "";				
				break;
		}
	}
}


function isValidEmail(anEmail){
	//will check for valid email based on a passed value
	regEmail = /^[A-Za-z0-9\-_]+(\.[A-Za-z0-9\-_]+)*@([A-Za-z0-9\-_\.]+\.)+[A-Za-z]{2,3}$/i;
	return regEmail.test(anEmail);
}


function isValidPass(pass, cpass){
	if(pass.length<5){
		return false;
	}
	if(pass!=cpass){
		return false;
	}
	return true;
}

/*function checkPass(field, cfield, tdiv){
	
	alert("In checkPass!");
	
	tdiv = document.getElementById(tdiv)
	if(field.value.length<5){
		try{
		tdiv.innerHTML = "Password must be at least 5 characters";
		field.style.borderColor = "#FF0000";
		field.style.color = "Red";
		}catch(err){
			//do nothing
		}
	}else if(field.value != cfield.value){
		try{
		tdiv.innerHTML = "Confirmation password must match.";
		field.style.borderColor = "#FF0000";
		field.style.color = "Red";
		cfield.style.borderColor = "#FF0000";
		cfield.style.color = "Red";
		}catch(err){
			//do nothing
		}
	
	}else{
		try{
		tdiv.innerHTML = "";
		field.style.borderColor = "";
		field.style.color = "Black";
		cfield.style.borderColor = "";
		cfield.style.color = "Black";
		}catch(err){
			//do nothing
		}
	}
} */

function checkAll(form)
{
	//get checkboxed
	var cb = $(form).getInputs('checkbox');
	for (i = 0; i < cb.length; i++)
		cb[i].checked = true;
}

function uncheckAll(form)
{
	var cb = $(form).getInputs('checkbox');
	for (i = 0; i < cb.length; i++)
		cb[i].checked = false;
}


/**
WYSIWYG INIT FUNCTIONS
**/
function checkforwysiwyg(w, h){
	var result = $$('.mceEditor');
	if(result.length>0){
		initwysiwyg(w, h);
	}
}


function myCustomCleanup(type, value) {
	alert(type);
	switch (type) {
		case "get_from_editor":
			alert("Value HTML string: " + value);
			// Do custom cleanup code here
			break;
		case "insert_to_editor":
		
			alert("Value HTML string: " + value);
			// Do custom cleanup code here
			break;
		case "submit_content":
			alert("Value HTML Element: " + value);
			// Do custom cleanup code here
			break;
		case "get_from_editor_dom":
			alert("Value DOM Element " + value);
			// Do custom cleanup code here
			break;
		case "insert_to_editor_dom":
			alert("Value DOM Element: " + value);
			// Do custom cleanup code here
			break;
		case "setup_content_dom":
			alert("Value DOM Element: " + value);
			// Do custom cleanup code here
			break;
		case "submit_content_dom":
			alert("Value DOM Element: " + value);
			// Do custom cleanup code here
			break;
	}

	return value;
}


function initwysiwyg(w,h){
	var mces = $$('.mceEditor');
	for (var i = 0; i<mces.length; i++){
		new Insertion.After(mces[i], "<input type='hidden' name='wysiwyg" + mces[i].name + "' value='true' />")
	}
	var t = tinyMCE.init({
		theme : "advanced",
		mode : "specific_textareas",
		editor_selector : "mceEditor",
		plugins : "bbcode",
		width: w,
		height: h,
		//cleanup : false,
		remove_linebreaks : false,
		theme_advanced_buttons1 : "bold,italic,underline,undo,redo,link,unlink,image,forecolor,removeformat,code",
		theme_advanced_buttons2 : "",
		theme_advanced_buttons3 : "",
		content_css : "/css/bb.css",
		theme_advanced_toolbar_location : "bottom",
		theme_advanced_toolbar_align : "center",
		theme_advanced_styles : "Code=codeStyle;Quote=bb-quote",
		theme_advanced_source_editor_width: w,
		theme_advanced_source_editor_height: h
	});
	//add hidden field to be submitted for check in php
}

function jspopup(location,name,width,height){
	window.open (location,name,'status=0,toolbar=0,menubar=0,directories=0,scrollbars=1,resizable=1,width='+width+',height='+height);
}

function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function toggedAdvanced(button, id) {
	if($(id).getStyle('display') == 'none') {
		slideDiv(id);
		$(button).update('Hide Advanced Options');
	}else{
		slideDiv(id);
		$(button).update('Show Advanced Options');
	}
}

function setAsActive(master, active_item) {
	active_item = Element.extend(active_item);
	$A(master.getElementsByTagName(active_item.tagName)).each(function(i) { 
		Element.extend(i).removeClassName('active');
	});
	active_item.addClassName('active');
}

function activateFormElements(element) {
	$(element).select('input, select, textarea').each(function(item) {
		if(item.disabled) {
			item.enable();
		}
	});
}

function deactivateFormElements(element) {
	$(element).select('input, select, textarea').each(function(item) {
		if(!item.disabled) {
			item.disable();
		}
	});
}

function disableSubmit(form) {
	$(form).getElementsBySelector('input[type="submit"]').each(function(item) { 
		item.disable();
	})
}

function hideAll(par) {
	$(par).up().next().childElements().each(function(item) {
		deactivateFormElements(item);
		item.hide();
	});
}

/************************************
*Application Specific Functions		*
*************************************/


