﻿/*
 * Common.js
 */
 var detect,browser,thestring,OS;
 var java=new Object();
 var d = document;
 var workingImageX = null;
 var workingImageY = null;
 var mcph = 'ctl00_MainContentPlaceHolder_'; //main content place holder

/************************************
	function VerifyPopup

	Check that the window we just tried to open was successfully opened.
   If it was not, display a message telling the user that their popup blocker
   has blocked the window.
*************************************/
function VerifyPopup(val) {
	if (val === null) {
		var popup = "Your browser appears to be blocking popups.  " + 
			"This content appears in a separate window to allow you to view it at " +
			"the same time that you continue using ProlabExpress.  " +
			"To access this content, you will need to allow this popup.  For many popup blockers " +
			"this can be done by holding down the CTRL key while clicking the link or button.";
		alert(popup);
	} else {
		// If window exists, put the focus on it, in case it was already
		// open, but buried under other windows.
		val.focus();
	}	
}//end VerifyPopup

/************************************	
	function KeyPressed

	Returns true if keypress was the given keyCode, false otherwise
*************************************/
function KeyPressed(keyCode, e) {
	var key;
	if (window.event) { key = window.event.keyCode; }  //IE
	else { key = e.which; }	//firefox
	if (key == keyCode) { return true; }
	else { return false;	}
}//end KeyPressed

/************************************
	function EnterKeyPressed

	Returns true if keypress was the Enter key, false otherwise
*************************************/
function EnterKeyPressed(e) {
	return KeyPressed(13, e);
}//end EnterKeyPressed

/************************************
	function EscapeKeyPressed

	Returns true if keypress was the Escape key, false otherwise
*************************************/
function EscapeKeyPressed(e) {
	return KeyPressed(27, e);
}//end EscapeKeyPressed

/************************************
	function DeleteKeyPressed

	Returns true if keypress was the Delete key, false otherwise
*************************************/
function DeleteKeyPressed(e) {
	return (KeyPressed(46, e) || KeyPressed(46, e));
}//end DeleteKeyPressed

/************************************
	function BackspaceKeyPressed

	Returns true if keypress was the Backspace key, false otherwise
*************************************/
function BackspaceKeyPressed(e) {
	return KeyPressed(8, e);
}//end BackspaceKeyPressed

/************************************
	function TabKeyPressed

	Returns true if keypress was the Tab key, false otherwise
*************************************/
function TabKeyPressed(e) {
	return KeyPressed(9, e);
}//end TabKeyPressed

/************************************
	function LeftArrowKeyPressed

	Returns true if keypress was the LeftArrow key, false otherwise
*************************************/
function LeftArrowKeyPressed(e) {
	return KeyPressed(37, e);
}//end LeftKeyPressed

/************************************
	function RightArrowKeyPressed

	Returns true if keypress was the RightArrow key, false otherwise
*************************************/
function RightArrowKeyPressed(e) {
	return KeyPressed(39, e);
}//end RightKeyPressed

function GetControlPrefix(ctl) {
   var idParts = ctl.id.split("_");
   return idParts[0] + "_" + idParts[1] + "_";
}//end GetControlPrefix

/************************************
	function ValidateField

	Validates a field and displays a message.
*************************************/
function ValidateField (fieldName, message) {
	if ($get(fieldName).value == "") {
		alert(message);
		$get(fieldName).focus();
		return false;
	} else {
		return true;
	}
}//end ValidateField

/************************************
	function ValidateFieldRegExp

	Validates that a field matches a regular expression and displays a 
	message if it does not.
*************************************/
function ValidateFieldRegExp (fieldName, regExpString, message) {
	var regExp = new RegExp(regExpString);
		
	if (! regExp.test($get(fieldName).value)) {
		alert(message);
		$get(fieldName).focus();
		return false;
	} else {
		return true;
	}
}//end ValidateFieldRegExp

/************************************
	function checkIt(string)

	Find instance of variable in string
*************************************/
function checkIt(string) {
	place = detect.indexOf(string) + 1;
	thestring = string;
	return place;
}//end CheckIt

/************************************
	function DetectBrowser

	Detect browser of the user
*************************************/
function DetectBrowser() {
	detect = navigator.userAgent.toLowerCase();
	if (checkIt('safari')) { browser = "Safari"; }
	else if (checkIt('opera')) { browser = "Opera"; }
	else if (checkIt('netscape')) { browser = "Netscape Navigator"; }
	else if (checkIt('firefox')) { browser = "FireFox"; }
	else if (checkIt('msie')) { browser = "Internet Explorer"; }
   else { browser = "An unknown browser"; }
}//end DetectBrowser

/************************************
	function DetectOS

	Detect operating system of the user
*************************************/
function DetectOS() {
	if (checkIt('linux')) { OS = "Linux"; }
	else if (checkIt('x11')) { OS = "Unix"; }
	else if (checkIt('mac')) { OS = "Mac"; }
	else if (checkIt('win')) { OS = "Windows"; }
	else { OS = "an unknown operating system"; }
}//end DetectOS

/************************************
	function DetectJava

	Detect if user has java installed and what version
*************************************/
function DetectJava() {
	java.installed=navigator.javaEnabled() ? true:false;
	java.version='0.0';

	var numPlugs=navigator.plugins.length;
	if (numPlugs) {
		for (var x=0; x<numPlugs; x++) {
			var pluginjava = navigator.plugins[x];
			if (pluginjava.name.toLowerCase().indexOf('java plug-in') != -1)	{
				java.version=pluginjava.description.toLowerCase().split('java plug-in ')[1].split(' for')[0];
				break;
			}
		}
	}
}//end DetectJava

/************************************
 Move an element directly on top of 
 another element (and optionally make
 it the same size)
*************************************/
function Cover(bottom, top, ignoreSize) {   
    top.style.position = 'absolute';
    top.style.top = bottom.style.top;
    top.style.left = bottom.style.left;
    if (!ignoreSize) {
        top.style.height = bottom.offsetHeight + 'px';
        top.style.width = bottom.offsetWidth + 'px';
    }
}//end Cover

/************************************
	function Trim, RTrim, LTrim

	Trim spaces off of an entry with these
	functions
*************************************/
function RTrim(VALUE){
	var w_space = String.fromCharCode(32);
	var w_tab = String.fromCharCode(9);
	var w_lf = String.fromCharCode(10);
	var w_cr = String.fromCharCode(13);
	
	var v_length = VALUE.length;
	var strTemp = "";
	if (v_length < 0){
		return "";
	}
	var iTemp = v_length -1;

	while (iTemp > -1){
		if (VALUE.charAt(iTemp) == w_space || VALUE.charAt(iTemp) == w_tab ||
			VALUE.charAt(iTemp) == w_lf || VALUE.charAt(iTemp) == w_cr) {
		}
		else {
			strTemp = VALUE.substring(0,iTemp +1);
			break;
		}
		iTemp = iTemp-1;
	} 
	return strTemp;

} //End RTrim

function LTrim(VALUE){
	var w_space = String.fromCharCode(32);
	var w_tab = String.fromCharCode(9);
	var w_lf = String.fromCharCode(10);
	var w_cr = String.fromCharCode(13);
	
	var v_length = VALUE.length;
	
	if(v_length < 1){
		return "";
	}

	var strTemp = "";

	var iTemp = 0;

	while (iTemp < v_length) {
		if (VALUE.charAt(iTemp) == w_space || VALUE.charAt(iTemp) == w_tab ||
			VALUE.charAt(iTemp) == w_lf || VALUE.charAt(iTemp) == w_cr) {
		}
		else {
			strTemp = VALUE.substring(iTemp,v_length);
			break;
		}
		iTemp = iTemp + 1;
	} 
	return strTemp;
} //End LTrim

function Trim(TRIM_VALUE){
	if (TRIM_VALUE.length < 1){
		return "";
	}
	TRIM_VALUE = RTrim(TRIM_VALUE);
	TRIM_VALUE = LTrim(TRIM_VALUE);
	if (TRIM_VALUE==""){
		return "";
	}
	else {
	return TRIM_VALUE;
	}
} //End Trim
/************************************/

/************************************
 Generic AJAX failure handler.
*************************************/
function AJAX_Failure(err,userContext,methodName) {
   var msg = "An unexpected error has occurred.\n\n";
   
   msg += "Error:  " + err.get_message() + "\n\n";
   
   if (debugFlag) {
      msg += err.get_stackTrace() + "\n\n";
   }
   
   msg += "If you continue to experience difficulties, please contact us at 1-888-537-1304.";

   alert(msg);
}//end AJAX_Failure

function BtnHover(btn,img) {
   btn.src = 'Images/Buttons/' + img;
}//end BtnHover

function BtnUnHover(btn,img) {
   btn.src = 'Images/Buttons/' + img;
}//end BtnUnHover

/************************************
	function openTemplateTool

	Open the template tool window and load the appropriate page into it.
*************************************/
function openTemplateTool(mode, templateGroupId) {
   var w;
	switch (mode) {
		case "S":
		   if (templateGroupId) {
			   w = window.open("TemplateTool/Templates.aspx?tg=" + templateGroupId, "templates", 
				   "width=1000,height=700,menubar=no,resizable=yes,scrollbars=no,status=no");
			} else {
			   w = window.open("TemplateTool/Templates.aspx", "templates", 
				"width=1000,height=700,menubar=no,resizable=yes,scrollbars=no,status=no");
			}
			break;	
		case "M":
		   if (templateGroupId) {
			   w = window.open("TemplateTool/ProofBooks.aspx?tg=" + templateGroupId, "proofbooks", 
				   "width=1000,height=700,menubar=no,resizable=yes,scrollbars=no,status=no");
			} else {
			   w = window.open("TemplateTool/ProofBooks.aspx", "proofbooks", 
				"width=1000,height=700,menubar=no,resizable=yes,scrollbars=no,status=no");
			}
			break;		
		case "F":
		   if (templateGroupId) {
			   w = window.open("TemplateTool/FinishedAlbums.aspx?tg=" + templateGroupId, "finishedalbums", 
				   "width=1000,height=700,menubar=no,resizable=yes,scrollbars=no,status=no");
			} else {
			   w = window.open("TemplateTool/FinishedAlbums.aspx", "finishedalbums", 
				"width=1000,height=700,menubar=no,resizable=yes,scrollbars=no,status=no");
			}
			break;
		case "P":
		   if (templateGroupId) {
			   w = window.open("TemplateTool/ProlabPress.aspx?tg=" + templateGroupId, "prolabpress", 
				   "width=1000,height=700,menubar=no,resizable=yes,scrollbars=no,status=no");
			} else {
			   w = window.open("TemplateTool/ProlabPress.aspx", "prolabpress", 
				"width=1000,height=700,menubar=no,resizable=yes,scrollbars=no,status=no");
			}
			break;
		case "O":
		   if (templateGroupId) {
			   w = window.open("TemplateTool/SpecialtyProducts.aspx?tg=" + templateGroupId, "specialtyproducts", 
				   "width=1000,height=700,menubar=no,resizable=yes,scrollbars=no,status=no");
			} else {
			   w = window.open("TemplateTool/SpecialtyProducts.aspx", "specialtyproducts", 
				   "width=1000,height=700,menubar=no,resizable=yes,scrollbars=no,status=no");
			}
			break;		
		case "B":
			w = window.open("TemplateTool/Books.aspx", "books", 
				"width=1000,height=700,menubar=no,resizable=yes,scrollbars=no,status=no");
			break;
	}
	VerifyPopup(w);
	return false;
} // end openTemplateTool	

function PositionWorkingImage() {
   var workingDiv = $get("workingDiv");
   if (workingImageX !== null) {workingDiv.style.left = workingImageX;}
   if (workingImageY !== null) {workingDiv.style.top = workingImageY;}
}//end ShowWorkingImage

function ShowWorkingImage() {
   PositionWorkingImage();
   $get("workingDiv").style.visibility = "visible";
}//end ShowWorkingImage

function HideWorkingImage() {
   $get("workingDiv").style.visibility = "hidden";
}//end HideWorkingImage

/************************************
	function CloseModalPopup

	CloseModalPopup should be called to close a modal popup instead of using
	the default CancelControlId property of the modalpopupextender to prevent
	popup reappearing during page navigation in some browsers.
   mpe = modalpopupextender Id
   pnl = Id of the panel to hide
*************************************/    
function CloseModalPopup(mpe, pnl) {
   $find(mpe).hide(); 
   $get(pnl).style.display = 'none';
   return false;
}//end CloseModalPopup

/************************************
	function CloseModalPopupPostBack

	Same as CloseModalPopup but called when modalpopup is visible when a page
	posts back.  FireFox has to be handled uniquely to work.
   mpe = modalpopupextender Id
   pnl = Id of the panel to hide
*************************************/  
function CloseModalPopupPostBack(mpe, pnl) {
   DetectBrowser();
   if (browser == "FireFox") {
      $get(pnl).style.height = "0px";
      $get(pnl).style.width = "0px";
      $get(pnl).style.overflow = "hidden"; 
      $find(mpe).hide(); 
      $get(pnl).style.display = 'none';
      return false;
   } else {
      $find(mpe).hide(); 
      $get(pnl).style.display = 'none';
      return false;
   }
}//end CloseModalPopupPostBack

/************************************
	function OpenPDF

	Open PDF in new window to designated page
   pdf = pdf name
   pg = page
*************************************/ 
function OpenPDF(pdf,pg) {
	var w = window.open("./PDFs/" + pdf + ".pdf#" + pg, "servicespdf","width=870, height=600,menubar=no,resizable=yes");
	VerifyPopup(w);
}//end OpenPDF

/************************************
	function OpenDuraCoatFAQ

	Open DuraCoat FAQ window
*************************************/ 
function PopupDuraCoatFAQ() {
   var w = window.open("DuraCoatFAQ.aspx", "DuraCoatFAQ", "width=775,height=675,scrollbars,resizable");
   VerifyPopup(w);
}//end PopupDuraCoatFAQ

/************************************
   function Body_OnLoad()
   
   Body_OnLoad should be called from the master page's body onload.
   It checks to see if a function named ContentPage_OnLoad exists, and 
   if so, it runs it.  ContentPage_OnLoad can be defined differently
   on each content page that uses the master.
*************************************/ 
function Body_OnLoad() {
   if (typeof ContentPage_OnLoad == 'function') {ContentPage_OnLoad();}  
   if (typeof UCL == 'object') {UCL.Initialize();}   
   if (typeof SetVariables == 'function') {setTimeout('SetVariables();',300);}
}//end Body_OnLoad