// located in: libs/javascript/javafunctions.js

function ShowZip() {
  var zip = parseInt(document.getElementById("zipcode").value);
  var rad = document.getElementById("radius").value;
  if (!zip) {
	alert("Please enter zip code");
	return false;
  }
  var url = "search.php?zip=" + zip + "&radius=" + rad;
  popUp(url, 600,500, zip);
}

function ShowJSMenu(myInOut, mID) {
	if (myInOut) {
		var oM = "window.mm_menu_" + mID + ",4,99,null,'mainarea'";
		MM_showMenu(oM);
	} else {
		MM_startTimeout();
	}
}

function ConfirmDelete(mesg, myUrl) {
  var answer = confirm(mesg);
  if (answer) {
	popUp(myUrl, 500, 400, 'addsub1');
  }
}

function ConfirmDeleteSame(mesg, myUrl) {
  var answer = confirm(mesg);
  if (answer) {
	location.href=myUrl;
  }
}

function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function swap_pic(image,directory,sel) {
   if ((x=MM_findObj(image))!=null){
      x.src = directory + "/" + sel.options[sel.selectedIndex].value;
   }
}

function setnsubmit(sentdate) {
   if ( document.all['modified'].value == "1") {
       alert("<center>Changes have been made....<br>Please save or disregard them before changing the date!</center>");
   } else {
      document.all['datebox'].value=sentdate;
      document.form1.submit();
   }
}

function swapImage(ImageName) {
   var picUrl = document.images[ImageName].src;
   var picStart = picUrl.indexOf("Graphics");
   var picEnd = picUrl.length;
   var picPath = picUrl.substring(picStart, picEnd);
   var ImageName2 = "hid" + ImageName;
   if (picPath == "Graphics/off.jpg") {
      document.images[ImageName].src = 'Graphics/on.jpg';
      document.all[ImageName2].value= 'on';
   } else {
      document.images[ImageName].src = 'Graphics/off.jpg';
      document.all[ImageName2].value= 'off';
   }
   document.all['modified'].value = '1';
}

function swapRow(ImageName) {
   var stopnow = 0;
   var colStart = ImageName.indexOf("c")
   var Row = ImageName.substring(1,colStart);
   var Col = ImageName.substring(colStart+1,ImageName.length);
   var nCol = Col;
   var startCol = Col;

   while (nCol >= 1 && stopnow == 0) {
      chkImage = "r" + Row + "c" + nCol
      var picUrl = document.images[chkImage].src;
      var picStart = picUrl.indexOf("Graphics");
      var picEnd = picUrl.length;
      var picPath = picUrl.substring(picStart, picEnd);
      if (picPath == "Graphics/on.jpg") {
         startCol = nCol;
         stopnow = 1;
      } else {
         startCol = nCol;
         nCol -= 1;
      }
   }

   for (i=startCol; i<=Col; i++) {
      var imgSlot = "r" + Row + "c" + i;
      var imgSlot2 = "hidr" + Row + "c" + i;
      document.images[imgSlot].src = 'Graphics/on.jpg';
      document.all[imgSlot2].value= 'on';
   }
   document.all['modified'].value = '1';
}

function ClearRow(row) {
   for (i=0; i<=47; i++) {
      var imgSlot = "r" + row + "c" + i;
      var imgSlot2 = "hidr" + row + "c" + i;
      document.images[imgSlot].src = 'Graphics/off.jpg';
      document.all[imgSlot2].value= 'off';
   }
   document.all['modified'].value = '1';
}

function ClearAll() {
   status = "Please Wait...while I clear the data";
   var last_row = document.all['numrows'].value;
   for (j=1; j<=last_row; j++) {
      for (i=0; i<=47; i++) {
         var imgSlot = "r" + j + "c" + i;
         var imgSlot2 = "hidr" + j + "c" + i;
         document.images[imgSlot].src = 'Graphics/off.jpg';
         document.all[imgSlot2].value= 'off';
      }
   }
   status = "Ready";
}

function employee_dropboxAction(hidfield) {
     document.all[hidfield].value=document.all['selName'].value
     document.form1.submit();
}

function popUp1(location,w,h,val) {
   if (!w) { w = 440 };
   if (!h) { h = 430 };
   posX = (screen.availWidth / 2) - (w / 2)
   posY = ((screen.availHeight-100)/ 2) - (h / 2)
   winProps = "resizeable=1,scrollbars=1,location=1,menubar=1,status=1,width=" + w + ",height=" + h + ",left=" + posX + ",top=" + posY
   window.open(location,'popup'+val,winProps);

}
function popUpSame(location,w,h,val) {
  window.location = location;
}
function popUp(location,w,h,val) {
   if (!w) { w = 640 };
   if (!h) { h = 430 };
   if (!val) { val = 1 };
   posX = (screen.availWidth / 2) - (w / 2)
   posY = ((screen.availHeight-100)/ 2) - (h / 2)
   if (location.match("store") != null)
   {
   winProps = "toolbar=0,scrollbars=1,location=0,status=1,menubar=0,resizable=1,left=" + posX + ",top=" + posY
   } else {
   winProps = "toolbar=0,scrollbars=1,location=0,status=1,menubar=0,resizable=1,width=" + w + ",height=" + h + ",left=" + posX + ",top=" + posY
   }
   var oWin = window.open(location,val,winProps);
   return oWin;

}

function popUp2(location,w,h,val) {
   if (!w) { w = 440 };
   if (!h) { h = 430 };
   posX = (screen.availWidth / 2) - (w / 2)
   posY = ((screen.availHeight-100)/ 2) - (h / 2)
   winProps = "toolbar=0,scrollbars=1,location=0,status=1,menubar=0,resizable=1,width=" + w + ",height=" + h + ",left=" + posX + ",top=" + posY
   window.open(location,'popup'+val,winProps);
}

function chgimage(location, picpath, picbox, pic, piclinkbox, piclink) {
   opener.document.images[location].src=picpath;
   if (document.all) { //Using IE browser
      opener.document.all[picbox].value = pic;
	  opener.document.all[piclinkbox].value = piclink;
	  self.close();
   } else {
      opener.document.form1.picbox.value = pic;
	  opener.document.form1.piclinkbox.value = piclink;
	  self.close;
   }
}

function getSelectedRadio(buttonGroup) {
   if (buttonGroup[0]) { // if the button group is an array (one button is not an array)
      for (var i=0; i<buttonGroup.length; i++) {
         if (buttonGroup[i].checked) {
            return true;
         }
      }
   } else {
      if (buttonGroup.checked) { return true; } // if the one button is checked, return zero
   }
   // if we get to this point, no radio button is selected
   return false;
}


function CheckForm(myForm) {
	var myFields = myForm.fields.value;
	var myTitles = myForm.ftitles.value;
	var x = 0;
	var myErr = "";
	arr_req   = myFields.split(",");
	arr_title = myTitles.split(",");
	var y = 1;
	for (x=0;x<arr_req.length;x++) {
	var myValField = eval("document." + myForm.name + "." + arr_req[x]);
        if (!myValField.disabled) {
	 if (arr_req[x] == "shows") {
		var myShow = getSelectedRadio(myValField);
		if (!myShow) {
			myErr = myErr + "    " + y +": " + arr_title[x] + " is missing.\n";
                	y = y + 1;
		}
	 } else {
	  myVal = eval("document." + myForm.name + "." + arr_req[x] + ".value");
	  myValType = eval("document." + myForm.name + "." + arr_req[x] + ".type");
	  myValLen = eval("document." + myForm.name + "." + arr_req[x] + ".length");
	  if (myVal.length < 1) {
		myErr = myErr + "    " + y +": " + arr_title[x] + " is missing.\n";
		y = y + 1;
	  }

	  if (arr_req[x] == "email") {
		if (myVal.indexOf('@',0)==-1 || myVal.indexOf('@',0)== 0 || myVal.indexOf('.',0)==-1) {
		  myErr = myErr + "    " + y + ": " + arr_title[x] + " is invalid.\n";
		  y = y + 1;
		}
	  }

	}
	}
	}
	if (y > 1) {
		myMsg = "ERROR: The following errors were found:\n";
		myMsg = myMsg + "--------------------------------------------------\n\n";

		myMsg = myMsg + myErr + "\n";
		myMsg = myMsg + "--------------------------------------------------\n";
		myMsg = myMsg + "Please correct the errors and try again.";
		alert(myMsg);
		return false;
	}
	return true;
}

function getCityState(zipCode, stripped, limit, source, cid_filter)
{
	if(limit == null) limit = '';
	if(cid_filter == null) cid_filter = '';
	if(source == null) source = 'vc';
	if( zipCode != "") 
	{
		zipCode = removeLZero(zipCode.toString());
		var ajaxLoader = document.getElementById("zipcode_result");
		ajaxLoader.style.display = "";
		strParams = "&zip=" + zipCode + "&radius=20&source="+ source +"&stripped="+ stripped +"&limit="+ limit +"&cid_filter="+cid_filter;
		$.ajax({
			url: 'http://www.mcdtoday.com/search.php',
			type: 'GET',
			data: strParams,
			timeout: 3000,
			error: function(strReponseResult){},
			success: function(strReponseResult){ 
				document.getElementById("zipcode_result").innerHTML = strReponseResult;	 
			}
		});
	}
	else
	{
		alert("Please Enter a Zipcode");
	}
}

function removeLZero(z)
{
	while (z.charAt(0) == "0")
	{
		z = z.substring(1, z.length);
	}
	if (z.charAt(0) == ".") z = "0" + z;
	
	return z;
}

function winResize(myWidth,myHeight) {
  //var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    window.innerWidth = myWidth;
    window.innerHeight = myHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    document.documentElement.clientWidth = myWidth; 
    document.documentElement.clientHeight = myHeight; 
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    document.body.clientWidth = myWidth;
    document.body.clientHeight = myHeight; 
  }
}