var errorColor = "#FF0000";
var mouseX = 0;
var mouseY = 0;

var IE = document.all?true:false

// If NS -- that is, !IE -- then set up for mouse capture
if (!IE) document.captureEvents(Event.MOUSEMOVE)

// Set-up to use getMouseXY function onMouseMove
document.onmousemove = getMouseXY;
document.onclick = docClick;

function docClick(e)
{
	var help = document.getElementById("help_msg");
	if(help)
	{
		//alert(help.mouseover);
		if(help.mouseover == false)
		{
			hideHelp();	
		}
	}
}

function getMouseXY(e) {
	var tempX = 0
	var tempY = 0
  if (IE) { // grab the x-y pos.s if browser is IE
    tempX = event.clientX + document.body.scrollLeft
    tempY = event.clientY + document.body.scrollTop
  } else {  // grab the x-y pos.s if browser is NS
    tempX = e.pageX
    tempY = e.pageY
  }  
  // catch possible negative values in NS4
  if (tempX < 0){tempX = 0}
  if (tempY < 0){tempY = 0}  
  // show the position values in the form named Show
  // in the text fields named MouseX and MouseY
  mouseX = tempX
  mouseY = tempY
  return true
}



function header(str, str2)
{
	//alert(str);
	var h = document.getElementById("h0");
	if(h)
	{
		h.innerHTML = str;
	}
	
	if(str2)
	{
		document.title = str2;
	}
}

function multiSelectAll(id)
{
	//alert(id);
	var obj = id;
	if(obj)
	{
		for (var i=0; i<obj.length; i++) 
		{ 
			obj.options[i].selected = true; 
		} 
	}
}
function multiSelectNone(id)
{
	//alert(id);
	var obj = id;
	if(obj)
	{
		for (var i=0; i<obj.length; i++) 
		{ 
			obj.options[i].selected = false; 
		} 
	}
}

function changestatus(msg)
{
	if(msg == "" || msg == null)	
	{	
		msg = "go|Swoop Study Abroad Community";
	}
	window.status = msg;
}
function takecarestatus()
{
	changestatus("");
}

function confirmation() {
	confirmation("", "");
}
function confirmation(msg) {
	confirmation("", msg);
}
function confirmation(url, msg) {
	//alert(msg);
	if(msg == "")
	{
		msg = "Do you want to delete?";
	}
	
	var answer = confirm(msg);
	if(url == "")
	{
		return answer;
	}else
	{
		if (answer){
			window.location = url;
		}
	}
}
function hide()
{
	var a = document.getElementById("hidden");
	var b = document.getElementById("hidden2");
	if(a != null)
	{
		document.body.removeChild(a);
	}
	if(b != null)
	{
		document.body.removeChild(b);
	}
}

function showHelp(id)
{
	hideHelp();
	if(!id)
	{
		id = -1;
	}
	
	x = parseInt(mouseX);
	y = parseInt(mouseY);
	var newdiv = document.createElement("div");
	newdiv.setAttribute("id","help_msg");
	newdiv.mouseover = true;
	
	newdiv.style.width = 175;
	newdiv.style.height = 100+15;
	newdiv.style.top = y + 5;
	var left =  (x - 54);
	
	if(left < 10)
	{
		left = 10;
	}
	newdiv.style.left = left;
	//alert(newdiv.style.left);
	
	newdiv.innerHTML = "<div id='help_msg_head'>&nbsp;</div><div id='help_msg_top'><div id='itemHeadLeft'>Loading...</div></div><div id='help_msg_bottom' style='height:100px'></br><div id='separator'>&nbsp;</div><div id='itemCenter'><img src='images/indicator.gif'/></div>";
	document.body.appendChild(newdiv);
	
	var obj = document.getElementById("help_msg_top");
	
	obj.onmouseover = function(){newdiv.mouseover = true;};
	obj.onmouseout = function(){newdiv.mouseover = false;};
	
	if(IE)
	{
		obj.style.marginTop = "-1";
	}
	
	var obj = document.getElementById("help_msg_bottom");
	
	obj.onmouseover = function(){newdiv.mouseover = true;};
	obj.onmouseout = function(){newdiv.mouseover = false;};
	
	var url = "ajax/helpmessageresponse.php?msgid="+id;
	var ajax = new MyAjax(url,"", function()
							  {
									var res = ajax.getResponseText();
									var dom = getDOMFromXML(res);
									if(dom)
									{
										dom = dom.getElementsByTagName("result")[0];
										t = dom.getAttribute("title");
										d = dom.getAttribute("body");
										d = d.replace( /\r?\n/g, "<br/>");
										//alert(d);
										w = dom.getAttribute("width");
										h = dom.getAttribute("height");
										updateHelpMessage(t,d,w,h);
									}
									
							   },
							   function()
							  {
									updateHelpMessage("Failed", "Due to some problem help message can not be loaded!!", -1, -1);
							   }, null);
	return newdiv;
}

function updateHelpMessage(title, description, width, height)
{
	
	var titleDiv = document.getElementById("help_msg_top");
	
	titleDiv.innerHTML = "<div id='itemHeadLeft'>"+title+"</div>";
	
	var b = document.getElementById("help_msg_bottom");
	b.innerHTML = "<div id='itemLeft'>"+description+"</div>";
	var div = document.getElementById("help_msg");
	if(width > 0 && height > 0)
	{
		div.style.width = width;
		div.style.height = height+15;
		b.style.height = height;
	}
	div.mouseover = false;
	
}

function hideHelp()
{
	var a = document.getElementById("help_msg");
	if(a != null)
	{
		document.body.removeChild(a);
	}
}

function show(width, height) {
 
	hide();
  	var newdiv = document.createElement("div");
	//var newdiv3 = document.createElement("frame");
  	var divIdName = "hidden";
  	newdiv.setAttribute("id",divIdName);
	//newdiv.setAttribute("class","select-free");
	newdiv.style.width = document.body.clientWidth;
	newdiv.style.height = document.body.clientHeight;
	newdiv.onmousedown = down;
	newdiv.style.cursor = "move";
	//newdiv.innerHTML = "<div id='itemRight'><b>x</b></div>";
	document.onmouseup = up;
	document.onmousemove = movePosition;
	var newdiv2 = document.createElement("div");
	newdiv2.setAttribute("id","hidden2");
	
	newdiv2.style.width = width;
	newdiv2.style.height = height;
	newdiv2.style.top = ((document.body.clientHeight-height)/2) + document.body.scrollTop;
	newdiv2.style.left = (document.body.clientWidth-width)/2;
	/*
	newdiv3.style.width = width;
	newdiv3.style.height = height;
	newdiv3.style.top = 30;
	newdiv3.style.left = 0;
	newdiv3.style.filter = "mask()";
	newdiv3.style.zindex = -1;
	newdiv3.style.cursor = "move";
	*/
	//newdiv2.style.top = 15;
	//newdiv2.style.left = 15;
	version=0
	if (navigator.appVersion.indexOf("MSIE")!=-1){
	temp=navigator.appVersion.split("MSIE");
	version=parseFloat(temp[1]);
	}

	if (version>=5.5) //NON IE browser will return 0
	{
		newdiv.style.width = width;
		newdiv.innerHTML = '<div id="itemHeadRightAlt"><a href="#" onclick="javascript:hide(); return false;">X</a>&nbsp;&nbsp;</div><iframe style="position:absolute;top:20;" width="'+width+'" height="'+(height-30)+'"></iframe>';
	}else
	{
		newdiv.style.width = width;
		newdiv.innerHTML = '<div id="itemHeadRightAlt"><a href="#" onclick="javascript:hide(); return false;">X</a>&nbsp;&nbsp;</div>';
	}
	
	newdiv.style.height = height;
	newdiv.style.top = ((document.body.clientHeight-(height+30))/2) + document.body.scrollTop;
	newdiv.style.left = (document.body.clientWidth-(width))/2;
	//newdiv.innerHTML = '<div id="itemRight"><a href="#" onclick="javascript:hide(); return false;"><img src="images/delete.gif" title="Close" border="0"></a></div><iframe style="position:absolute;top:20;" width="'+width+'" height="'+(height-30)+'"></iframe>';
	
	
	document.body.appendChild(newdiv2);
  	//newdiv.appendChild(newdiv3);
	document.body.appendChild(newdiv);
	//alert(newdiv.innerHTML);
	
	//newdiv.innerHTML = '<!--[if lte IE 6.5]><iframe width="'+newdiv.style.width+'" height="500" id="hidden"><![endif]--><div id="itemRight"><a href="#" onclick="javascript:hide(); return false;"><img src="images/delete.gif" title="Close" border="0"></a></div><!--[if lte IE 6.5]></iframe><![endif]-->';
	//newdiv.innerHTML = '<div id="itemRight"><a href="#" onclick="javascript:hide(); return false;"><img src="images/delete.gif" title="Close" border="0"></a></div>>';
	return newdiv2;
}
function compact(a)
{
	
	var b = document.getElementById("hidden2");
	var d = document.getElementById("hidden");
	var c = document.getElementById(a);
	if(b != null && c != null && d != null)
	{
		
		b.style.overflow = "hidden";
		//b.style.width = b.style.width;
		b.style.height = c.style.height;
		//alert(b.clientHeight);
		
		if(parseInt(b.clientHeight) > 450)
		{
			b.style.overflow = "auto";
			//b.clientHeight = 400;
			b.style.height = 450;
			//alert(b.clientHeight);
			d.style.top = ((document.body.clientHeight-(450+30))/2) + document.body.scrollTop;
			b.style.top = ((document.body.clientHeight-450)/2) + document.body.scrollTop;
			d.style.height = parseInt(b.style.height)+15;
		
			var i = d.getElementsByTagName("iframe");
			if(i[0])
			{
				i[0].setAttribute("height",parseInt(b.style.height)-6);
			}
			//i.height = d.style.height;
		//b.style.left = (document.body.clientWidth-(b.style.width))/2;
		}else
		{
			//b.style.overflow = "auto";
			//b.clientHeight = 400;
			b.style.overflow = "visible";
			b.style.height = parseInt(b.clientHeight)+15;
			d.style.height = parseInt(b.style.height)+15;
		
			var i = d.getElementsByTagName("iframe");
			if(i[0])
			{
				i[0].setAttribute("height",parseInt(b.style.height)-15);
				i[0].height = parseInt(b.style.height)-15;
				//alert(""+i[0].height);
			}else
			{
				//alert("Not Found");
			}
			//alert(b.clientHeight);
			//d.style.top = ((document.body.clientHeight-(450+30))/2) + document.body.scrollTop;
			//b.style.top = ((document.body.clientHeight-450)/2) + document.body.scrollTop;
		}
		
		//alert(i[0].height);
	}
}
/*
function recenter()
{
	var newdiv = document.getElementById("hidden");
	var newdiv2 = document.getElementById("hidden2");
	
	if(newdiv != null && newdiv2 != null)
	{
		newdiv2.style.top = ((document.body.clientHeight-height)/2) + document.body.scrollTop;
		newdiv2.style.left = (document.body.clientWidth-width)/2;
		newdiv.style.top = ((document.body.clientHeight-(height+25))/2) + document.body.scrollTop;
		newdiv.style.left = (document.body.clientWidth-(width))/2;
	}

}
*/
var move = false;
function down()
{	
	move = true;
	document.body.style.cursor = "move";
}
function up()
{	
	move = false;
	document.body.style.cursor = "default";
}
var oldX = 0;
var oldY = 0;
function movePosition(e)
{
	var x, y;
  		if (window.event) {
  		  x = window.event.clientX;
  		  y = window.event.clientY;
 		 } else {
 		   x = e.pageX;
 		   y = e.pageY;
		  }
	x = parseInt(x);
	y = parseInt(y);
	if(move)
	{
		
		difX = x - oldX;
		difY = y - oldY;
		
		oldX = x;
		oldY = y;

		var newdiv = document.getElementById("hidden");
		var newdiv2 = document.getElementById("hidden2");
		
		newdiv.style.left = parseInt(newdiv.style.left) + difX;
		newdiv.style.top = parseInt(newdiv.style.top) + difY;
		newdiv2.style.left = parseInt(newdiv2.style.left) + difX;
		newdiv2.style.top = parseInt(newdiv2.style.top) + difY;
		return false;
	}else
	{
		oldX = x;
		oldY = y;
	}
}
function movein(div)
{
	//div.style.backgroundColor = "#D0D0D0";
}
	
function moveout(div)
{
	//div.style.backgroundColor = "";
}

function trimAll(sString) 
{
	while (sString.substring(0,1) == ' ')
	{
		sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == ' ')
	{
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}

function refreshPage()
{
	window.document.refresh();
}

function isEmail(str)
{
	
	//alert(str.indexOf("@"));
	if((str.length > 0) && (str.indexOf("@") > -1) && (str.lastIndexOf(".") > str.lastIndexOf("@")))
	{
		//alert(str);
		return true;
	}else
	{
		//alert("Invalid");
		return false;
	}
}


function getDOMFromXML(data)
{
	if (window.ActiveXObject)
 	 {
  		var doc=new ActiveXObject("Microsoft.XMLDOM");
 	 	doc.async="false";
 	 	doc.loadXML(data);
  	}
	// code for Mozilla, Firefox, Opera, etc.
	else
  	{
 		var parser=new DOMParser();
  		var doc=parser.parseFromString(data,"text/xml");
  	}// documentElement always represents the root node
	//alert("X:"+data);
	var x=doc.documentElement;
	if(!x)
	{
		//alert("X:"+data);
		return false;
	}else
	{
		return x;
	}
}
function changeComboOption(combo, data)
{
	//alert(data);
	/*
	if (window.ActiveXObject)
 	 {
  		var doc=new ActiveXObject("Microsoft.XMLDOM");
 	 	doc.async="false";
 	 	doc.loadXML(data);
  	}
	// code for Mozilla, Firefox, Opera, etc.
	else
  	{
 		var parser=new DOMParser();
  		var doc=parser.parseFromString(data,"text/xml");
  	}// documentElement always represents the root node
	//alert("X:"+data);
	var x=doc.documentElement;
	*/
	var x = getDOMFromXML(data);
	if(!x)
	{
		//alert("X:"+data);
		return false;
	}
	
	combo.options.length = 0;
	var r = x.getElementsByTagName("result");
	if(!r)
	{
		//alert("r:"+data);
		return false;
	}
	for(var i = 0; i < r.length; i++)
	{
		var obj = r[i];
		//alert(obj.getAttribute("name"));
		combo.options[combo.options.length] = new Option(obj.getAttribute("name"), obj.getAttribute("value"));
	}
	//alert(combo.options.length);
	return true;
}

function valueExistInCombo(combo, value)
{
	for(var i = 0; i < combo.options.length; i++)
	{
		if(combo.options[i].value == value)
		{
			return true;
		}
	}
	return false;
}

function gotoURL(url)
{
	window.location = url;
}

function setCaretTo(obj, pos) {   
    if(obj.createTextRange) {   
        /* Create a TextRange, set the internal pointer to  
           a specified position and show the cursor at this  
           position  
        */  
        var range = obj.createTextRange();   
        range.move("character", pos);   
        range.select();   
    } else if(obj.selectionStart) {   
        /* Gecko is a little bit shorter on that. Simply  
           focus the element and set the selection to a  
           specified position  
        */  
        obj.focus();   
        obj.setSelectionRange(pos, pos);   
    }   
} 

function makeAutoComplete(obj, hobj, type, addsign)
{
	var element = document.getElementById(obj);
	var hElement = document.getElementById(hobj);
	
	if(addsign == null)
	{
		return new AutoComplete(element, type, hElement);
	}else
	{
		//alert(addsign);
		return new AutoComplete(element, type, hElement, null, addsign);
	}
}

function submitButton(text, btnId)
{
	var btn = document.getElementById(btnId);
	//alert(btn.disabled);
	//alert(text.value.length);
	if(text.value.length > 0)
	{
		btn.disabled = false;
		//alert("false:"+btn.disabled);
		
	}else
	{
		btn.disabled = true;
		//alert("true:"+btn.disabled);
	}
}
