// functions to help display div based on select choices
function wordwrap(str,len) {
   var counter=0;
   var val = "";
   for(z=0;z<str.length;z++) {     
      val = val + str.charAt(z);
      if(str.charAt(z) == " ") counter = 0;
      else if(counter++ >= len) {
         counter = 0;
         val = val + " ";
      }
   }
   return val;         
}

function getNewCountry() {
   var governingLawCountry = "USA";
   var index = getObject('countrysel').selectedIndex;
   if(getObject('countrysel').options[index].text == "Canada")
      governingLawCountry = "Canada";
   return governingLawCountry;
}

function getCountry() {
   var governingLawCountry = "USA";
   for(i=0;i<getObject('countrysel').length;i++) {
      if(getObject('countrysel').options[i].text.toUpperCase() == "USA")
         governingLawCountry = "Canada";
   }
   return governingLawCountry;
}


function displayHintDiv() {
   setVisDisp("hintDiv", getFormObject("music-recording-contract","hintDisplay").checked);   
}

function checkVars() {
   var checkVarsReturn = true;

   var total = 0;
   var invalid = false;	
   for(var i=1;i<=getObject('numberOfArtists').value;i++) {
      var per = getObject('artist'+i+'PercentRoyalties').value;
      if(per.length == 0) {
         invalid = true;
         i = getObject('numberOfArtists').value;
      } else
         total += parseFloat(per);
   }
   if(!getObject('royaltiesDivShown').checked || invalid || getObject('numberOfArtists').value == 1) checkVarsReturn = true;
   else if(total <= 99.8 || total >= 100.2) {
      checkVarsReturn = false;
      getObject('artist1PercentRoyalties').focus();
   }
   setVisDisp('royaltyWarning',!checkVarsReturn);
   return checkVarsReturn;
}

function setSelectOptions(isPresent, optionInput, options) {
   if(isPresent) {
      if(optionInput.options.length != options.length) {
         optionInput.options.length = 0;
         for(var i=0;i<options.length;i++)
            optionInput.options[i] = options[i];
      }
   } else {
      if(optionInput.options.length != options.length-1) {
         optionInput.options.length = 0;
         for(i=0;i<options.length-1;i++)
            optionInput.options[i] = options[i+1];
         optionInput.options[options.length-1] = null;
      }
   }
}
       
function divdisplay() {
   displayHintDiv();

   for(i=1;i<=10;i++) {
      setVisDisp('artist'+i+'Box',i <= getObject('numberOfArtists').value);
      setVisDisp('artist'+i+'Royalties',i <= getObject('numberOfArtists').value);
      var name = getObject('artist'+i+'Name').value;
      if(name.length == 0)
         getObject('artist'+i+'RoyaltiesName').innerHTML = "Artist " + i;
      else
         getObject('artist'+i+'RoyaltiesName').innerHTML = name;
   }
   setVisDisp('USRoyaltiesBox',getObject('territory').value != "Canada");
   setVisDisp('CARoyaltiesBox',getObject('territory').value != "the USA");
   
   setVisDisp('exclusivityDurationBox',getObject('exclusiveTrue').checked);
   setVisDisp('exclusivityDurationExp',getObject('exclusiveTrue').checked);
   
   setVisDisp('majorMarketReleaseTerminateBox',getObject('terminationAllowedTrue').checked);
   setVisDisp('buyBackAllowedBox',getObject('terminationAllowedTrue').checked);
   setVisDisp('basicBuyBackAmountBox',getObject('terminationAllowedTrue').checked && getObject('buyBackAllowedTrue').checked);

   setVisDisp('specificEffectiveDateBox',getObject('effectiveDate').value == "specify date");
   
   setVisDisp('promoInfoBox',getObject('promoAppearances').checked);
   setVisDisp('memberRoyaltiesBox',getObject('royaltiesDivShown').checked);

   if(getObject('numberOfArtists').value == 1)
      getObject('artist1Number').innerHTML = "";
   else
      getObject('artist1Number').innerHTML = "First ";
	
	updatePercentage('royaltyTotal', 'numberOfArtists', 'artist', 'PercentRoyalties', 'royaltyWarning');	
	setCurrencySign();

	setVisDisp('memberRoyaltiesBox',(getObject('numberOfArtists').value != 1 && getFormObject('music-recording-contract','royaltiesEqual')[1].checked));
	setVisDisp('memberRoyaltiesEqualBox', getObject('numberOfArtists').value != 1);
}

function setCurrencySign(){
	switch(getObject('currency').value){
		case 'GBP':
			getObject('currencySign1').innerHTML="&#163;";
			break;
		case 'EUR':
			getObject('currencySign1').innerHTML="&euro;";
			break;
		default:	
			getObject('currencySign1').innerHTML="$";
			break;
	}
}

function updatePercentage(percentid, numArtistsid, percentPrefix, percentSuffix, warningdiv){
	var percentdiv, numArtists, total, temp, tempval;
	percentdiv=document.getElementById(percentid);
	numArtists=document.getElementById(numArtistsid).value;
	total=0;
	for(var i=1; i<=numArtists; i++){
		temp=document.getElementById(percentPrefix+i.toString()+percentSuffix);
		temp.value=stripLeadingZeros(temp.value);
		tempval=temp.value;
		if(tempval == "") tempval=0;
		total+=parseInt(tempval);
	}
	percentdiv.innerHTML=total;
	if(total != 100){
		percentdiv.style.color='red';
		document.getElementById(warningdiv).style.display='';
	}
	else{
		percentdiv.style.color='';
		document.getElementById(warningdiv).style.display='none';
	}			
}

function updatePercentBox(source, dest){
	if(source.value > 0 && source.value <= 100)
		dest.value = 100 - source.value;
	else if(source.value.length==''){
		source.value=100-dest.value;
	} 
	else if(source.value>100){
		source.value=100; 
		dest.value=0;
	}	
	source.value=stripLeadingZeros(source.value);
}

function stripLeadingZeros(str){
	while(str.charAt(0) == '0'){
		str=str.substring(1,str.length);
	}
	return str;
}

function setCurrency() {
   getFormObject('music-recording-contract','governingLawState').selectedIndex = 0;
//   getObject('currencyNation').value = 0;
//   if(getNewCountry() == "Canada")
//      getObject('currencyNation').value = 1;
}
