
//The first version of the Common Library (Version 1.1)
//Need to be enhanced by changing browserSnipper with browserInfo()


function createDiv(position) { // creates an absolute positioning div element without margins etc.

	var myDiv;

	myDiv=document.createElement("div");
	if (typeof(position)!="string")
		myDiv.style.position="absolute";
	else {
		switch (position) {
			case "relative":
			case "absolute":
			case "static": {
				myDiv.style.position=position;
				break;
			}
			default: {
				window.alert("Positioning not allowed in createDiv");
				break;
			}
		} // switch
	} //else
	clearMargins(myDiv);
	return myDiv;
}


function xImage(src) {
window.onerror = function() { return true; }

	//input: the source path of the image
	//output: image object that is invisible, you have to make is visible right after you gave its position to avoid the weird effect.

	var myImage = new Image();
	myImage.style.position = "absolute";
	myImage.style.visibility = "hidden";
	myImage.style.backgroundImage = "url(images/loading.gif)";
	myImage.style.backgroundRepeat = "no-repeat";
	myImage.style.backgroundPosition = "center center";

	if(typeof(src) != "string") {
		alert("xImage: src required");
		return false;
	}

	myImage.src = src;

	if(myImage.readyState == "complete" || myImage.readyState == "loaded" || myImage.complete == true) { //check if the images is already at the computer
		myImage.style.background = "";
		if ((browserInfo().browserShortName=="IE")&&(navigator.appVersion.indexOf("MSIE 7")<0)) {
			if(getFileType(src) == ".png") {
				myImage.div = document.createElement("div");
				myImage.div.pngSource = new Image();
				myImage.div.myPNG=true;
				myImage.div.mySrc=src;
				myImage.div.pngSource.src=src;
				myImage.div.pngSource.style.position = "absolute";
				myImage.div.pngSource.id=myUniqueId();
				myImage.div.pngSource.myParent=myImage;
				myImage.div.pngSource.style.visibility="hidden";
				document.body.appendChild(myImage.div.pngSource);
				if (myImage.div.pngSource.readyState=="complete") {
					temp="changePNG(\""+myImage.div.pngSource.id+"\")";
					window.setTimeout(temp,0);
				} else
					myImage.div.pngSource.onreadystatechange=changePNG;
				myImage.style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(src="+src+" sizingmethod=scale)";
				myImage.src = "images/empty.gif";
				myImage.orgWidth = myImage.div.pngSource.width;
				myImage.orgHeight = myImage.div.pngSource.heigth;
				myImage.style.height = myImage.div.pngSource.height;
				myImage.style.width = myImage.div.pngSource.width;
//				myImage.style.visibility = "visible";
				return myImage;
			} else {
				return myImage;
			}
		} else {
//			myImage.style.visibility = "visible";
			return myImage;
		}
	}

	myImage.http_request = null;
	
	if(window.XMLHttpRequest) // code for Mozilla, etc.
		myImage.http_request=new XMLHttpRequest();
	else if(window.ActiveXObject)// code for IE 
		myImage.http_request=new ActiveXObject("Microsoft.XMLHTTP");
			
	if(myImage.http_request!=null){
		myImage.http_request.open("GET", "serverAnswers.php?image="+src, true);
		myImage.http_request.onreadystatechange = function() {
			if(myImage.http_request.readyState==4){
				if(myImage.http_request.status==200){
					var response;
					response=myImage.http_request.responseText;
					indexName1 = eval(response.lastIndexOf("`"));
					indexName2 = eval(response.lastIndexOf("*"));
					indexName3 = eval(response.lastIndexOf(","));
					
					width = eval(response.substring(indexName2+1, indexName3));
					height = eval(response.substr(indexName3+1));
					src = response.substring(indexName1+1, indexName2);

					switch(getFileType(src)) {
						case ".png": {
							if ((browserInfo().browserShortName=="IE")&&(navigator.appVersion.indexOf("MSIE 7")<0)) { // No native support for IE6 and lower
								myImage.div = document.createElement("div");
								myImage.div.pngSource = new Image(width, height);
								myImage.div.myPNG=true;
								myImage.div.mySrc=src;
								myImage.div.pngSource.src=src;
								myImage.div.pngSource.style.position = "absolute";
								myImage.div.pngSource.id=myUniqueId();
								myImage.div.pngSource.myParent=myImage;
								myImage.div.pngSource.style.visibility="hidden";
								myImage.src = "images/empty.gif";
								myImage.orgWidth = width;
								myImage.orgHeight = height;
								myImage.style.width = width + "px";
								myImage.style.height = height + "px";
								document.body.appendChild(myImage.div.pngSource);
								if (myImage.div.pngSource.readyState=="complete") {
									temp="changePNG(\""+myImage.div.pngSource.id+"\")";
									window.setTimeout(temp,0);
								} else
									myImage.div.pngSource.onreadystatechange=changePNG;
								myImage.style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(src="+src+" sizingmethod=scale)";
							} else {
								myImage.orgWidth=width;
								myImage.orgHeight=height;
							}
							break;
						}
						case ".bmp":
						case ".jpg":
						case ".gif": {
							myImage.orgWidth=width;
							myImage.orgHeight=height;
							myImage.width = width;
							myImage.height = height;
							break;
						}
						//default:
						//	window.alert("Image type not supported");
						//	break;
					}//switch
					myImage.style.background = "";
					resizeFunc();
				}else if(myImage.http_request.status==404) {
					myImage.src = "images/empty.gif";
					myImage.orgWidth = 45;
					myImage.orgHeight = 50;
				} else {
//					alert("Problem retrieving XML data");
				}
			}
		}
		myImage.http_request.send(null);
	}else
		alert("Your browser does not support XMLHTTP.");
	return myImage;
}
 
 
 function changePNG(id) { // only to be called in IE
	
	var sourceElement;
	
	if (typeof(id)=="string") {
		sourceElement=document.getElementById(id);
	}
	else {
		sourceElement=event.srcElement;
		if (sourceElement.readyState!="complete")
			return; // just wait for the next readyStateChange
	}
	// object is ready
	sourceElement.orgWidth=sourceElement.offsetWidth;
	sourceElement.orgHeight=sourceElement.offsetHeight;
	sourceElement.myParent.orgWidth=sourceElement.offsetWidth;
	sourceElement.myParent.orgHeight=sourceElement.offsetHeight;
	document.body.removeChild(sourceElement);
}


function createP(s) {	
	var retval;
	
	if (typeof(s)=="undefined")
		s="";
	retval=document.createElement("p");
	retval.innerHTML=s;
	return retval;
}


function createBlankTable(rowLength, colLength) {
	
	var myTable, myTHead, myTBody, i, j, cell, cells;
      
    myTable=document.createElement("TABLE");
    myTBody=document.createElement("TBODY");
    clearMargins(myTable);
    clearMargins(myTBody);
    myTable.appendChild(myTBody);
    i=0;
    cells=new Array();
    while(i<rowLength){
		cells[i]=new Array();
		tr=document.createElement("TR");
        myTBody.appendChild(tr);
        j=0;
        while(j<colLength) {
                       cell=document.createElement("TD");
                       cells[i][j]=cell;
                        tr.appendChild(cell);
                        j++;
                }
                i++;
    }
    myTable.nrOfColumns=colLength;
    myTable.nrOfRows=rowLength;
    myTable.mycells=cells;
    return myTable;
}

// this funciton is use to inert the column on the given table
function insertColumn(tableObj, columnNo, columnValue){

        var table, cell;

        for(i=0;i<columnValue.length;i++){
                var table=tableObj.rows[i];
                var cell;
                cell=table.insertCell(columnNo)
                clearMargins(cell);
                cell.innerHTML=columnValue[i];
        }
}
//end
// Below is the new createDataTable function, still contains lay-out info, to be described and improved
function createDataTable(rowInfo, columnInfo) {
        var obj, i, l, lc, pEl, name, myTHead, row, cell, title1, title2, headingRow;
        var myTable = new Array();

        i=0;
        while(i<rowInfo.length){
                myTable[i]= new Array();
                j=0;
                while(j<columnInfo.length){
                        myTable[i][j] = new Object();

                        myTable[i][j].content=getDataElement(rowInfo[i].id, columnInfo[j].id);
                        myTable[i][j].header= new Array();
                        myTable[i][j].header[i]=columnInfo[j].text;

                j++
                }
        i++
        }
        return myTable;
}


function changeCursor(obj){
        var myObj;

    if(typeof(obj)=="undefined"){
          myObj=this;
    }
    else{
         myObj=obj;
        }
        if(!browserSnipper()){
                myObj.style.cursor="pointer";
        }
        else{
                myObj.style.cursor="hand";
        }
}


function createMailObject(obj){

// returns a form

var myForm, i, len;
			
	myForm=document.createElement("form");	//form
	myForm.style.position="absolute";
	myForm.method="Post";
	myForm.action="mailer1234.php";	
	myForm.onsubmit=submitForm;
	myForm.fieldLength=obj.fieldInfo.length;
	document.body.appendChild(myForm);
				
	myForm.recipient=document.createElement("input");//recipient
	myForm.recipient.type="hidden";
	myForm.recipient.name="recipient";
	myForm.recipient.id="recipient";
	myForm.recipient.value=obj.recipient;
	myForm.appendChild(myForm.recipient);
				
	myForm.subject=document.createElement("input");//subject
	myForm.subject.type="hidden";
	myForm.subject.name="subject";
	myForm.subject.id="subject";
	myForm.subject.value=obj.subject;
	myForm.appendChild(myForm.subject);
				
	myForm.sender=document.createElement("input");//sender
	myForm.sender.type="hidden";
	myForm.sender.name="sender";
	myForm.sender.id="sender";
	myForm.sender.value=obj.sender;
	myForm.appendChild(myForm.sender);
				
	myForm.title1=document.createElement("input");//title
	myForm.title1.type="hidden";
	myForm.title1.name="title";
	myForm.title1.id="title";
	myForm.title1.value=obj.title;
	myForm.appendChild(myForm.title1);
				
	myForm.redirect=document.createElement("input");//redirect page
	myForm.redirect.type="hidden";
	myForm.redirect.name="redirect";
	myForm.redirect.id="redirect";
	myForm.redirect.value=obj.redirect;
	myForm.appendChild(myForm.redirect);
				
	myForm.message=document.createElement("input");	//temporary container 
	myForm.message.type="hidden";
	myForm.message.name="message";
	myForm.message.id="message";
	myForm.message.value=""
	myForm.appendChild(myForm.message);
				
	myForm.mailTitle=document.createElement("p");
	myForm.mailTitle.innerHTML=obj.title;
	myForm.mailTitle.style.position="absolute";
	myForm.appendChild(myForm.mailTitle);
				
	len=obj.fieldInfo.length;						
	myForm.field= new Array();
	myForm.caption=new Array();
	myForm.hiddenField= new Array();
	for(i=0; i<len; i++){
		myForm.caption[i]=document.createElement("p");
		myForm.caption[i].innerHTML=obj.fieldInfo[i].caption;
		myForm.caption[i].style.position="absolute";				
		myForm.appendChild(myForm.caption[i]);
		clearMargins(myForm.caption[i]);
										
		myForm.field[i]=createInput(obj.fieldInfo[i]);
		myForm.field[i].style.position="absolute";
		clearMargins(myForm.field[i]);
		myForm.appendChild(myForm.field[i]);
				
	}
	myForm.bttSubmit=document.createElement("input");
	myForm.bttSubmit.type="submit";
	myForm.bttSubmit.style.position="absolute";
	if(typeof(obj.bttnCaption)=="undefined"){
		obj.bttnCaption="Submit";
	}
	myForm.bttSubmit.value=obj.bttnCaption;
	myForm.appendChild(myForm.bttSubmit);
				
	return myForm;
}


function createMailObject(obj, area){
var myForm, i, len;
			
	myForm=document.createElement("form");	//form
	myForm.style.position="absolute";
	myForm.method="Post";
	myForm.action="mailer1234.php";	
	myForm.onsubmit=submitForm;
	myForm.fieldLength=obj.fieldInfo.length;
	document.body.appendChild(myForm);
				
	myForm.recipient=document.createElement("input");//recipient
	myForm.recipient.type="hidden";
	myForm.recipient.name="recipient";
	myForm.recipient.id="recipient";
	myForm.recipient.value=obj.recipient;
	myForm.appendChild(myForm.recipient);
				
	myForm.subject=document.createElement("input");//subject
	myForm.subject.type="hidden";
	myForm.subject.name="subject";
	myForm.subject.id="subject";
	myForm.subject.value=obj.subject;
	myForm.appendChild(myForm.subject);
				
	myForm.sender=document.createElement("input");//sender
	myForm.sender.type="hidden";
	myForm.sender.name="sender";
	myForm.sender.id="sender";
	myForm.sender.value=obj.sender;
	myForm.appendChild(myForm.sender);
				
	myForm.title1=document.createElement("input");//title
	myForm.title1.type="hidden";
	myForm.title1.name="title";
	myForm.title1.id="title";
	myForm.title1.value=obj.title;
	myForm.appendChild(myForm.title1);
				
	myForm.redirect=document.createElement("input");//redirect page
	myForm.redirect.type="hidden";
	myForm.redirect.name="redirect";
	myForm.redirect.id="redirect";
	myForm.redirect.value=obj.redirect;
	myForm.appendChild(myForm.redirect);
				
	myForm.message=document.createElement("input");	//temporary container 
	myForm.message.type="hidden";
	myForm.message.name="message";
	myForm.message.id="message";
	myForm.message.value=""
	myForm.appendChild(myForm.message);
				
	myForm.mailTitle=document.createElement("p");
	myForm.mailTitle.innerHTML=obj.title;
	myForm.mailTitle.style.position="absolute";
	myForm.appendChild(myForm.mailTitle);
				
	len=obj.fieldInfo.length;						
	myForm.field= new Array();
	myForm.caption=new Array();
	myForm.hiddenField= new Array();
	for(i=0; i<len; i++){
		myForm.caption[i]=document.createElement("p");
		myForm.caption[i].innerHTML=obj.fieldInfo[i].caption;
		myForm.caption[i].style.position="absolute";				
		myForm.appendChild(myForm.caption[i]);
		clearMargins(myForm.caption[i]);
										
		myForm.field[i]=createInput(obj.fieldInfo[i]);
		myForm.field[i].style.position="absolute";
		clearMargins(myForm.field[i]);
		myForm.appendChild(myForm.field[i]);
				
	}
	myForm.bttSubmit=document.createElement("input");
	myForm.bttSubmit.type="submit";
	myForm.bttSubmit.style.position="absolute";
	if(typeof(obj.bttnCaption)=="undefined"){
		obj.bttnCaption="Submit";
	}
	myForm.bttSubmit.value=obj.bttnCaption;
	myForm.appendChild(myForm.bttSubmit);
	if(typeof(area)!="undefined"){
		myForm.area=document.createElement("textArea");
		myForm.area.style.position="absolute";
		myForm.appendChild(myForm.area);
	}						
	return myForm;
}

function createInput(obj, type){
	var txtBox;
	if(typeof(type)=="undefined"){
		type="text";
	}
	if(typeof(obj.value)=="undefined"){
		obj.value="";
	}
	txtBox=document.createElement("input");
	
	txtBox.name=obj.name;
	txtBox.id=obj.name;
	txtBox.value=obj.value;
	txtBox.type=type;

	return txtBox;
}
			
function submitForm() {
	var i, len, msg="";	
	len=this.fieldLength;
	//concatenate all the text
	for(i=0; i<len; i++){					
		msg=msg + this.caption[i].innerHTML+"  " +this.field[i].value+"\n";	
	}
	this.message.value=msg;				
	return true;
}


function addSound(source){

	var sound, isPlayer=false, obj;
	
	if(source=="" || typeof(source)=="undefined"){
		return null;
	}

	type=getFileType(source);
	switch (type){
		//check for the mime types for quick time only 
		case ".wma":{
				isPlayer=detectMimetypes("audio/x-wma")
		break;
		     	}
	       	case ".wav":{
	       			isPlayer=detectMimetypes("audio/x-wav");
	       	break;
	       		}
	       	case ".mp3":{
	       			isPlayer=detectMimetypes("audio/mpeg");
	       	break;
	       	     }
	       	case ".mid":
	       	case ".midi":{
	       			isPlayer=detectMimetypes("application/x-midi");
	       	break;
	       	     }
	       	     
	       	default:{
	       		alert("File type not Supported");
	       		return null;
	       	}
	}
	
	if(!isPlayer){		
		return null;
	}
	else{
		if(!browserSnipper()){
			//detect for windows media player
			winPlug=detectPlugin("Windows Media Player")
			if(winPlug){
				alert("You have Windows Media Player and QuickTime plug-in but we recommend to use the QuickTime");
			}			
			obj=document.createElement("object");
			obj.setAttribute("data", source);		
			obj.playMe=playSound;
			obj.stopMe=stopSound;
			obj.addParam=addParam;
			obj.loaded=false;
			obj.timer=0;
			mySound1234=obj;
			checkSoundLoad();	
		return obj;	
		}
		else{
			sound=document.createElement("embed");
			sound.src=source;
			sound.playMe=playSound;
			sound.stopMe=stopSound;
			sound.loaded=false;
			sound.timer=0;
			mySound1234=sound;			
			sound.addParam=addParam;
		checkSoundLoad();	
		}
	}
return sound;
}


function playSound(){
//	alert(typeof(this.Play))
	if(typeof(this.Play)!="undefined"){
		this.Play();
	}						
}		


function stopSound(){
	if(typeof(this.Stop)!="undefined"){
		this.Stop();
	}			
}


function checkSoundLoad(){
	if(typeof(mySound1234.Play)=="function" || typeof(mySound1234.Play)=="unknown"){
		mySound1234.loaded=true;
		window.clearTimeout(mySound1234.timer);
	}
	else{
		window.setTimeout("checkSoundLoad()", 1000)				
	}
}


function addParam(name, value){
		
	if(navigator.appName=="Netscape"){
		var param;
		param=document.createElement("param");
		param.name=name;
		param.value=value;
		this.appendChild(param);								
	}
	else{
		this.setAttribute(name, value);
	}
}