// Default parameters
var default_search_url      = "http://www.gov.bc.ca/bvprd/bc/search.do?action=searchresult&navId=NAV_ID_province&qp=&nh=10&qt=a";
var default_search_query    ="";
var default_advsearch_url   = "http://www.gov.bc.ca/bvprd/bc/search.do?action=advsearch";
var default_help_url        = "http://www.gov.bc.ca/bvprd/bc/content.do?brwId=@20uAj|0YQtuW";
var default_contact_url     = "http://www.gov.bc.ca/bvprd/bc/channel.do?action=govOrg&channelID=-8353";
var default_main_url        = "http://www.gov.bc.ca/bvprd/bc/index.do";



/*
 * Header Search submit method.
 */
function submit_search() {

    var strUrl = default_search_url;//getElementValue( document, 'dynamic_search_url' );
    var strQuery = getElementValue( document, 'dynamic_search_query' );
    var ministryHeader = getElementValue( document, 'ministry_search' );// User selected either "entire site" = 0 OR "this section" = 1
    var ministrySearch = "";
    
    if ( ministryHeader.length > 0 ) { // If we are using the radio buttons then which one was chosen?
        ministrySearch = getTheCheckedValue( document.seekheader1.ministry_search );// User selected either "entire site" = 0 OR "this section" = 1
    }

    if ( strQuery.length > 0 && ministrySearch == "1" ) { // then lets use the custom url path and assign it to the qp value of the form before submission
        document.seekheader1.qp.value = strQuery; //add custom data here!
    }
    else {
        document.seekheader1.qp.value = default_search_query; // Currently empty string
    }

    if ( strUrl.length > 0 ) { //Create the url for the location
        strUrl = strUrl + getQueryString( document.seekheader1, strUrl );
        setLocation( document, strUrl );
	}
    else {
        document.seekheader1.submit();
    }

    return;
}

/*
 * Header Search Form submit method.
 */
function submit_searchform() {

    var strUrl = default_search_url;//getElementValue( document, 'dynamic_search_url' );
    var strQuery = getElementValue( document, 'dynamic_search_query' );
    var ministryHeader = getElementValue( document, 'ministry_search' );
    var ministrySearch = "";
    
    if ( ministryHeader.length > 0 ) {
        ministrySearch = getTheCheckedValue( document.seekheader1.ministry_search );
    }

    if ( strQuery.length > 0 && ministrySearch == "1" ) {
        document.seekheader1.qp.value = strQuery;
    }
    else {
        document.seekheader1.qp.value = default_search_query;
    }

    if ( strUrl.length > 0 ) {
        strUrl = strUrl + getQueryString( document.seekheader1, strUrl );
        setLocation( document, strUrl );
        return false;
    }
    else {
        return true;
    }

}
/*
 * Header Advanced Search submit method.
 */
function submit_advsearch() {
    var strUrl = getElementValue( document, 'dynamic_advsearch_url' );

    if ( strUrl.length <= 0 ) {
        strUrl = default_advsearch_url;
    }

    setLocation( document, strUrl);
}

/*
 * Header Help submit method.
 */
function submit_help() {
    var strUrl = getElementValue( document, 'dynamic_help_url' );

    if ( strUrl.length <= 0 ) {
        strUrl = default_help_url;
    }

    setLocation( document, strUrl);
}

/*
 * Header Contact Us submit method.
 */
function submit_contact() {
    var strUrl = getElementValue( document, 'dynamic_contact_url' );

    if ( strUrl.length <= 0 ) {
        strUrl = default_contact_url;
    }

    setLocation( document, strUrl );
}

/*
 * Header Main Index submit method.
 */
function submit_main() {
    var strUrl = getElementValue( document, 'dynamic_main_url' );

    if ( strUrl.length <= 0 ) {
        strUrl = default_main_url;
    }

    setLocation( document, strUrl);
}

/*
 * Return the value of the specified element on any form.
 */
function getElementValue( theDocument, elementName ) {
    for ( var i=0; i < theDocument.forms.length; i++ ) {
        for ( var j=0; j < theDocument.forms[i].elements.length; j++ ) {
            if ( theDocument.forms[i].elements[j].name == elementName ) {
                return theDocument.forms[i].elements[j].value;
            }
        }
    }
    
    return '';
}

/*
 * Return a formatted query string generated from the Form elements.
 */
function getQueryString( theForm, strUrl ) {
    var strQuery = '';

    if ( strUrl.indexOf( "?" ) < 0 ) {
        strQuery = "?";
    }
    else if ( strUrl.charAt( strUrl.length - 1 ) != "&" ) {
        strQuery = "&";
    }

    for ( var i=0; i < theForm.elements.length; i++ ) {
        strQuery = strQuery + escape(theForm.elements[i].name) + '=' + escape(theForm.elements[i].value).replace(/\+/g,"%2B");
        
        if ( i < theForm.elements.length - 1 ) {
            strQuery = strQuery + '&';
        }
    }

    return strQuery;
}

function getTheCheckedValue( radioarray ) {

  for ( var i = 0; i < radioarray.length; i++ ) {
    if ( radioarray[i].checked ) {
       return radioarray[i].value;
    }
  } 
}

/*
 * Set the location of the browser to the URL parameter.
 */
function setLocation( theDocument, strUrl ) {
//    alert( strUrl );
    theDocument.location = strUrl;
    return;
}

function randomimage()
{
	<!-- This is the array that holds the image names for all images -->
	var insidearray = [
		"image1.jpg",
		"image2.jpg",
		"image3.jpg"
		];

		<!-- This will choose a random number between 0 and how many items there are in the array.  It uses 0 instead of 1 as the first item in an array is in position 0 -->
		var randomnumber = Math.floor(Math.random() * insidearray.length);
		
		<!-- This will preload the image that is choosen to be displayed -->
		MM_preloadImages("'/template_support/images/random/" + insidearray[randomnumber] + "'");
	
		<!-- This points to the images and tells the page which image to display on the page -->
		document.randompicture.src = eval("'/template_support/images/random/" + insidearray[randomnumber] + "'");

} <!-- Random Image function ends here -->

