var fullpath = "http://www.businessroute.co.uk";

if(window.attachEvent) window.attachEvent("onload", setListeners);

function check_advert_employees () {

	var theSelect = document.getElementById ( "has_employees" );
	var theValue = theSelect[theSelect.selectedIndex].value;
	var theDiv = document.getElementById ( "employee_details_div" );
	
	if ( theValue == "1" ) theDiv.style.display = 'block';
	else theDiv.style.display = 'none';
	
}

function handleAdvancedSearch() {

	var theField = document.getElementById ( "advanced_search" );
	var theValue = theField.value;
	
	if ( theValue == "1" ) theField.value = 0;
	else theField.value = 1;
	
}

function check_forgot_select () {

	var theSelect = document.getElementById ( "forgot_type" );
	var theValue = theSelect[theSelect.selectedIndex].value;
	
	var theUsername = false;
	var thePassword = false;
	var theEmail = false;
	
	var usernameDiv = document.getElementById ( "username_div" );
	var emailDiv = document.getElementById ( "email_div" );
	var passwordDiv = document.getElementById ( "password_div" );

	switch ( theValue ) {
	
		case "1": theUsername = true; break;
		
		case "2": theEmail = true; break;
		
		case "3": theEmail = true; break;
		
	}
	
	if ( theUsername ) usernameDiv.style.display = 'block';
	else usernameDiv.style.display = 'none';
	
	if ( theEmail ) emailDiv.style.display = 'block';
	else emailDiv.style.display = 'none';
	
	if ( thePassword ) passwordDiv.style.display = 'block';
	else passwordDiv.style.display = 'none';
	
}

function check_profit_loss () {

	var theSelect = document.getElementById ( "profit_loss" );
	var theValue = theSelect[theSelect.selectedIndex].value;
	var theSpan = document.getElementById ( "profit_loss_span" );
	
	switch ( theValue ) {
	
		case "1": theSpan.innerHTML = "Net profit"; break;
		case "2": theSpan.innerHTML = "Net loss"; break;
		
	}
	
}

function check_advert_type() {

	var price_label = document.getElementById("price_label");
	var invest_details = document.getElementById("invest_details");

	if( document.getElementById("type_invest").checked ) {

		invest_details.style.display = 'block';
		price_label.innerHTML = "Investment required";
	
	}
	else {
	
		price_label.innerHTML = "Asking price";
		invest_details.style.display = 'none';
	
	}

}

function check_for_support_articles () {

	alert ( "Hello" );
	
}

function add_new_file(theDiv){

	theDiv = document.getElementById(theDiv);
	
	var file_name = "file_" + Math.floor(Math.random()*100+1);
	
	tempLabel = document.createElement("label");
	tempLabel.htmlFor = file_name;	
	
	tempText = document.createTextNode("Select file");
	tempLabel.appendChild(tempText);
	theDiv.appendChild(tempLabel);
	
	tempFile = document.createElement("input");
	tempFile.type = "file";
	tempFile.id = file_name;
	tempFile.name = file_name;
	tempFile.className = "form";
	theDiv.appendChild(tempFile);
	
	tempBr = document.createElement("br");
	tempBr.className = "clear";
	theDiv.appendChild(tempBr);
	
}
 
function moveDiv(theDiv){

	heightWidth = getScrollOffset();
	scroll_x = heightWidth[0];
	scroll_y = heightWidth[1];

	heightWidth = getInnerDim();
	x = heightWidth[0];
	y = heightWidth[1];
	
	theDiv.style.left = 600 + "px";
	theDiv.style.top = 0 + (scroll_y - 10) + "px";
	
}

function getScrollOffset(){

	var iebody=(document.compatMode && document.compatMode != "BackCompat")? document.documentElement : document.body;
	var dsocleft=document.all? iebody.scrollLeft : pageXOffset;
	var dsoctop=document.all? iebody.scrollTop : pageYOffset;
	
	return Array(dsocleft, dsoctop);
}

function setListeners(){
	
	inputList = document.getElementsByTagName("INPUT");    
	for(i=0;i<inputList.length;i++){
		inputList[i].attachEvent("onpropertychange",restoreStyles);
	    inputList[i].style.backgroundColor = "";
	}
	selectList = document.getElementsByTagName("SELECT");

	for(i=0;i<selectList.length;i++){
		selectList[i].attachEvent("onpropertychange", restoreStyles);
		selectList[i].style.backgroundColor = "";
	}  
}

function restoreStyles(){
	if(event.srcElement.style.backgroundColor != "") event.srcElement.style.backgroundColor = "";
}

function addBookmark(title,url) {
  if (window.sidebar) {
    window.sidebar.addPanel(title, url,"");
  } else if( document.all ) {
    window.external.AddFavorite( url, title);
  } else if( window.opera && window.print ) {
    return true;
  }
}

function MsgOkCancel(message, url){
	var fRet;
	fRet = confirm(message);
	if (fRet == true) {
	 window.location = url;
	}
}

function addPollOption(textvalue, optId, optionsName){
	pollOptCount++;
	theHolder = document.getElementById('option_holder');
	
	if(optId != undefined) pollOptId = optId
	else pollOptId = '';
	
	if(optionsName == '' || optionsName == undefined) optionsName = 'poll_options';

	var theDiv = document.createElement('div');
	theDiv.id = 'opt' + pollOptCount.toString();
	theDiv.style.marginBottom = '5px';

	var theIpt = document.createElement('input');
	theIpt.className = 'form';
	theIpt.style.width = "60%";
	theIpt.style.marginRight = "2px";
	theIpt.name = optionsName + "[" + pollOptId + "]";
	theIpt.id = 'poll_opt' + pollOptId;
	theDiv.appendChild(theIpt);
	if(textvalue != undefined) theIpt.value = textvalue;
	
	var theBtn = document.createElement('img');
	theBtn.style.verticalAlign = 'middle';
	theBtn.src = 'images/icons/delete.gif';
	theBtn.alt = 'Remove option';
	theBtn.id = "opt_rem" + pollOptCount.toString();

	theBtn.onclick = function (){	
		theHolder.removeChild(theDiv);
	}
	theDiv.appendChild(theBtn);
	
	var theBr = document.createElement('br');
	theDiv.appendChild(theBr);
	
	theHolder.appendChild(theDiv);	
}

function hideUnhide(theDiv){
	var theDiv = document.getElementById(theDiv);

	if(theDiv.style.display == 'block') theDiv.style.display = 'none';
	else theDiv.style.display = 'block';

}

function expand_message ( id ) {

	var theMessageDiv = document.getElementById ( "message_content_" + id );
	var thePreviewDiv = document.getElementById ( "message_preview_" + id );
	var theOptionsDiv = document.getElementById ( "message_options_" + id );

	//alert ( theMessageDiv.style.display );
	
	if ( theMessageDiv.style.display == 'none' || theMessageDiv.style.display == '' ) theMessageDiv.style.display = 'block';
	else theMessageDiv.style.display = 'none';
	
	if ( thePreviewDiv.style.display == 'none' || thePreviewDiv.style.display == '' ) thePreviewDiv.style.display = 'block';
	else thePreviewDiv.style.display = 'none';
	
	if ( theOptionsDiv.style.display == 'none' || theOptionsDiv.style.display == '' ) theOptionsDiv.style.display = 'block';
	else theOptionsDiv.style.display = 'none';
	
	//alert ( theMessageDiv.style.display );


}

function hideUnhideTableRow(theDiv){
	var theDiv = document.getElementById(theDiv);

	if( theDiv.style.display == 'table-row' || theDiv.style.display == 'block' ) theDiv.style.display = 'none';
	else {
	
		try { theDiv.style.display = 'table-row'; }
		catch (e) { theDiv.style.display = 'block'; }
	
	}

}

function setOpacity(id, value)
{
	var testObj = document.getElementById(id);
	testObj.style.opacity = value/10;
	testObj.style.filter = 'alpha(opacity=' + value*10 + ')';
}

function hideUnhide_val(theDiv, match_value, value, mode){

	var theDiv = document.getElementById(theDiv);
	var theDisplay = "none";
	
	switch(mode){
	
		case "==":  if(value == match_value) theDisplay = "block"; break;
		case "!=":  if(value != match_value) theDisplay = "block"; break;
		case ">": if(value > match_value) theDisplay = "block"; break;
		
	}
	
	theDiv.style.display = theDisplay;

}

function hideElement(theDiv){
	
	theDiv = document.getElementById(theDiv);
	theDiv.style.display = 'none';

}

function unHideElement(theDiv){
	
	theDiv = document.getElementById(theDiv);
	theDiv.style.display = 'block';
	
}

function NewWindow(mypage,myname,w,h,scroll, status, menubar){
	var win = null;
	LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
	settings = 'width='+w+', height='+h+', top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+', status=' + status + ', menubar=' + menubar; 
	win = window.open(mypage, myname, settings);
	win.focus();
}

function addToTextBox(textbox, text){
	var eleTextarea = document.getElementById(textbox);

	 eleTextarea.focus();
	 if( eleTextarea.createTextRange ) {
	   document.selection.createRange().text += text;
	 }
	 else if(eleTextarea.setSelectionRange){
	   var len = eleTextarea.selectionEnd;
	   eleTextarea.value = eleTextarea.value.substr( 0, len ) + text + eleTextarea.value.substr( len );
	   eleTextarea.setSelectionRange(len+text.length,len+text.length);
	 }
	 else{ 
		eleTextarea.value += text;
	 }
}

function readKey(e){
	var key = window.event ? e.keyCode : e.which;
	var keychar = String.fromCharCode(key);
	var theTextarea = document.getElementById("ticket");

	if(parseInt(key) == 32){
		//send text off to be searched for relevant articles
		var theText = theTextarea.value;
		theText = encodeURI(theText);
		makePOSTRequest(fullpath + "ajax/getArticles.php", "text=" + theText, 3);
	}
}

function findPosX(obj)
{
var curleft = 0;
if(obj.offsetParent)
	while(1) 
	{
	  curleft += obj.offsetLeft;
	  if(!obj.offsetParent)
		break;
	  obj = obj.offsetParent;
	}
else if(obj.x)
	curleft += obj.x;
return curleft;
}

function findPosY(obj)
{
var curtop = 0;
if(obj.offsetParent)
	while(1)
	{
	  curtop += obj.offsetTop;
	  if(!obj.offsetParent)
		break;
	  obj = obj.offsetParent;
	}
else if(obj.y)
	curtop += obj.y;
return curtop;
}


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

function stripHTML(value){
	var re = /<\S[^><]*>/g
	return value.replace(re, "");
}


function testAddOption(selectBox){
	var theSelect = selectBox;
	
	optionTest = true;
	lgth = theSelect.options.length = 1;
	theSelect.options[lgth] = null;
	if (theSelect.options[lgth]) optionTest = false;
	
	return optionTest;
}
	

function addOption(selectbox,text,value ){
	var optn = document.createElement("OPTION");
	var selectbox = document.getElementById(selectbox);
	
	optn.text = text;
	optn.value = value;
	selectbox.options.add(optn);
}

// From http://www.massless.org/mozedit/
function mozWrap(txtarea, open, close)
{
	var selLength = txtarea.textLength;
	var selStart = txtarea.selectionStart;
	var selEnd = txtarea.selectionEnd;
	if (selEnd == 1 || selEnd == 2)
		selEnd = selLength;

	var s1 = (txtarea.value).substring(0,selStart);
	var s2 = (txtarea.value).substring(selStart, selEnd)
	var s3 = (txtarea.value).substring(selEnd, selLength);
	txtarea.value = s1 + open + s2 + close + s3;
	return;
}

// Insert at Claret position. Code from
// http://www.faqts.com/knowledge_base/view.phtml/aid/1052/fid/130
function storeCaret(textEl) {
	if (textEl.createTextRange) textEl.caretPos = document.selection.createRange().duplicate();
}