 var SearchTimer = null;


 if(window.attachEvent)
    window.attachEvent("onload",setListeners);

  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 echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    return false
		 }

 		 return true					
	}


function compositionSelected(place,compositionId)
{
	
	// Sets up the array of field values
	var fieldIds = new Array(3);
	fieldIds[0] = $('firstVoteId');
	fieldIds[1] = $('secondVoteId');
	fieldIds[2] = $('thirdVoteId');
	
	// Check if the destination field is empty
	var confirmReplace = true;
	if (fieldIds[place-1].value != "")
		confirmReplace = confirm ("Are you sure you want to replace your selection?");
		
	if (confirmReplace)
	{
		// Check if the value exists already in one of the fields, if yes, it is deleted
		for (var x = 0; x <= 2; x++)
   		{
   			if (fieldIds[x].value == compositionId)
   			fieldIds[x].value = "";
   		}
   	
   		// Sets the value of selected composition   	
   		fieldIds[place-1].value = compositionId;
	
		// Runs AJAX's function to fill in the names by the ID	
		xajax_fillTheNames(xajax.getFormValues('frmVotingForm'));
	
	
		location.href = "#votingForm";
	}
}

function needSomeHelp()
{

        $('needSomeHelpPopup').style.display='block';
}

function manualEntry(field)
{
	
	// Sets up the array of field values
	var fieldIds = new Array(3);
	fieldIds[0] = $('firstVoteId');
	fieldIds[1] = $('secondVoteId');
	fieldIds[2] = $('thirdVoteId');
	
	
	// Check if the destination field is empty
	var confirmReplace = true;
	if (fieldIds[field-1].value != "")
		confirmReplace = confirm ("Are you sure you want to replace your selection?");
		
	if (confirmReplace)
	{
	
		$('ComposerFirstName').value	= '';
		$('ComposerSurname').value	= '';
		$('PieceName').value	= '';
		$('ManualEntryFieldId').value=field;
		$('divManualEntryPopup').style.display='block';	
		//location.href = "#manualEntry";
		$('ComposerFirstName').focus();
	}
}

function saveManualEntry()
{
	if( $('ComposerFirstName').value=='' || $('PieceName').value=='') {		
		alert('Please enter Composer Name and Piece Name or cancel manual entry and try our powered search.');
		return false;	
	}
	
	//if($('ManualEntryFieldId').value == 1)
        if($('rank').value == 1)
	{
			$('firstVoteComposer').value = $('ComposerFirstName').value;
			$('firstVoteComposerSurname').value = $('ComposerSurname').value;
			$('firstVoteComposerFirstName').value = $('ComposerFirstName').value
			$('firstVoteComposition').value = $('PieceName').value;
			$('firstVoteId').value	= -1;
	}
	
	if($('rank').value == 2)
	{
			$('secondVoteComposer').value = $('ComposerFirstName').value;
			$('secondVoteComposerSurname').value = $('ComposerSurname').value;
			$('secondVoteComposerFirstName').value = $('ComposerFirstName').value
			$('secondVoteComposition').value = $('PieceName').value;
			$('secondVoteId').value	= -1;
	}
	
	if($('rank').value == 3)
	{
			$('thirdVoteComposer').value = $('ComposerFirstName').value;
			$('thirdVoteComposerSurname').value = $('ComposerSurname').value;
			$('thirdVoteComposerFirstName').value = $('ComposerFirstName').value
			$('thirdVoteComposition').value = $('PieceName').value;
			$('thirdVoteId').value	= -1;
	}
	
	$('ComposerFirstName').value	= '';
	$('ComposerSurname').value	= '';
	$('PieceName').value	= '';
	$('rank').value	= 0;
	//$('divPopupContainer').style.display	= 'none';
        swc.popup.closeLast();  
        window.location = '#votingForm';
}

function moveUp(field)
{

	if (field == 1)
	{
		fldComposer = $('firstVoteComposer');
		fldComposition = $('firstVoteComposition');
		fldId = $('firstVoteId');
		
		fldComposer2 = $('thirdVoteComposer');
		fldComposition2 = $('thirdVoteComposition');
		fldId2 = $('thirdVoteId');
	
	}
	else if (field == 2)
	{
		fldComposer = $('secondVoteComposer');
		fldComposition = $('secondVoteComposition');
		fldId = $('secondVoteId');
		
		fldComposer2 = $('firstVoteComposer');
		fldComposition2 = $('firstVoteComposition');
		fldId2 = $('firstVoteId');
	}
	else if (field == 3)
	{
		fldComposer = $('thirdVoteComposer');
		fldComposition = $('thirdVoteComposition');
		fldId = $('thirdVoteId');
		
		fldComposer2 = $('secondVoteComposer');
		fldComposition2 = $('secondVoteComposition');
		fldId2 = $('secondVoteId');
	}
	
	tmpComposer = fldComposer2.value;
	tmpComposition = fldComposition2.value;
	tmpId = fldId2.value;
	
	fldComposer2.value = fldComposer.value;
	fldComposition2.value = fldComposition.value;
	fldId2.value = fldId.value;
	
	fldComposer.value = tmpComposer;
	fldComposition.value = tmpComposition;
	fldId.value = tmpId;

}


function moveDown(field)
{

	if (field == 1)
	{
		fldComposer = $('firstVoteComposer');
		fldComposition = $('firstVoteComposition');
		fldId = $('firstVoteId');
		
		fldComposer2 = $('secondVoteComposer');
		fldComposition2 = $('secondVoteComposition');
		fldId2 = $('secondVoteId');
	
	}
	else if (field == 2)
	{
		fldComposer = $('secondVoteComposer');
		fldComposition = $('secondVoteComposition');
		fldId = $('secondVoteId');

		fldComposer2 = $('thirdVoteComposer');
		fldComposition2 = $('thirdVoteComposition');
		fldId2 = $('thirdVoteId');
	}
	else if (field == 3)
	{
		fldComposer = $('thirdVoteComposer');
		fldComposition = $('thirdVoteComposition');
		fldId = $('thirdVoteId');
				
		fldComposer2 = $('firstVoteComposer');
		fldComposition2 = $('firstVoteComposition');
		fldId2 = $('firstVoteId');
	}
	
	tmpComposer = fldComposer2.value;
	tmpComposition = fldComposition2.value;
	tmpId = fldId2.value;
	
	fldComposer2.value = fldComposer.value;
	fldComposition2.value = fldComposition.value;
	fldId2.value = fldId.value;
	
	fldComposer.value = tmpComposer;
	fldComposition.value = tmpComposition;
	fldId.value = tmpId;

}





function doVote()
{
	fld1Id = $('firstVoteId');
	fld2Id = $('secondVoteId');
	fld3Id = $('thirdVoteId');

	//if ((fld1Id.value == "") | (fld2Id.value == "") | (fld3Id.value == ""))
	if (fld1Id.value == "")
	{
		alert ("Please complete your 1st vote entries")
	}
	else
	{
		xajax_sendVotes(xajax.getFormValues('frmVotingForm'));
	}
}

function sendDetails(){	
	if (!($('voterTermConditions').checked))
	{  
		alert ("Please check the \"I have read and agreed to the terms and conditions and Classic FM's privacy policy\" field");
	}
	else if (($('voterFirstName').value == "") 
		| ($('voterSurname').value == "") 
		| ($('voterEmail').value == "")	
		| ($('voterGender').value == "")
                | ($('voterDobDay').value == "") | ($('voterDobMonth').value == "") | ($('voterDobYear').value == "")
		| ($('voterPostCode').value == "")	
		| !(echeck($('voterEmail').value)))
	{
		alert ("One of the requested fields is not filled in or e-mail address is invalid");
	}
	
	else
	{
		xajax_sendDetails(xajax.getFormValues('frmVotingForm'));
	}
}

function composerSelected(AuthorId)
{
	// Sets up author id and runs the search function again to get particular results
	
//	if ($('SearchAuthorId').value != "")
//	{
		$('SearchAuthorId').value = AuthorId;
		//$('Search').value = "";
		performAuthorSearch('yes');
}

function searchAuthors() {
    if(SearchTimer) {
       clearTimeout(SearchTimer);
    }

    SearchTimer = setTimeout("performAuthorSearch('no')",200);
}

function performAuthorSearch(result) {
    xajax_searchAuthors(xajax.getFormValues('frmSearchAuthors'), result);
}

function selectAuthor(AuthorId) {
    if(SelectedAuthor) {
        if(document.getElementById('divAuthor'+SelectedAuthor)) {
            document.getElementById('divAuthor'+SelectedAuthor).style.background = 'none';
            SelectedAuthor  = null;
        }
    }
    
    document.getElementById('divAuthor'+AuthorId).style.background = '#FFEB90';
    SelectedAuthor  = AuthorId;
    xajax_getBooks(AuthorId);
}

function callBackOnFinish(obj){
	obj.element.style.display=='none';
}

function toggleInstructions(){
	var e=document.getElementById('divInstructions');
	if(e.style.display=='none'){
		Effect.SlideDown(e);
		e.style.display=='block';
	}else{
		Effect.SlideUp(e,{afterFinish: callBackOnFinish});
	}
}
