// Global JavaScript Functions

// Highlights required fields with red background. If the fields are occupied, red background turns to white
function validate(ElementID) {
mNv=document.getElementById(ElementID).value;
if (mNv!=='') {
document.getElementById(ElementID).style.backgroundColor='FFFFFF';
}
if (mNv=='') {
document.getElementById(ElementID).style.backgroundColor='FFE8E8';
}
}

// Inserts a tab into textareas
function handleTAB(e, field) {
e = window.event ? event : e && e.keyCode ? e : null;
if (e && e.keyCode == 9) {
field.value += '\t';
return false;
}
return true;
}

// Confirm Deletion
function deleteobject() 
{ 
  if(confirm("Are you sure you want to PERMANENTLY DELETE this object?"))
  {
     return true;
  }
  else
  {
     return false;
  }
}

function removeapplication() 
{ 
  if(confirm("Removing this application will not delete it from the system.\n You will be still be able to plug the application in to this or other pages in the future.\n Do you wish to continue and remove this application?"))
  {
     return true;
  }
  else
  {
     return false;
  }
}

function selectmoretowns() 
{ 
  if(confirm("This facility enables you to select more than one town for\nyour advertisement. If this is what you want to do, please\nclick OK below. Alternatively, if this is not what you want, \nplease click CANCEL."))
  {
     return true;
  }
  else
  {
     return false;
  }
}

// checks contact consultant textarea
 var limit = 2000;
 function checkcontactconsultantcharacters() {
   if(document.contactconsultantform.contactconsultantmessagebody.value.length > limit) {
     alert('You have exceeded the character limit for your message.\nPlease ensure that your message contains no more than 2000 characters.');
     document.contactconsultantform.contactconsultantmessagebody.focus();
     return false; }
   else
     return true; }
 function updatecontactconsultantcharacters() {
   document.contactconsultantform.counter.value=document.contactconsultantform.contactconsultantmessagebody.value.length;
   if(document.contactconsultantform.counter.value > limit) {
	 document.getElementById('contactconsultantmessagebodylimitwarning').style.visibility='visible';
	 
     if(document.styleSheets) {
       document.contactconsultantform.counter.style.fontWeight = 'bold';
	   document.contactconsultantform.counter.style.fontFamily = 'verdana';
	   document.contactconsultantform.counter.style.fontSize = '70%';
       document.contactconsultantform.counter.style.color = '#ff0000'; } }
   else if(document.contactconsultantform.counter.value <= limit
	   && document.styleSheets ) {
	   document.getElementById('contactconsultantmessagebodylimitwarning').style.visibility='hidden';
       document.contactconsultantform.counter.style.fontWeight = 'bold';
	   document.contactconsultantform.counter.style.fontFamily = 'verdana';
	   document.contactconsultantform.counter.style.fontSize = '70%';
       document.contactconsultantform.counter.style.color = '#009966'; } 
	   
   }
   

// checks icd administrators textarea for maximum allowed characters
 var limit = 2000;
 function checkicdprofilecharacters() {
   if(document.icdadmin.icdprofile.value.length > limit) {
     alert('You have exceeded the character limit for this consultant\'s profile.\nPlease ensure that this consultant\'s profile contains no more than 2000 characters.');
     document.icdadmin.icdprofile.focus();
     return false; }
   else
     return true; }
 function updateicdprofilecharacters() {
   document.icdadmin.counter.value=document.icdadmin.icdprofile.value.length;
   if(document.icdadmin.counter.value > limit) {
	 document.getElementById('icdprofilelimitwarning').style.visibility='visible';
	 
     if(document.styleSheets) {
       document.icdadmin.counter.style.fontWeight = 'bold';
	   document.icdadmin.counter.style.fontFamily = 'verdana';
	   document.icdadmin.counter.style.fontSize = '70%';
       document.icdadmin.counter.style.color = '#ff0000'; } }
   else if(document.icdadmin.counter.value <= limit
	   && document.styleSheets ) {
	   document.getElementById('icdprofilelimitwarning').style.visibility='hidden';
       document.icdadmin.counter.style.fontWeight = 'bold';
	   document.icdadmin.counter.style.fontFamily = 'verdana';
	   document.icdadmin.counter.style.fontSize = '70%';
       document.icdadmin.counter.style.color = '#009966'; } 
	   
   }

// CMS - copies CMS data to hidden form field
function myOnSubmitEventHandler() {
document.getElementById('editor').EscapeUnicode = true;
document.getElementById('objectcontent').value = document.getElementById('editor').value;
}

//add multi select option
function addOption(a,b){
	flag = 0;
	lengthoftwo = b.options.length;
	
	if(a.selectedIndex == -1 || a.options[a.selectedIndex].value == 0){
		alert('Please select a town, city or London borough');
	  	return false;
	  	flag = 1
	 }
	 
	 for (i = 0; i <= lengthoftwo -1; i++){
     if(b.options[i].value == a.value){
	  alert('The town, city or London borough you have selected has already been added to the selected items');
	  return false;
	  flag = 1}
    }

	for (i = 0; i <= lengthoftwo -1; i++){
     if(b.options[i].value == a.value){
	  flag = 1
	 }

    }
	
	if (flag != 1){
		b.options[lengthoftwo] = new Option(a.options[a.selectedIndex].innerHTML,a.options[a.selectedIndex].value);
	//document.getElementById(selectedtowns).style.backgroundColor='FFFFFF';
	document.forms.jobadsform.selectedtowns.style.backgroundColor='FFFFFF';
	}
}

//remove multi select option
function removeOption(a){
	if (a.options.selectedIndex >= 0){
		lengthoftwo = a.options.selectedIndex;
		
		if (a.options[lengthoftwo]){
		
			a.options[lengthoftwo] = null;
		}
	}
	if (a.options.length == 0) {
	document.forms.jobadsform.selectedtowns.style.backgroundColor='FFE8E8';
	}
}

// Submit the town list to a hidden form variable
function saveTowns(){
	myval = '';
	comma = '';
	lengthoftwo = document.forms.jobadsform.selectedtowns.options.length;
	for (i = 0; i <= lengthoftwo -1; i++){
     myval = myval + comma + jobadsform.selectedtowns.options[i].value;
	 comma = ',';
    }
	jobadsform.addedtowns.value = myval;
}

// Final form actions when the form gets submitted
function checkForm(){
		saveTowns();
		return true;
}

function undoClosingDateRequired() {
	if (document.forms.jobadsform.noclosingdate.checked==true) {
	document.forms.jobadsform.closingdateday.style.backgroundColor='FFFFFF'
	document.forms.jobadsform.closingdateday.selectedIndex=''
	document.forms.jobadsform.closingdatemonth.style.backgroundColor='FFFFFF'
	document.forms.jobadsform.closingdatemonth.selectedIndex=''
	document.forms.jobadsform.closingdateyear.style.backgroundColor='FFFFFF'
	document.forms.jobadsform.closingdateyear.selectedIndex=''
	}
	else {
	document.forms.jobadsform.closingdateday.style.backgroundColor='FFE8E8'
	document.forms.jobadsform.closingdatemonth.style.backgroundColor='FFE8E8'
	document.forms.jobadsform.closingdateyear.style.backgroundColor='FFE8E8'
	}
}

// Create the popup object. For predictable results, always create
// a global popup object.
var oPopup = window.createPopup();
function openPopup()
{
    // The popup object exposes the document object and its
    // properties.
    var oPopBody = oPopup.document.body;
    // The following HTML that populates the popup object with a string.
	oPopBody.style.backgroundColor="#FBF0FB"
	oPopBody.style.border="solid #CCCCCC 1px"
	//oPopBody.style.padding="10px"
	oPopBody.style.fontFamily="verdana"
	oPopBody.style.fontSize="70%"
    oPopBody.innerHTML = "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr><td align=\"right\" height=\"20\" style=\"background-image: url(/img/fake_window_title_bar.gif); background-repeat: repeat-x;\"><img src=\"/img/Icon_close_window.gif\" onclick=\"parent.oPopup.hide();\" style=\"cursor:hand;\" alt=\"Close Window\" /></td></tr><tr><td style=\"font-size:70%;padding:10px;background-color:#FBF0FB;\">This is a pop up window</td></tr></table>"
//This is a popup. <button onclick=\"parent.oPopup.hide();\" style=\"border:1px solid black; border-left:1px solid white; border-top:1px solid white; background:#cccccc \">Close Message</button>"
    // Parameters of the show method are in the following order: x-coordinate,
    // y-coordinate, width, height, and the element to which the x,y 
    // coordinates are relative. Note that this popup object is displayed
    // relative to the body of the document.
    oPopup.show(350, 150, 150, 150, document.body);
}





