var dragObj = null;
var windows = new Array();
var objIndex = 10000;
var stdAlertID = 0;
var stdAlertID2 = 0
var errorAlert = 0;
var mainZIndex = 1;


var btHome = 1;
var btClose = 2;
var btCart = 3;
var btAccount = 4;


function loadStates(pCountry) {
	
	url = "index.php?n=Ajax&o=getStates&id=" + pCountry;
	el = document.getElementById('StateID');
	el.options.length= 0;
	option = new Option();
	option.text = ' loading, please wait .... ';
	option.value = 0;
	el.options[0] = option;

	if (window.XMLHttpRequest)
	  {
	  xmlhttp=new XMLHttpRequest();
	  }
	// code for IE
	else if (window.ActiveXObject)
	  {
	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	if (xmlhttp!=null)
	  {
	  xmlhttp.onreadystatechange=_loadStates;
	  xmlhttp.open("GET",url,true);
	  xmlhttp.setRequestHeader( "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT" );
	  xmlhttp.send(null);
	  }
	else
	  {
	  alert("Your browser does not support XMLHTTP.");
	  }


}

function _loadStates() {
  if(checkReadyState(xmlhttp)) {
	var response = xmlhttp.responseXML.documentElement;
	response.normalize;
	x1=response.getElementsByTagName("id");
	x2=response.getElementsByTagName("name");
	el = document.getElementById('StateID');
	
	el.options.length = 0;
	for (i=0;i<x1.length ;i++ )
	{
		option = new Option();
		option.text = x2[i].firstChild.data;
		option.value = x1[i].firstChild.data;
		el.options[el.options.length] = option;
	}

  }
 }

function generateID() {
	objIndex = objIndex + 1;
	return objIndex;
}


function showAlert(alertID, msg, title) {
	if (alertID == 0) mainZIndex ++;
	setOpacity(10);
	

	if (!alertID)
	{
		alertID = generateID();
		txt = '<div id="'+alertID+'" >';
		txt += '<table border="0" align="center" cellpadding="0" cellspacing="0">';
		txt += '	<tr>';
		txt += '	  <td width="16" align="left" valign="top"><img src="images/popborder_tl.gif" width="16" height="16" /></td>';
		txt += '	  <td align="right" valign="bottom" bgcolor="#FFFFFF" class="borderbluetop" style="padding-right:8px;"><a href="#self" onclick="hideAlert('+alertID+');" onmouseover="MM_swapImage(\'popclose\',\'\',\'images/bttn_closeover.gif\',1)" onmouseout="MM_swapImgRestore()"><img src="images/bttn_closeup.gif" name="popclose" width="13" height="13" border="0" id="popclose" /></a></td>';
		txt += '	  <td width="16" align="right" valign="top"><img src="images/popborder_tr.gif" width="16" height="16" /></td>';
		txt += '	</tr>';
		txt += '	<tr>';
		txt += '	  <td bgcolor="#FFFFFF" class="borderblueleft">&nbsp;</td>';
		txt += '	  <td align="left" valign="top" bgcolor="#FFFFFF">';
		txt += '<span id="alertTitle_'+alertID+'">';
		if (title)
		{
			txt += '<div class="pagetitletop" style=" width:600px; margin-top:10px;"><img src="images/pagehead_'+title+'.gif" /></div>';
		}
		txt += '</span>';
		txt += '<span id="alertBody_'+alertID+'">' + msg + '</span>';
		txt += '</td>';
		txt += '	  <td bgcolor="#FFFFFF" class="borderblueright"><img src="images/transparent.gif" width="11" height="11" /></td>';
		txt += '	</tr>';
		txt += '	<tr>';
		txt += '	  <td align="left" valign="bottom"><img src="images/popborder_bl.gif" width="16" height="16" /></td>';
		txt += '	  <td bgcolor="#FFFFFF" class="borderbluebot"><img src="images/transparent.gif" width="1" height="11" /></td>';
		txt += '	  <td align="right" valign="bottom"><img src="images/popborder_br.gif" width="16" height="16" /></td>';
		txt += '	</tr>';
		txt += ' </table>';
		txt += '</div>';

		el = document.createElement("div");
		el.id = "divAlert_" + alertID;
		el.style.position =  'absolute';
		el.style.width =  'auto';
		el.style.zIndex =  10;
		el.innerHTML = txt;

		bdy = document.getElementsByTagName("body");
		bdy[0].appendChild(el);
	} else {
		x = document.getElementById("alertTitle_" + alertID);
		if (title)
		{
			x.innerHTML = '<div class="pagetitletop" style=" width:600px; margin-top:10px;"><img src="images/pagehead_'+title+'.gif" /></div>';
		} else {
			x.innerHTML = "";
		}
		x = document.getElementById("alertBody_" + alertID);
		x.innerHTML = msg;
	}
	el = document.getElementById("divAlert_" + alertID);

	el.style.display='block';
	el.style.zIndex = mainZIndex ;

	h = el.offsetHeight;
	w = el.offsetWidth;

	c = getCenter2(w,h);
	el.style.top = Math.max(0, c.y) + "px";
	el.style.left = Math.max(0, c.x) + "px";
	
	return alertID;
}

function hideAlert(id) {
	if (dragObj) endDrop();

	el = document.getElementById("divAlert_" + id);
	el.style.display='none';

	mainZIndex --;
	if (mainZIndex <= 1) setOpacity(100);

}

function moveAlert(id, tp, lft) {
	el = document.getElementById("divAlert_" + id);
	if (tp >=0) el.style.top = tp + "px";
	if (lft >=0) el.style.left = lft + "px";
}

function resizeAlert(id, wd, ht) {
	el = document.getElementById("divAlertID_" + id + "_mainTable");	el.style.width=wd + "px";	el.style.height=ht + "px";
//	el = document.getElementById("divAlertID_" + id + "_secondTD");	el.style.width=wd + "px";	el.style.height=ht + "px";
//	el = document.getElementById("divAlertID_" + id + "_firstTD");	el.style.width=wd + "px";	
//	el = document.getElementById("divAlertID_" + id + "_innerTable");	el.style.width=wd + "px";	el.style.height=ht + "px";
	el = document.getElementById("divAlert_" + id);	el.style.width=wd + "px";	el.style.height=ht + "px";
}

function showError(msg) {
	showAlert(errorAlert, msg, "");
}


function showWait() {
	el = document.getElementById("mainBody");
	el.innerHTML = "please wait ... ";
}

function catchEnter() {
	if (window.event.keyCode==13){
		hideAlert();
	}
}


function getForm(fobj) {
var str = "";
var ft = "";
var fv = "";
var fn = "";
var els = "";
for(var i = 0; i < fobj.elements.length; i++) {
	els = fobj.elements[i];
	ft = els.title;
	fv = els.value;
	fn = els.name;
	
	reqFlag = false;
	if (els.title != "") {
		msg = els.title.split("_");
		if (msg[0] == "required")
		{
			reqFlag = true;
		}
	}
	switch(els.type) {
		 case "text":
		 case "hidden":
		 case "password":
		 case "textarea":
		 // is it a required field?
		   if (els.title && reqFlag && fv == "")
		   {
			   alert("Please fill up all required fields! The following field requres a value: " + els.name);
			   return "error";
		   }
		  str += fn + "=" + encodeURIComponent(fv) + "&";
		  break;

		  case "checkbox":
		  case "radio":
			   if (els.type == "checkbox" && els.title && reqFlag &&  !els.checked )
			   {
				   alert("Please fill up all required fields!" + els.name);
				   return "error";
			   }

		if(els.checked) str += fn + "=" + encodeURIComponent(fv) + "&";
		  break;


		  case "select-one":
			   if (els.title && reqFlag &&  els.selectedIndex == 0)
			   {
				   alert("Please fill up all required fields!" + els.name);
				   return "error";
			   }

		 str += fn + "=" +

		 els.options[els.selectedIndex].value + "&";
		  break;
		  } // switch
	 } // for
	 str = str.substr(0,(str.length - 1));
	 return str;
}



function centerElement(elid) {
	el = document.getElementById(elid);
	center = getCenter(el.offsetWidth, el.offsetHeight);
	el.style.left = parseInt(center.x) + "px";
	el.style.top = parseInt(center.y) + "px";
	el.style.position = "absolute";
	el.style.zIndex = 11;
}

function closeAlert(alertID) {
	el = document.getElementById(alertID);
	el.style.display = "none";
  	setOpacity(100) ;
}

function setOpacity(level) {
	if (level<100) openPopup("divWait", "#ffffff"); else closePopup("divWait");
	return false;

	try
	{
		
//		el = document.getElementsByTagName("table");
//		for (i=0; i<el.length ;i++ )
//		{
//			el[i].style.filter = "alpha(opacity="+level+")";
//			el[i].style.opacity = level/100;
//			el[i].style.mozOpacity = level/100;
//		}

		el = document.getElementById("mainContent");
		el.style.filter = "alpha(opacity="+level+")";
		el.style.opacity = level/100;
		el.style.mozOpacity = level/100;

		el = document.getElementById("pageContent");
		el.style.filter = "alpha(opacity="+level+")";
		el.style.opacity = level/100;
		el.style.mozOpacity = level/100;

		el = document.getElementById("mainNav");
		el.style.filter = "alpha(opacity="+level+")";
		el.style.opacity = level/100;
		el.style.mozOpacity = level/100;

	}
	catch (e)
	{
		;
	}

}


var alt = false;
var ctrl = false;
var shift = false;

function keyHandler(ev) {
		alt = (ev.altKey) ? true : false;
		ctrl = (ev.ctrlKey) ? true : false;
		shift = (ev.shiftKey) ? true : false;
		macAlt = (ev.metaKey) ? true: false;
		cancelEv = false;

	if ((ev.keyCode<16)||(ev.keyCode>18)) {
		 if (ctrl) {
			switch (ev.keyCode)
			{
				case 78: //Alt + N
					window.location = "index.php?n=Projects&o=new";
					return false;
				case 65: //Alt + A
					showAllProjects(); 
					ctrl=false; return false;break;
				case 77: //Alt + M
					showMyProjects();
					ctrl=false; return false; break;
				case 84: //Alt + T
					window.location = "index.php?n=Projects&o=tools";
					return false;
				case 69: //Alt + E
					exportProjectList();
					ctrl=false; return false; break;
				case 76: //Alt + L
					window.location = "index.php?n=Users&o=logout";						
					return false;
				case 72: //Alt + H
					window.location = "index.php";						
					return false;
			}
		 }
		 alt = false;
         ctrl = false;
         shift = false; 
		 if (cancelEv) return false;
	}
	if (ctrl) return false;
	return true;
}


function openPopup(popupid, bgcolor) {
	dv = document.getElementById(popupid);
	if (!dv)
	{
		x = Math.max(document.body.offsetWidth, getWindowWidth());
		y = Math.max(document.body.offsetHeight, getWindowHeight());

		dv = document.createElement("div");
		dv.style.position = "absolute";
		dv.id = popupid;
		dv.style.top = "0px";
		dv.style.left = "0px";
		dv.style.height = y + "px";
		dv.style.width = x + "px";
		dv.style.backgroundColor = bgcolor;
		opacity(dv, 95);
		dv.style.zIndex = 1;	

		document.body.appendChild(dv);
	} else {
		x = document.body.offsetWidth;
		y = document.body.offsetHeight;
		dv.style.top = "0px";
		dv.style.left = "0px";
		dv.style.height = y + "px";
		dv.style.width = x + "px";

		dv.style.display = "block";
	}
}

function closePopup(popupid) {
		dv = document.getElementById(popupid);
		dv.style.display = "none";
}

function showWait(msg) {
	if (!msg)
	{
		msg = "<div><b>Please wait .... </b><br><br><img src='images/loadingAnimation.gif'></div>";
	}
	openPopup("divWait", "#83c3e6");
	dv = document.getElementById("divWaitMessage");
	if (!dv)
	{
		dv = document.createElement("div");

		dv.style.position = "absolute";
		dv.style.display = "block";
		dv.innerHTML = "<table><tr><td align=right>"+'<a href="javascript:;" onclick="closeWait()" onmouseover="MM_swapImage(\'bttnclosepop\',\'\',\'images/closepop_over.gif\',0)" onmouseout="MM_swapImgRestore()"><img src="images/closepop_up.gif" name="bttnclosepop" width="20" height="20" border="0" id="bttnclosepop" /></a>'+ "</td></tr><tr><td>"+msg+"</td></tr></table>";
		opacity(dv, 0);
		dv.style.top = "0px";
		dv.style.left = "0px";
		dv.style.backgroundColor = "#ffffff";
		dv.style.color = "#1568b3";
		dv.style.zIndex = 2;	
		dv.style.borderWidth = "2px";	
		dv.style.borderStyle = "solid";	
		dv.style.borderColor = "#bfbfbf";	
		dv.style.padding = "5px";
		dv.id = "divWaitMessage";
		document.body.appendChild(dv);
		c = getCenter(208, 240);
		dv.style.left = c.x + "px";
		dv.style.top = c.y + "px";
	} else  {
		dv.innerHTML = "<table><tr><td align=right>"+'<a href="javascript:;" onclick="closeWait()" onmouseover="MM_swapImage(\'bttnclosepop\',\'\',\'images/closepop_over.gif\',0)" onmouseout="MM_swapImgRestore()"><img src="images/closepop_up.gif" name="bttnclosepop" width="20" height="20" border="0" id="bttnclosepop" /></a>'+ "</td></tr><tr><td>"+msg+"</td></tr></table>";
		dv.style.display = "block";
	}
	opacity(dv, 100);
 }

 function closeWait() {
		dv = document.getElementById("divWaitMessage");
		dv.style.display = "none";
		closePopup("divWait");
 }

function validateForm() {
var str = "";
var ft = "";
var fv = "";
var fn = "";
var els = "";
for(var i = 0; i < fobj.elements.length; i++) {
	els = fobj.elements[i];
	ft = els.title;
	fv = els.value;
	fn = els.name;
	
	reqFlag = false;
	if (els.title != "") {
		msg = els.title.split("_");
		if (msg[0] == "required")
		{
			reqFlag = true;
		}
	}
	switch(els.type) {
		 case "text":
		 case "hidden":
		 case "password":
		 case "textarea":
		 // is it a required field?
		   if (els.title && reqFlag && fv == "")
		   {
			   alert("Please fill up all required fields! The following field requres a value: " + els.name);
			   return false;
		   }
		  str += fn + "=" + encodeURIComponent(fv) + "&";
		  break;

		  case "checkbox":
		  case "radio":
			   if (els.type == "checkbox" && els.title && reqFlag &&  !els.checked )
			   {
				   alert("Please fill up all required fields!" + els.name);
				   return false;
			   }

		if(els.checked) str += fn + "=" + encodeURIComponent(fv) + "&";
		  break;


		  case "select-one":
			   if (els.title && reqFlag &&  els.selectedIndex == 0)
			   {
				   alert("Please fill up all required fields!" + els.name);
				   return false;
			   }

		 str += fn + "=" +

		 els.options[els.selectedIndex].value + "&";
		  break;
		  } // switch
	 } // for
	 str = str.substr(0,(str.length - 1));
	 return str;
}