var x,y,zInterval,zTitle,zText, windowWidth, windowHeight, xScroll, yScroll,mx,my;
function submit_values(type)
{
	if(type == 'note')
	{	
		var id =  document.getElementById('id').value;				
		var note =  document.getElementById('note').value;
		var url="/checklist/add-note2.php?id="+id+"&note="+note;
		winopen(url,'note'+id,'2');
	}
	else if(type == 'alert')
	{
		var id =  document.getElementById('id').value;				
		var alert_id = document.getElementById('alert_id').value;
		var alert_date = document.getElementById('alert_date').value;
		var alert_time = document.getElementById('alert_time').value;
		var timezone = document.getElementById('timezone').value;
		var emailid = document.getElementById('emailid').value;
		var url="/checklist/add-alert2.php?id="+id+"&alert_id="+alert_id+"&alert_date="+alert_date+"&alert_time="+alert_time+"&timezone="+timezone+"&emailid="+emailid;
		//alert(url);
		winopen(url,'alert'+id,'2');
	}
	else if(type == 'tl')
	{
		var id =  document.getElementById('id').value;				
		var timeline_id=  document.getElementById('timeline_id').value;
		var url="/checklist/change-timeline2.php?id="+id+"&timeline_id="+timeline_id;
		winopen(url,'tl'+id,'2');
	}
	else if(type == 'del')
	{
		var id =  document.getElementById('id').value;				
		var submit=  document.getElementById('submit').value;
		var url="/checklist/delete-task.php?id="+id+"&submit="+submit;
		winopen(url,'del'+id,'2');
	}
	else if(type == 'send')
	{
		var semail =  document.getElementById('semail').value;				
		var submit =  document.getElementById('submit').value;
		var status =  document.getElementById('status').value;
		var url="/checklist/send-mail.php?semail="+semail+"&submit="+submit+"&status="+status;
		winopen(url,'send','2');
	}
	else if(type == 'addtask')
	{
		var item =  document.getElementById('item').value;				
		var note =  document.getElementById('note').value;				
		var timeline_id =  document.getElementById('timeline_id').value;				
		var submit =  document.getElementById('submit').value;
		var url="/checklist/add-task2.php?item="+item+"&note="+note+"&timeline_id="+timeline_id+"&submit="+submit;
		winopen(url,'send','2');
	}
	refresh_frame();
}

function hidepopup() {
	document.getElementById("PopUp").style.display = "none";
}

function winopen(url,obj1,at) {
	var ht=getPageSize();
	if(at != '2')
	{
		var obj=document.getElementById(obj1);
		var x=findPosX(obj);
		var y=findPosY(obj);
		if(obj1 =='send' || obj1 =='addtask')
		{
			mx=x+150;
		}
		else{
			mx=x-100;
		}
		if(y+100 > ht/2 && ht/2 > 200)
		{
			my = (y + 292)-150;
		}
		else
		{
			my = y + 292;
		}
			
	}
	else
	{
		my=parent.document.getElementById("PopUp").style.top;
		mx=parent.document.getElementById("PopUp").style.left;
		my=my.replace(/px/i, "");
		mx=mx.replace(/px/i, "");
	}
xmlHttp=GetXmlHttpObject_popup();
if (xmlHttp==null)
{
	alert ("Your browser does not support AJAX!");
	return;
} 
if(url.substr(url.length-4,url.length) == '.php')
{
	url = url + "?sid="+Math.random();
}
else {
	url = url + "&sid="+Math.random();
}
xmlHttp.onreadystatechange=stateChanged_popup;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}

function GetXmlHttpObject_popup()
{
  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 stateChanged_popup() 
{ 
	if (xmlHttp.readyState==4)
	{ 
		parent.document.getElementById("PopUp").style.top = my + "px";
		parent.document.getElementById("PopUp").style.left = mx + "px";
		parent.document.getElementById("PopUp").innerHTML=xmlHttp.responseText;
		parent.document.getElementById("PopUp").style.display = "block";
	}
	else
	{
		parent.document.getElementById('PopUp').innerHTML="";
	}
} 

function hidepopup() {
	parent.document.getElementById("PopUp").style.display = "none";
}

/*get size of page*/
 function getPageSize()
{
    if (parent.window.innerHeight && parent.window.scrollMaxY)
    {
        xScroll = parent.document.body.scrollWidth;
        yScroll = parent.window.innerHeight + parent.window.scrollMaxY;
    }
    else if (document.body.scrollHeight > document.body.offsetHeight)
    {
        // all but Explorer Mac
        xScroll = parent.document.body.scrollWidth;
        yScroll = parent.document.body.scrollHeight;
    }
    else if (parent.document.documentElement && parent.document.documentElement.scrollHeight > parent.document.documentElement.offsetHeight)
    {
        // Explorer 6 strict mode
        xScroll = parent.document.documentElement.scrollWidth;
        yScroll = parent.document.documentElement.scrollHeight;
    }
    else
    {
        // Explorer Mac...would also work in Mozilla and Safari
        xScroll = parent.document.body.offsetWidth;
        yScroll = parent.document.body.offsetHeight;
    }

    
    if (self.innerHeight)
    {
        // all except Explorer
        windowWidth = self.innerWidth;
        windowHeight = self.innerHeight;
    }
    else if (parent.document.documentElement && parent.document.documentElement.clientHeight)
    {
        // Explorer 6 Strict Mode
        windowWidth = parent.document.documentElement.clientWidth;
        windowHeight = parent.document.documentElement.clientHeight;
    }
    else if (parent.document.body)
    {
        // other Explorers
        windowWidth = parent.document.body.clientWidth;
        windowHeight = parent.document.body.clientHeight;
    }

    // for small pages with total height less then height of the viewport
    if(yScroll < windowHeight)
    {
        pageHeight = windowHeight;
    }
    else
    {
        pageHeight = yScroll;
    }

    // for small pages with total width less then width of the viewport
    if(xScroll < windowWidth)
    {
        pageWidth = windowWidth;
    }
    else
    {
        pageWidth = xScroll;
    }

    arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
     return windowHeight;
}

//---Funation to get postion of text
function findPosX(obj)
  {
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
  }

  function findPosY(obj)
  {
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
  }

function refresh_frame()
{
	var browser=navigator.appName;
	frms=parent.document.getElementsByTagName("IFRAME");
	//alert(frms.length);
	for(i=0, x=frms.length; i<x; i++)
	{
		var src= frms[i].src;
		if(src != 'javascript:false;')//--this condition to check for datepicker frame---//
		{
			src = src + "?sid="+Math.random();
			frms[i].src=src;
		}
	}
}

function getDocHeight(doc) {
  var docHt = 0, sh, oh;
  if (doc.height) docHt = doc.height;
  else if (doc.body) {
    if (doc.body.scrollHeight) docHt = sh = doc.body.scrollHeight;
    if (doc.body.offsetHeight) docHt = oh = doc.body.offsetHeight;
    if (sh && oh) docHt = Math.max(sh, oh);
  }
  return docHt;
}

function setIframeHeight(iframeName) {
  var iframeWin = window.frames[iframeName];
  var iframeEl = document.getElementById? document.getElementById(iframeName): document.all? document.all[iframeName]: null;
  if ( iframeEl && iframeWin ) {
    iframeEl.style.height = "auto"; // helps resize (for some) if new doc shorter than previous  
    var docHt = getDocHeight(iframeWin.document);
    // need to add to height to be sure it will all show
    if (docHt) iframeEl.style.height = docHt + "px";
  }
}

function loadIframe(iframeName, url,type) {
	parent.document.getElementById("ifrm").src = url;
	parent.document.getElementById("toolTip").innerHTML="";
	parent.document.getElementById("toolTip").style.display ="none";

	parent.document.getElementById("PopUp").innerHTML="";
	parent.document.getElementById("PopUp").style.display ="none";

	/*if(type == 'complete')
	{
		parent.document.getElementById("list").innerHTML="";
		parent.document.getElementById("list").innerHTML="<a href=\"#\" onclick=\"javascript:loadIframe('ifrm','open-checklist.php','open');return false\" class=\"bottomlink\">View Open items </a>";
	}
	if(type == 'open')
	{
		parent.document.getElementById("list").innerHTML="";
		parent.document.getElementById("list").innerHTML="<a href=\"#\" onclick=\"javascript:loadIframe('ifrm','completed-checklist.php','complete');return false\" class=\"bottomlink\">View completed items </a>";

	}*/
}

function confirm_start()
{
	var con = confirm("Are you sure to revert all changes");
	if(con)
	{
		document.location.href='/checklist/start-over.php';
	}
	return false;
}

function updateitem(f) {
	//alert(f);
	if (!checkclick(f)) {
		return;
	}
	f.submit();
	//f.submit();
}
/** make sure at least one checkbox is checked */
function checkclick(f) {
	for (var i = 0; i < f.elements.length; i++) {
		if (f.elements[i].name.indexOf("done") == 0) {
			if (f.elements[i].checked) {
				return true;
			}
		}
	}
	alert('Please choose at least one Task');
	return false;
}

function call_state(id,val,type,sel,country)
{
	var newc=new showCity(id,val,type);
	newc.update();
	//alert(type);
	if(type == 'state'){id='cityid'; ctype='city';}
	if(type == 'wstate'){id='wcityid';ctype='wcity';}
	var newc1=new showCity(id,'',ctype,'',val);
	newc1.update();
}

/* --- Ajax function to show city list accoring to selected country-----*/
function showCity(id,val,type,sel,country)
{
	var that=this;  
	// A workaround for some javascript idiosyncrocies
	var updating = false;
	// Set to true if this object is already working on a request 
	this.callback = function() {}  
	// A post-processing call -- a stub you overwrite.
	this.update = function() { 
	// Initiates the server call.  
	if (updating==true) { return false; }  
	// Abort if we're already processing a call.
	updating=true;                        
	/*if (val.length==0)
	{
	document.getElementById(id).innerHTML="";
	return;
	}*/
	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");
      }
    }
	if (xmlHttp==null)
	  {
	  alert ("Your browser does not support AJAX!");
	  return;
	  } 
	  else
		{
		 xmlHttp.onreadystatechange=function(){
			 if (xmlHttp.readyState==4)
			{ 
				document.getElementById(id).innerHTML=xmlHttp.responseText;
				delete xmlHttp;
				updating=false;                                       //   Set the updating flag to false so we can do a new request
				that.callback();
			}
			else
			{
				document.getElementById(id).innerHTML="";
			}
		}
	var url="/checklist/ssi/city-state-array.php";
	url=url+"?value="+val;
	url=url+"&type="+type;
	url=url+"&sel="+sel;
	url=url+"&country="+country;
	url=url+"&sid="+Math.random();
	//xmlHttp.onreadystatechange=stateChanged(id);
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	}
	}
}

function calcCharLeft(sForm, sTextArea, sTextInput, iMaxLimit)
{
	var _oDF        = document.forms[sForm];
	var _oTxtA      = _oDF.elements[sTextArea];
	var _iMaxLength = (!iMaxLimit) ? 100 : iMaxLimit;
	var _iCharLeft  = _oTxtA.value.length;

	

	if(_iCharLeft > _iMaxLength)
	{
		_oTxtA.value = _oTxtA.value.substring(0, _iMaxLength);
		alert('You can enter only '+_iMaxLength+' characters.');
	}
}