// convert all characters to lowercase to simplify testing
var agt = navigator.userAgent.toLowerCase();

//  BROWSER VERSION 
// Note: On IE5, these return 4, so use is_ie5up to detect IE5.
var is_major = parseInt( navigator.appVersion );

var is_nav   =( (agt.indexOf('mozilla')    != -1) && (agt.indexOf('spoofer') == -1) &&
				(agt.indexOf('compatible') == -1) && (agt.indexOf('hotjava') == -1) );
var is_nav6up = ( is_nav && ( is_major >= 5 ) );

var is_ie     = ( (agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1) );
//if( is_ie || is_nav6up ){
//	document.write( '<style>' );
//	document.write( '.singleinput  {' );
//	document.write( 'font-size: 13px;' );
//	document.write( 'font-family: verdana,arial,helvetica,sans-serif;' );
//	document.write( '}' );
//	document.write( '.paragraphinput  {' );
//	document.write( 'font-size: 9px;' );
//	document.write( 'font-family: verdana,arial,helvetica,sans-serif;' );
//	document.write( '}' );
//	document.write( '</style>' );
//}
function openWindow( target, popupURL, w, h ){
            return popup( target, popupURL, 'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=1,left=20,top=20,width=' + w + ',height=' + h );
        }
function popup( target, popupURL, params ){
            var adname = target;
            win = window.open(popupURL,adname,params);

            if( win != null ){
                if( stringMatch( navigator.appName.substring( 0, 8 ), "Netscape" ) ){
                    win.opener = self;
                }
                if( stringMatch( navigator.appName, "Netscape" ) &&
                    navigator.appVersion.substring( 0, 1 ) >= 3 ){
                    win.focus();
                }
                else if( stringMatch( navigator.appName, "Microsoft Internet Explorer" ) &&
                    navigator.appVersion.substring( 0, 1 ) >= 4 ){
                    win.focus();
                }
            }
            return win;
        }
function switchGroup( idx ){
	document.forms[0].elements['next.group'].value = idx;
	doSubmit( 'switch' );
}

function confirm_cancel( resource ){
	if( window.confirm( resource ) ){
		window.location.href = '/rnavmap/evaluate.rnav/pidjWalhH71Esi2bgynKQW-32J329?activepage=ecampaign.wizard.what&action=cancel&pageName=ecampaign.wizard.what&agent.uid=1101124957637';
	}
}

function abortPage( resource ){
	if( window.confirm( resource ) ){
		return true;
	}
	return false;
}

function confirm_defaults( resource ){
	if( window.confirm( resource ) ){
		doSubmit('backtodefaults');
	}
}

function colorPicker( poTextbox ){
	var origColor = document.forms[0].elements[poTextbox].value;
	var url = '/templates/color_picker.jsp?tb=' + escape( poTextbox ) + '&oc=' + escape(origColor);
	var cp = openWindow( 'ColorPicker', url, '395', '230' );
}

function enableIntegration(group,intgr) {
	if( window.confirm('Important Message: By enabling/disabling this feature you are clearing any previously entered unsaved promotional text.') ){
		document.forms[0].integration.value=intgr;
		document.forms[0].group.value=group;
		doSubmit('integration');
	}
}

function collectIntegrationID( url ){
	openWindow( 'Integration', url, '450', '300' );
}

function addGroup( name, seq ){
	document.forms[0].elements['newgroup.seq'].value = seq;
	document.forms[0].repeat.value = name;
	doSubmit('addgroup');
}

function addProp(name){
	saveScrollPosition();
	document.forms[0].repeat.value = name;
	doSubmit('addprop');
}

function doImage(prop){
	var hname  = prop+'.HEIGHT';
	var wname  = prop+'.WIDTH';
	var aname  = prop+'.ALIGN';
	var altname = prop+'.ALT';
	var agentUID = '1101124957637';

	var h = document.forms[0].elements[hname].value;
	var w = document.forms[0].elements[wname].value;
	var a = document.forms[0].elements[aname].value;
	var alt = document.forms[0].elements[altname].value;
	var v = document.forms[0].elements[prop].value;

	var url = '/templates/image_attributes.jsp?prop='+escape(prop)+'&'+hname+'='+h+'&'+wname+'='+w+'&'+aname+'='+a+'&'+altname+'='+escape(alt)+'&v='+escape(v)+'&agent.uid='+agentUID;
	openWindow( 'ImageWin', url, 400, 400 );
}

function changeImageTab(prop){
	saveScrollPosition();
	document.forms[0].elements['image'].value = prop;
	doSubmit('image');
}

function doImageHosting(prop){
	var selectedValue = document.forms[0].elements[prop].value;

	
	var url = '/templates/upload_image.jsp?prop='+escape(prop)+'&value='+escape(selectedValue)+'&agent.uid=1101124957637';
	

	openWindow( 'ImageWin', url, '700', '550' );
}

function resetImageURLAttrs( input, origURL, prop ){
	var curURL = input.value;
	if( curURL != origURL ){
		var hname   = prop+'.HEIGHT';
		var wname   = prop+'.WIDTH';

		document.forms[0].elements[hname].value = '';
		document.forms[0].elements[wname].value = '';
	}
}

function doSelectImg( prop, stock ){
	var img = document.forms[0].elements[stock].value;
	document.forms[0].elements[prop].value = img;
	var thumbnail = img;
	var idx = thumbnail.lastIndexOf( '.' );
	if( idx > -1 ){
		thumbnail = thumbnail.substring( 0, idx )+'TN'+thumbnail.substring( idx, thumbnail.length );
	}
	if( document.images[prop+'.THUMBNAIL'] ){
		if( img.length > 0 ){
			document.images[prop+'.THUMBNAIL'].src = thumbnail;
		}
		else{
			document.images[prop+'.THUMBNAIL'].src = 'spacer.gif';
		}
	}
}

function resetStock( prop, stock ){
	var img = document.forms[0].elements[prop].value;

	if( img.length > 0 ){
		var stockImgs = document.forms[0].elements[stock];
		var foundMatch = false;
		for( var i=0; i<stockImgs.length; i++ ){
			if( img == stockImgs.options[i].value ){
				stockImgs.selectedIndex = i;
				var idx = img.lastIndexOf( '.' );
				img = img.substring( 0, idx )+'TN'+img.substring( idx, img.length );
				foundMatch = true;
				break;
			}
		}
		if( foundMatch == false ){
			stockImgs.selectedIndex = 0;
		}
	}

	if( document.images[prop+'.THUMBNAIL'] ){
		if( img.length > 0 ) {
			document.images[prop+'.THUMBNAIL'].src = img;
		}
		else{
			document.images[prop+'.THUMBNAIL'].src = 'spacer.gif';
		}
	}
}

function doFont(prop){
	var fontname  = prop+'.FONT';
	var sizename  = prop+'.SIZE';
	var stylename = prop+'.STYLE';
	var colorname = prop+'.COLOR';

	var font  = document.forms[0].elements[fontname].value;
	var size  = document.forms[0].elements[sizename].value;
	var style = document.forms[0].elements[stylename].value;
	var color = document.forms[0].elements[colorname].value;

	var url = '/templates/font_attributes.jsp?prop='+escape(prop)+'&'+fontname+'='+font+'&'+sizename+'='+size+'&'+stylename+'='+style+'&'+colorname+'='+escape(color)+'&agent.uid=1101124957637';
	openWindow( 'FontWin', url, '395', '230' );
}

function insertTag(prop, tag){
	
	var url = 'yoyo/js/include/format.asp?prop='+escape(prop)+'&format='+tag;
	openWindow( 'ListWin', url, '500', '270' );
//	if( document.selection ){
//		var selection    = document.selection.createRange();
//		var selectedText = document.selection.createRange().text;
//	}
//	else{
//		alert("HI:"+ document.getElementById("Content").getSelection());
//		var selectedText    = getSelection();
//	}
//		if( selectedText.length == 0 ){
//			window.alert('Please highlight text first');
//		}
//		else{
//			var replaceValue = '<'+tag+' class="notHidden">'+selectedText+'</'+tag+'>';
//			selection.text = replaceValue;
//		}	
}

function doNumbers(prop){
	var url = 'yoyo/js/include/Numbers.asp?prop='+escape(prop)+'&format=ordered';
	openWindow( 'FormatWin', url, '500', '270' );
}

function doBullets(prop){
	var url = 'yoyo/js/include/bullets.asp?prop='+escape(prop)+'&format=unordered';
	openWindow( 'ListWin', url, '500', '440' );
}

function doLink(prop){		
	var url = 'yoyo/js/include/link.asp?prop='+escape(prop);
	openWindow( 'LinkWin', url, '500', '270' );
}

function doMailLink(prop){
	var url = 'yoyo/js/include/Maillink.asp?prop='+escape(prop);
	openWindow( 'MailWin', url, '500', '270' );
}

function doImg(prop){
	if( document.selection ){
		var elementValue = document.forms[0].elements[prop].value;
		var selectedText = document.selection.createRange().text;
		var url = 'img.asp?prop='+escape(prop)+'&txt='+escape(selectedText);
		openWindow( 'ImgWin', url, '500', '270' );
	}
}

//===========================================
function edInsertImage(myField) {
	var myValue = prompt('Enter the URL of the image', 'http://');
	if (myValue) {
		myValue = '<img src="' 
				+ myValue 
				+ '" />';
		edInsertContent(myField, myValue);
	}
}

function edInsertImageLeft(myField) {
	var myValue = prompt('Enter the URL of the image', 'http://');
	if (myValue) {
		myValue = '<img src="' 
				+ myValue 
				+ '" align="left" />';
		edInsertContent(myField, myValue);
	}
}

function edInsertImageRight(myField) {
	var myValue = prompt('Enter the URL of the image', 'http://');
	if (myValue) {
		myValue = '<img src="' 
				+ myValue 
				+ '" align="right" />';
		edInsertContent(myField, myValue);
	}
}

function edInsertContent(myField, myValue) {
	
	//IE support
	if (document.selection) {
		myField.focus();
		sel = document.selection.createRange();
		sel.text = myValue;
		myField.focus();
	}
	//MOZILLA/NETSCAPE support
	else if (myField.selectionStart || myField.selectionStart == '0') {
		
		var startPos = myField.selectionStart;
		var endPos = myField.selectionEnd;
		var scrollTop = myField.scrollTop;
		myField.value = myField.value.substring(0, startPos)
		              + myValue 
                      + myField.value.substring(endPos, myField.value.length);
		myField.focus();
		myField.selectionStart = startPos + myValue.length;
		myField.selectionEnd = startPos + myValue.length;
		myField.scrollTop = scrollTop;
	} else {
		alert('x');
		myField.value += myValue;
		myField.focus();
	}
}


function syncForward(){
	if( !document.forms[0].elements['LETTER.FWTF.ADD'].checked ){
		document.forms[0].elements['LETTER.FWTF.SUBSCRIBE.ADD'].checked = false;
	}
}
function syncSubscribe(){
	if( document.forms[0].elements['LETTER.FWTF.SUBSCRIBE.ADD'].checked ){
		document.forms[0].elements['LETTER.FWTF.ADD'].checked = true;
	}
}

function doSubmit( act ){
	document.forms[0].elements['action'].value = act;
	document.forms[0].submit();
}

//
// Do Preview work
var previewError = 0;
var action = 'switch';

//if( !document.selection ){
//	document.write('<center>');
//	document.write('<table width="90%" border="0" cellpadding="0" cellspacing="0" class="bgcolor3">' );
//	document.write('<tr>');
//	document.write('<td class="bgcolor1" height="1" colspan="3"><img src="../images/spacer.gif" width="1" height="1"></td>');
//	document.write('</tr>');
//	document.write('<tr><td colpan="3" height="3"><img src="../images/spacer.gif" width="1" height="3"></td></tr>');
//	document.write('<tr>');
//	document.write('<td>&nbsp;&nbsp;</td>');
//	document.write('<td width="100%"><font class="mainsmallblackfont">');
//	document.write('<font class="headersmallfont">Attention</font><br>');
//	document.write('We have detected that your configuration (browser type or use of Apple Macintosh) does not allow the HTML toolbar to display.  You can still use HTML formatting by typing the standard commands directly into the text boxes. ');
//	document.write('</td>');
//	document.write('<td>&nbsp;&nbsp;</td>');
//	document.write('</tr>');
//	document.write('<tr><td colpan="3" height="3"><img src="../images/spacer.gif" width="1" height="3"></td></tr>');
//	document.write('<tr>');
//	document.write('<td class="bgcolor1" height="1" colspan="3"><img src="../images/spacer.gif" width="1" height="1"></td>');
//	document.write('</tr>');
//	document.write('</table>');
//	document.write('</center><br>');
//}