
//	Used by Quotation page
function onchangelist(v) {
	var s  = v.options[v.selectedIndex].text;
	if( s == "Other, please specify" ){
		show('MktgSourceCommentsId');
	} else {
		hide('MktgSourceCommentsId');
	}
	return;
}

//	No longer used
function onchangeDocTypelist(v) {
	var s  = v.options[v.selectedIndex].text;
	if( s == "Other, please specify" ){
		show('OtherDocumentTypeId');
	} else {
		hide('OtherDocumentTypeId');
	}
	return;
}

//	Used in conjunction with hide to show information boxes
function show(object) {
    if (document.getElementById && document.getElementById(object) != null)
         node = document.getElementById(object).style.visibility='visible';
    else if (document.layers && document.layers[object] != null)
        document.layers[object].visibility = 'visible';
    else if (document.all)
        document.all[object].style.visibility = 'visible';
}

function hide(object) {
    if (document.getElementById && document.getElementById(object) != null)
         node = document.getElementById(object).style.visibility='hidden';
    else if (document.layers && document.layers[object] != null)
        document.layers[object].visibility = 'hidden';
    else if (document.all)
         document.all[object].style.visibility = 'hidden';
}

//	Shows document in separate window, e.g. Privacy Policy and Terms & Conditions
function popup(mylink, windowname)
{
	if (! window.focus)return true;
	var href;
	if (typeof(mylink) == 'string')
	   href=mylink;
	else
	   href=mylink.href;
	window.open(href, windowname, 'width=700,height=500,scrollbars=yes,status=no,resizable=yes,location=no');
	return false;
}

/**
 * Convert a single file-input element into a 'multiple' input list
 *
 * Usage:
 *
 *   1. Create a file input element (no name)
 *      eg. <input type="file" id="first_file_element">
 *
 *   2. Create a DIV for the output to be written to
 *      eg. <div id="files_list"></div>
 *
 *   3. Instantiate a MultiSelector object, passing in the DIV and an (optional) maximum number of files
 *      eg. var multi_selector = new MultiSelector( document.getElementById( 'files_list' ), 3 );
 *
 *   4. Add the first element
 *      eg. multi_selector.addElement( document.getElementById( 'first_file_element' ) );
 *
 *   5. That's it.
 *
 *   You might (will) want to play around with the addListRow() method to make the output prettier.
 *
 *   You might also want to change the line 
 *       element.name = 'file_' + this.count;
 *   ...to a naming convention that makes more sense to you.
 * 
 * Licence:
 *   Use this however/wherever you like, just don't blame me if it breaks anything.
 *
 * Credit:
 *   If you're nice, you'll leave this bit:
 *  
 *   Class by Stickman -- http://www.the-stickman.com
 *      with thanks to:
 *      [for Safari fixes]
 *         Luis Torrefranca -- http://www.law.pitt.edu
 *         and
 *         Shawn Parker & John Pennypacker -- http://www.fuzzycoconut.com
 *      [for duplicate name bug]
 *         'neal'
 */
function MultiSelector( list_target, max ){

  // Where to write the list
  this.list_target = list_target;
  // How many elements?
  this.count = 0;
  // How many elements?
  this.id = 0;
  // Is there a maximum?
  if( max ){
    this.max = max;
  } else {
    this.max = -1;
  };
  
  /**
   * Add a new file input element
   */
  this.addElement = function( element ){
    
    // Make sure it's a file input element
    if( element.tagName == 'INPUT' && element.type == 'file' ){
      
      // Element name -- what number am I?
      element.name = 'file_' + this.id++;
      
      // Add reference to this object
      element.multi_selector = this;
      
      // What to do when a file is selected
      element.onchange = function(){
	
	// New file input
	var new_element = document.createElement( 'input' );
	new_element.type = 'file';
	
	// Add new element
	this.parentNode.insertBefore( new_element, this );
	
	// Apply 'update' to element
	this.multi_selector.addElement( new_element );
	
	// Update list
	this.multi_selector.addListRow( this );
	
	// Hide this: we can't use display:none because Safari doesn't like it
	this.style.position = 'absolute';
	this.style.left = '-1000px';
	
      };
      
      // If we've reached maximum number, disable input element
      if( this.max != -1 && this.count >= this.max ){
	element.disabled = true;
      };
      
      // File element counter
      this.count++;
      // Most recent element
      this.current_element = element;
      
    } 
    else {
      // This can only be applied to file input elements!
      alert( 'Error: not a file input element' );
    };
  };
  
  /**
   * Add a new row to the list of files
   */
  this.addListRow = function( element ){
    
    // Row div
    var new_row = document.createElement( 'div' );
    
    // Delete button
    var new_row_button = document.createElement( 'input' );
    new_row_button.type = 'button';
    new_row_button.value = ' Delete ';
    
    // References
    new_row.element = element;
    
    // Delete function
    new_row_button.onclick= function(){
    
      // Remove element from form
      this.parentNode.element.parentNode.removeChild( this.parentNode.element );

      // Remove this row from the list
      this.parentNode.parentNode.removeChild( this.parentNode );
 
      // Decrement counter
      this.parentNode.element.multi_selector.count--;
      
      // Re-enable input element (if it's disabled)
      this.parentNode.element.multi_selector.current_element.disabled = false;
      
      // Appease Safari
      //    without it Safari wants to reload the browser window
      //    which nixes your already queued uploads
      return false;
    };
    
    // Set the file name and path from my_file_element
	// Create a separate div to hold the file name within the row
	
    var new_row_file = document.createElement( 'div' );
    var attrib = document.createAttribute( 'class' );
    attrib.value = "UploadFile";
    new_row_file.setAttributeNode(attrib);
    new_row_file.innerHTML = element.value;
    attrib = document.createAttribute('class');
    attrib.value = "UploadDeleteButton";
    new_row_button.setAttributeNode(attrib);
    
    // Add button
    new_row.appendChild( new_row_button );
    new_row.appendChild( new_row_file );
    
    // Add it to the list
    this.list_target.appendChild( new_row );
    
  };

};


/*
CSS Browser Selector v0.4.0 (Nov 02, 2010)
Rafael Lima (http://rafael.adm.br)
http://rafael.adm.br/css_browser_selector
License: http://creativecommons.org/licenses/by/2.5/
Contributors: http://rafael.adm.br/css_browser_selector#contributors
*/
function css_browser_selector(u){var ua=u.toLowerCase(),is=function(t){return ua.indexOf(t)>-1},g='gecko',w='webkit',s='safari',o='opera',m='mobile',h=document.documentElement,b=[(!(/opera|webtv/i.test(ua))&&/msie\s(\d)/.test(ua))?('ie ie'+RegExp.$1):is('firefox/2')?g+' ff2':is('firefox/3.5')?g+' ff3 ff3_5':is('firefox/3.6')?g+' ff3 ff3_6':is('firefox/3')?g+' ff3':is('gecko/')?g:is('opera')?o+(/version\/(\d+)/.test(ua)?' '+o+RegExp.$1:(/opera(\s|\/)(\d+)/.test(ua)?' '+o+RegExp.$2:'')):is('konqueror')?'konqueror':is('blackberry')?m+' blackberry':is('android')?m+' android':is('chrome')?w+' chrome':is('iron')?w+' iron':is('applewebkit/')?w+' '+s+(/version\/(\d+)/.test(ua)?' '+s+RegExp.$1:''):is('mozilla/')?g:'',is('j2me')?m+' j2me':is('iphone')?m+' iphone':is('ipod')?m+' ipod':is('ipad')?m+' ipad':is('mac')?'mac':is('darwin')?'mac':is('webtv')?'webtv':is('win')?'win'+(is('windows nt 6.0')?' vista':''):is('freebsd')?'freebsd':(is('x11')||is('linux'))?'linux':'','js']; c = b.join(' '); h.className += ' '+c; return c;}; css_browser_selector(navigator.userAgent);


//	Opens external links as blank targets via unobtrusive javascript
function externalLinks() { 
	if (!document.getElementsByTagName) return; 
	var anchors = document.getElementsByTagName("a"); 
	for (var i=0; i<anchors.length; i++) { 
		var anchor = anchors[i]; 
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") anchor.target = "_blank"; 
	} 
} 
window.onload = externalLinks; 
	
//	Used by Quotation and DownloadCreate pages when action button pressed
function doSubmit(submitType) {
	// Runs validateFields if submitType is 1, i.e. source is Quotation.php
	// Skips validateFields if submitType is 0, e.g. source is DownloadCreate.php
	if (! submitted) {
		if (submitType == 1) {
			if (! validateFields()) return false;
		}
		submitted = true;
		ProgressBtn = document.getElementById('clickSubmit');
		//ProgressBtn.disabled = true;
		ProgressBtn.value = 'Submitting, please wait...';
		ProgressImg = document.getElementById('inprogress_img');
		document.getElementById("inprogress").style.visibility = "visible";
		setTimeout("ProgressImg.src = ProgressImg.src", 100);
		
		if (submitType == 1 || submitType == 0) {
		
			messages = new Array();
			messages[0] = "Your document is being uploaded to Wordzworth - please wait for it to complete successfully";
			messages[1] = "A confirmation screen will appear once the upload is complete";
			messages[2] = "Please be patient as large documents may take a while - you'll get confirmation once it's done";
			messages[3] = "How long it takes to upload will depend on the speed of your line";
			messages[4] = "If your document is small it should soon be loaded successfully";
			messages[5] = "A 2Mb file takes less than a minute to upload on a normal broadband line";
			messages[6] = "Sometimes you need to submit much larger files to us for formatting - that's fine";
			messages[7] = "A 20Mb file might take 5 to 10 minutes to upload over a broadband line";
			messages[8] = "A larger file could take 20 minutes so just leave your browser and check back in a while for confirmation";
			messages[9] = "Your file is still uploading and may take a while yet so please don't close the page";
			messages[10] = "Please leave your browser to upload your files and check back in a while for comfirmation";
			
			msgcolor = new Array();
			msgcolor[0] = "red";
			msgcolor[1] = "green";
			msgcolor[2] = "blue";
			
			counter = 0;
			
			Next();
			IntervalId	= setInterval('Next()', 10000);
			// setTimeout("clearInterval(IntervalId)", 20000);
		}
		
		return true;
	} else {
		return false;
	}
}

//	Used within doSubmit function
function Next() {
	// Issue message
	if  (counter > 10) counter = 8;
	document.getElementById("uploadmsg").innerHTML = messages[counter];
	document.getElementById("uploadmsg").style.color = msgcolor[counter%3];
	counter++;
}

//	Called by doSubmit to validate input fields
function validateFields() {
	// Validate client name field
	var ClientName = document.getElementById("ClientName");
	var ClientNameMsg = document.getElementById("ClientNameMsg");
	ClientNameMsg.innerHTML = "";
	if (ClientName.value == null || ClientName.value == "") {
		ClientNameMsg.innerHTML = "Please enter your name";
		ClientName.focus();
		return false; }
	ClientNameRegex = /^[a-zA-Z _\'.-]{1,30}$/;
	if (!ClientName.value.match(ClientNameRegex)) {
		ClientNameMsg.innerHTML = "Name - up to 30 characters";
		ClientName.focus();
		return false; }
	
	// Validate client email field
	var ClientEmail = document.getElementById("ClientEmail");
	var ClientEmailMsg = document.getElementById("ClientEmailMsg");
	ClientEmailMsg.innerHTML = "";
	if (ClientEmail.value == null || ClientEmail.value == "") {
		ClientEmailMsg.innerHTML = "Please enter your email address";
		ClientEmail.focus();
		return false; }
	ClientEmailRegex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,20}$/;
	if (!ClientEmail.value.match(ClientEmailRegex)) {
		ClientEmailMsg.innerHTML = "Email address not valid";
		ClientEmail.focus();
		return false; }

	// Validate phone number
	var PhoneNumber = document.getElementById("PhoneNumber");
	var PhoneNumberMsg = document.getElementById("PhoneNumberMsg");
	PhoneNumberMsg.innerHTML = "";
	PhoneNumberRegex = /^[a-zA-Z0-9.+ -]{0,25}$/;
	if (!PhoneNumber.value.match(PhoneNumberRegex)) {
		PhoneNumberMsg.innerHTML = "Phone number not valid";
		PhoneNumber.focus();
		return false; }
			
	// Validate instructions
	var Instructions = document.getElementById("Instructions");
	var DeadlineMsg = document.getElementById("DeadlineMsg");
	DeadlineMsg.innerHTML = "";
	if (Instructions.value == null || Instructions.value == "") {
		DeadlineMsg.innerHTML = "Please enter your instructions for us";
		Instructions.focus();
		return false; }
	InstructionsRegex = /^[^<>]{1,5000}$/;
	if (!Instructions.value.match(InstructionsRegex)) {
		DeadlineMsg.innerHTML = "Please use alphanumeric characters only";
		Instructions.focus();
		return false; }

	// Check to see whether a deadline time has been selected
	var DeadlineTime = document.getElementById("DeadlineTime");
	
	// Validate the Deadline date
	var Deadline = document.getElementById("DPC_Deadline");
	DeadlineMsg.innerHTML = "";
	DeadlineRegex = /^[0-3][0-9]\/[01][0-9]\/20[0-9][0-9]$/;
	if (!Deadline.value.match(DeadlineRegex)) {
		DeadlineMsg.innerHTML = "Enter a date in dd/mm/yyyy format";
		Deadline.focus();
		return false; }
		
	// Validate MktgSourceComments
	var MktgSourceComments = document.getElementById("MktgSourceCommentsId");
	MktgSourceCommentsRegex = /^[^<>]{0,40}$/;
	if (!MktgSourceComments.value.match(MktgSourceCommentsRegex)) {
		MktgSourceComments.value = "normal characters only";
		MktgSourceComments.focus();
		return false; }
		
	return true;
}

function focusOnFirst() {
	if (document.forms.length > 0) {
		for (var i=0; i < document.forms[0].elements.length; i++) {
			var oField = document.forms[0].elements[i];
			if (oField.type != "hidden") {
				oField.focus();
				return;
			}
		}
	}
}

function doRegister() {

	if (! submitted) {

		// Validate client name field
		var ClientName = document.getElementById("ClientName");
		var ClientNameMsg = document.getElementById("ClientNameMsg");
		ClientNameMsg.innerHTML = "";
		if (ClientName.value == null || ClientName.value == "") {
			ClientNameMsg.innerHTML = "Please enter your name";
			ClientName.focus();
			return false; }
		ClientNameRegex = /^[a-zA-Z _\'.-]{4,30}$/;
		if (!ClientName.value.match(ClientNameRegex)) {
			ClientNameMsg.innerHTML = "Up to 30 alphanumeric characters";
			ClientName.focus();
			return false; }
		
		// Validate client email field
		var ClientEmail = document.getElementById("ClientEmail");
		var ClientEmailMsg = document.getElementById("ClientEmailMsg");
		ClientEmailMsg.innerHTML = "";
		if (ClientEmail.value == null || ClientEmail.value == "") {
			ClientEmailMsg.innerHTML = "Please enter your email address";
			ClientEmail.focus();
			return false; }
		ClientEmailRegex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,5}$/;
		if (!ClientEmail.value.match(ClientEmailRegex)) {
			ClientEmailMsg.innerHTML = "Email address not valid";
			ClientEmail.focus();
			return false; }	
		submitted = true;
		return true;
	} else {
		return false;
	}	
}

function theRotator() {
	//Set the opacity of all images to 0
	$('div.rotator ul li').css({opacity: 0.0});
	
	//Get the first image and display it (gets set to full opacity)
	$('div.rotator ul li:first').css({opacity: 1.0});
		
	//Call the rotator function to run the slideshow, 6000 = change to next image after 6 seconds
	setInterval('rotate()',3000);
}
function theRotator2() {
	//Set the opacity of all images to 0
	$('div.rotator2 ul li').css({opacity: 0.0});
	
	//Get the first image and display it (gets set to full opacity)
	$('div.rotator2 ul li:first').css({opacity: 1.0});
		
	//Call the rotator function to run the slideshow, 6000 = change to next image after 6 seconds
	setInterval('rotate2()',3000);
}
function theRotator3() {
	//Set the opacity of all images to 0
	$('div.rotator3 ul li').css({opacity: 0.0});
	
	//Get the first image and display it (gets set to full opacity)
	$('div.rotator3 ul li:first').css({opacity: 1.0});
		
	//Call the rotator function to run the slideshow, 6000 = change to next image after 6 seconds
	setInterval('rotate3()',3000);
}
function theRotatorFeedback() {
	//Set the opacity of all images to 0
	$('div.rotatorfeedback ul li').css({opacity: 0.0});
	
	//Get the first image and display it (gets set to full opacity)
	$('div.rotatorfeedback ul li:first').css({opacity: 1.0});
		
	//Call the rotator function to run the slideshow, 6000 = change to next image after 6 seconds
	setInterval('rotatefeedback()',9000);
}

function rotate() {	
	//Get the first image
	var current = ($('div.rotator ul li.show') ?  $('div.rotator ul li.show') : $('div.rotator ul li:first'));

    if ( current.length == 0 ) current = $('div.rotator ul li:first');

	//Get next image, when it reaches the end, rotate it back to the first image
	var next = ((current.next().length) ? ((current.next().hasClass('show')) ? $('div.rotator ul li:first') :current.next()) : $('div.rotator ul li:first'));

	//Set the fade in effect for the next image, the show class has higher z-index
	next.css({opacity: 0.0})
	.addClass('show')
	.animate({opacity: 1.0}, 2000);

	//Hide the current image
	current.animate({opacity: 0.0}, 2000)
	.removeClass('show');
};
function rotate2() {	
	//Get the first image
	var current = ($('div.rotator2 ul li.show') ?  $('div.rotator2 ul li.show') : $('div.rotator2 ul li:first'));

    if ( current.length == 0 ) current = $('div.rotator2 ul li:first');

	//Get next image, when it reaches the end, rotate it back to the first image
	var next = ((current.next().length) ? ((current.next().hasClass('show')) ? $('div.rotator2 ul li:first') :current.next()) : $('div.rotator2 ul li:first'));

	//Set the fade in effect for the next image, the show class has higher z-index
	next.css({opacity: 0.0})
	.addClass('show')
	.animate({opacity: 1.0}, 2000);

	//Hide the current image
	current.animate({opacity: 0.0}, 2000)
	.removeClass('show');
};
function rotate3() {	
	//Get the first image
	var current = ($('div.rotator3 ul li.show') ?  $('div.rotator3 ul li.show') : $('div.rotator3 ul li:first'));

    if ( current.length == 0 ) current = $('div.rotator3 ul li:first');

	//Get next image, when it reaches the end, rotate it back to the first image
	var next = ((current.next().length) ? ((current.next().hasClass('show')) ? $('div.rotator3 ul li:first') :current.next()) : $('div.rotator3 ul li:first'));
	
	//Set the fade in effect for the next image, the show class has higher z-index
	next.css({opacity: 0.0})
	.addClass('show')
	.animate({opacity: 1.0}, 2000);

	//Hide the current image
	current.animate({opacity: 0.0}, 2000)
	.removeClass('show');
};
function rotatefeedback() {	
	//Get the first image
	var current = ($('div.rotatorfeedback ul li.show') ?  $('div.rotatorfeedback ul li.show') : $('div.rotatorfeedback ul li:first'));

    if ( current.length == 0 ) current = $('div.rotatorfeedback ul li:first');

	//Get next image, when it reaches the end, rotate it back to the first image
	var next = ((current.next().length) ? ((current.next().hasClass('show')) ? $('div.rotatorfeedback ul li:first') :current.next()) : $('div.rotatorfeedback ul li:first'));
	
	//Set the fade in effect for the next image, the show class has higher z-index
	next.css({opacity: 0.0})
	.addClass('show')
	.animate({opacity: 1.0}, 2000);

	//Hide the current image
	current.animate({opacity: 0.0}, 2000)
	.removeClass('show');
};

$(document).ready(function() {		
	//Load the slideshow
	theRotator();
	theRotator2();
	theRotator3();
	theRotatorFeedback();
	$('div.rotator').fadeIn(2000);
    $('div.rotator ul li').fadeIn(2000); // tweek for IE
	$('div.rotator2').fadeIn(2000);
    $('div.rotator2 ul li').fadeIn(2000); // tweek for IE
	$('div.rotator3').fadeIn(2000);
    $('div.rotator3 ul li').fadeIn(2000); // tweek for IE
	$('div.rotatorfeedback').fadeIn(2000);
    $('div.rotatorfeedback ul li').fadeIn(2000); // tweek for IE
});



