// JavaScript Document
//ajax object intialization stuff

function GetXmlHttpObject()
{
	var xmlHttp=null;
	
	try
	 {
	 // Firefox, Opera 8.0+, Safari
	 	xmlHttp=new XMLHttpRequest();
	 }
	catch (e)
	 {
	 // Internet Explorer
		 try
		  {
		  	xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		  }
		 catch (e)
		  {
		 	 xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		  }
	 }
	return xmlHttp;
}
function setCookie(c_name,value,expiredays)
{
    var exdate=new Date();
    exdate.setDate(exdate.getDate()+expiredays);
    try{
    document.cookie=c_name+ "=" +escape(value)+
    ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
    }catch(err){alert('Can not set cookie'+err);}
}
function getCookie(c_name)
{
  if (document.cookie.length>0)
 	  {
 	 	 c_start=document.cookie.indexOf(c_name + "=");
		  if (c_start!=-1)
			{
				c_start=c_start + c_name.length+1;
				c_end=document.cookie.indexOf(";",c_start);
				if (c_end==-1) c_end=document.cookie.length;
					return unescape(document.cookie.substring(c_start,c_end));
			}
 	  }
 	return "";
 
}
function talk_to_server(action, wID){	


	 if(wID=='none')
	  {
	    window.location.replace(action);
	}
	 else
	  {
	     //alert("Telling server: "+action+" for : "+wID);
		reponse.abort();
		  if (reponse ==null){ alert("Sorry Please Upgrade your browser"); return;}//if browser does not support ajax well then is nothing we can do here
		  reponse.onreadystatechange= function() {//replace table when a response is received
		   if((reponse.readyState==4)|| (reponse.readyState=="complete"))
				  document.getElementById(wID).innerHTML=  reponse.responseText;
		 }
			//request refresh
			
			document.getElementById(wID).innerHTML="<img src='/img/progress.gif' width='100%'/>";
			reponse.open("GET",action+(action[0]=='/'?"":"&aj="+(Math.random()*110)),true);
			reponse.send(null);
			
		}
	
}
function form2GET(id)
{
	var form = document.getElementById(id);
	var GETstr="";
	for(var i=0; i<form.length; i++)
	   GETstr+="&"+form.elements[i].name+"="+form.elements[i].value;
	   
	return GETstr;
}
function expand(id){
	var e = document.getElementById(id).style;
	e.display="inline";
	
}
function collapse(id){
	var e = document.getElementById(id).style;
	e.display="none";
	
}
function toggleWithImg(id, img)
{
  if(document.getElementById(id).style.display=='none'){
	expand(id);
	img.src=img.src.replace(/down/,"up");
  }else{
    collapse(id);
	img.src=img.src.replace(/up/,"down");
  }
}
function toggle(obj)
{
	//alert(displayItemId);
  if(obj.style.display=='none'){
	obj.style.display='inline';
	
  }else{
    obj.style.display='none';
  }
}
function setVisiblity(obj,vis){
	if(vis<=1)
	 {
	 
		obj.style.opacity=vis;
		obj.style.filter="alpha(opacity="+(vis*100)+")";
	}else if(vis<=100){
		obj.style.opacity=vis/100;
		obj.style.filter="alpha(opacity="+vis+")";
	}
}
function toggleDialog()
{
	if (document.getElementById('dialog') == null) {
	//	alert('creating dialog');
		createDialog();
	}
	else {
		toggle(document.getElementById('dialog'));
		toggle(document.getElementById('blackscreen'));
	}
     try {//hidding my flash input interface because google chrome sets it z-index as auto
	 	toggleVisibility(document.getElementById('input'));
		document.getElementById('input').style.padding="0px";
		//setVisiblity(document.getElementById('input'),document.getElementById('dialog').style.display=='none'?1:0);
	 }catch(err){}
}
function toggleInputDialog(obj){
	editorObj = obj;
	toggleDialog();
}
function insertSoundIcon(){
	var mp3= new RegExp(".mp3");
	
	if (!mp3.test(document.getElementById('input_url').value)){
		alert("Error: Sound File URL  must point to an MP3");
	}else if(document.getElementById('input_url').value.length<6){
		alert("Error: Sound File URL  format is incorrect")
	}
	else {
		editorObj.execCommand('inserthtml', '<img src="/img/speak.gif" class="clickable" onclick="play(\'' + document.getElementById('input_url').value + '\')" border="0">');
		toggleDialog();
	}
}


function createInputDialog(label){
	
	var inp = document.createElement("input");
	var inB = document.createElement("input");
	
	inp.setAttribute('type','text');
	inp.setAttribute('size', '90');
	inp.setAttribute('id', 'input_url');
	
	
	inB.setAttribute('type','button');
	inB.setAttribute('onclick', 'insertSoundIcon();');
	inB.setAttribute('value', 'Add '+label);
	
	createDialog();
	//toggleDialog();
	with (document.getElementById("dialog").style) {
		width = "580px";
		height = "140px";
		margin = "auto";
	}
	
	with(document.getElementById("dialogContent")){
		innerHTML=label+" URL: ";
		appendChild(inp);
		appendChild(inB);
		
	}
	
}
function createDialog(){
 var dialog = document.createElement("div");
 var dialogContent = document.createElement("div");
 var dialogTitle = document.createElement("span");
 var dialogClose = document.createElement("span");
 
 dialogTitle.appendChild(dialogClose);
 dialog.appendChild(dialogTitle);
 dialog.appendChild(dialogContent);
 
 dialog.setAttribute('id','dialog');
dialog.setAttribute('className','dialog'); 
	dialog.setAttribute('class','dialog');
 dialogContent.setAttribute('id', 'dialogContent');
 dialogTitle.setAttribute('className', "titleBar"); 
 dialogTitle.setAttribute('class', "titleBar");
 dialogTitle.setAttribute('onclick', "toggleDialog();") ;
 dialogClose.setAttribute('className',  "close"); 
 dialogClose.setAttribute('class',  "close");
 dialogClose.innerHTML="Close";
 
 document.body.appendChild(dialog);
 createBackground();
 dialog.style.display='inline';
}
function createBackground(){
	
 var background = document.createElement("div");
 background.setAttribute('id','blackscreen');
 background.setAttribute('class',"blackscreen");
 document.body.appendChild(background);
// alert(document.getElementById('blackscreen').className);
 background.style.display='inline';
}
function openDialog(url){
	toggleDialog();
	if (url.match("http://") != null || url.match(".html") != null ) {
		if(document.getElementById('dialogContent').innerHTML.match(url)==null)
		document.getElementById('dialogContent').innerHTML = "<center  id='progress' ><img src='/img/progress.gif' style='margin:auto; display:block; position:relative;'/></center><iframe style='position:relative; top:0px; left:0px; right:0px; bottom:0px; border:0px; height:100%; width:100%;' src='" + url + "' onload='toggle(document.getElementById(\"progress\")); this.contentDocument.body.style.padding=\"0px\";' ></iframe>";
	}
	else 
		talk_to_server(url, 'dialogContent')	;
}
function play(url){
	var player=null;
	if(url.match("/snd/")==null && url.match("http://")==null )
	url = '/snd/'+url;
	try
 	{
 		
 		player = document.getElementById('playerID');
 	}catch(err)
 	{
       alert('Init: Player');
        player =addPlayer();
		// document.getElementById('playerID');
        
 	}
	if(player==null) player =addPlayer();
	
	if (player != null) {
		try {
			player.innerHTML = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" width="60" height="25" id="player" align="middle" >			<param name="allowScriptAccess" value="sameDomain" />	<param name="movie" value="/snd/soundPlayer.swf?playURL=' + url + '&aj='+Math.random()+'" />	<param name="quality" value="high" /> <param name="bgcolor" value="transparent" /> <embed     src="/snd/soundPlayer.swf?playURL=' + url +  '&aj='+Math.random()+'" quality="high" bgcolor="#ffffff" width="60" height="35" name="player" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /> </object>';
		}catch(err){
			alert(player.id);
		}
	}else{
		alert('Sound Player Init Failure ');
	}

}
function showToolTip(txt,x,y)
{
	try{
  with(document.getElementById('toolTipText'))
  {
  //alert(txt);
     innerHTML=txt;
	 with(style)
	 {
	    top=10+y+"px";
		left=5+x+"px";
		zIndex="20";
		display="inline";
	 }
	}}catch(err){}
}
function hideToolTip()
{
	try{
  with(document.getElementById('toolTipText'))
  {
     innerHTML='';
	 with(style)
	 {
	    
		zIndex="-20";
		display="none"
	 }
	}}catch(err){}
}
function addPlayer(){
	try {
		var plyr = document.createElement("div");
		plyr.id = "playerID";
		with (plyr.style) {
			top = "105px";
			right = "8%";
			position = "absolute";
			zIndex = "100";
			backgroundColor = "transparent";
			border = "0px";
			padding = "0px";
			maxHeight = "20px";
			maxWidth = "25px";
			opacity = "0.4";
			filter = "alpha(opacity=40)";
		}
		document.body.appendChild(plyr);
		return plyr;
	} 
	catch (err) {
		alert("Sorry can not add sound player");
	}
}
//alert(document.body.width);
var reponse = GetXmlHttpObject();//get com obj

function validate(aForm)
{
  for (var i=0;  i < aForm.element.length; i++)
  if(aForm.elements[i]!=undefined)
   with(aForm.elements[i]){
   	alert(aForm.type);
   if(type!="select")
   if (value==undefined||value==null||value.length<2)
	  {
		alertUser(aForm.elements[i],"*** Please Fill Out this portion ");
		disabled=false;
		focus();
		return false;
	  }
	  }
	  
	if(aForm.email)
	 return validate_email(aForm.email);  
	 
	 return true;
 }  	   
 
 function alertUser(elm,alerttxt)
 {
    //alert(elm.tagName);
	var p =elm.parentNode;
	elm.style.borderColor="red";
	
	if(p!=undefined && p.tagName=='TD')
	{
		var gp = elm.parentNode.parentNode;
		
	}
	else
	{
		alert("Invalid Input for: "+elm.name.toUpperCase());
		elm.focus();
		return;
	}
	var label="";
	var tblCells = new Array();
	j=0;
	for (i in gp.childNodes)
	 if(gp.childNodes[i].tagName=='TD')
	   {
	   	tblCells[j] = gp.childNodes[i];
		j++
		}
		j--;
	tblCells[j].innerHTML= alerttxt;
	
	gp.style.color="red";
	gp.style.border="1px solid red";
	elm.select();
	elm.focus();
 }
 function validate_email(field)
{

	//with (field)
	//{
	var apos=field.value.indexOf("@");
	var dotpos=field.value.lastIndexOf(".");
	if (apos<1||((dotpos-apos)<2)) 
	  {alertUser(field,"***Please Enter a valid email address");return false;}
	else {return true;}
	//}
}
function setupEditor(textarea){
	
	createInputDialog('Sound');
	toggleDialog();

	myEditor = new YAHOO.widget.Editor(textarea, {
    height: '300px',
    width: '600px',
    dompath: true, //Turns on the bar at the bottom
    animate: true, //Animates the opening, closing and moving of Editor windows
	collapse: false,
	toolbar:{
				titlebar: ' Editing Page Content ',
				draggable: true,
				buttonType: 'advanced',
				buttons: [
				    { group: 'fontstyle', label: 'Font Name and Size',
				        buttons: [
				            { type: 'select', label: 'BJCree UNI', value: 'fontname', disabled: true,
				                menu: [
				                    { text: 'Arial'},
				                    { text: 'Arial Black' },
									{ text: 'BJCree UNI', checked: true },
				                    { text: 'Comic Sans MS' },
				                    { text: 'Courier New' },
				                    { text: 'Lucida Console' },
				                    { text: 'Tahoma' },
				                    { text: 'Times New Roman' },
				                    { text: 'Trebuchet MS' },
				                    { text: 'Verdana' }
				                ]
				            },
				            { type: 'spin', label: '13', value: 'fontsize', range: [ 9, 75 ], disabled: true }
				        ]
				    },
				    { type: 'separator' },
				    { group: 'textstyle', label: 'Font Style',
				        buttons: [
				            { type: 'push', label: 'Bold CTRL + SHIFT + B', value: 'bold' },
				            { type: 'push', label: 'Italic CTRL + SHIFT + I', value: 'italic' },
				            { type: 'push', label: 'Underline CTRL + SHIFT + U', value: 'underline' },
				            { type: 'separator' },
				            { type: 'push', label: 'Subscript', value: 'subscript', disabled: true },
				            { type: 'push', label: 'Superscript', value: 'superscript', disabled: true },
				            { type: 'separator' },
				            { type: 'color', label: 'Font Color', value: 'forecolor', disabled: true },
				            { type: 'color', label: 'Background Color', value: 'backcolor', disabled: true },
				            { type: 'separator' },
				            { type: 'push', label: 'Remove Formatting', value: 'removeformat', disabled: true },
				            { type: 'push', label: 'Show/Hide Hidden Elements', value: 'hiddenelements' }
				        ]
				    },
				    { type: 'separator' },
					{ group :'undoredo', label: 'Undo/Redo',
						buttons:[
							{ type: 'push', label: 'Undo' , value:'undo'},
							{ type: 'push', label: 'Redo' , value:'redo'}
						]
					
					},
					{ type: 'separator' },
				    { group: 'alignment', label: 'Alignment',
				        buttons: [
				            { type: 'push', label: 'Align Left CTRL + SHIFT + [', value: 'justifyleft' },
				            { type: 'push', label: 'Align Center CTRL + SHIFT + |', value: 'justifycenter' },
				            { type: 'push', label: 'Align Right CTRL + SHIFT + ]', value: 'justifyright' },
				            { type: 'push', label: 'Justify', value: 'justifyfull' }
				        ]
				    },
				    { type: 'separator' },
				    { group: 'parastyle', label: 'Paragraph Style',
				        buttons: [
				        { type: 'select', label: 'Normal', value: 'heading', disabled: true,
				            menu: [
				                { text: 'Normal', value: 'none', checked: true },
				                { text: 'Header 1', value: 'h1' },
				                { text: 'Header 2', value: 'h2' },
				                { text: 'Header 3', value: 'h3' },
				                { text: 'Header 4', value: 'h4' },
				                { text: 'Header 5', value: 'h5' },
				                { text: 'Header 6', value: 'h6' }
				            ]
				        }
				        ]
				    },
				    { type: 'separator' },
				    { group: 'indentlist', label: 'Indenting and Lists',
				        buttons: [
				            { type: 'push', label: 'Indent', value: 'indent', disabled: true },
				            { type: 'push', label: 'Outdent', value: 'outdent', disabled: true },
				            { type: 'push', label: 'Create an Unordered List', value: 'insertunorderedlist' },
				            { type: 'push', label: 'Create an Ordered List', value: 'insertorderedlist' }
				        ]
				    },
				    { type: 'separator' },
				    { group: 'insertitem', label: 'Insert Item',
				        buttons: [
				            { type: 'push', label: 'HTML Link CTRL + SHIFT + L', value: 'createlink', disabled: true },
				            { type: 'push', label: 'Insert Image', value: 'insertimage' },
							{ type: 'push', label: 'Insert Sound Button', value: 'insertsound'}
				        ]
				    }
				]
		}
	});
	myEditor.on('toolbarLoaded', function(){
	
	
	        myEditor.toolbar.on('insertsoundClick', function(ev) {
			 
	            toggleInputDialog(this);
	            
	        }, myEditor, true);
	});
	myEditor.render();
	with (document.getElementById(textarea)) {
		
		onfocus = myEditor.saveHTML();
		with(style){
			top = "20px";
			position = "relative";
			display = 'block';
		}
	}
}
function insertInputKeyboard(){
		var keyboard='<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" width="450" height="180" id="keyboard4input" align="middle" style="background:transparent"><param name="allowScriptAccess" value="sameDomain" /><param name="movie" value="/keyboard4input.swf" /><param name="quality" value="high" /><param name="bgcolor" value="#cccccc" /><embed src="/keyboard4input.swf" quality="high" bgcolor="#cccccc" width="450" height="180" name="keyboard4input" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>';
		var dialog = document.createElement("div");
 
 
 dialog.setAttribute('id','flashKeyboard');
// dialog.setAttribute('class','dialog'); 
 document.body.appendChild(dialog);
 with (document.getElementById("flashKeyboard")) {
 	innerHTML = keyboard;
 	with (style) {
 		display = 'block';
 		minWidth = "450px";
 		minHeight = "180px";
		top="45%";
		left="35%";
 		margin = "auto";
 		backgroundColor = "transparent";
 		position = "absolute";
 	}
 }
}
function showInputKeyboard(inputInFocus){
	inputFocus=inputInFocus;
	
	try {
		toggle(document.getElementById("flashKeyboard"));
	} 
	catch (err) {
		insertInputKeyboard();
		//toggle(document.getElementById("flashKeyboard"));
	}
	try {
		toggle(document.getElementById("blackscreen"));
	} 
	catch (err) {
		createBackground();
		//toggle(document.getElementById("background"));
	}
	
}
function closeInput(text){
	
	try {
		toggle(document.getElementById("blackscreen"));
	}catch(err){}
	try{
		if (inputFocus) 
			inputFocus.value += rtou(text);
			
		toggle(document.getElementById("flashKeyboard"));
		
	}catch(err){}
	
}
function toggleVisibility(obj){
	obj.style.zIndex=1;
	for (var i = obj.childNodes.length - 1; i >= 0; i--){
	toggleVisibility(obj.childNodes[i]);
};
	return;
	if(obj.style.opacity || obj.style.filter=='alpha(opacity=100)')
	{
		obj.style.opacity=0; 
		obj.style.filter='alpha(opacity=0)';
		
	}else{
		obj.style.opacity=1; 
		obj.style.filter='alpha(opacity=100)';
	}
}
var inputFocus=null;
var formChecker = null;
var editorObj =null;
var myEditor = null;
var displayItemId=null;