// ------------------------------------------------------------------------
// MC_UTIL.JS
// Copyright: © 1998-2006 Maintenance Connection, Inc. All rights reserved.
// ------------------------------------------------------------------------

var securepageprotocol = 'https://';
var unsecurepageprotocol = 'http://';

// ------------------------------------------------------------------------
// DO NOT EDIT BELOW THIS LINE
// ------------------------------------------------------------------------

var digits = "0123456789";
var lowercaseLetters = "abcdefghijklmnopqrstuvwxyz"
var uppercaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
var whitespace = " \t\n\r";
var decimalPointDelimiter = "."
var phoneNumberDelimiters = "()- ";
var validUSPhoneChars = digits & phoneNumberDelimiters;
var validWorldPhoneChars = digits & phoneNumberDelimiters & "+";
var digitsInUSPhoneNumber = 10;
var ZIPCodeDelimiters = "-";
var ZIPCodeDelimeter = "-"
var validZIPCodeChars = digits & ZIPCodeDelimiters
var digitsInZIPCode1 = 5
var digitsInZIPCode2 = 9
var creditCardDelimiters = " "

var fraExternal = null;

var exp = new Date();
exp.setTime(exp.getTime() + (7 * 24 * 60 * 60 * 1000));
var expirestag = '<META HTTP-EQUIV="expires" CONTENT="' + exp.toGMTString() + '">';

var path = self.location.pathname.substring(0,self.location.pathname.lastIndexOf("/") + 1);
websitepath = path;
//alert(websitepath);
var nonsecurepath = unsecurepageprotocol + location.host + websitepath
//alert(nonsecurepath);
var apppath = '';

function fieldfocus(thefield)
{
	thefield.className = 'accessible';
}

function fieldfocus2(thefield)
{
	thefield.className = 'accessible2';
}

function fieldblur(thefield)
{
	thefield.className = 'normal';
}

function playsound(thesound) {
	mcmusic.volume = "0";
	mcmusic.src = thesound;
}

function loadmodeless(url,param,w,h,scroll,dirtycheck,t,l)
{
	if (w == null)
	{
		w = 580;
	}
	
	if (h == null)
	{
		h = 800;
	}

	if (scroll == null || scroll == false)
	{
		scroll = 'No';
	}
	else
	{
		scroll = 'Yes';
	}

	if (param == null)
	{	
		param = new Object();
		param.caller = top;
	}
	
	if (url.toLowerCase().indexOf('http') == -1)
	{
		if (url.toLowerCase().indexOf(':') == -1)
		{
			xurl = self.path + url;	
		}
		else
		{
			// it's a file so use window.open!!
			self.mcWindow(url,'_blank',t,l,w,h,'scrollbars=yes,resizable=yes');	
			return;
		}
	}
	else
	{
		xurl = url;	
	}
	
	if (t == null && l == null)
	{
		self.fraExternal = self.showModelessDialog(xurl, param,'dialogHeight: ' + h + 'px; dialogWidth: ' + w + 'px; dialogTop: px; dialogLeft: px; center: Yes; help: No; resizable: No; status: No; scroll: '+scroll );    
	}
	else
	{
		self.fraExternal = self.showModelessDialog(xurl, param,'dialogHeight: ' + h + 'px; dialogWidth: ' + w + 'px; dialogTop: ' + t + 'px; dialogLeft: ' + l + 'px; center: No; help: No; resizable: Yes; status: No; scroll: '+scroll );    		
	}
}

function loadmodal(url,param,w,h,scroll,dirtycheck,t,l)
{
	if (w == null)
	{
		w = 580;
	}
	
	if (h == null)
	{
		h = 800;
	}

	if (scroll == null || scroll == false)
	{
		scroll = 'No';
	}
	else
	{
		scroll = 'Yes';
	}

	if (param == null)
	{	
		param = new Object();
		param.caller = top;
	}
	
	if (url.toLowerCase().indexOf('http') == -1)
	{
		if (url.toLowerCase().indexOf(':') == -1)
		{
			xurl = self.path + url;	
		}
		else
		{
			// it's a file so use window.open!!
			self.mcWindow(url,'_blank',t,l,w,h,'scrollbars=yes,resizable=yes');	
			return true;
		}
	}
	else
	{
		xurl = url;	
	}
	
	if (t == null && l == null)
	{
		retval = self.showModalDialog(xurl, param,'dialogHeight: ' + h + 'px; dialogWidth: ' + w + 'px; dialogTop: px; dialogLeft: px; center: Yes; help: No; resizable: No; status: No; scroll: '+scroll );    
		// below for debugging only
		//self.mcWindow(xurl,'_blank',100,100,w,h,'scrollbars=yes,resizable=yes');	
			
		return param;
	}
	else
	{
		retval = self.showModalDialog(xurl, param,'dialogHeight: ' + h + 'px; dialogWidth: ' + w + 'px; dialogTop: ' + t + 'px; dialogLeft: ' + l + 'px; center: No; help: No; resizable: Yes; status: No; scroll: '+scroll );    		
		return param;
	}
}

function mcWindow( url, name, x, y, width, height, windowFeatures )
{
	var args = mcWindow.arguments.length;
	var features = new String();

	if( args == 2 )
	{
		features = "toolbar,location,status,menubar,scrollbars,resizable";
	}
	else
	{
		if( args >= 4 )
		{
		
			if (x == null && y == null)
			{
				if (screen) x = (screen.availWidth - width) / 2;
				if (screen) y = (screen.availHeight - height) / 2;
				y = y - 10;
			}
	
			if( x != null )
			{
				
				if (name == '_blank')
				{
					features += ",left=" + x;		// IE
					features += ",screenX=" + x;		// Netscape
				}
				else
				{
					features += ",left=" + x;		// IE
					features += ",screenX=" + x;		// Netscape				
				}
			}			
	
			if( y != null )
			{
				features += ",screenY=" + y;	// Netscape
				features += ",top=" + y;		// IE
			}
		}
	
		if( args >=6 )
		{
			if( width != null )
				features += "," + "width=" + width;
	
			if( height != null )
				features += "," + "height=" + height;
		}
	
		if( args == 7 && windowFeatures != null )
		{
			features += "," + windowFeatures;
		}

		if( features.length > 0 && features.charAt(0) == "," )
		{
			features = features.substring(1,features.length);
		}
	}		
	if (name == '_blank')
	{
		newWindow = ( features.length > 0 ) ? window.open( url, name, features ) : window.open( url, name );	
		// If the window already exists but is now hidden, bring it to the front.
		self.newWindow.focus();
	}
	else
	{		
		
		if (self.fraExternal == null || self.fraExternal.closed)
		{
			self.fraExternal = ( features.length > 0 ) ? window.open( url, name, features ) : window.open( url, name );	
		}
		else
		{
			self.fraExternal.resizeTo(width,height);
			self.fraExternal.location.href = url;			
			self.fraExternal.focus();
		}
											
	}

}

function phonecheck(fld)
{
	if (stripCharsNotInBag (fld.value, digits).length == 10)
	{
		fld.value = reformat (stripCharsNotInBag (fld.value, digits),"(", 3, ") ", 3, "-", 4)
	}
}
	
function reformat (s)
{   var arg;
    var sPos = 0;
    var resultString = "";

    for (var i = 1; i < reformat.arguments.length; i++) {
       arg = reformat.arguments[i];
       if (i % 2 == 1) resultString += arg;
       else {
           resultString += s.substring(sPos, sPos + arg);
           sPos += arg;
       }
    }
    return resultString;
}    

function stripCharsInBag (s, bag)

{   var i;
    var returnString = "";

    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }

    return returnString;
}


function stripCharsNotInBag (s, bag)

{   var i;
    var returnString = "";

    // Search through string's characters one by one.
    // If character is in bag, append to returnString.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) != -1) returnString += c;
    }

    return returnString;
}

function validEmail(email) {
	invalidChars = " /:,;"

	if (email == "") {						// cannot be empty
	    window.alert("Please enter your Email address.    ");
		return false
	}
	for (i=0; i<invalidChars.length; i++) {	// does it contain any invalid characters?
		badChar = invalidChars.charAt(i)
		if (email.indexOf(badChar,0) > -1) {
		    window.alert("Your Email address contains invalid characters. Please try again.    ");
			return false
		}
	}
	atPos = email.indexOf("@",1)			// there must be one "@" symbol
	if (atPos == -1) {
	    window.alert("You didn't include the @ symbol in your Email address. Please try again.    ");
		return false
	}
	if (email.indexOf("@",atPos+1) != -1) {	// and only one "@" symbol
	    window.alert("Your Email address contains too many @ symbols. Please try again.    ");
		return false
	}
	periodPos = email.indexOf(".",atPos)
	if (periodPos == -1) {					// and at least one "." after the "@"
	    window.alert("Your Email address is not entered correctly. Please try again.    ");
		return false
	}
	if (periodPos+3 > email.length)	{		// must be at least 2 characters after the "."
	    window.alert("Your Email address is not entered correctly. Please try again.    ");
		return false
	}
	return true
}

function closechildren()
{
	if (self.fraExternal != null && self.fraExternal.closed == false)
	{
		self.fraExternal.close();
	}
}

function setCookie(cName, Props , Years) 
{
    var today = new Date();
    var expiration = new Date(today.getTime() + ( Years * 365 * 24 * 60 * 60 * 1000 ));
    if (Years == null)
    {
		document.cookie = escape(cName) + "=" + Props + "; path=/;";
    }
    else
    {
		document.cookie = escape(cName) + "=" + Props + "; path=/; expires=" + expiration.toGMTString();
	}
}

function ReadCookie(name) {
	if(name==null) {
		return self.document.cookie
	} else {
		if(self.document.cookie.indexOf(name+'=')!=-1) {
			end=self.document.cookie.indexOf(";",self.document.cookie.indexOf(name+'='))
			if(end<0)
			{
				end=self.document.cookie.length;
			}
			var myval = self.document.cookie.substring(self.document.cookie.indexOf(name+'=')+name.length+1,end);
			if (myval == ';')
			{
				return '';
			}
			else
			{
				return myval;
			}
		}
		else
		{
			return '';
		}
	}
}

function deleteCookie(name, path, domain) {
	document.cookie = name + "=" + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + "; expires=Thu, 01-Jan-00 00:00:01 GMT";
}

function 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=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=path + a[i];}}
}

function preloadImagesMAPP() { //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=preloadImagesMAPP.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];}}
}

var newbanner = -1;
var current_banner = -1;

function ChooseRandomPhoto(rotate,instant)
{
	// set industry dropdown
	
	var svalue = ReadCookie('mcindustry');
	if (svalue != null && svalue != '')
	{
		var sel = document.getElementById('Industry');
		if (sel)
		{
			sel.value = svalue;
		}
	}
	
	var banner = new Array();
	banner[0] = "images/cmms_banner_1.jpg";
	banner[1] = "images/cmms_banner_2.jpg";
	banner[2] = "images/cmms_banner_3.jpg";

	if (instant != null)
	{	
		new_banner = current_banner;
	}
	else
	{
		new_banner = 0;
	}
		
	while (current_banner == new_banner)		
	{
		new_banner = Math.floor( Math.random() * banner.length );	
	}

    current_banner = new_banner;
    		
	var SlideShowSpeed = 9000;
	var CrossFadeDuration = 20;

	var tss;
	
	if (document.all && instant != null){
	document.images.cmms_banner.style.filter="blendTrans(duration=2)";
	document.images.cmms_banner.style.filter="blendTrans(duration=CrossFadeDuration)";
	document.images.cmms_banner.filters.blendTrans.Apply();}
	if (instant != null)
	{
	document.images.cmms_banner.src = banner[ current_banner ];
	}
	if (document.all && instant != null) document.images.cmms_banner.filters.blendTrans.Play();
	if (rotate != null && rotate == true)
	{
		tss = setTimeout('ChooseRandomPhoto(true,false)', SlideShowSpeed);			
	}
}
