﻿Home = function() {
    return {
        load: function(hrefLocation) {
            $get('tdHome').className = 'StaticTab';
        }
    };
} ();
    
    


Project = function() {
return {
        
        load: function(hrefLocation) {
            $get('tdProjects').className = 'StaticTab';
        },
        
        getSelectedProject: function(ProjectId) {
            Content.GetSelectedProject(ProjectId, this.gotProject, this.failedToGetProject);
            return false;
        },

        gotProject: function(project) {


         AddContentImage(project.ProjectImagePath, $get('ctl00_ContentPlaceHolder1_ProjectImage'));
            
            $get('ProjectDisplay').style.display = 'block';
            $get('ProjectImageDisplay').style.display = 'none';

            $get('ctl00_ContentPlaceHolder1_lblSelectedProjectName').innerHTML = project.ProjectName;
            $get('ctl00_ContentPlaceHolder1_lblSelectedProjectDescription').innerHTML = project.ProjectDescription;
            $get('ctl00_ContentPlaceHolder1_ProjectImage').onclick = function() { Project.getProjectImages(project.ProjectId); return false; };
            Content.GetProjectImages(project.ProjectId, Project.gotProjectImages, Project.failedToGetImages);

        },

        failedToGetProject: function() {
            RedirectToErrorPage();
        },

        getProjectImages: function(ProjectId) {
            $get('ProjectDisplay').style.display = 'none';
            $get('ProjectImageDisplay').style.display = 'block';
            obj = $get('ctl00_ContentPlaceHolder1_lblImagesProjectName');
            obj.innerHTML = $get('ctl00_ContentPlaceHolder1_lblSelectedProjectName').innerHTML;

            return false;
        },

        gotProjectImages: function() {
            __doPostBack('ctl00$ContentPlaceHolder1$btnUpdateImages', '');
        },

        failedToGetImages: function() {
            RedirectToErrorPage();
        },

        back: function() {
            $get('ProjectDisplay').style.display = 'block';
            $get('ProjectImageDisplay').style.display = 'none';
            return false;
        }
    };
} ();



Service = function() {
    return {
        load: function(hrefLocation) {
            var page = hrefLocation.split('?LinkId=') != undefined ? hrefLocation.split('?LinkId=') : '';
            if (page[1]) {
                EventLink.getSelectedService(page[1])
            }
            $get('tdServices').className = 'StaticTab';
        },

        getSelectedService: function(ServiceId) {
            Content.GetSelectedService(ServiceId, this.gotService, this.failedToGetService);
            return false;
        },

        gotService: function(srv) {

            $get('ServiceDisplay').style.display = 'block';


            AddContentImage(srv.ServiceImagePath, $get('ctl00_ContentPlaceHolder1_ServiceImage'));

            $get('ctl00_ContentPlaceHolder1_lblSelectedServiceName').innerHTML = srv.ServiceName;
            $get('ctl00_ContentPlaceHolder1_lblSelectedServiceDescription').innerHTML = srv.ServiceDescription;
            $get('ctl00_ContentPlaceHolder1_ServiceImage').onclick = function() { Service.getServiceImages(srv.ServiceId); return false; };
            $get('ctl00_ContentPlaceHolder1_spnManagerLink').onclick = function() {
                Service.redirectToContact(srv.ContactId);
            };

        },

        redirectToContact: function(ContactId) {
            if (_isIE) {
                window.navigate('http://www.Diversified.net/Content_Pages/ContactUs.aspx?ContactId=' + ContactId);
            }
            else {
                location.href = 'http://www.Diversified.net/Content_Pages/ContactUs.aspx?ContactId=' + ContactId;
            }
        },

        failedToGetService: function() {
            RedirectToErrorPage();
        }
    };
} ();


EventLink = function() {
    return {
        load: function(hrefLocation) {
            var page = hrefLocation.split('?EventId=') != undefined ? hrefLocation.split('?EventId=') : '';
            if (page[1]) {
                EventLink.getSelectedEventLink(page[1])
            }
            $get('tdCurrentNews').className = 'StaticTab';
        },

        getSelectedEventLink: function(EventLinkId) {
            Content.GetSelectedEventLink(EventLinkId, this.gotEventLink, this.failedToGetEventLink);
            return false;
        },

        gotEventLink: function(EventLink) {

            $get('ctl00_ContentPlaceHolder1_lblSelectedEventLinkName').innerHTML = EventLink.EventLinkName;
            $get('ctl00_ContentPlaceHolder1_lblSelectedEventLinkDescription').innerHTML = EventLink.EventLinkDescription;
            $get('ctl00_ContentPlaceHolder1_lblEventLinkDate').innerHTML = EventLink.EventLinkDate.format("MM/dd/yyyy");
            $get('CompanyLinkInfo').style.display = 'block';
            $get('Products').style.display = 'none';
            if (EventLink.IsEvent) {
                $get('CompanyItems').style.display = 'none';
            }
            else {
                $get('CompanyItems').style.display = 'block';
                $get('ctl00_ContentPlaceHolder1_lblLinkText').innerHTML = EventLink.LinkText;         
                __doPostBack('ctl00$ContentPlaceHolder1$btnUpdateProducts', EventLink.EventLinkId);
            }

            $get('ctl00_ContentPlaceHolder1_lblLinkText').onclick = function() {
                if (_isIE) {
                    window.navigate(EventLink.LinkText);
                }
                else {
                    location.href = EventLink.LinkText;
                }
            };

        },

        failedToGetEventLink: function() {
            RedirectToErrorPage();
        },

        showProducts: function(span) {
        $get('CompanyLinkInfo').style.display = 'none';
            $get('Products').style.display = 'block';
        }
    };
} ();

var yr = '';
var displaymessage = "Days Since Last Incident.<br/>"
var montharray = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
var crosscount = '';

Safety = function() {
return {

        getLastOccurrance: function() {
            Content.GetLastOccurance(this.gotLastOccurrance, this.failedToGetLastOccurance);
            $get('tdSafety').className = 'StaticTab';
        },

        gotLastOccurrance: function(dt) {
            var lastDate = new Date(dt);
            Safety.setCountUp(lastDate.getFullYear(), lastDate.getMonth(), lastDate.getDate());
        },

        setCountUp: function(theyear, themonth, theday) {
            yr = theyear; mo = themonth; da = theday;
            Safety.startCountUp();
        },

        startCountUp: function() {
            crosscount = $get('ctl00_ContentPlaceHolder1_lblNumberOfDays');
            Safety.Countup();
        },

        Countup: function() {
            var today = new Date();
            var todayy = today.getYear();
            if (todayy < 1000) {
                todayy += 1900;
            }
            var todaym = today.getMonth();
            var todayd = today.getDate();
            var todayh = today.getHours();
            var todaymin = today.getMinutes();
            var todaysec = today.getSeconds();
            var todaystring = montharray[todaym] + " " + todayd + ", " + todayy + " " + todayh + ":" + todaymin + ":" + todaysec;
            paststring = montharray[mo] + " " + da + ", " + yr;
            dd = Date.parse(todaystring) - Date.parse(paststring);
            dday = Math.floor(dd / (60 * 60 * 1000 * 24) * 1);
            dhour = Math.floor((dd % (60 * 60 * 1000 * 24)) / (60 * 60 * 1000) * 1);
            dmin = Math.floor(((dd % (60 * 60 * 1000 * 24)) % (60 * 60 * 1000)) / (60 * 1000) * 1);
            dsec = Math.floor((((dd % (60 * 60 * 1000 * 24)) % (60 * 60 * 1000)) % (60 * 1000)) / 1000 * 1);

            crosscount.innerHTML = displaymessage + dday + " days, " + dhour + " hours, " + dmin + " minutes, and " + dsec + " seconds";

            setTimeout("Safety.Countup()", 1000)
        }

    };
} ();


AboutUs = function() {
    return {
        load: function(hrefLocation) {
            $get('tdAboutUs').className = 'StaticTab';
        },
        showAwards: function(span) {
            if (span.innerHTML.toLowerCase() === 'back') {
                span.innerHTML = 'AWARDS AND RECOGNITION';
                $get('AboutUsText').style.display = 'block';
                $get('AwardsAndRecognitions').style.display = 'none';
            }
            else {
                $get('AboutUsText').style.display = 'none';
                $get('AwardsAndRecognitions').style.display = 'block';
                span.innerHTML = 'BACK';
            }

        }
    };
} ();

ContactUs = function() {
    return {

        load: function(hrefLocation) {
            var page = hrefLocation.split('?ContactId=') != undefined ? hrefLocation.split('?ContactId=') : '';
            if (page[1]) {
                ContactUs.getSelectedContact(page[1])
            }
            $get('tdContactUs').className = 'StaticTab';


	    var img = $get('ctl00_ContentPlaceHolder1_imgContactImage');
	    var newImg = new Image();
            newImg.setAttribute('src', img.getAttribute('src'));
	     if (newImg.width > newImg.height){
	    	divFactor = newImg.width/130;
	    }
	    else {
	    	divFactor = newImg.height/130;
	    }
			
		
	    var newWidth = Math.round(newImg.width/ divFactor);
	    var newHeight = Math.round(newImg.height / divFactor);
	    img.style.width = newWidth + "px";
	    img.style.height = newHeight + "px";

	    newImg=null;
	    img.style.display = 'block';
        },

        sendMail: function() {
            if (Page_ClientValidate() === true) {
                var name = $get('ctl00_ContentPlaceHolder1_txbYourName').value;
                var email = $get('ctl00_ContentPlaceHolder1_txbEmail').value;
                var company = $get('ctl00_ContentPlaceHolder1_txbCompany').value;
                var phone = $get('ctl00_ContentPlaceHolder1_txbPhone').value;
                var message = $get('ctl00_ContentPlaceHolder1_txbMessage').value;
                var contactsEmail = $get('ctl00_ContentPlaceHolder1_lblEmail').innerHTML;

                Content.SendContactUsMail(name, email, company, phone, message, contactsEmail, this.mailSent, this.mailSentFailed)
            }
        },

        mailSent: function() {
            $get('ctl00_ContentPlaceHolder1_txbYourName').value = '';
            $get('ctl00_ContentPlaceHolder1_txbEmail').value = '';
            $get('ctl00_ContentPlaceHolder1_txbCompany').value = '';
            $get('ctl00_ContentPlaceHolder1_txbPhone').value = '';
            $get('ctl00_ContentPlaceHolder1_txbMessage').value = '';

            alert("Your Message Was Sent Successfully! You'll Be Hearing From Us Very Soon!");
        },

        mailSentFailed: function() {
            RedirectToErrorPage();
        },


        getSelectedContact: function(ContactId) {
            Content.GetSelectedContact(ContactId, this.gotContact, this.failedToGetContact);
            return false;
        },

        gotContact: function(Contact) {

            var img = $get('ctl00_ContentPlaceHolder1_imgContactImage');
            img.setAttribute('src', Contact.ContactPicturePath);

            var newImg = new Image();
            newImg.setAttribute('src', Contact.ContactPicturePath);
	    var divFactor=0;

	    
	    if (newImg.width > newImg.height){
	    	divFactor = newImg.width/130;
	    }
	    else {
	    	divFactor = newImg.height/130;
	    }
			
		
	    var newWidth = Math.round(newImg.width/ divFactor);
	    var newHeight = Math.round(newImg.height / divFactor);
	    img.style.width = newWidth + "px";
	    img.style.height = newHeight + "px";

	    newImg=null;

            $get('ctl00_ContentPlaceHolder1_lblName').innerHTML = Contact.ContactName;
            $get('ctl00_ContentPlaceHolder1_lblTitle').innerHTML = Contact.ContactTitle;
            $get('ctl00_ContentPlaceHolder1_lblDuty').innerHTML = 'Role: ' + Contact.ContactDuty;
            $get('ctl00_ContentPlaceHolder1_lblEmail').innerHTML = 'Email: ' + Contact.ContactEmail;
            $get('ctl00_ContentPlaceHolder1_lblPhone').innerHTML = 'Phone: ' + Contact.ContactPhone;
            $get('ctl00_ContentPlaceHolder1_lblCell').innerHTML = 'Cell: ' + Contact.ContactCell;
            $get('ctl00_ContentPlaceHolder1_lblFax').innerHTML = 'Fax: ' + Contact.ContactFax;
            $get('ctl00_ContentPlaceHolder1_lblContactName').innerHTML = Left(Contact.ContactName, (Contact.ContactName.indexOf(' ')));

        },

        failedToGetContact: function() {
            RedirectToErrorPage();
        }

    };
} ();


function AddContentImage(imagePath, container) {
    var newImg = new Image();
    var filePath = imagePath;
    newImg.setAttribute('src', filePath);
    newImg.style.height = '500px';
    newImg.style.width = '530px';

    if (container.childNodes.length > 0) {
        for (var i = 0; i < container.childNodes.length; i++) {
            if (container.childNodes[i].nodeType === 1) {
                container.removeChild(container.childNodes[i]);
            }
        }
    }
    container.insertBefore(newImg, container.firstChild);
}

function Left(str, n) {
    if (n <= 0)
        return "";
    else if (n > String(str).length)
        return str;
    else
        return String(str).substring(0, n);
}
