function daylightsavingpresets(){
	thispreset=tForm.DSpresets.value.split('&');
	tForm.DSstartPos.value=thispreset[3];
	tForm.DSstartDay.selectedIndex=thispreset[0];
	tForm.DSstartMonth.selectedIndex=thispreset[1];
	tForm.DSstartHour.selectedIndex=thispreset[2];
	tForm.DSendPos.value=thispreset[7];
	tForm.DSendDay.selectedIndex=thispreset[4];
	tForm.DSendMonth.selectedIndex=thispreset[5];
	tForm.DSendHour.selectedIndex=thispreset[6];
}



function monthCode(){
	if(tForm.MonthFormat.selectedIndex<2){
		cvariables[1]=varNames[1][sSize]+"=new Array("+monthnames[tForm.MonthFormat.selectedIndex][tForm.Language.selectedIndex]+")";
	}
	switch(tForm.MonthFormat.selectedIndex){
		case 0 : return varNames[1][sSize]+'['+funNames[0][sSize]+'().getMonth()]';
		case 1 : return varNames[1][sSize]+'['+funNames[0][sSize]+'().getMonth()]';
		case 2 : return (tForm.leadingZero2.checked) ? funNames[4][sSize]+'(('+funNames[0][sSize]+'().getMonth()+1))' : '('+funNames[0][sSize]+'().getMonth()+1)';
	}
}

function dayCode(){
	cvariables[0]=varNames[0][sSize]+"=new Array("+daynames[tForm.DayFormat.selectedIndex][tForm.Language.selectedIndex]+")";
	return varNames[0][sSize]+'['+funNames[0][sSize]+'().getDay()]';
}

function dateCode(){
	switch(tForm.DateFormat.selectedIndex){
		case 1 : return (tForm.leadingZero1.checked) ? funNames[4][sSize]+'('+funNames[0][sSize]+'().getDate())' : funNames[0][sSize]+'().getDate()';
		case 0 : cfunctions[6]=funNames[6][2]; return funNames[0][sSize]+'().getDate()+'+funNames[6][sSize]+'('+funNames[0][sSize]+'().getDate())';
	}
}

function hourCode(){
	var fff=funNames[0][sSize]+'().getHours()';
	if(tForm.hourType.selectedIndex==0){
		cfunctions[5]="(x){\nif(x==0){\nx=12;\n}\nreturn (x>12)?x-=12:x;\n}";
		cfunctions[9]="(x){\nreturn (x>11)?'"+tForm.pm.value+"':'"+tForm.am.value+"';\n}"
		fff=funNames[5][sSize]+"("+fff+")";
	}
	if((tForm.hourType.selectedIndex==1)||(tForm.leadingZero.checked)){
		fff=funNames[4][sSize]+"("+fff+")";
	}
	return fff;
}



var tForm=document.timeSetup;
var cvariables=new Array("","","","","","");
var cfunctions=new Array("","","","","","","","","","");
var sSize=0;
var funNames=new Array()
funNames[0]=new Array("tS","timeSource");
funNames[1]=new Array("tN","timeNow");
funNames[2]=new Array("dS","daylightSaving");
funNames[3]=new Array("fD","findDay");
funNames[4]=new Array("lZ","leadingZero","(x){\nreturn (x>9)?x:\'0\'+x;\n}");
funNames[5]=new Array("tH","twelveHour");
funNames[6]=new Array("dE","dateEnding","(x){\nif(x==1||x==21||x==31){\nreturn 'st';\n}\nif(x==2||x==22){\nreturn 'nd';\n}\nif(x==3||x==23){\nreturn 'rd';\n}\nreturn 'th';\n}");
funNames[7]=new Array("y2","fixYear2","(x){\nx=(x<500)?x+1900:x;\nreturn String(x).substring(2,4)\n}");
funNames[8]=new Array("dT","displayTime");
funNames[9]=new Array("aP","amPMsymbol","(x){\nreturn (x>11)?'pm':'am';\n}");
funNames[10]=new Array("y4","fixYear4","(x){\nreturn (x<500)?x+1900:x;\n}");


var varNames=new Array();
varNames[0]=new Array("dN","dayNames");
varNames[1]=new Array("mN","monthNames");
varNames[2]=new Array("aP","amPMsymbol");
varNames[3]=new Array("fR","firstRun");
varNames[4]=new Array("oT","outputTime");

var scrpos1="";
var scrpos2="";
var spacing="               ";



function buildCode(){
	cfunctions=new Array("","","","","","","","","","");
	cvariables=new Array("","","","","","");
	gothours=0;
	scrpos1="";
	scrpos2="";
	
	
	// Buzz Form
	tForm.leadingZero1.disabled=(tForm.DateFormat.selectedIndex!=0)?false:true;
	tForm.leadingZero2.disabled=(tForm.MonthFormat.selectedIndex==2)?false:true;
	if(tForm.hourType.selectedIndex==1){
		tForm.leadingZero.checked=true;
		tForm.leadingZero.disabled=true;
		tForm.am.disabled=true;
		tForm.pm.disabled=true;
	}else{
		tForm.leadingZero.disabled=false;
		tForm.am.disabled=false;
		tForm.pm.disabled=false;
	}
	tForm.timeZones.disabled=(tForm.TimeSource.selectedIndex!=3)?true:false;
	dsiu=(tForm.daylightSaving.checked)?true:false;
	tForm.DSpresets.disabled=(dsiu)?false:true;
	tForm.DSstartPos.disabled=(dsiu)?false:true;
	tForm.DSstartDay.disabled=(dsiu)?false:true;
	tForm.DSstartMonth.disabled=(dsiu)?false:true;
	tForm.DSstartHour.disabled=(dsiu)?false:true;
	tForm.DSendPos.disabled=(dsiu)?false:true;
	tForm.DSendDay.disabled=(dsiu)?false:true;
	tForm.DSendMonth.disabled=(dsiu)?false:true;
	tForm.DSendHour.disabled=(dsiu)?false:true;
	sSize=tForm.scriptSize.selectedIndex;
	


	// Build outputTime Variable
	cvariables[4]=varNames[4][sSize]+'="';
	codedd=tForm.displayFormat.value.split('');
	for (n=0;n<codedd.length;n++){
		cvariables[4]+=(n>0)?"+":"";
		notdun=1;
		switch(codedd[n]){
			case "Y" :
				thisfun=7
				if(tForm.YearFormat.selectedIndex==1){
					cfunctions[7]=funNames[7][2];
				}else{
					cfunctions[10]=funNames[10][2];
					thisfun=10
				}
				cvariables[4]+=funNames[thisfun][sSize]+'('+funNames[0][sSize]+'().getYear())';
				notdun=0;
				break;
			case "D" : cvariables[4]+=dateCode(); notdun=0; break;
			case "d" : cvariables[4]+=dayCode(); notdun=0; break;
			case "M" : cvariables[4]+=monthCode(); notdun=0; break;
			case "h" : cvariables[4]+=hourCode(); gothours=1; notdun=0; break;
			case "m" : cvariables[4]+=funNames[4][sSize]+'('+funNames[0][sSize]+'().getMinutes())'; notdun=0; break;
			case "s" : cvariables[4]+=funNames[4][sSize]+'('+funNames[0][sSize]+'().getSeconds())'; notdun=0; break;
		}
		if(notdun==1) cvariables[4]+="'"+codedd[n]+"'";
	}
	if(tForm.hourType.selectedIndex==0&&gothours){
		cvariables[4]+='+'+funNames[9][sSize]+'('+funNames[0][sSize]+'().getHours())';
	}
	cvariables[4]+='"';
	
	// Build displayTime Function
	cfunctions[8]='(){';
	if(tForm.statusbar.checked){ cfunctions[8]+="\nwindow.status='"+tForm.statusPrefix.value+"'+eval("+varNames[4][sSize]+")+'"+tForm.statusSuffix.value+"';"; }
	
	
	if(tForm.titlebar.checked){ cfunctions[8]+="\ndocument.title='"+tForm.titlePrefix.value+"'+eval("+varNames[4][sSize]+")+'"+tForm.titleSuffix.value+"';"; }
	
	
	
	if((tForm.pageastext.checked&&tForm.StaticDynamic.selectedIndex!=1)||(tForm.TimeSource.selectedIndex==1)){
		cfunctions[8]+="\ndocument.write(";
		if(tForm.prefix.value!=""){
			cfunctions[8]+="'"+tForm.prefix.value+"'+";
		}
		cfunctions[8]+="eval(oT)";
		if(tForm.suffix.value!=""){
			cfunctions[8]+="+'"+tForm.suffix.value+"'";
		}
		cfunctions[8]+=");";
	}
	if(tForm.pageastext.checked&&tForm.StaticDynamic.selectedIndex==1&&tForm.TimeSource.selectedIndex!=1){
		if(tForm.prefix.value!=""){
			thisprefix="'"+tForm.prefix.value+"'+";
		}else{
			thisprefix="";
		}
		if(tForm.suffix.value!=""){
			thissuffix="+'"+tForm.suffix.value+"'";
		}else{
			thissuffix="";
		}
		cfunctions[8]+="\ndocument.getElementById('tP').innerHTML="+thisprefix+"eval("+varNames[4][sSize]+")"+thissuffix+";";
	}	
	if(tForm.StaticDynamic.selectedIndex==1&&tForm.TimeSource.selectedIndex!=1) cfunctions[8]+="\nsetTimeout('"+funNames[8][sSize]+"()',1000);";
	cfunctions[8]+="\n}";
	
	
	scripttime=new Date();
	if(document.timeSetup.scriptDest.selectedIndex==0){
		scrpos1='<!-- Clock Part 1 - Holder for Display of Clock -->\n\n<span id="tP">&nbsp;</span>\n\n<!-- Clock Part 1 - Ends Here -->\n\n\n\n<!-- Clock Part 2 - Put Anywhere AFTER Part 1 -->\n\n<script type="text/javascript">\n';
		scrpos2='if(!document.all){ window.onload='+funNames[8][sSize]+'; }else{ '+funNames[8][sSize]+'(); }\n</scr'+'ipt>\n\n<!-- Clock Part 2 - Ends Here  -->\n';
	}else{
		scrpos2+='if(!document.all){ window.onload='+funNames[8][sSize]+'; }else{ '+funNames[8][sSize]+'(); }\n\n\n/'+'/ Put the following HTML on your page where you would like the Clock to be displayed:\n';
		scrpos2+='/'+'/ <span id="tP">&nbsp;</span>\n\n';
	}
	
	
	
	
	
	
	scrpos1+="/"+"/ Clock Script Generated By Maxx Blade's Clock v2.0d\n/"+"/ http://www.maxxblade.co.uk/clock\n";
	
	
	// Build timeSource function
	cfunctions[4]=funNames[4][2];
	var fff='(){\nx=new Date(';
	if(tForm.TimeSource.selectedIndex>1){
		fff+=funNames[1][sSize]+'().getUTCFullYear(),'+funNames[1][sSize]+'().getUTCMonth(),'+funNames[1][sSize]+'().getUTCDate(),'+funNames[1][sSize]+'().getUTCHours(),'+funNames[1][sSize]+'().getUTCMinutes(),'+funNames[1][sSize]+'().getUTCSeconds()';
		cfunctions[1]='(){\nreturn new Date();\n}';
	}
	fff+=');\nx.setTime(x.getTime()';
	if(tForm.daylightSaving.checked==true){
		fff+='+'+funNames[2][sSize]+'()';
		cfunctions[1]='(){\nreturn new Date();\n}';
		cfunctions[2]='(){\nreturn (('+funNames[1][sSize]+'().getTime()>'+funNames[3][sSize]+'('+tForm.DSstartDay.selectedIndex+','+tForm.DSstartMonth.selectedIndex+','+(tForm.DSstartHour.selectedIndex+1)+','+tForm.DSstartPos.value+').getTime())&&('+funNames[1][sSize]+'().getTime()<'+funNames[3][sSize]+'('+tForm.DSendDay.selectedIndex+','+tForm.DSendMonth.selectedIndex+','+(tForm.DSendHour.selectedIndex+1)+','+tForm.DSendPos.value+').getTime()))?3600000:0;\n}';
		cfunctions[3]='(d,m,h,p){\nvar week=(p<0)?7*(p+1):7*(p-1),nm=(p<0)?m+1:m,x=new Date('+funNames[1][sSize]+'().getUTCFullYear(),nm,1,h,0,0),dOff=0;\nif(p<0){\nx.setTime(x.getTime()-86400000);\n}\nif(x.getDay()!=d){\ndOff=(x.getDay()<d)?(d-x.getDay()):0-(x.getDay()-d);\nif(p<0&&dOff>0){\nweek-=7;\n}\nif(p>0&&dOff<0){\nweek+=7;\n}\nx.setTime(x.getTime()+((dOff+week)*86400000));\n}\nreturn x;\n}';
	}
	if(tForm.TimeSource.selectedIndex==3){
		fff+=tForm.timeZones.value;
	}
	fff+=');\nreturn x;\n}';
	cfunctions[0]=fff;
	
	
	newtestoutput="";
	for(n=0;n<cfunctions.length;n++){
		indent=0;
		if(cfunctions[n]!=""){
			newtestoutput+="function "+funNames[n][sSize];
			if(sSize==1){
				thisfunc=cfunctions[n].split("\n");
				for(q=0;q<thisfunc.length;q++){
					newtestoutput+=spacing.substring(0,3*indent)+thisfunc[q]+"\n";
					indent=(thisfunc[q].indexOf('{')!=-1)?indent+1:indent;
					if((q<thisfunc.length-1)){
						indent=(thisfunc[q+1].indexOf('}')!=-1)?indent-1:indent;
					}
				}
			}else{
				thisfunc=cfunctions[n].split("\n");
				for(q=0;q<thisfunc.length;q++){
					newtestoutput+=thisfunc[q]+" ";
				}
				newtestoutput+="\n";
			}
		}
	}
	newtestoutput+="var ";
	usevars=new Array();
	for(n=0;n<cvariables.length;n++){
		if(cvariables[n]!=""){
			usevars[usevars.length]=cvariables[n];
		}
	}
	for(n=0;n<usevars.length;n++){
		newtestoutput+=usevars[n];
		if(n<usevars.length-1){
			newtestoutput += (sSize == 0) ? ',' : ';\nvar ';
		}
	}
	newtestoutput+=";\n";
	tForm.timeOutput.value=scrpos1+newtestoutput+scrpos2;
}



///////////////////////
// Display Test Page //
///////////////////////

	function testScript(){
	
		buildCode();

		output_html = '<HTML>\n';
		output_html += '\t<HEAD>\n';
		output_html += '\t\t<TITLE>Clock Test</TITLE>\n';
		output_html += '\t</HEAD>\n\n';
		output_html += '\t<BODY>\n\n';

		if(tForm.scriptDest.selectedIndex==1){
			output_html += '\t\t<!-- Clock Part 1 - Display Holder -->\n';
			output_html += '\t\t<SPAN id="tP">&nbsp;</SPAN>\n';
			output_html += '\t\t<!-- Clock Part 1 - Ends Here -->\n\n';
			output_html += '\t\t<SCRIP'+'T type="text/javascrip'+'t">\n\n';
		}

		output_html += tForm.timeOutput.value;

		if(tForm.scriptDest.selectedIndex==1){
			output_html += '\t\t</SCRIP'+'T>\n\n';
		}

		output_html += '\t\t<P>View the source of this page to see the script in position</P>\n';
		output_html += '\t</BODY>\n';
		output_html += '</HTML>';

		testpage=open("about:blank","Test","height=150,width=400,status=1,menu=1");
		testpage.document.write(output_html);
		testpage.focus();
	}


//////////////////////
// Initalize Script //
//////////////////////
	
	buildCode();