// initialize some global vars
// Global variables
xMousePos = 0; // Horizontal position of the mouse on the screen
yMousePos = 0; // Vertical position of the mouse on the screen
xMousePosMax = 0; // Width of the page
yMousePosMax = 0; // Height of the page
var xCoord, yCoord, br, ver;


// very basic check for browser type and version
if (navigator.appName == "Netscape") {
	br="ns";
}
ver = navigator.appVersion.substring(0,1);

// function to make tool tip visible
// lyr is NOT an object


function rollOn1(lyr,xx,yy) {
	// if it's a netscape browser and not over version 4
    if (br == "ns" && ver <= 4) {
		lyr = document.layers[lyr];
		lyr.position='absolute';
		lyr.left = xMousePos-xx;
		lyr.top = yMousePos-yy;
		lyr.visibility = 'visible';
		lyr.zIndex=2;
    } else {
    	lyr = document.getElementById(lyr);
		lyr.style.position='absolute';
		lyr.style.left = xMousePos-xx;
		lyr.style.top = yMousePos-yy;
		lyr.style.visibility = 'visible';
		lyr.style.zIndex=2;
    }
}


function rollOn(lyr) {
	// if it's a netscape browser and not over version 4
    if (br == "ns" && ver <= 4) {
		lyr = document.layers[lyr];
		lyr.position='absolute';
		lyr.left = xMousePos+15;
		lyr.top = yMousePos+15;
		lyr.visibility = 'visible';
		lyr.zIndex=2;
    } else {
    	lyr = document.getElementById(lyr);
		lyr.style.position='absolute';
		lyr.style.left = xMousePos+15;
		lyr.style.top = yMousePos+15;
		lyr.style.visibility = 'visible';
		lyr.style.zIndex=2;
    }
}



// function to make tool tip hidden
// lyr is NOT an object

function rollOut(lyr) {
	if(br == "ns" && ver <= 4) {
		lyr = document.layers[lyr];
		lyr.visibility = 'hidden';
	} else {
		lyr = document.getElementById(lyr);
		lyr.style.visibility='hidden';
	}
}

// this code just tracks the mouse coordinates
// need these so we know where to pop up the tool tips

// Set Netscape up to run the "captureMousePosition" function whenever
// the mouse is moved. For Internet Explorer and Netscape 6, you can capture
// the movement a little easier.
if (document.layers) { // Netscape
    document.captureEvents(Event.MOUSEMOVE);
    document.onmousemove = captureMousePosition;
} else if (document.all) { // Internet Explorer
    document.onmousemove = captureMousePosition;
} else if (document.getElementById) { // Netcsape 6
    document.onmousemove = captureMousePosition;
}


function captureMousePosition(e) {
    if (document.layers) {
        // When the page scrolls in Netscape, the event's mouse position
        // reflects the absolute position on the screen. innerHight/Width
        // is the position from the top/left of the screen that the user is
        // looking at. pageX/YOffset is the amount that the user has
        // scrolled into the page. So the values will be in relation to
        // each other as the total offsets into the page, no matter if
        // the user has scrolled or not.
        xMousePos = e.pageX;
        yMousePos = e.pageY;
        xMousePosMax = window.innerWidth+window.pageXOffset;
        yMousePosMax = window.innerHeight+window.pageYOffset;
    } else if (document.all) {
        // When the page scrolls in IE, the event's mouse position
        // reflects the position from the top/left of the screen the
        // user is looking at. scrollLeft/Top is the amount the user
        // has scrolled into the page. clientWidth/Height is the height/
        // width of the current page the user is looking at. So, to be
        // consistent with Netscape (above), add the scroll offsets to
        // both so we end up with an absolute value on the page, no
        // matter if the user has scrolled or not.
        xMousePos = window.event.x+document.body.scrollLeft;
        yMousePos = window.event.y+document.body.scrollTop;
        xMousePosMax = document.body.clientWidth+document.body.scrollLeft;
        yMousePosMax = document.body.clientHeight+document.body.scrollTop;
    } else if (document.getElementById) {
        // Netscape 6 behaves the same as Netscape 4 in this regard
        xMousePos = e.pageX;
        yMousePos = e.pageY;
        xMousePosMax = window.innerWidth+window.pageXOffset;
        yMousePosMax = window.innerHeight+window.pageYOffset;
    }
}


function popup(url,width,height){
	if(window.innerWidth){LeftPosition =(window.innerWidth-width)/2;TopPosition =((window.innerHeight-height)/4)-50;}
	else{LeftPosition =(parseInt(window.screen.width)-width)/2;TopPosition=((parseInt(window.screen.height)-height)/2)-50;}
	attr = "resizable=no,scrollbars=yes,width=" + width + ",height=" + height + ",screenX=300,screenY=200,left=" + LeftPosition + ",top=" + TopPosition + "";
	msgWindow=open(url, "new_window", attr);
	if (msgWindow.opener == null) msgWindow.opener = self;
	msgWindow.focus();
}

function callback_popup(url,width,height,win){
	if(window.innerWidth){LeftPosition =(window.innerWidth-width)/2;TopPosition =((window.innerHeight-height)/4)-50;}
	else{LeftPosition =(parseInt(window.screen.width)-width)/2;TopPosition=((parseInt(window.screen.height)-height)/2)-50;}
	attr = "resizable=no,scrollbars=yes,width=" + width + ",height=" + height + ",screenX=300,screenY=200,left=" + LeftPosition + ",top=" + TopPosition + "";
	msgWindow2=open(url, win, attr);
	if (msgWindow2.opener == null) msgWindow2.opener = self;
	msgWindow2.focus();
}


function nottab(e)
{
	var code;
	if (!e) var e = window.event;
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;
	if(code==9)return false;
	else return true;
}

function logout(width,height){
	if(window.innerWidth){LeftPosition =(window.innerWidth-width)/2;TopPosition =((window.innerHeight-height)/4)-50;}
	else{LeftPosition =(parseInt(window.screen.width)-width)/2;TopPosition=((parseInt(window.screen.height)-height)/2)-50;}
	attr = "resizable=no,scrollbars=yes,width=" + width + ",height=" + height + ",screenX=300,screenY=200,left=" + LeftPosition + ",top=" + TopPosition + "";
	msgWin=open('', "new_window", attr);
	msgWin.document.write('<head><title>Log out?<\/title><\/head><body style="font-family:Arial, Helvetica, sans-serif;font-size: 10pt;" bgcolor="#ffffff">');
msgWin.document.write('<span class=text><p align=justify>If you have added or created any new data within <i>The Customer Factor</i>,<br>it is recommended that you back up your data before logging out.<br> Would you like to do that now?</span>');
msgWin.document.write('<p align=center><input type=button onclick="window.opener.location=\'myaccount.php?action=backup#bkup\';self.close();" value=Yes>&nbsp;&nbsp;&nbsp;&nbsp;<input type=button onclick="window.opener.location=\'logout.php\';self.close();" value="No, log me out">');
msgWin.document.write('<\/p><\/body><\/html>');

	if (msgWin.opener == null) msgWin.opener = self;
	msgWin.focus();

}



function uncheckall() { document.f1.reset(); }

function checkall() {
	with (document.f1) {
	for (var i=0; i < elements.length; i++) {
	if (elements[i].type == 'checkbox' && elements[i].name == 'selected_id[]'){ 	elements[i].checked = true; }
											}
						}
					}

function expc()
{
 var found = 0;
 var selids="";

 with (document.f1)
 {
  for (var i=0; i < elements.length; i++)
  {
   if (elements[i].type == 'checkbox' && elements[i].name == 'selected_id[]')
   {
    if (elements[i].checked == true)
     {
       if (selids!="")
        selids=selids+",";

       selids=selids+elements[i].value;
       found=1;
     }
   }
  }
 }

 if (found == 0)
 {
  alert('There are no Customers selected.');
 } else {
          var printWin = window.open("customers_print.php?id="+selids+"","fupdate","scrollbars=yes,width=775,height=500");
         document.f1.reset();
         document.getElementById("c_upall").style.display = "block";
         document.getElementById("u_upall").style.display = "none";
         document.getElementById("c_downall").style.display = "block";
         document.getElementById("u_downall").style.display = "none";
         if(!printWin)
         {
           alert("Customers Print Screen has been blocked. Please disable your popup blocker to view the Print Screen.");
         }
 }
}

function del_cus()
{

 var found = 0;

 with (document.f1)
 {
  for (var i=0; i < elements.length; i++)
  {
   if (elements[i].type == 'checkbox' && elements[i].name == 'selected_id[]')
   {
    if (elements[i].checked == true)
     found = 1;
   }
  }
 }

 if (found == 0)
 {
  alert('There are no Customers selected.');
 } else {if (confirm("Are you sure you want to delete selected Customer(s)?"))
         {
          document.f1.task.value='delete';
  	      document.f1.submit();
         }
       }
}

function del_pros()
{

 var found = 0;

 with (document.f1)
 {
  for (var i=0; i < elements.length; i++)
  {
   if (elements[i].type == 'checkbox' && elements[i].name == 'selected_id[]')
   {
    if (elements[i].checked == true)
     found = 1;
   }
  }
 }

 if (found == 0)
 {
  alert('There are no Prospects selected.');
 } else {if (confirm("Are you sure you want to delete selected Prospect(s)?"))
         {
          document.f1.task.value='delete';
  	      document.f1.submit();
         }
       }
}

function expp()
{
 var found = 0;
 var selids="";

 with (document.f1)
 {
  for (var i=0; i < elements.length; i++)
  {
   if (elements[i].type == 'checkbox' && elements[i].name == 'selected_id[]')
   {
    if (elements[i].checked == true)
     {
       if (selids!="")
        selids=selids+",";

       selids=selids+elements[i].value;
       found=1;
     }
   }
  }
 }

 if (found == 0)
 {
  alert('There are no Prospects selected.');
 } else {
          var printWin = window.open("prospects_print.php?id="+selids+"","fupdate","scrollbars=yes,width=775,height=500");
         document.f1.reset();
         document.getElementById("c_upall").style.display = "block";
         document.getElementById("u_upall").style.display = "none";
         document.getElementById("c_downall").style.display = "block";
         document.getElementById("u_downall").style.display = "none";
         if(!printWin)
         {
           alert("Prospects Print Screen has been blocked. Please disable your popup blocker to view the Print Screen.");
         }

 }
}
function exppmod()
{
 var found = 0;

 with (document.f1)
 {
  for (var i=0; i < elements.length; i++)
  {
   if (elements[i].type == 'checkbox' && elements[i].name == 'selected_id[]')
   {
    if (elements[i].checked == true)
     found = 1;
   }
  }
 }

 if (found == 0)
 {
  alert('There are no Prospects selected.');
 } else {
          document.f1.action='prospects_export.php';
  	      document.f1.submit();
 }
}
function expcmod()
{
 var found = 0;

 with (document.f1)
 {
  for (var i=0; i < elements.length; i++)
  {
   if (elements[i].type == 'checkbox' && elements[i].name == 'selected_id[]')
   {
    if (elements[i].checked == true)
     found = 1;
   }
  }
 }

 if (found == 0)
 {
  alert('There are no Customers selected.');
 } else {
          document.f1.action='customers_export.php';
  	      document.f1.submit();
 }
}
function enable()
{
document.getElementById("u_all").style.display = "none";
}
function check_up()
	{
    if (document.f1.toggleAllup.checked)
      {
      document.f1.toggleAlldown.checked=true;
      document.getElementById("c_upall").style.display = "none";
      document.getElementById("u_upall").style.display = "block";
      document.getElementById("c_downall").style.display = "none";
      document.getElementById("u_downall").style.display = "block";
      }
      else
      {
      document.f1.toggleAlldown.checked=false;
      document.getElementById("c_upall").style.display = "block";
      document.getElementById("u_upall").style.display = "none";
      document.getElementById("c_downall").style.display = "block";
      document.getElementById("u_downall").style.display = "none";
      }
        var checkflag;

	    var prefix='selected_id';

        if (document.f1.toggleAllup.checked)
           checkflag=true;

	    else
	       checkflag=false;

	    if (prefix)
	        var reg = new RegExp('^'+prefix, '');

	    var form=document.f1;

	    for (var i = 0; i < form.elements.length; i++) {
	        if (form.elements[i].type == 'checkbox' && (!prefix || form.elements[i].name.search(reg) == 0) && !form.elements[i].disabled)
	            form.elements[i].checked = checkflag;
	    }

	}
function check_down()
	{

    if (document.f1.toggleAlldown.checked)
      {
      document.f1.toggleAllup.checked=true;
      document.getElementById("c_downall").style.display = "none";
      document.getElementById("u_downall").style.display = "block";
      document.getElementById("c_upall").style.display = "none";
      document.getElementById("u_upall").style.display = "block";
      }
      else
      {
      document.f1.toggleAllup.checked=false;
      document.getElementById("c_downall").style.display = "block";
      document.getElementById("u_downall").style.display = "none";
      document.getElementById("c_upall").style.display = "block";
      document.getElementById("u_upall").style.display = "none";
      }
        var checkflag;

	    var prefix='selected_id';

	    if (document.f1.toggleAlldown.checked)
	       checkflag=true;
	    else
	       checkflag=false;

	    if (prefix)
	        var reg = new RegExp('^'+prefix, '');

	    var form=document.f1;

	    for (var i = 0; i < form.elements.length; i++) {
	        if (form.elements[i].type == 'checkbox' && (!prefix || form.elements[i].name.search(reg) == 0) && !form.elements[i].disabled)
	            form.elements[i].checked = checkflag;
	    }

	}
function save() {
  	document.f2.submit2.value=1;
	document.f2.submit();
						}

function add() {
  	document.f5.submit5.value=1;
	document.f5.submit();
						}

function update_rec_jobs() {
  	document.f5.mode.value="update";
  	document.f5.submit5.value=1;
	document.f5.submit();
						}

function delete_rec_jobs() {
  if (confirm("You are about to delete All recurring jobs information and All scheduled recurring jobs for this customer. Continue?"))
  {
  	document.f5.mode.value="delete";
  	document.f5.submit5.value=1;
	document.f5.submit();
  }
}

function save_create() {
  	document.f2.submit2.value=2;
	document.f2.submit();
						}
function profile_save() {
  	document.f2.action="cprofile.php";
  	document.f2.submit2.value=1;
	document.f2.submit();
						}

function profile_save_create() {
  	document.f2.action="invoice_create.php";
  	document.f2.submit2.value=2;
	document.f2.submit();
						}

function create() {
	document.f2.action='customers_invoice_create.php';
  	document.f2.submit2.value=2;
	document.f2.submit();
						}

function uncheckall() { document.f1.reset(); }

function checkall() {
	with (document.f1) {
	for (var i=0; i < elements.length; i++) {
	if (elements[i].type == 'checkbox' && elements[i].name == 'selected_id[]'){ 	elements[i].checked = true; }
											}
						}
					}

function del() {
 document.f1.task.value='delete';
document.f1.submit();
			}
function exp() {
 document.f1.task.value='export';
document.f1.submit();
			}
function pjobs_same()
{
	if(document.f4.same_jobs.checked==1)
	{
      document.f4.sjob_type1.options.selectedIndex=document.f2.job_type1.options.selectedIndex;
      document.f4.sjob_type2.options.selectedIndex=document.f2.job_type2.options.selectedIndex;
      document.f4.sjob_type3.options.selectedIndex=document.f2.job_type3.options.selectedIndex;
      document.f4.sjob_type4.options.selectedIndex=document.f2.job_type4.options.selectedIndex;
    }

}

function jobs(){
if(document.f4.same_jobs.checked == true){
	document.f4.sjob_type1[document.f4.sjob_type1.selectedIndex].text =document.f2.job_type1[document.f2.job_type1.selectedIndex].text;
	document.f4.sjob_type1[document.f4.sjob_type1.selectedIndex].value =document.f2.job_type1[document.f2.job_type1.selectedIndex].value;
	document.f4.sjob_type2[document.f4.sjob_type2.selectedIndex].text =document.f2.job_type2[document.f2.job_type2.selectedIndex].text;
	document.f4.sjob_type2[document.f4.sjob_type2.selectedIndex].value =document.f2.job_type2[document.f2.job_type2.selectedIndex].value;
	document.f4.sjob_type3[document.f4.sjob_type3.selectedIndex].text =document.f2.job_type3[document.f2.job_type3.selectedIndex].text;
	document.f4.sjob_type3[document.f4.sjob_type3.selectedIndex].value =document.f2.job_type3[document.f2.job_type3.selectedIndex].value;
	document.f4.sjob_type4[document.f4.sjob_type4.selectedIndex].text =document.f2.job_type4[document.f2.job_type4.selectedIndex].text;
	document.f4.sjob_type4[document.f4.sjob_type4.selectedIndex].value =document.f2.job_type4[document.f2.job_type4.selectedIndex].value;
															}
if(document.f4.same_jobs.checked == false){
		window.location.href=window.location.href;															}
						}
function prices(){
if(document.f4.same_prices.checked == true){
			document.f4.sest1.value =document.f2.est1.value;
			document.f4.sest2.value =document.f2.est2.value;
			document.f4.sest3.value =document.f2.est3.value;
			document.f4.sest4.value =document.f2.est4.value;
															}
if(document.f4.same_prices.checked == false){
			document.f4.sest1.value ='';
			document.f4.sest2.value ="";
			document.f4.sest3.value ="";
			document.f4.sest4.value ="";
														}
							}
function locations(){
if(document.f4.same_locations.checked == true){
	document.f4.sjob_location1[document.f4.sjob_location1.selectedIndex].text =document.f2.job_location1[document.f2.job_location1.selectedIndex].text;
	document.f4.sjob_location1[document.f4.sjob_location1.selectedIndex].value =document.f2.job_location1[document.f2.job_location1.selectedIndex].value;
	document.f4.sjob_location2[document.f4.sjob_location2.selectedIndex].text =document.f2.job_location2[document.f2.job_location2.selectedIndex].text;
	document.f4.sjob_location2[document.f4.sjob_location2.selectedIndex].value =document.f2.job_location2[document.f2.job_location2.selectedIndex].value;
	document.f4.sjob_location3[document.f4.sjob_location3.selectedIndex].text =document.f2.job_location3[document.f2.job_location3.selectedIndex].text;
	document.f4.sjob_location3[document.f4.sjob_location3.selectedIndex].value =document.f2.job_location3[document.f2.job_location3.selectedIndex].value;
	document.f4.sjob_location4[document.f4.sjob_location4.selectedIndex].text =document.f2.job_location4[document.f2.job_location4.selectedIndex].text;
	document.f4.sjob_location4[document.f4.sjob_location4.selectedIndex].value =document.f2.job_location4[document.f2.job_location4.selectedIndex].value;
															}
if(document.f4.same_locations.checked == false){
		window.location.href=window.location.href;															}
						}
function clearRB(buttonGroup)
{
   for (i=0; i < buttonGroup.length; i++)
   {
     if (buttonGroup[i].checked == true)
     {
         buttonGroup[i].checked = false;
     }
   }
}

function ajax_get_details1(btype,cat)
{
   var xmlHttp;

   try
   {
      // Firefox, Opera 8.0+, Safari
      xmlHttp=new XMLHttpRequest();
   }
   catch (e)
   {
      // Internet Explorer
      try
      {
         xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
      catch (e)
      {
         try
         {
           xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
         }
         catch (e)
         {
           alert("Your browser does not support AJAX!Please upgrade your browser to the latest version");
           return false;
         }
      }
   }
   xmlHttp.onreadystatechange=function()
   {
      if(xmlHttp.readyState==4)
      {
         duplicatecheck(xmlHttp.responseText);
      }
    }
    t=new Date().getTime();
    if(cat=="residential")
    {
    var data="first_name="+escape(document.f2.first_name.value)+"&last_name="+escape(document.f2.last_name.value)+"&cat="+escape(cat)+"&btype="+escape(btype)+"&bustcache="+t;
    }
    else
    {
    var data="company_name="+escape(document.f2.company_name.value)+"&cat="+escape(cat)+"&btype="+escape(btype)+"&bustcache="+t;
    }
    xmlHttp.open("GET", "duplicate_check.php?"+data, true);
    xmlHttp.send(null);
}

 //returns the absolute position of some element within document
var __isFireFox=navigator.userAgent.match(/gecko/i);
function GetElementAbsolutePos(element) {
 var res = new Object();
 res.x = 0; res.y = 0;
 if (element !== null) {
  res.x = element.offsetLeft;
  res.y = element.offsetTop;

  var offsetParent = element.offsetParent;
  var parentNode = element.parentNode;

  while (offsetParent !== null) {
   res.x += offsetParent.offsetLeft;
   res.y += offsetParent.offsetTop;

   if (offsetParent != document.body && offsetParent != document.documentElement) {
    res.x -= offsetParent.scrollLeft;
    res.y -= offsetParent.scrollTop;
   }
   //next lines are necessary to support FireFox problem with offsetParent
   if (__isFireFox) {
    while (offsetParent != parentNode && parentNode !== null) {
     res.x -= parentNode.scrollLeft;
     res.y -= parentNode.scrollTop;

     parentNode = parentNode.parentNode;
    }
   }
   parentNode = offsetParent.parentNode;
   offsetParent = offsetParent.offsetParent;
  }
 }
    return res;
}

function popup_pdfdiv()
{
    var div_w=400;
    var div_h=600;

 var tempX = (screen.width - div_w) / 2;
 var tempY = (screen.height - div_h) / 2;

    link_pos=GetElementAbsolutePos(document.getElementById('pdfbutton'));

    if (link_pos.y > 0)
    {
       tempY=link_pos.y-(div_h/2);
    }
    else
       tempY=500;

    document.getElementById('div_openpdf').style.top=tempY+"px";
    document.getElementById('div_openpdf').style.left=tempX+"px";

    document.getElementById('div_openpdf').style.visibility='visible';
}

function pdfdiv()
{
    document.write('<div id="div_openpdf" style="position:absolute; visibility:hidden; height:165px; width:400px; z-index:5;border-style:solid;border-width: 1px; border-color:#0053A6" class="noprint"><form name="f3" method="post"><table width=100% height=100% cellpadding=4 cellspacing=0 bgcolor="#FFFFCC" class="text"><tr><td><input type="radio" name="selectpdf" value="price" checked>Do you want to save the calendar in pdf format with job pricing showing?</td></tr><tr><td><input type="radio" name="selectpdf" value="noprice">Would you rather save the calendar in pdf format with no job pricing showing?</td></tr><tr><td align="center"><input type="button" value="Ok" onclick="if (document.f3.selectpdf[0].checked) {savepdf(); } else {savepdf_noprice();}document.getElementById(\'div_openpdf\').style.visibility=\'hidden\';">&nbsp;&nbsp;<input type="button" onClick="javascript:document.getElementById(\'div_openpdf\').style.visibility=\'hidden\';;" value="Cancel"></td></tr></form></table></div>');

}
function openDiv_color(s,sign)
{
    var e=0;
    if(document.getElementById(sign).innerHTML== "+")
    {
        e=1;
    }
    document.getElementById("lclick").innerHTML= "+";
    document.getElementById("div_lcolor").style.display= "none";
    if(e==1)
    {
      document.getElementById(sign).innerHTML= "-";
      document.getElementById(s).style.display= "block";
    }

}