﻿/************************************** Splendid *************************************
 * Created By:		James Churchouse
 * Creation Date:	6th June 2008
 * Edited ----------------------------------------------------------------------------
 *      By:              On:  
 * Description -----------------------------------------------------------------------
 *      This file has a number of function used to control page elements around the site
 *      Uses the JQuery plugin.
 *
 *      Functions:
 *          CreateProject()                     // Toggles the new project form details
 *          OpenCalendar()                      // Toggles the calendar div on/off and sets the input box to return the chosen date to
 *          txtDate_focus()                     // Clears the date/time boxes when they have focus
 *          homeSearch()                        // Changes between the supplier and project tab on the homepage.
 *			
 **************************************************************************************/

/*********************************** Project Related **********************************/

/****
 * Toggles the new project form details
 ****/
function CreateProject()
{
    if ($('div#panelItem1').css("display") == "block") {
        $('div#panelItem1').slideUp("fast");
        $('div#panelBtn1 a:first-child').text("Create New Project...");
        $('input#panelItem1State').attr("value","none");
    }
    else {  
        $('div#panelItem1').slideDown("fast");
        $('div#panelBtn1 a:first-child').text("Create New Project: Project Overview");
        $('input#panelItem1State').attr("value","block");
    }    
}

/****
 * Toggles the calendar div on/off and sets the input box to return the chosen date to
 ****/
function OpenCalendar( btnID, inputID )
{
    calendar.output = $('div#calPopup');
    calendar.sender = $('input#'+inputID);
    if (calendar.senderid == $('input#'+inputID).attr("id")) 
    {
        calendar.senderid = "";
        calendar.output.slideUp("slow");
    }
    else 
    {
        calendar.senderid = $('input#'+inputID).attr("id");
        var btn = document.getElementById(btnID);
        calendar.top = btn.offsetParent.offsetTop;
        calendar.left = btn.offsetLeft;
        calendar.showMonth(new Date().getMonth(),new Date().getFullYear());
    }
}

/****
 * Clears the date/time boxes when they have focus
 ****/
function txtDate_focus(obj) 
{
    if((obj.val() == "dd/mm/yyyy") || (obj.val() == "hh:mm"))
    {
       obj.val("");
    }
}

/****
 * Changes between the supplier and project tab on the homepage.
 ****/
function homeSearch(type) {
	if (type == 'projects') {
		$('#HomeTabs').html($([
			'<li class="supFirst"><a href="javascript:homeSearch(\'supplier\');">Supplier</a></li>',
			'<li class="current"><span>Projects</span></li>'
		].join('')));
		$('#HomeSearchType').attr("value", "2");
		try {
			$('#HomeSearchPara').html(arrProjSuppSearch[0].text);
		} catch (e) { // Just in case something has gone wrong...
			$('#HomeSearchPara').html('Search our project database by product, service or company name.');
		}
	} else if (type == 'supplier') {
		$('#HomeTabs').html($([
			'<li class="first currentFirst"><span>Supplier</span></li>',
			'<li><a href="javascript:homeSearch(\'projects\');">Projects</a></li>'
		].join('')));
		$('#HomeSearchType').attr("value", "1");
		try {
			$('#HomeSearchPara').html(arrProjSuppSearch[1].text);
		} catch (e) { // Just in case something has gone wrong...
			$('#HomeSearchPara').html('Search our supplier database by product, service or company name.');
		}
	}
}


/******************** Can we pull these out into proper functions please! **********************/

$(document).ready(function() {

    $('div#panelBtn2').click(function() {
        if ($('div#panelItem2').css("display") == "block") {
            $('div#panelItem2').slideUp("fast");
        }
        else {
            $('div#panelItem2').slideDown("fast");
        }
    });

    $('select#selSort2').change(function() {
        $('form#frmProject').submit();
    });

    $('select#selSort3').change(function() {
        $('form#frmProject').submit();
    });


    $('input#StartDate').focus(function() {
        txtDate_focus($('input#StartDate'));
    });


    $('input#EndDate').focus(function() {
        txtDate_focus($('input#EndDate'));
    });

    $('input#QuoteTime').focus(function() {
        txtDate_focus($('input#QuoteTime'));
    });

    $('select#CountryID').change(function() {
        if ($('select#CountryID').val() != 0) {
            $('form#frmServiceDetail').submit();
        }
    });

    $('select#landlineDialCode').change(function() {
        if ($('select#mobileNumberDialCode').val() == 0) {
            document.getElementById("mobileNumberDialCode").selectedIndex = document.getElementById("landlineDialCode").selectedIndex;
        }
    });

    //    $('select#landlineDialCode').change (function() {
    //        if ($('select#landlineDialCode').val()!=0) {
    //            $('form#register').submit();
    //        }
    //    });


    $('div.panelBtn').click(function() {
        var panelItem = $(this).parent().nextAll()[0];
        if ($(panelItem).css("display") == "block") {
            $(panelItem).slideUp("slow");
            $(this).children().html("click to open");
        }
        else {
            $(panelItem).slideDown("slow");
            $(this).children().html("click to close");
        }
    });

    $("tr.clickQuote").click(function() {
        var theLink = this.cells[2].childNodes[2].getAttribute("href");
        if (theLink != undefined) {
            window.location = theLink;
        }
    });

    $('select#ContinentID').change(function() {
        $.get('/WebServices/RestLocation.aspx', { ContinentID: this.options[this.selectedIndex].value }, function(data) {
            $('span#CountryIDSpan').html(data.xml);
            $('select#CountryID').val(0);
        }
        );
    });

    $('#helpSlideBtn').toggle(function() {
        $('#helpSlideBtn').css("background", "url(/img/help/helpMinus.gif) right 5px no-repeat");
        $("#helpcontent").slideDown("slow");
    }, function() {
        $('#helpSlideBtn').css("background", "url(/img/help/helpPlus.gif) right 5px no-repeat");
        $("#helpcontent").slideUp("slow");
    });

});     //document.ready



/* IE6 Flickering image fix */
try {
	document.execCommand('BackgroundImageCache',false,true);
	}catch(ignoreme){
}
