var isLteIE6 = false;
var isLteIE7 = false;
var isIE7 = false;
var isIE6 = false;

/*
 javascript include functions
 allows simple, 'include_once' functionality for javascripts, before or after the page loads.
*/

var includedScripts = new Array();
includedScripts['before']= new Array();
includedScripts['after']= new Array();

if (document.addEventListener) this.addEventListener('load', includeAfter, false);// mozy
else if (document.attachEvent) this.attachEvent('onload', includeAfter);// IE

// supply arg of form timing, scriptpath1, scriptpath2, scriptpath3 etc.
function includeScripts(timing) {
	
		for (var i = 1; i< arguments.length; i++) {		
	    if (!in_array(arguments[i], includedScripts[timing])) {
	    		
	        includedScripts[timing][includedScripts[timing].length] = arguments[i];
	        if (timing=='before') appendScript(arguments[i]);
	    }
	  }
	}

function appendScript(scriptpath) {
		
		var pathdata=scriptpath.split('.');
		
		if (pathdata[1]) {
			switch (pathdata[1]) {
				case 'js':
				    var script = document.createElement('script');
				    script.setAttribute('language', 'javascript');
				    script.setAttribute('type', 'text/javascript');
				    script.setAttribute('src', scriptpath);
				    break;
				  
				 case 'css': 
						var script=document.createElement('link');
						script.setAttribute('rel', 'stylesheet');
						script.setAttribute('type', 'text/css');
						script.setAttribute('href', scriptpath);
						break;
				}  
	    document.getElementsByTagName('head')[0].appendChild(script);
	  }
    return false;
}

function includeAfter(){
    for (var i in includedScripts['after']) {
    		appendScript(includedScripts['after'][i]);
  	}
  	if (typeof pageInit=='function') pageInit();
}



// standard string replace function
function str_replace(haystack, needle, replacement) {
	var temp = haystack.split(needle);
	return temp.join(replacement);
}

//standard in_array function
function in_array(needle, haystack) {
		if (!needle||!haystack) return false;
    for (var i = 0; i < haystack.length; i++) {
        if (haystack[i] == needle) {
            return true;
        }
    }
    return false;
}

//switch shows hidden tabs and makes the tab selected active
function switchTab(tab) {
    //switch the content
    document.getElementById(tab).style.display = 'block';
    document.getElementById(tab+'Tab').className =  'active';
    if ((tab!=activeTab)&&(activeTab)) {
      document.getElementById(activeTab).style.display = 'none';
      document.getElementById(activeTab+'Tab').className = '' ;
    }
    activeTab = tab;
}
    
  

// ----------------------------------------------------------------------
// Form functions.
// ----------------------------------------------------------------------
	
function formSubmit(formName,formAction) {
		if (!formName) formName=0;
		document.forms[formName].action.value=formAction;
		document.forms[formName].submit();	
}
	
function confirmDelete(delUrl, contentType) {
	if (confirm("Are you sure you want to delete this " + contentType + "?"))
		document.location = delUrl;
}

function confirmDelete2(formName,formAction,contentType) {
	if (confirm("Are you sure you want to delete this " + contentType + "?")) {
		if (!formName) formName=0;
		if (formAction) document.forms[formName].action.value=formAction;
		document.forms[formName].submit();
		}
}

function dateSubmit(part,dts,formName) {
	if (!formName) formName='form1';
	if (dts) document.forms[formName]['date[dts]'].value=dts;
	document.forms[formName]['date[part]'].value=part;
	document.forms[formName].submit();
	
}


// ----------------------------------------------------------------------
// REPEAT FUNCTION
// ----------------------------------------------------------------------


function repeatDisplay2(ct) {
	
		var periods= ['',' day',' week',' month',' month',' year'];
		var months = ["January","February","March","April","May","June","July","August","September","October","November","December"];
		var days = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
		//var mtend = [31,28,31,30,31,30,31,31,30,31,30,31];
		var ordinal = ["th","st","nd","rd","th","th","th","th","th","th","th"];


		var repeat =document.getElementById(ct+'_repeat_interval');
		
		if (repeat) {
				var interval= parseInt(repeat.value,10);				
				var day=parseInt(document.form1[ct+'_date[start][day]'].value,10);
				var month_year=document.form1[ct+'_date[start][month]'].value;
				var date_array=month_year.split(",");
				var month=parseInt(date_array[0],10)-1;
				var year=parseInt(date_array[1],10);
				
				var dDate=new Date(year,month,day);
				var weekday=dDate.getDay();
				
				var repeatString=periods[interval];
				var frequency=document.form1[ct+'['+ct+'_repeat_frequency]'].value;
				if (frequency=='0') {
					document.form1[ct+'['+ct+'_repeat_frequency]'].value=1;
					frequency=1;
					
				}
				frequency=parseInt(frequency,10);
				if (frequency>1) repeatString+='s';
				
				switch(interval) {
				
					case 1: //daily
						//repeatString='Every '+days[weekday];
						break;
					case 2: //weekly
					  repeatString+=' on a '+days[weekday];
						break;
					case 3: //monthly - date
						repeatString+=' on the '+day+ordinal[day%10];
						break;
					case 4: //monthly - day
					  week=Math.ceil(day/7);
					  if (week>4)
					  	repeatString+=' on the last '+days[weekday];
					  else
					  	repeatString+=' on the '+week+ordinal[week]+' '+days[weekday];
						break;
					case  5: //yearly
					  repeatString+=' on '+months[month]+' '+day+ordinal[day%10];
						break;
					}
				
					
					repeat=document.getElementById('repeatString');
					repeat.innerHTML=repeatString;
					var repeatData=document.getElementById('repeatData');
					var repeatData2=document.getElementById('repeatData2');
					if (interval) {
						repeatData.style.display='inline';
						repeatData2.style.display='block';
					}
					else {
						repeatData.style.display='none';
						repeatData2.style.display='none';
					}
				}						
		}
