
var promotion = false;

function setAdAmount () {
 	var duration = document.getElementById('durationRegular');
 		duration = duration.selectedIndex;
 	var adType = document.getElementById('adType');
 	 	adType = adType.selectedIndex;	
	var amounts = new Array(20, 30, 40, 50, 70, 125, 90, 130, 225);
 	var position = duration + 3*adType;
	 //alert(position+ " " +duration+ " " +adType);
	document.getElementById('amount').value = "$" + amounts[position] + ".00";
	}
	
function setAdAmountInstant (source) {
 	var lengthOption = 	source[source.selectedIndex].value;
	var duration = 	lengthOption.value;
	var amounts = new Array(0,30,30,30,60,60,60,100,100,100,100,100,100);
	var price = amounts[lengthOption];
	//debugAlert('promotion: ' + promotion);
	var originalPrice = (promotion) ? '$'+ price : '';
	price = (promotion) ? price/2 : price;
	document.getElementById('amountInstant').value =  price ;
	document.getElementById('originalPrice').innerHTML =  ' '+originalPrice + ' ' ;
	document.getElementById('price').innerHTML = ' $'+ price ;
	document.getElementById('costStatement').style.visibility='visible';
	if(duration == 12)
		document.getElementById('cheaperStatement').style.visibility='visible';
	else
		document.getElementById('cheaperStatement').style.visibility='hidden';
	return price;
	}
	
 function changeColor (source) {
	var paraSample = document.getElementById('paraSample');
	var background = paraSample.style.background;	// existiong bg color
	var customColor = document.getElementById ('customColor')
	var customColorOption = document.getElementById ('customColorOption');
	var customColorOption2 = document.getElementById ('customColorOption2');
	var newColor = 	source[source.selectedIndex].value;
	var customColorArea = document.getElementById ('customColorArea');

	if(source[source.selectedIndex].id.indexOf('custom') >= 0) {
		newColor = customColor.value;
		customColor.disabled=false;
		customColorArea.style.visibility="visible";
		alert("Not all color names are recognised by all browsers, but you are welcome to use any color name you wish. The pull down menu includes all color names that are recognised by all browers. If you want a custom color name, we recommend you try it on more than one browser to be sure it looks like the ones here.\n\n"+
				"You may also use the six-digit numeric codes for colors. These will be accepted by all browsers. Use '#' followed by six hexadecimal (0-9 and a-f)digits");
		if(newColor && validColor(newColor)) {
			customColor.style.color=newColor;				//custom color field
			customColorOption.style.color = newColor;		// the option item
			customColorOption2.style.color = newColor;		// the option item
			}
		}
	else {
		customColorArea.style.visibility="hidden";
		customColor.disabled=true;
		}
	
	if(background ==  newColor) 	
		alert('Oops ... text and background are the same (' + newcolor + ' on ' + newColor + '). You will not be able to see the text.\n To see it again, just change either the text color or the background color.');
	paraSample.style.color=newColor;
	paraSample.style.borderColor = newColor;
	source.style.color=newColor;
	}
	
 function changeBackground (source) {
	var paraSample = document.getElementById('paraSample');
 	var newBackground = source[source.selectedIndex].value;
	var textColor = document.getElementById('textColor');
	var customBackground= document.getElementById ('customBackground')
	var color = textColor[textColor.selectedIndex].value;	// existing text color for comparison
	var customBackgroundOption = document.getElementById ('customBackgroundOption');
	var customBackgroundOption2 = document.getElementById ('customBackgroundOption2');
	
	if(source[source.selectedIndex].id.indexOf('ustom') >= 0) {
		document.getElementById ('customBackgroundArea').style.visibility="visible";
		customBackground.disabled=false;
		customBackground.value = newBackground;
		alert("Not all color names are recognised by all browsers, but you are welcome to use any color name you wish. The pull down menu includes all color names that are recognised by all browers. We recommend you try any custom color name on more than one browser to be sure it looks like the ones here.\n\n"+
				"You may also use the six-digit numeric codes for colors. These will be accepted by all browsers. Use '#' followed by six hexadecimal (0-9 and a-f)digits");
		if(newBackground && validColor(newBackground)) {
			customBackgroundOption.style.background = newBackground;		// the option item
			customBackgroundOption2.style.background = newBackground;		// the option item
			customBackground.style.background=newBackground;				//custom color field
			}
		}
	else {
		//debugAlert("hiding custom area");
		document.getElementById ('customBackgroundArea').style.visibility="hidden";
		document.getElementById ('customBackground').disabled=true;
		}
		
	if(newBackground ==  color) 	
		alert('Oops ... text and background are the same (' + color + ' on ' + newBackground + '). You will not be able to see the text.\n To see it again, just change either the text color or the background color.');
	paraSample.style.background=newBackground;	
 	source.style.background=newBackground;
	}
	
function validColor(newColor) {
	return newColor;			// for now assume that all are valid -- except blank
	}
	
function	setCustomColor(element) {
		var customColorOption = document.getElementById('customColorOption');
		var customColorOption2 = document.getElementById('customColorOption2');
		var paraSample = document.getElementById('paraSample');
		var newColor = element.value;
		
		var reg1 =  /^#/; 				// number intended
		var reg2 =  /^#[0-9a-fA-F]{6}$/; // valid number
   		if (reg1.test(newColor)) { 
			//debugAlert("number intended");		
			if (!(reg2.test(newColor))) { 
				return highlightAndFail(element, "Not a valid numeric color. All number colors must be of the form '#nnnnnn' -- axactly 6 'n's where each n is a digit 0-9 or a letter a-f.");
				}
			unHighlight(element);s
			}
		
		element.style.color = newColor;
		customColorOption.value = newColor;
		customColorOption.style.color = newColor;
		customColorOption2.value = newColor;
		customColorOption2.style.color = newColor;
		paraSample.style.color=newColor;
		paraSample.style.borderColor = newColor;
		return true;
		}

function setCustomBackground(element) {
		var customBackgroundOption = document.getElementById('customBackgroundOption');
		var customBackgroundOption2 = document.getElementById('customBackgroundOption2');
		var paraSample = document.getElementById('paraSample');
		var newBackground = element.value;
		var reg1 =  /^#/; 				// number intended
		var reg2 =  /^#[0-9a-fA-F]{6}$/; // valid number
   		if (reg1.test(newBackground)) { 
			//debugAlert("number intended");		
			if (!(reg2.test(newBackground))) { 
				return highlightAndFail(element, "Not a valid numeric color. All number colors must be of the form '#nnnnnn' where n is in 0-9 or a-f.");
				}
			unHighlight(element);
			}
			
		element.style.background = newBackground;		//whatever called us
		customBackgroundOption.value = newBackground;	// the two option items
		customBackgroundOption.style.background = newBackground;	
		customBackgroundOption2.value = newBackground;
		customBackgroundOption2.style.background = newBackground;
		paraSample.style.background=newBackground;		// the sample
		return true
		}
	
function changeFamily (source) {
	var paraSample = document.getElementById('paraSample');
 	var newFamily = source[source.selectedIndex].value;
 	//var newFamilyShort = source[source.selectedIndex].innerHTML;
	//paraSample.style.fontFamily=newFamily;
	paraSample.className ='advert ' + newFamily;
	//debugAlert('new class is: '+ paraSample.className);
	source.style.className=newFamily;
	resize();
	if ((newFamily.indexOf('ursive') >= 0) || (newFamily.indexOf('antasy')  >= 0))
		alert('Caution: '+ newFamily + ' fonts are may vary from computer to computer. The text will be correct, but it may visually appear slightly different. Even if it looks good for you here, it is a good idea to look at it in other browsers or other computers.')
	//alert('new: '+paraSample.style.fontFamily);
	}
	
function changeBorderWidth (source) {
	var paraSample = document.getElementById('paraSample');
 	var width = 	source[source.selectedIndex].value;
	if(!isNaN(width)) {
		width = width*0.2;
		width = width+'em';
		}
	paraSample.style.borderWidth = width;
	}
	
function changeStyle (source) {
	var paraSample = document.getElementById('paraSample');
 	var newStyle = 	source[source.selectedIndex].value;
	//alert('new: '+newStyle+ '; old: '+paraSample.style.fontWeight+paraSample.style.fontStyle);
	if(newStyle.indexOf('old') >= 0)
		paraSample.style.fontWeight='bold';
	else
		paraSample.style.fontWeight='normal';
	//alert('new: '+paraSample.style.fontWeight);

	if(newStyle.indexOf('talic') >= 0)
		paraSample.style.fontStyle='italic';
	else
		paraSample.style.fontStyle='normal';
		
	source.style.fontStyle = paraSample.style.fontStyle;
	resize();
	}
	
function changeLink (source) {
	var anchorSample = document.getElementById('anchorSample');
 	//newLink = 	source.value;
 	var newLink = 	source.value;
	if(newLink == source.defaultValue) {
		document.getElementById('linkTest').style.visibility='hidden';
		return highlightAndFail ("Not a legal web address — either add an address or remove the request.", source);
		}
	if(!newLink) {
		if(!confirm("You have removed the link to your festival from the ad. This is ok, but your ad will be more powerful if you include the link.")) {
			document.getElementById('linkTest').style.visibility='hidden';
			return highlightAndFail('',source);
			}
		return true;
		}
	//debugAlert("before vW call: " + source.value)
	if(!validWeb(source)) {
		document.getElementById('linkTest').style.visibility='hidden';
		return false;
		}
	//debugAlert("after vW call: " + source.value)

	anchorSample.href = "http://"+source.value;
	document.getElementById('linkTest').style.visibility='visible';
	}
	
function changeText (element) {
	//debugAlert("entered changetext");
	//var defaultHeight = 350;
	if(element.id.indexOf('sub') >= 0)
		var whichTarget = 'subSample';
	else
		var whichTarget = 'textSample';
	var target = document.getElementById(whichTarget);
	var newText = safeDisplay(element);
	newText = newText.replace('%','');	// removing % because this expression gets sent back as a GET

	if(newText != element.value)
		alert("Note: illegal characters (such as '<' or '>') were stripped from your title");
	target.innerHTML = newText;
	element.value = newText;
	resize();
	if(whichTarget == 'textSample') {
		if(!newText || element.value == element.defaultValue)
			return highlightAndFail(element, "Oops, you really do want to have some text in your ad");
		unHighlight(element);
		}
	return true;
	}	
	
function resize() {
	//var normalLength = new Array(23,23,21,15,49,22); 
	//var boldLengths  = new Array(19,19,18,13,28,20);
	var widths = new Array(100, 100, 110, 150, 100,110);
	var boldFactor = new Array(1, 18/20, 1, 1, 1, 19/20); 
	var newFamily = document.getElementById('family').selectedIndex;
	var newStyle = document.getElementById('textStyle').selectedIndex;
	var paraSample = document.getElementById('paraSample');
	//var mainText = document.getElementById('mainText');
	var actualText = document.getElementById('textSample');
	var newLength = document.getElementById('mainText').value.length;
	var bold = (document.getElementById('textStyle').options[newStyle].value.indexOf('old') < 0) ? 0 : 1; //(not bold, bold)
	//var maxLength = (bold) ? boldLengths[newFamily] : normalLength[newFamily];
	var maxLength = 18 * widths[1]/widths[newFamily];
	if(bold) {
		maxLength = maxLength * boldFactor[newFamily];
		}	
	//debugAlert('newLength = '+newLength +' maxLength= '+maxLength);
	fontSize = defaultHeight;
	if(newLength > maxLength) {	// -2 to make thing start getting smaller a bit earlier then essential
		fontSize = defaultHeight * ((maxLength+2) / (newLength+2));
		//debugAlert('first change: setting height to '+ fontSize);
		if (fontSize < 100)
			fontSize = 100;
		if (fontSize > defaultHeight)		//safety clause... should never be invoked
			fontSize = defaultHeight;
		}
	//debugAlert('final setting height to '+ fontSize);
	fontSize = fontSize.toFixed(2);
	actualText.style.fontSize = fontSize +'%';
	}

	
function prepareInstant(source) {
  	var durationSource = 	document.getElementById('duration');
	var price = setAdAmountInstant (durationSource);
	document.getElementById('amountInstant').value = price;
	
	var duration = 	durationSource[durationSource.selectedIndex].value;	
	var mainText = document.getElementById('mainText').value;
	//debugAlert("enter PI: "+mainText);
	//somethingChanged = false;	// is this used?
	if(!mainText || ( mainText == document.getElementById('mainText').defaultValue)) {
		return highlightAndFail(document.getElementById('mainText'), "Oops, you did not give a text for your ad");
		}
		
 	//debugAlert("after mt validity: "+mainText);

	var subText = document.getElementById('subText').value;
	//debugAlert('subtext before check: '+ subText);
	if(!subText || subText ==  document.getElementById('subText').defaultValue)
		if(!confirm("You did not use the optional subtext. That is ok (it is not required) If you did intend to, cancel now so you can add it. (If you do not need it, just say 'ok')"))
			return highlightAndFail(document.getElementById('subText'));
	if(subText == document.getElementById('subText').defaultValue) {
		document.getElementById('subText').value = '';
		if (!confirm("Oops, you did not give a subtext for your ad. If you want a subtext, cancel now so you can add it. (If you do not need it, just say 'ok')")) {
			highlightAndCaution(document.getElementById('subText'));
			return false;
			}
		}
		
	// make sure that some attribute has changed has changed
	var featureChanged = false;
	var checkChangedItem = document.getElementById('textColor').options[document.getElementById('textColor').selectedIndex].innerHTML;
	if(checkChangedItem.indexOf('Select') >= 0) {
		if(!confirm("You did not select a text color. The default is black. If you want another color, click 'cancel'. if you want black, click 'Ok'."))
			return false;
		}
	else
		featureChanged = true;
 
	checkChangedItem = document.getElementById('background').options[document.getElementById('background').selectedIndex].innerHTML;
	if(checkChangedItem.indexOf('Select') >= 0) {
		if(!confirm("You did not select a backgrownd color. The default is white. If you want another color, click 'cancel'. if you want white, click 'Ok'."))
			return false;
		}
	else
		featureChanged = true;
		
	checkChangedItem = document.getElementById('family').options[document.getElementById('family').selectedIndex].innerHTML;
	if(checkChangedItem.indexOf('Select') >= 0) {
		if(!confirm("You did not select a font family. The default is serif. If you want another family, click 'cancel'. if you want serif, click 'Ok'."))
			return false;
		}
	else
		featureChanged = true;
 
	checkChangedItem = document.getElementById('textStyle').options[document.getElementById('textStyle').selectedIndex].innerHTML;
	if(checkChangedItem.indexOf('Select') >= 0) {
		if(!confirm("You did not select a font style. The default is normal (i.e., not bold, not italic). If you want another style, click 'cancel'. if you want normal, click 'Ok'."))
			return false;
		}
	else
		featureChanged = true;
 
		
	checkChangedItem = document.getElementById('border').options[document.getElementById('border').selectedIndex].innerHTML;
	if((checkChangedItem.indexOf('Select') >= 0) || (checkChangedItem == 0)) {
		if(!confirm("You did not select a border width. The default is narrow. If you want another width, click 'cancel'. if you want narrow, click 'Ok'."))
			return false;
		}
	else
		featureChanged = true;
	
	if(!featureChanged)
		if(!confirm("You did not select any appearance options. That means that your ad will appear with a very generic appearance (black on white, serif font, neither italic or bold, narrow border). That is ok, and if it is what you, select 'Ok'. But if you did want to set some appearnce values, then select 'Cancel' below to return to the design menu."))
			return false;
		
	var webLink = document.getElementById('webLink').value;
	if(webLink.indexOf('thedancegypsy') >= 0) 
		if(!confirm("This seems to link to the Dance Gypsy's website. Is that really what you want? You can link it to your own website\n. Select 'cancel' if you want to change this link before submitting, or 'cancel' if you do not want to link to your own site.")) {
			document.getElementById('webLink').value ='';
			return highlightAndFail(document.getElementById('webLink'));
			}
	if(webLink == 'Web address to link ad to') 
		if(!confirm("No web address provided. If you want your ad to link to your website, click 'cancel' below; add a web address; and resubmit. If you do not want a link, just select 'Ok'.")) {
			document.getElementById('webLink').value ='';
			return highlightAndFail(document.getElementById('webLink'));
			}
		else
			webLink = '';
			
	var colorSource = document.getElementById('textColor');
 	var color = 	colorSource[colorSource.selectedIndex].value;
			
	var familySource = document.getElementById('family');
 	var family = 	familySource[familySource.selectedIndex].value;	
	//debugAlert('family: '+ family);
 	var familyShort = 	familySource[familySource.selectedIndex].value;	
	
	var backgroundSource = document.getElementById('background');
 	var background = 	backgroundSource[backgroundSource.selectedIndex].value;	
	var borderSource = document.getElementById('border');
 	var border = 	borderSource[borderSource.selectedIndex].value;	
	var borderLong= border;
	if(border == 0)
		borderLong = 'none';
	else if(border == '.2em')
		borderLong = ' .2 em (narrow)';
	else if(border == '.4em')
		borderLong = '.4 em (medium)';
	else if(border == '.6em')
		borderLong = '.6 em (wide)';
					
	var textStyleSource = document.getElementById('textStyle');
 	var textStyle = 	textStyleSource[textStyleSource.selectedIndex].value;	
	
	var durationSource = document.getElementById("duration");
	var lengthOption = 	durationSource[durationSource.selectedIndex];
	var duration = 	lengthOption.value;
	if(durationSource.selectedIndex < 1) {
		alert("Oops, no time range selected. Please select the number of months for this ad to run.");
		return false;
		}
	
	var confirmMessage = "Order Summary:\n"+
					"\tMain Text: " + mainText + "\n" +
					"\tSub Text: " + subText + "\n" +
					"\tYour URL: " + webLink + "\n" +
					"\tText Color: " + color + "\n" +
					"\tBackground Color: " + background + "\n" +
					"\tBorder Thickness: " + borderLong + "\n" +
					"\tFont Family: " + familyShort + "\n" +
					"\tText Style: " + textStyle + "\n" +
					"\tDuration: " + duration + " months\n\n" +
					"\tTotal Price: $" + price + "\n\n" +
					"Click 'OK' if this is correct, or 'Cancel' if you need to fix a problem first.";
	if(!confirm(confirmMessage))
		return false;
	
	//actualy changing of values is the very last thing before the real return
	//debugAlert("before assugn itemname: "+mainText);
	var itemName = "Dance Gypsy AD: " + mainText;				// placed ahead of escape so that it will look ok when theyh see it at payal
	document.getElementById('item_nameInstant').value = itemName; // but it could ghenerate an error
	
	//mainText = escape(mainText);		// potect against improper characters
	//subText = escape(subText);
	//webLink = escape(webLink);
	document.getElementById('mainText').value = mainText;	// put the values back in the INPUT firleds so that it will be tyransfered
	document.getElementById('subText').value = subText;
	document.getElementById('webLink').value = webLink;
	var seperator = String.fromCharCode(7);
	var custom =  mainText + seperator +
 						 subText + seperator +
 						 webLink + seperator +
  						 color + seperator +
  						 background + seperator +
  						 border + seperator +
  						 family + seperator +
  						 textStyle + seperator +
  						 fontSize + seperator +
  						 duration + seperator;
	if(custom.length > 256) {
		alert("oops. The Dance Gypsy has run into an internal problem. The full ad description was too long. You can make your description shorter in a few ways: wewer words or characters is the most obvious, but also fewer special characters ('$', '%', '<', etc), colors with shorter names ('LightGoldenRodYellow' is longer than 'yellow').\n\nPease email the dance gypsy if you run into this problem... we can probbaly find a way around it.");
		return false;
		}

 	document.getElementById('customInstant').value = custom;
 
	return true;
	}
						
function alignValues () {
	document.getElementById('anchorSample').href = 'http://'+document.getElementById('webLink').value;
	//alert ('href now is: '+document.getElementById('anchorSample').href);
	var mainText = document.getElementById('mainText');
	mainText.value = unescape(mainText.value);
	if(mainText.value != mainText.defaultValue)
	   changeText(mainText);
	var subText = document.getElementById('subText');
	subText.value = unescape(subText.value);
	changeText(subText);
	var webLink = document.getElementById('webLink');
	subText.webLink = unescape(webLink.value);
	if(webLink.value != webLink.defaultValue)		// don't worry until it has changed
		changeLink (webLink);
	
	changeColor (document.getElementById('textColor'));
	changeBackground (document.getElementById('background'));
	changeBorderWidth (document.getElementById('border'));
	changeFamily (document.getElementById('family'));
	changeStyle (document.getElementById('textStyle'));
	//changeLink (document.getElementById('changeLink'));
	resize();
	var durationSource = 	document.getElementById('duration');
	setAdAmountInstant (durationSource);
	}
	
function setPromotion(status) {
	promotion = status;
	}
						
