function setAttribute_IE_HACK( node, attribute, value )
{
	var prev	= node.getAttribute( attribute );
	if ( document.all )
	{
		if ( prev )
		{
			var braces = /{|}/gim;
			prev	= prev.toString( ).replace( 'function anonymous()', '' ).replace( braces, '' );
			value	= prev + value;
		}
		switch( attribute )
		{
			case 'onmouseover':
				node.onmouseover = Function( value );
			break;

			case 'onmouseout':
				node.onmouseout = Function( value );
			break;

			/*case 'onfocus':
				node.onfocus = Function( value );
			break;*/

			case 'onblur':
				node.onblur = Function( value );
			break;

			case 'onclick':
				node.onclick = Function( value );
			break;

			case 'onsubmit':
				node.onsubmit = Function( value );
			break;

			case 'onkeypress':
				node.onkeypress	= Function( value );
			break;

			case 'onkeyup':
				node.onkeyup	= Function( value );
			break;

			case 'onchange':
				node.onchange	= Function( value );
			break;
		}


	} else
	{
		prev ?
			value = prev + value :
			value = value;
		node.setAttribute( attribute, value );
	}
}
var orig;
var w;
document.all ?
	w = '#fff' :
	w = 'rgb(255, 255, 255)';
function toggleColour( elIdE )
{
	var elId = document.getElementById( elIdE );
	if ( elId )
	{
		if ( ! orig )
		{
			orig = elId.style.backgroundColor ?
				orig = orig :
				orig = w;
		}
		var col;
		document.all ?
			col = '#f0fbd7':
			col = 'rgb(240, 251, 215)';
		elId.style.backgroundColor == col ?
			elId.style.backgroundColor = orig:
			elId.style.backgroundColor = col;
	}
	return true;
} // end of member function toggleColour

function curveFix( )
{
	var curve = document.getElementById( 'navCurve' );
	if ( curve )
	{
		curve.style.display == 'none' ?
			curve.style.display = '' :
			curve.style.display = 'none';
	}
}

function menuFlick( ele, status, header )
{
	document.getElementById( ele ).style.display = status;
	var eleColor = document.getElementById( header ).style.backgroundColor;

	if ( status == 'inline' )
	{
		document.getElementById( header ).style.backgroundColor = '#E5E5E5';
	} else {
		document.getElementById( header ).style.backgroundColor = '#C2C2C2';
	}


}

function menuFlickOut( ele, status, header )
{
	document.getElementById( ele ).style.display = status;
}

function initSettings( )
{
//	accountSummaryEffect( );
	formFocusEffect( );
//  makeSubNavs( );
  if ( document.all )
  {
  	controlStatusIE_HACK( );
  }
	getInputFocus( );
	fixUpTextarea( );
	if ( typeof window.formChecker == 'function' )
	{
		formChecker( );
	}
	if ( document.getElementById( 'roiCalc' ) )
	{
		roiCalcInit( );
	}
	makeLinkStatus( );
//	customUploadButton( );
//	menuFix( );
//  doFormCheck( );
}

function roiCalc( )
{
	var cpc		= Number( document.getElementById( 'costPerClick' ).value );
	var conR	= Number( document.getElementById( 'conversionRate' ).value );
	var custLT	= Number( document.getElementById( 'customerLifeTime' ).value );
	var curtON	= Number( document.getElementById( 'customerOrderNum' ).value );
	var avgOr	= Number( document.getElementById( 'orderValue' ).value );

	var custVal	= document.getElementById( 'customerLifeTimeValue' );
	var roiTot	= document.getElementById( 'roiTotal' );
	var roiPer	= document.getElementById( 'roiPercent' );
	try
	{
		custVal.value		= custLT * avgOr * curtON;
		var totBus			= ( 500 / cpc ) * ( custVal.value * ( conR / 100 ) )
		totBus				= totBus.toFixed( 2 );
		if ( totBus == 'NaN' )
		{
			totBus = '0';
		}
		var roiPerC			= ( totBus / 5 ).toFixed( 0 )
		if ( roiPerC == 'NaN' )
		{
			roiPerC = '0';
		}
		roiTot.innerHTML	= totBus;
		roiPer.innerHTML	= roiPerC;
		
	} catch( e )
	{
		alert( e );
	}

}

function roiCalcInit( )
{
	var roiCalcE	= document.getElementById( 'roiCalc' );
	document.getElementById( 'customerLifeTimeValue' ).readOnly = 'readonly';
	document.getElementById( 'costPerClick' ).value=  0;
	document.getElementById( 'conversionRate' ).value=  0;
	document.getElementById( 'customerLifeTime' ).value=  0;
	document.getElementById( 'customerOrderNum' ).value=  0;
	document.getElementById( 'orderValue' ).value=  0;
	setAttribute_IE_HACK( roiCalcE, 'onclick', 'roiCalc( )' );
	setAttribute_IE_HACK( roiCalcE, 'onkeyup', 'roiCalc( )' );
	try
	{
		roiCalc( );
	} catch( e )
	{
		alert( e )
	}
}

var accountSum;
var ulBlock;
var titleBlock;
function accountSummaryEffect( )
{
	accountSum	= document.getElementById( 'accountSummaryBlock' );
	if ( accountSum )
	{
		titleBlock	= document.getElementById( 'accountSummaryBlockTitle' );
		ulBlock		= document.getElementById( 'accountSuumaryBlockUL' );
		accountSum.style.borderBottomWidth = 0 + 'px';
		ulBlock.style.display	= 'none';
		setAttribute_IE_HACK( accountSum, 'onmouseover', 'toggleAccountSummary( )' );
		setAttribute_IE_HACK( accountSum, 'onmouseout', 'toggleAccountSummary( )' );
	}
}

function toggleAccountSummary( )
{
	if ( accountSum )
	{
		if ( ulBlock.style.display == 'block' )
		{
			ulBlock.style.display = 'none';
			accountSum.style.borderBottomWidth = 0 + 'px';
		} else
		{
			accountSum.style.borderBottomWidth = 1 + 'px';
			ulBlock.style.display = 'block';
		}
	}
}

function formFocusEffect( )
{
	var forms	= document.getElementsByTagName( 'form' );
	if ( forms )
	{
		var formsLen	= forms.length;
		var i;
		for( i = 0; i < formsLen; i++ )
		{
			if ( forms[i].name != 'formsPPC' )
			{
				var formsEle	= forms[i].elements;
				if ( formsEle )
				{
					var formsEleLen	= formsEle.length;
					var j			= 0;
					for( j = 0; j < formsEleLen; j++ )
					{
						if ( formsEle[j].id &&
							! ( formsEle[j].type.toLowerCase( ) == 'radio'
								|| formsEle[j].type.toLowerCase( ) == 'checkbox'
								|| formsEle[j].type.toLowerCase( ) == 'submit' )
							 )
						{
							setAttribute_IE_HACK( formsEle[j], 'onfocus', 'toggleColour( this.id )' );
							setAttribute_IE_HACK( formsEle[j], 'onblur', 'toggleColour( this.id )' );
						}
					}
				} // end if formsEle
			}
		}
	} // end if forms
}

function getHeight( el )
{
	return el.offsetHeight;
}

function fixUpTextarea( )
{
	var ta	= document.getElementsByTagName( 'textarea' );
	if ( ta )
	{
		var taLen = ta.length;
		var i;
		for( i = 0; i < taLen; i++ )
		{
			if ( ta[i].value == ' ' )
			{
				ta[i].value = '';
			}
		}
	}
} // end of function fixUpTextarea

function getInputFocus( )
{
	var ins = document.getElementsByTagName( 'input' );
	var noFocus	= document.getElementById( 'roiCalc' );
	if ( ins && ! noFocus )
	{
		var insLen = ins.length;
		var i;
		for( i = 0; i < insLen; i++ )
		{
			if ( ins[i].type.toLowerCase( ) == 'text' && ! ( ( ins[i].disabled && ins[i].disabled == 'disabled' ) || ( ins[i].readOnly && ins[i].readOnly == true ) ) )
			{
				orig	= ins[i].style.backgroundColor;
				//ins[i].focus( );
				return true;
			}
		}
	}
	return false;
}

function makeLinkStatus( )
{
	var hrefs = document.getElementsByTagName( 'a' );
	if ( hrefs )
		{
		var hrefsLen = hrefs.length;
		var i;
//		var regEx = /"/gmi;
//		var regExBr = /<br \/>/gmi;
//		var done = "window.status = 'Done'; return true;";
		for ( i = 0; i < hrefsLen; i++ )
		{
			if ( hrefs[i].className && hrefs[i].className.indexOf( 'BLANKPOP' ) != -1 )
			{
				setAttribute_IE_HACK( hrefs[i], 'onclick', "window.open('" + hrefs[i].href + "', '" + hrefs[i].id + "', 'width=780,height=575,noresize,scrollbars=yes,status=0'); return false;" );
			} else if ( hrefs[i].className && hrefs[i].className.indexOf( 'BLANKPROFILE' ) != -1 )
			{
				setAttribute_IE_HACK( hrefs[i], 'onclick', "window.open('" + hrefs[i].href + "', '" + hrefs[i].id + "', 'width=440,height=575,noresize,scrollbars=yes,status=0'); return false;" );
			} else if ( hrefs[i].className && hrefs[i].className.indexOf( 'BLANKQUESTIONS' ) != -1 )
			{
				setAttribute_IE_HACK( hrefs[i], 'onclick', "window.open('" + hrefs[i].href + "', '" + hrefs[i].id + "', 'width=900,height=750,noresize,scrollbars=yes,status=0'); return false;" );
			} else if ( hrefs[i].className && hrefs[i].className.indexOf( 'BLANKDEMO' ) != -1 )
			{
				setAttribute_IE_HACK( hrefs[i], 'onclick', "window.open('" + hrefs[i].href + "', '" + hrefs[i].id + "', 'width=588,height=325,noresize,scrollbars=no,status=0'); return false;" );
			} else if ( hrefs[i].className && hrefs[i].className.indexOf( 'BLANK_LEAD_DETAILS' ) != -1 )
			{
				setAttribute_IE_HACK( hrefs[i], 'onclick', "var newWin" + hrefs[i].id + " = window.open('" + hrefs[i].href + "', '" + hrefs[i].id + "', 'width=560,height=440,resize=yes,scrollbars=yes,status=0'); newWin" + hrefs[i].id + ".focus( ); return false;" );
			} else if ( hrefs[i].className && hrefs[i].className.indexOf( 'DEMOPOP' ) != -1 )
			{
				setAttribute_IE_HACK( hrefs[i], 'onclick', "var newWin" + hrefs[i].id + " = window.open('" + hrefs[i].href + "', '" + hrefs[i].id + "', 'width=780,height=480,noresize,scrollbars=no,status=0'); newWin" + hrefs[i].id + ".focus( ); return false;" );
			} else if ( hrefs[i].className && hrefs[i].className.indexOf( 'LEADPOP' ) != -1 )
			{
				setAttribute_IE_HACK( hrefs[i], 'onclick', "var newWin" + hrefs[i].id + " = window.open('" + hrefs[i].href + "', '" + hrefs[i].id + "', 'width=780,height=480,noresize,scrollbars=yes,status=0'); newWin" + hrefs[i].id + ".focus( ); return false;" );
			} else if ( hrefs[i].className && hrefs[i].className.indexOf( 'BLANK' ) != -1 )
			{
				hrefs[i].target = '_blank';
			}
			if ( hrefs[i].title )
			{
				hrefs[i].title = '';
//      	var urlString = ( hrefs[i].title ).replace( regEx, '\"' ).replace(regExBr, ' - ');
//        setAttribute_IE_HACK(hrefs[i], 'onmouseover', 'window.status = "' + urlString + '"; return true;');
//        setAttribute_IE_HACK(hrefs[i], 'onmouseout', 'window.status = "Done"; return true;');
			}
		}
	}
}

var currShowing;
function toggleSubNav( id )
{
	var subNav = document.getElementById( id + 'Sub' );
	if ( currShowing )
	{
		currShowing.style.display = 'none';
	}
	if ( subNav )
	{
		subNav.style.display = 'block';
		currShowing = subNav;
	}
	return true;
} // end of function toggleSubNav

function makeSubNavs( href )
{
	var containers = new Array( 'headerNav', 'mainNavContainer', 'mainNavContainerB' );
	var containersLen = containers.length;
	var i;
	var regEx = /"/gmi;
	var regExBr = /<br \/>/gmi;
	for ( i = 0; i < containersLen; i++ )
	{
		var tmp = document.getElementById( containers[i] );
		var refs = tmp.getElementsByTagName( 'a' );
		var refsLen = refs.length;
		var j = 0;
		for ( j = 0; j < refsLen; j++ )
		{
			var childNav = document.getElementById( containers[i] + refs[j].id.replace( containers[i], '' ) + 'Sub' );
			if ( childNav && childNav.id )
			{
				childNav.style.display = 'none';
				var urlString = ( refs[j].title ).replace( regEx, '\"' ).replace(regExBr, ' - ');
				setAttribute_IE_HACK(refs[j], 'onmouseover', 'window.status = \'' + urlString + '\'; return toggleSubNav(\'' + refs[j].id + '\' );' );
			}
		} // end for j
	} // end for i
} // end of function makeSubNavs


/**
 * function toggleBg
 * Toggle the background color of an element
 *
 * @param id - The id of the element
 * @param colour - The color to toggle to
 * @return bool
 *
 */
function toggleBg( id, colour )
{
	var ele = document.getElementById( id );
	if ( ele )
	{
		if ( ! document.all )
		{
			colour = '#' + colour + ';';
		}
		ele.style.backgroundColor = colour;
	}
	return true;
} // end of function toggleBg

function rpcMake( )
{
	rpc = document.getElementById( 'rpc' );
	if ( ! rpc )
	{
		var rpcF = document.createElement( 'iframe' );
		rpcF.id	= 'rpc';
		rpcF.setAttribute( 'name', 'rpc' );
		rpcF.style.width = 0 // '100%';
		rpcF.style.height = 0 //'600px';
		rpcF.style.border = '0px';
		rpcF.frameBorder = 'no';
		document.body.appendChild( rpcF );
		rpc = document.getElementById( 'rpc' );
	}
	return rpc;
}

var rpc
function catToggle( id )
{
	var ele = document.getElementById( id );
	var index = ele.id.replace( 'toggle', '' );
	ele.blur();
//	window.status = ele.id + ', ' + ele.innerHTML;
	rpcMake( )
	var contents;
	document.all ?
		contents = ele.innerHTML :
		contents = ele.textContent;
	switch ( contents )
	{
		case '+':
			getSubs( id, index );
		break;

		case '-':
			delSubs( id, index );
		break;
	} // end switch ( ele.textContent )
//	document.getElementById( 'catListForm' ).submit( );
	return false;
}

function getSubs( id )
{
	var ele = document.getElementById( id );
	var exist	= document.getElementById( 'child' + id.replace( 'toggle', '') )

	if ( exist )
	{
		exist.style.display = 'block';
	} else
	{
		rpc.src = ele.href + '?rpc&showXm';
	}
	ele.innerHTML = '-';
	return false;
} // end function getSubs

function delSubs( id )
{
	var ele = document.getElementById( id );
	var exist	= document.getElementById( 'child' + id.replace( 'toggle', '') )
	exist.style.display = 'none';
	ele.innerHTML = '+';
	return false;
} // end function delSubs

function maxPerDayChange( id )
{
	var cell = document.getElementById( id );
	if ( cell )
	{
		var val = cell.innerHTML;
		var inp = val.indexOf( "value" );
		if ( inp != -1 )
		{
			//alert( val.indexOf( 'value=' ) );
			val = val.substring( inp, inp + 1 );
		} else
		{
			var cContent = '<form id="maxPerDayForm" target="rpc" method="post" action="' + location.href + 'maxPerDay/" onsubmit="return false;">';
			cContent += '<input type="text" size="3" value="' + val + '" name="maxPerDayVal" id="maxPerDay" style="border: 1px solid #000000; text-align: right; padding: 0px;" onblur="submitMaxPerDay( \'' + id + '\' )" />';
			cContent += '<input type="hidden" name="catListId" value="' + id + '" /></form>';
			cContent += '&nbsp;√';
			cell.innerHTML = cContent;
			document.getElementById( 'maxPerDay' ).focus( );
		}
	}
}

function submitMaxPerDay( id )
{
	var cell = document.getElementById( id );
	if ( cell )
	{
		var inp = cell.getElementsByTagName( 'input' );
		if ( inp )
		{
			var newVal	= inp[0].value;
			var catId	= inp[1].getAttribute( 'value' );
			rpcMake( );
			var theForm = document.getElementById( 'maxPerDayForm' );
			rpc.src = location.href + 'maxPerDay/?maxPerDayVal=' + newVal + '&catListId=' + catId;
			if ( newVal == 0 )
			{
				newVal = '∞';
			}
			cell.innerHTML = newVal;
			return false;
		}
	}
}

function selectAllTableForm( tbId )
{
	var tbl = document.getElementById( tbId );
	if ( tbl )
	{
		var inps = tbl.getElementsByTagName( 'input' );
		var inpsLen = inps.length;
		if ( inps && inpsLen > 0 )
		{
			var i;
			var found = 'getMe';
			for ( i = 0; i < inpsLen; i++ )
			{
				if ( inps[i] && inps[i].type == 'checkbox' )
				{
					if ( found == 'getMe' )
					{
						inps[i].checked == false ?
							found = false :
							found = 'checked';
					} // end if found
					inps[i].checked = found;
				} // end if inputs
			} // end for i
		} // end if inps && inspLen
	}
}

/**
 * function controlStatusIE_HACK
 * Fix up IE's poor support for :active, :hover pseudonyms
 * by altering the class to support it
 *
 * @return void
 */
function controlStatusIE_HACK( )
{
	var contStat = document.getElementById( 'manageHeader' );
	if ( contStat )
	{
		// onfocus="this.className += '_Click';" onblur="this.className = this.className.replace( /_Click/g, '');"
		var child = contStat.childNodes;
		var childLen = child.length;
		var i;
		for ( i = 0; i < childLen; i++ )
		{
			if ( child[i] && child[i].type && child[i].type == 'submit' )
			{
//				setAttribute_IE_HACK( child[i], 'onclick', 'this.className += \'_Click\';' );
//				setAttribute_IE_HACK( child[i], 'onfocus', 'this.className += \'_Click\';' );
				setAttribute_IE_HACK( child[i], 'onblur', 'this.className = this.className.replace( /_Click|_Over/g, \'\' );' );
				setAttribute_IE_HACK( child[i], 'onmouseover', 'this.className += \'_Over\';' );
				setAttribute_IE_HACK( child[i], 'onmouseout', 'this.className = this.className.replace( /_Over|_Click/g, \'\' );' );
			}
		}
	}
} // end of function controlStatusIE_HACK

/**
 * function controlStatus
 * Check that some categories are selected and update to the correct status
 *
 * @param action What type of setting to control
 * @return bool
 */
function controlStatus( e, action )
{
	var toRet = false;
	if (document.all )
	{
		var classN = e.srcElement.className;
		e.srcElement.className = classN + '_Click';
	}
	var table = document.getElementById( 'manageCatAndSummary' );
	if ( table )
	{
		var checks = table.getElementsByTagName( 'input' );
		var checksLen = checks.length;
		if ( checks && checksLen > 0 )
		{
			var i;
			var isChecked = false;
			for ( i = 0; i < checksLen; i++ )
			{
				if ( action == 'Pause All' )
				{
					if ( checks[i].type == 'checkbox' )
					{
						checks[i].checked = 'checked';
					}

				}
				if ( checks[i].checked == true )
				{
					isChecked = true;
				}
			}
			if ( isChecked )
			{
				toRet = true

			} else
			{
				alert( 'Please select a category to \'' + action + '\'' );
			}
		}
	}
	if ( action == 'Pause All' )
	{
		action = 'Pause';
	}
	if (document.all )
	{
		var t = 0;
		e.srcElement.className = classN;
		e.srcElement.blur( );
	} else
	{
		e.target.blur( );
	}
	return toRet;
} // end of function controlStatus

/**
 * function enabledDates
 * Enable the dates to be altered
 *
 * @param startDateSelect
 * @param startDateSelect
 * @return void
 */
function enabledDates( style, startRange, endRange )
{
	this.enableEles = function( ele )
	{
		var eleKid = ele.childNodes;
		var eleLen = eleKid.length;
		var i;
		for ( i = 0; i < eleLen; i++ )
		{
			if ( eleKid[i] && eleKid[i].tagName.toLowerCase( ) == 'select' )
			{
				style == 'enable' ?
					eleKid[i].disabled = false :
					eleKid[i].disabled = true;
			}
		}
	}
	var startDiv	= document.getElementById( startRange );
	var endDiv		= document.getElementById( endRange );
	var setRange	= document.getElementById( 'setRange' );
	if ( startDiv )
	{
		this.enableEles( startDiv );
	}
	if ( endDiv )
	{
		this.enableEles( endDiv );
	}
	if ( setRange )
	{
		style == 'enable' ?
			setRange.disabled = true :
			setRange.disabled = false;

	}
}
// end of function enabledDates

function checkEmail( email )
{
	var inputField	= document.getElementById( email );
	var emailPass = true;
	if ( inputField )
	{
		var emailReg	= "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
		var regex		= new RegExp( emailReg );
		emailPass		= regex.test( inputField.value );
		if ( ! emailPass )
		{
			alert( 'Please enter your email address' );
		}
		inputField.select( );
	}
	return emailPass ? true : false;
}

/**
 * function for flipping error messages
 */
function flip(rid)
{
	var current = (document.getElementById(rid).style.display == 'none') ? 'block' : 'none';
	document.getElementById(rid).style.display = current;
	return false;
}
// check to see if function exists;
if(typeof window.flip == 'function')
{
//alert( 'good' );
}


function validEmail( email )
{
	return checkEmail( email );
}
function validEmailConf( emailAddress )
{
	var email	= document.getElementById( 'email' );
	if ( email && validEmail( email.value ) )
	{
		return ( emailAddress == email.value ) ? true : false;
	}
	return false;
}



var theFormSubmit = false;
function disputeConfirm( checkbox )
{
	if ( checkbox.checked == false )
	{
		var areYouSure	= confirm( 'You need to accept these terms before being able to continue.' );
		if ( ! areYouSure )
		{
			alert( 'Please Accept the terms & conditions' );
			theFormSubmit	= document.getElementById( 'formElement42' );
			if ( theFormSubmit )
			{
				setAttribute_IE_HACK( theFormSubmit, 'onclick', 'return false;' );
			}
		} else
		{
			if ( theFormSubmit )
			{
				theFormSubmit.onclick = 'return true';
//				setAttribute_IE_HACK( theFormSubmit, 'onclick', '; return true;' );
			}
			checkbox.checked = 'checked';
		}
	} else
	{
		if ( theFormSubmit )
		{
			theFormSubmit.onclick = '';
			theFormSubmit.onclick = 'return true';
			if( ! document.all )
			{
				theFormSubmit.setAttribute( 'onclick', 'return true;' );
			}
//			setAttribute_IE_HACK( theFormSubmit, 'onclick', '; return true;' );
		}
		checkbox.checked = 'checked';
	}
}

function validPassConf( ele )
{
	if ( ele.id == 'passwordConf' )
	{
		var pass	= document.getElementById( 'password' );
		if ( validPassConf( pass ) && pass.value == ele.value )
		{
			return true;
		}
	} else
	{
		if ( ele.value.length > 5 )
		{
			return true;
		}
	}
	return false;
}




function textCounterInit( charLimit, elem, minLength )
{
	/*
	 * NASTY HACK FOR IE THROWING STUPID BUG FOR KEYWORD NOT = SERVICES
	 */
	if ( elem == "counterkeywords" )
	{
		elem = "counterservices";
	}
	minLength > 0 ?
		minLength = minLength :
		minLength = 0;
	var counterDiv	= document.getElementById( elem );
	var textArea	= document.getElementById( elem.replace( 'counter', '' ) );

	if ( counterDiv && textArea )
	{
		setAttribute_IE_HACK( textArea, 'onkeyup', 'return textCounter( ' + charLimit + ', "' + counterDiv.id + '", "' + textArea.id + '", ' + minLength + ', event )' );
		if ( ( charLimit - textArea.value.length ) < 0 )
		{
			counterDiv.innerHTML	= 0;
		} else {
			counterDiv.innerHTML	= charLimit - textArea.value.length;
		}
		if ( minLength )
		{
			setCounterColor( counterDiv, textArea.value.length, minLength );
		}
	}
}

function textCounter( charLimit, counterDivId, textAreaId, minLength, e )
{
	var countDiv	= document.getElementById( counterDivId );
	var textArea	= document.getElementById( textAreaId );
	var len			= parseFloat( textArea.value.length );
	if ( ( charLimit - len ) < 0 )
	{
		countDiv.innerHTML = 0;
	} else {
		countDiv.innerHTML = ( charLimit - len );
	}

	if ( minLength )
	{
		setCounterColor( countDiv, len, minLength );
	}
	var currentLen	= charLimit - len;
	if ( currentLen < 1 )
	{
		if ( e.keyCode == 8		|| e.keyCode == 9	||
			 e.keyCode == 37	|| e.keyCode == 38	||
			 e.keyCode == 39	|| e.keyCode == 40	||
			 e.keyCode == 46
			)
		{
			return true;
		}
		if ( currentLen <= 0 )
		{
			textArea.value = textArea.value.substring( 0, charLimit );
		}
		return false;
	}
	return true;
}

function setCounterColor( ele, len, minLength )
{
	if ( len < minLength )
	{
		ele.style.color	= '#f00';
	} else if ( len >= minLength )
	{
		ele.style.color	= '#797979';
	}
}

function yOffset( obj )
{
	var parentOffset	= obj.offsetParent;
	var y				= 0;
	if ( parentOffset.offsetParent )
	{
		while( parentOffset.offsetParent )
		{
			y	= y + parentOffset.offsetTop;
			parentOffset = parentOffset.offsetParent;
		}
	} else if ( parentOffset.y )
	{
		y += parentOffset.y;
	}
	return y;
}

function xOffset( obj )
{
	var parentOffset	= obj;
	var x				= 0;
	if ( parentOffset.offsetParent )
	{
		while( parentOffset.offsetParent )
		{
			x	= x + parentOffset.offsetLeft;
			parentOffset = parentOffset.offsetParent;
		}
	} else if ( parentOffset.x )
	{
		x += parentOffset.x;
	}
	return x;
}

function defaultAddressCheck( obj, addressArray )
{
	if ( obj && obj.checked == true )
	{
		document.getElementById( 'compAddress1' ).value = addressArray['address1'];
		document.getElementById( 'compAddress2' ).value = addressArray['address2'];
		document.getElementById( 'compAddress3' ).value = addressArray['address3'];
		document.getElementById( 'compAddress4' ).value = addressArray['address4'];
		document.getElementById( 'compPostcode' ).value = addressArray['compPostcode'];
	} else
	{
		var doDelete = confirm( 'Remove default values?' );
		if ( doDelete )
		{
		document.getElementById( 'compAddress1' ).value = '';
		document.getElementById( 'compAddress2' ).value = '';
		document.getElementById( 'compAddress3' ).value = '';
		document.getElementById( 'compAddress4' ).value = '';
		document.getElementById( 'compPostcode' ).value = '';
		}
	}
}

function quickJump( section )
{
	location.href	= 'http://' + document.domain + '/index/QuoteModule/' + section + '/';
}

function toggleComp( compId )
{
	var theDiv	= document.getElementById( 'company' + compId );
	if ( theDiv )
	{
		if ( theDiv.style.display == 'table-row' || theDiv.style.display == 'block' )
		{
			theDiv.style.display = 'none';
		} else
		{
			if (navigator.appName == "Microsoft Internet Explorer") {
				var displayType = 'block';
			} else {
				var displayType = 'table-row';
			}
			theDiv.style.display = displayType;
		}
	}
}

function setCompanies( displayType )
{
	var i = 1;
	if ( displayType == false )
	{
		displayType = 'none';
		var toggleTxt	= 'Expand';
	} else
	{
		var toggleTxt	= 'Collapse';
	}
	var doLoop	= true;
	while( doLoop )
	{
		var theDiv	= document.getElementById( 'company' + i );
		var theTog	= document.getElementById( 'toggle' + i );
		if ( theDiv && theTog )
		{
			theDiv.style.display	= displayType;
			theTog.innerHTML		= toggleTxt;
			++i;
		} else
		{
			doLoop = false;
		}
	}
}

function toggleAll( )
{
	var theToggle	= document.getElementById( 'expandAll' );
	if ( theToggle.innerHTML.indexOf( 'Expand' ) != -1 )
	{
		theToggle.innerHTML = 'Collapse All';

		if (navigator.appName == "Microsoft Internet Explorer") {
			var displayType = 'block';
		} else {
			var displayType = 'table-row';
		}
		setCompanies( displayType );

	} else
	{
		theToggle.innerHTML = 'Expand All';
		setCompanies( false );
	}
}

function flip(rid)
  {
  	if (navigator.appName == "Microsoft Internet Explorer") {
			var current=(document.getElementById(rid).style.display == 'none') ? 'block' : 'none';
		} else {
			var current=(document.getElementById(rid).style.display == 'none') ? 'table-row' : 'none';
		}

	document.getElementById(rid).style.display = current;
  }

var formPassed = false;
function getQuoteCheck(form)
{
	var errors = '';
	var focused	= false;
	if( document.getElementById( 'name' ).value=="" )
	{
		errors += "\n" + 'Please Enter Your Name';
		if ( ! focused )
		{
			document.getElementById( 'name' ).focus();
			focused = true;
		}
	}
	if ( document.getElementById( 'organisation' ).value == "" )
	{
		errors += "\n" + 'Please enter your Organisation';
		if ( ! focused )
		{
			document.getElementById( 'email' ).focus();
			focused = true;
		}
	}
	if( document.getElementById( 'email' ).value=="" )
	{
		errors += "\n" + 'Please Enter your Email';
		if ( ! focused )
		{
			document.getElementById( 'email' ).focus();
			focused = true;
		}
	}

	if( document.getElementById( 'message' ).value.length < 10 )
	{
		errors += "\n" + 'Please Enter Your Requirements';
		if ( ! focused )
		{
			document.getElementById( 'message' ).focus();
			focused = true;
		}
	}getQuoteCheck
	if( document.getElementById( 'telephone' ).value.length < 1 )
	{
		errors += "\n" + 'Please Enter Your Telephone number';
		if ( ! focused )
		{
			document.getElementById( 'telephone' ).focus();
			focused = true;
		}
	}
	if ( document.getElementById( 'organisationSize' ).options[document.getElementById( 'organisationSize' ).selectedIndex].value == 'select' )
	{
		errors	+= "\n" + 'Please Select your Organisation Size';
		if ( ! focused )
		{
			document.getElementById( 'organisationSize' ).focus( );
			focused	= true;
		}
	}
	if( document.getElementById( 'verify' ).value.length != 5 )
	{
		errors += "\n" + 'Please Enter the text shown on the image';
		if ( ! focused )
		{
			document.getElementById( 'verify' ).focus();
			focused = true;
		}
	}
	if ( errors.length > 1 )
	{
		alert( errors );
		return false;
	}
	formPassed = true;
	return true;
}



function checkAdd()
{
 var il,imga,imgatxt;

// get all LIs in imagelist, loop over them
 il=document.getElementById('catQuestions').getElementsByTagName('li');

 for(i=0;i<il.length;i++)
 {

	pickLink=document.createElement('a');
	pickText=document.createTextNode('pick a date');
	pickLink.appendChild(pickText);

//	var newP = document.createElement("p");
//	var txt = 'Kilroy was here.';
//	var newT = document.createTextNode(txt);
//	newP.appendChild(newT);
//	var p2 = document.getElementsByTagName('p')[1];
//	p2.parentNode.insertBefore(newP,p2);

// delete the wording (new window) in the link text
// (which is the nodeValue of the first node)

 il[i].insertBefore(pickLink, il[i]);
 }
}

function liveProfileUpdate( value, updateField ) {
	if(document.all){
		 document.getElementById(updateField).innerHTML = string_replace( value, "\n", "<br />" );
	} else{
		document.getElementById(updateField).innerHTML = string_replace( value, "\n", "<br />" );
	}
	/*document.getElementById(updateField).textContent = value;*/
}

function string_replace(string, search, replace) {
   var new_string = "";
   var i = 0;
   while(i < string.length) {
	  if(string.substring(i, i + search.length) == search) {
		 new_string = new_string + replace;
		 i = i + search.length - 1;
	  }
	  else
		 new_string = new_string +
					  string.substring(i, i + 1);
   i++;
  }
  return new_string;
}

/**
 * Function to enable/disable VAT Number field on PPC Sign Up Form
 */
function flipPpcField( value )
{
	if( value == '3' || value == '4' )
	{
		document.getElementById( 'vat_number').disabled = true;
	} else {
		document.getElementById( 'vat_number').disabled = false;
	}
}

/**
 * Function to validate the PPC Sign Up Form, and submit it if correct
 */
function validate_and_submit()
{
	if (document.getElementById( 'CompanyDetails' ).first_name.value == "")
   	{
		alert("Please enter your first name.");
		  document.getElementById( 'CompanyDetails' ).first_name.focus();
   	}
   	else if (document.getElementById( 'CompanyDetails' ).last_name.value == "")
   	{
		 alert("Please enter your last name.");
		  document.getElementById( 'CompanyDetails' ).last_name.focus();
   	}
   	else if (document.getElementById( 'CompanyDetails' ).address1.value == "")
   	{
		  alert("Please enter your address.");
		  document.getElementById( 'CompanyDetails' ).address1.focus();
   	}
   	else if (document.getElementById( 'CompanyDetails' ).city.value == "")
   	{
		  alert("Please enter your city.");
		  document.getElementById( 'CompanyDetails' ).city.focus();
   	}
   	else if (document.getElementById( 'CompanyDetails' ).state.options[document.getElementById( 'CompanyDetails' ).state.selectedIndex].value == "" && document.getElementById( 'CompanyDetails' ).country.options[document.getElementById( 'CompanyDetails' ).country.selectedIndex].value == '826')
   	{
		  alert("Please choose your county from the list.");
		  document.getElementById( 'CompanyDetails' ).state.focus();
   	}
   	else if (document.getElementById( 'CompanyDetails' ).zip.value == "")
   	{
		  alert("Please enter a valid UK postal code.");
		  document.getElementById( 'CompanyDetails' ).zip.focus();
   	}
   	else if (document.getElementById( 'CompanyDetails' ).country.options[document.getElementById( 'CompanyDetails' ).country.selectedIndex].value == "")
   	{
		  alert("Please choose your country from the list.");
		  document.getElementById( 'CompanyDetails' ).country.focus();
   	}
   	else if (!document.getElementById( 'CompanyDetails' ).vat_location[0].checked && !document.getElementById( 'CompanyDetails' ).vat_location[1].checked && !document.getElementById( 'CompanyDetails' ).vat_location[2].checked && !document.getElementById( 'CompanyDetails' ).vat_location[3].checked)
   	{
		alert ("Please choose a relevant VAT Option. If VAT is not applicable to you, Please select Based in the EU and Non VAT Registered");
		//document.form.medattention.focus();
	}
	else if (document.getElementById( 'CompanyDetails' ).vat_number.value == "" && (document.getElementById( 'CompanyDetails' ).vat_location[0].checked || document.getElementById( 'CompanyDetails' ).vat_location[1].checked))
	{
		  alert("Please enter your VAT Number.");
		  document.getElementById( 'CompanyDetails' ).vat_number.focus();
	}
	else if (document.getElementById( 'CompanyDetails' ).telephone.value == "")
	{
		alert("Please fill in your Phone Number");
		document.getElementById( 'CompanyDetails' ).telephone.focus();
	}
	else if (document.getElementById( 'CompanyDetails' ).telephone.value.length < 10)
	{
		alert("Your phone number contains too few numbers. Have you included your area code?");
		document.getElementById( 'CompanyDetails' ).telephone.value = "";
		document.getElementById( 'CompanyDetails' ).telephone.focus();
	}
	else if (document.getElementById( 'CompanyDetails' ).email1.value == "")
	{
		alert("Please fill in your Email Address");
		document.getElementById( 'CompanyDetails' ).email1.focus();
	}
	else if (!validEmail(document.getElementById( 'CompanyDetails' ).email1.value))
	{
		alert("Your Email Address is invalid");
		document.getElementById( 'CompanyDetails' ).email1.value = "";
		document.getElementById( 'CompanyDetails' ).email1.focus();
	}
   	else if (document.getElementById( 'CompanyDetails' ).user_name.value == "")
   	{
		  alert("Please enter your Log-in name.");
		  document.getElementById( 'CompanyDetails' ).user_name.focus();
   	}
   	else if (document.getElementById( 'CompanyDetails' ).user_name.value.length < 6)
   	{
		  alert("Your Log-in name must be at least 6 characters long.");
		  document.getElementById( 'CompanyDetails' ).user_name.focus();
		  document.getElementById( 'CompanyDetails' ).user_name.select();
   	}
   	else if (document.getElementById( 'CompanyDetails' ).password.value == "")
   	{
		  alert("Please enter your password.");
		  document.getElementById( 'CompanyDetails' ).password.focus();
   	}
   	else if (document.getElementById( 'CompanyDetails' ).password.value.length < 6)
   	{
		  alert("Your password must be at least 6 characters long.");
		  document.getElementById( 'CompanyDetails' ).password.value = "";
		  document.getElementById( 'CompanyDetails' ).passwd2.value = "";
		  document.getElementById( 'CompanyDetails' ).password.focus();
   	}
	else if (document.getElementById( 'CompanyDetails' ).passwd2.value == "")
   	{
		  alert("Please confirm your password.");
		  document.getElementById( 'CompanyDetails' ).passwd2.focus();
   	}
   	else if (document.getElementById( 'CompanyDetails' ).password.value != document.getElementById( 'CompanyDetails' ).passwd2.value)
   	{
		  alert("Your password confirmation does not match your original password.");
		  document.getElementById( 'CompanyDetails' ).password.value = "";
		  document.getElementById( 'CompanyDetails' ).passwd2.value = "";
		  document.getElementById( 'CompanyDetails' ).password.focus();
   	}
   	else if (document.getElementById( 'CompanyDetails' ).acct_name.value == "")
   	{
		  alert("Please enter your account name.");
		  document.getElementById( 'CompanyDetails' ).acct_name.focus();
   	}
   	else if (document.getElementById( 'CompanyDetails' ).master_cat_id.options[document.getElementById( 'CompanyDetails' ).master_cat_id.selectedIndex].value == "")
   	{
		 alert("You must select a category from the list.");
		 document.getElementById( 'CompanyDetails' ).master_cat_id.focus;
   	}
   	else if (document.getElementById( 'CompanyDetails' ).cat_id.options[document.getElementById( 'CompanyDetails' ).cat_id.selectedIndex].value == "")
   	{
		 alert("You must select a sub category from the list.");
		 document.getElementById( 'CompanyDetails' ).cat_id.focus;
   	}
   	else if (document.getElementById( 'CompanyDetails' ).acct_name.value.length < 4)
   	{
		  alert("Your account name must be at least 4 characters long.");
		  document.getElementById( 'CompanyDetails' ).acct_name.focus();
		  document.getElementById( 'CompanyDetails' ).acct_name.select();
   	}
   	else if (document.getElementById( 'CompanyDetails' ).prompt.value == "NULL")
   	{
		  alert("Please select a security question from the drop down.");
		  document.getElementById( 'CompanyDetails' ).prompt.focus();
   	}
	else if (document.getElementById( 'CompanyDetails' ).answer.value == "")
   	{
		  alert("Please provide an answer to your security question.");
		  document.getElementById( 'CompanyDetails' ).prompt.focus();
   	}
   	else if (!document.getElementById( 'CompanyDetails' ).TermCheck.checked)
   	{
		  alert("You must agree to the Terms & Conditions before continuing.");
		  document.getElementById( 'CompanyDetails' ).TermCheck.focus();
   	}
  	else // if everything is valid, then submit
   	{
		  document.getElementById( 'CompanyDetails' ).submit();
   	}
}

/**
 * Function to check the validity of the Email entered on the PPC Sign Up Form
 */
function validEmail(email1) {
	invalidChars = " /:,;"
	for (i=0; i<invalidChars.length; i++) {
		badChar = invalidChars.charAt(i)
		if (email1.indexOf(badChar,0) > -1) {
			return false
		}
	}
	atPos = email1.indexOf("@",1)
	if (atPos == -1) {
		return false
	}
	if (email1.indexOf("@",atPos+1) > -1) {
		return false
	}
	periodPos = email1.indexOf(".",atPos)
	if (periodPos == -1) {
		return false
	}
	if (periodPos+3 > email1.length)    {
		return false
	}
	return true
}

/**
 * Function to check the validity of the Phone Number entered on the PPC Sign Up Form
 */
function vtphone(telephone) {
	phone=telephone;
	valid="true";
	invalidchars = "0123456789";
	for (i=0; i<phone.length; i++) {
		badchar = phone.charAt(i);
 		if (invalidchars.indexOf(badchar,0) == -1) {
  			valid="false";
 		}
	}
	if (valid=="false") {
		return false
	}
	else
	{
		return true
	}
}

/**
 * Function to check the validity of the Phone Number entered on the PPC Sign Up Form
 */
function validPNum(telephone) {
	if (telephone.charAt(0) != "0") {
		return false
	}
	return true
}

function openTarget (form, features, windowName) {
  if (!windowName)
	windowName = 'formTarget' + (new Date().getTime());
  form.target = windowName;
  open ('', windowName, features);
}


/* ### UPDATE DROP DOWN FROM PPC SIGN UP FORM ### */

/* ### AJAX CALL ### */

var request = false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
try {
	request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
	request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e2) {
	request = false;
}
}
@end @*/
if (!request && typeof XMLHttpRequest != 'undefined') {
	request = new XMLHttpRequest();
}

function updateSubSection( root, dbId )
{
	if( dbId != "" )
	{
		var url = root + "PayPerClick/grabOptionsSub/" + dbId + "/";
		request.open( "GET", url, true );
		request.onreadystatechange = goGet;
		request.send( null );
	} else {
		document.getElementById('cat_id').disabled = true;
	}
}

function goGet() {
	if (request.readyState == 4)
	{
		if (request.status == 200)
		{
			// RETRIEVE THE RESPONSE
			var response = request.responseText;
			
			// ENABLE THE SUB CAT DROP DOWN
			document.getElementById( 'cat_id' ).disabled = false;
			
			// EMPTY THE SUB CAT DROP DOWN
			document.getElementById( 'CompanyDetails' ).cat_id.options.length = 0;
			
			// SPLIT THE RESPONSE INTO INDIVIDUAL OPTIONS
			var options = response.split( '||' );
			
			// SET PARENT SELECT
			var parentNode = document.getElementById( 'cat_id' );
			
			for( i = 0; i < ( options.length - 1 ); i++ )
			{
				var optionValues = options[i].split( '::' );

				var x = document.createElement( 'option' );
				x.setAttribute( 'value', optionValues[0] );

				var y = document.createTextNode( optionValues[1] );

				x.appendChild( y );
				parentNode.appendChild( x );
			}
		}
	}
}

function confirmDeleteCard( url )
{
	var answer = confirm( "Are you sure you want to remove this payment method?" )
	if( answer ){
		window.location = url;
	}
}
