/** 
*	@defgroup Javascript
*/

/**
* @defgroup Public
*/

/**
 * @file		main.js
 *
 * @package App
 *
 * @brief		Site Common Javascript functions
 * 
 * @details	Includes those functions common to most files
 *
 * 	@date	06/19/2010 Copyright (c) 2010
 * 	@author Vicky Garrison
 *		-	Site: http://garelle.com
 * 		-	Email: vicky@garelle.com
 * 
 *	@todo 
 */
 
  
/**
*	
*	<b>Details: </b>
*
* Takes an object as an input parameter and return true if it exists, false if it doesn't exist
* 
* @date		08/14/2010
*
*	@param theVal - object to check
*
*	@retval - true if object exists, false if object does not exist
*
*/
function chkObject(theVal)
{
    if (document.getElementById(theVal) != null)
    {
        return true;
    }
    else
    {
       return false;
    }
}

/**
*	
*	<b>Details: </b>
*
*	Takes a number & length as input and returns a padded value
*
* @date		08/14/2010
*
*	@param number - number to pad
*	@param length - length (2 will add 1 leading zero)
*
*	@retval - str (padded string)
*
*/
function pad(number, length) {
   
    var str = '' + number;
    while (str.length < length) {
        str = '0' + str;
    }
    return str;
}


/**
*	
*	<b>Details: </b>
*
*	Tinymce initializer function
* 
* @date		08/14/2010
*
*	@param w - width of editor
*
*	@retval - none
*
*/
function variableWidthEditor(w) {
	tinyMCE.init({
		// General options
		mode : "textareas",
		theme : "advanced",
		width: w,
		height: "500",
		editor_selector: "ta",
		editor_deselector : "plain",
		theme_advanced_path : false,
		plugins : "safari,spellchecker,advimage,advlink,emotions,iespell,insertdatetime,preview,searchreplace,contextmenu,paste,xhtmlxtras,htmlcharcount",
	
		// Theme options
		theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,|,formatselect,fontselect,fontsizeselect,|,hr,advhr,emotions,iespell",
		theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,|,undo,redo,|,link,unlink,anchor,image,|,insertdate,inserttime,preview,code",
		theme_advanced_buttons3: "",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		theme_advanced_statusbar_location : "bottom",
		theme_advanced_resizing : true,
		file_browser_callback : function(field_name, url, type, win) {
	     var w = window.open('http://pinkheartfunds.com/externals/explorer/elfinder2.php', null, 'width=600,height=500');
	      // Save required parameters in global variables of window (not the best solution, can offer better?)
	      // else you can pass parameters using GET and than parse them in elfinder.html
				w.tinymceFileField = field_name;
	      w.tinymceFileWin = win;
	  },
	  relative_urls: true,
		convert_urls: false,
		//Very important for imagemanager paths
		external_link_list_url : "http://pinkheartfunds.com/assets_protected/a_js/linklist.js",
		entity_encoding : "named",
		force_p_newlines : false,
		force_br_newlines : false
	});
}

/**
*	
*	<b>Details: </b>
*
*	Searches an array for element
*
* @date		08/14/2010
*
*	@param arr - array to search
*	@param obj - element to search for
*
*	@retval - return element index if found otherwise return -1
*
*/
function searchArr(arr,obj) {
  return $.inArray(obj,arr);
	//removed due to incompatibility with ie
  //return (arr.indexOf(obj) != -1);
}

function pause(milliseconds) {
	var dt = new Date();
	while ((new Date()) - dt <= milliseconds) { /* Do nothing */ }
}

//Valid Email Address
function isValidEmail(str) {
   return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
}

function isValidPhone(str) {
	//^([\(]{1}[0-9]{3}[\)]{1}[ ]{1}[0-9]{3}[\-]{1}[0-9]{4})$
	var phone2 = /^(\+\d)*\s*(\(\d{3}\)\s*)*\d{3}(-{0,1}|\s{0,1})\d{2}(-{0,1}|\s{0,1})\d{2}$/; 
	if (str.match(phone2)) {
   		return true;
 	} else {
 		return false;
 	}
}

 //Validate individual element - called onblur 
  function validateElement(el,val,type) {
  	var strLen = el.length; 
		var elem = el.slice(0,strLen-2);
  	switch(type)
		{
			case 'text':
  			if(val == '') {
  				$('#'+el).removeClass('valid');
  				$('#'+el).addClass('error');
  			}
  			else {
  				$('#'+el).removeClass('error');
  				$('#'+el).addClass('valid');
  				$('#'+elem).css('background-color','#ffffff');
  			}
  		break;
  		case 'email':
  			if(val=='') {
  				$('#'+el).text("Email cannot be blank.");
  				$('#'+el).removeClass('valid');
  				$('#'+el).addClass('error');
  			}
  			else {
  				var validEmail=isValidEmail(val);
  				$('#'+el).text("Please enter a valid email");

  				if(!validEmail){
  					$('#'+el).removeClass('valid');
  					$('#'+el).addClass('error');
  				}
  				else {
  					$('#'+el).removeClass('error');
  					$('#'+el).addClass('valid');
  					$('#'+elem).css('background-color','#ffffff');
  				}
  			}
  		break;
			case 'date':
  			$('#'+el).text('Checking date availability...');
  			pause(1000);
  			var d=$('#datepicker').val();
  			if(d=='') {
  				$('#'+el).text('Date cannot be blank');
  					$('#'+el).removeClass('valid');
  					$('#'+el).addClass('error');
  			}
  			else {
  			$('#'+el).text('That date is not available');
					var search=$.inArray(d,blocked_arr);
					if(search != -1) {
  					$('#'+el).removeClass('valid');
  					$('#'+el).addClass('error');
					}
  				else {
  					$('#'+el).removeClass('error');
  					$('#'+el).addClass('valid');
  					$('input#datepicker').css('background-color','#ffffff');
  				}
  			}
   		break;
   		case 'phone':
  			var valid=isValidPhone(val);
				if(!valid) {
  				$('#'+el).removeClass('valid');
  				$('#'+el).addClass('error');
				}
  			else {
  				$('#'+el).removeClass('error');
  				$('#'+el).addClass('valid');
  				$('#'+elem).css('background-color','#ffffff');
  			}
   		break;

			default:
				//code to be executed if n is different from case 1 and 2
		}
  }
	






