var JS_SUBCAT_N_FIELDS = 40;

function DeleteContributor( id )
{
	if( confirm( "Are you sure you want delete this CMS account?" ))
	{
		document.theform.id.value = id;
		document.theform.action = "adminContributorsDelete.php";
		document.theform.submit();
	}
}

function IsEmpty( e )
{
	var temp, re

	re = /\s+/;
	temp = e.value.replace( re, "" )
	if( temp == "" )
		return( true );
	else
		return( false );
}

// this needs to be dynamically generated as entry form is created
function CheckContentInput( bDirectory, contentType ) {
	return true;
	//return( false );
	/*
	define( "HEADLINE", 1 );
	define( "COMPANY", 2 );
	define( "ARTICLE", 3 );
	define( "CALENDAR", 4 );
	define( "DOWNLOAD", 5 );
	define( "INFO", 6 );
	define( "POLL", 7 );
	define( "TIDBIT", 8 );
	*/

	if( bDirectory == 0 ) {
		if( IsEmpty( document.theform.title )) {
			window.alert( "Please enter a title." );
			document.theform.title.focus();
			return( false );
		}
		if( contentType == 1 || contentType == 2 || contentType == 3 ) {
			if( IsEmpty( document.theform.intro )) {
				window.alert( "Please enter an introduction." );
				document.theform.intro.focus();
				return( false );
			}
		}
		if( IsEmpty( document.theform.bodyText )) {
			if( contentType == 4 ) {
				window.alert( "Please enter an event description." );		
			} else {
				window.alert( "Please enter a body text." );		
			}
			document.theform.bodyText.focus();
			return( false );
		}

		if( contentType == 4 ) {
			var formDate;
			formDate = document.theform.eventdate_month.value + "/" + document.theform.eventdate_day.value + "/" + document.theform.eventdate_year.value;
			//window.alert( formDate );
			if( !IsValidDate( formDate )) {
				document.theform.eventdate_month.focus();
				return( false );
			}
		}	
	} else {
		if( IsEmpty( document.theform.title )) {
			window.alert( "Please enter a title." );
			document.theform.title.focus();
			return( false );
		}
		if( IsEmpty( document.theform.url )) {
			window.alert( "Please enter an url." );
			document.theform.url.focus();
			return( false );
		}
		if( IsEmpty( document.theform.description )) {
			window.alert( "Please enter a description." );
			document.theform.description.focus();
			return( false );
		}
	}
	return( true );
}

function CheckAccountInput( updateType ) {
	if( updateType == "a" || updateType == "ma" ) {
		if( IsEmpty( document.theform.username )) {
			window.alert( "Please enter a username." );
			document.theform.username.focus();
			return( false );
		}
		if( IsEmpty( document.theform.password )) {
			window.alert( "Please enter a password." );
			document.theform.password.focus();
			return( false );
		}
		if( document.theform.password.value.length < 4 ) {
			window.alert( "The password must be at 4 characters long." );
			document.theform.password.focus();
			return( false );
		}
	}
	if( IsEmpty( document.theform.name )) {
		window.alert( "Please enter a name." );
		document.theform.name.focus();
		return( false );
	}
	if( IsEmpty( document.theform.companyText )) {
		window.alert( "Please enter a company name." );
		document.theform.companyText.focus();
		return( false );
	}
	return( true );
}

	var LastELement;
	LastElement = '';

function OnBlurRecord( name )
{
	LastElement = name;
}

var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		//alert("The date format should be : mm/dd/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month.")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day.")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date.")
		return false
	}
return true
}

function IsValidDate( strDate ){
	if (isDate(strDate)==false){
		return false
	}
    return true
}

function ChangedListingTypeOption( topID ) {
	if( topID == 1 ) {
		if( document.theform.listingtype[ 1 ].checked ) {
			commission_pre.style.display="none";
			commission_post.style.display="block";
		}
		else if( document.theform.listingtype[ 4 ].checked ) {
			commission_pre.style.display="none";
			commission_post.style.display="none";
		}
		//if( document.theform.listingtype[ 0 ].checked || document.theform.listingtype[ 2 ].checked || document.theform.listingtype[ 3 ].checked ) {
		else {
			commission_pre.style.display="block";
			commission_post.style.display="none";
		}

		if( document.theform.listingtype[ 4 ].checked) {
			nichefocus_block.style.display="none";
			nichefocus_block_space.style.display="none";
			nNiches_block.style.display="none";
			nNiches_block_space.style.display="none";
			bPromo_block.style.display="none";
			bPromo_block_space.style.display="none";
		} else {
			nichefocus_block.style.display="block";
			nichefocus_block_space.style.display="block";
			nNiches_block.style.display="block";
			nNiches_block_space.style.display="block";
			bPromo_block.style.display="block";
			bPromo_block_space.style.display="block";
		}		
	}

	if( topID == 4 ) {
		if( document.theform.listingtype[ 3 ].checked ) {
			storage_block.style.display="block";
			storage_block_space.style.display="block";
			bFTP_block.style.display="block";
			bFTP_block_space.style.display="block";
			bScript_block.style.display="block";
			bScript_block_space.style.display="block";
		} else {
			storage_block.style.display="none";
			storage_block_space.style.display="none";
			bFTP_block.style.display="none";
			bFTP_block_space.style.display="none";
			bScript_block.style.display="none";
			bScript_block_space.style.display="none";
		}		
	}
}

function DeleteCat( id, contenttype )
{
	if( confirm( "This will delete the category AND all of its sub-categories.  All related content might become invalid.  This action cannot be undone.  Continue?" ))
	{
		document.theform.id.value = id;
		//document.theform.contentType.value = ct;
		document.theform.actionType.value = "delete";
		document.theform.action = "adminSettingsCatOneUpdate.php?s=" + contenttype;
		document.theform.returnURL.value = OneSettingPageUpdateLink( document.theform.returnURL.value );
		document.theform.submit();
	}
}

function ReOrderCat( parentid, id, increment, contenttype ) {
	if( increment == 1 ) {
		document.theform.actionType.value = "down";
	} else {
		document.theform.actionType.value = "up";
	}
	document.theform.id.value = id;
	document.theform.parentid.value = parentid;
	document.theform.action = "adminSettingsCatOneUpdate.php?s=" + contenttype;
	document.theform.returnURL.value = OneSettingPageUpdateLink( document.theform.returnURL.value );
	document.theform.submit();
}

function CheckCategoryInput( ct, id ) {
	if( ct == 1 ) {
		if( IsEmpty( document.theform.name )) {
			window.alert( "Please enter a title." );
			document.theform.name.focus();
			return( false );
		}		
	}

	if( ct == 2 ) {
		
	}
	return( true );
}

function CheckBizDirFieldInput( id ) {
	if( IsEmpty( document.theform.name )) {
		window.alert( "Please enter a title." );
		document.theform.name.focus();
		return( false );
	}		
	if( document.theform.optionstable.value == 0 ) {
		var optionsraw = document.theform.optionstext.value;
		var optionstrim;

		optionstrim = optionsraw;
		if( optionsraw.charAt( 0 ) == ',' ) {
			optionstrim = optionsraw.substr( 1 );
		}
		if( optionstrim.charAt( optionstrim.length - 1 ) == ',' ) {
			optionstrim = optionstrim.substr( 0, optionstrim.length - 1 );
		}

		//window.alert( "original: " + optionsraw );
		//window.alert( "trimmed: " + optionstrim );

		var i, j, nCommas, nEquals, arrValues, arrKeys, strTemp
		
		nCommas = 0;
		nEquals = 0;
		arrKeys = new Array();
		for(i=0; i<optionstrim.length; i++) {
			if( optionstrim.charAt( i ) == ',' ) {
				nCommas++;
			}
			if( optionstrim.charAt( i ) == '=' ) {
				nEquals++;
				j = i + 1;
				strTemp = "";
				while( optionstrim.charAt( j ) != ',' && j <= optionstrim.length - 1 ) {
					strTemp += optionstrim.charAt( j );
					j++;
				}
				//window.alert( "new key: " + strTemp );
				arrKeys.push( strTemp );
			}
		}
		//window.alert( "total no. of keys: " + arrKeys.length );
		if( nEquals > 0 && nCommas + 1 != nEquals ) {
		//if( arrKeys.length > 0 && arrValues.length != arrKeys.length ) {
			window.alert( "The custom values list is invalid." );
			document.theform.optionstext.focus();
			return( false );
		}
		if( arrKeys.length > 1 ) {
			arrKeys.sort();
			for(i=0; i<arrKeys.length; i++) {
				if( arrKeys[ i ] == arrKeys[ i + 1 ] ) {
					window.alert( "The keys must be unique." );
					document.theform.optionstext.focus();
					return( false );
				}
			}
		}
		document.theform.optionstext.value = optionstrim;
		//return( false );
	}
	return( true );
}

function ChangedBizFieldTypeOption() {
	if( document.theform.fieldtype.value == 2 ) {
		defaultvalue_multi_block.style.display="block";
		rows_block.style.display="block";
		rows_block_space.style.display="block";
		defaultvalue_single_block.style.display="none";
	} else {
		rows_block.style.display="none";
		rows_block_space.style.display="none";
		defaultvalue_multi_block.style.display="none";
		if( document.theform.fieldtype.value == 8 ) {
			defaultvalue_single_block.style.display="none";
		} else {
			defaultvalue_single_block.style.display="block";
		}
	}

	if( document.theform.fieldtype.value == 1 ) {
		bNumeric_block.style.display="block";
		bNumeric_block_space.style.display="block";
		prefix_block.style.display="block";
		prefix_block_space.style.display="block";
		postfix_block.style.display="block";
		postfix_block_space.style.display="block";
		maxlength_block.style.display="block";
		maxlength_block_space.style.display="block";
	} else {
		bNumeric_block.style.display="none";
		bNumeric_block_space.style.display="none";
		prefix_block.style.display="none";
		prefix_block_space.style.display="none";
		postfix_block.style.display="none";
		postfix_block_space.style.display="none";
		maxlength_block.style.display="none";
		maxlength_block_space.style.display="none";
	}		

	if( document.theform.fieldtype.value == 1 || document.theform.fieldtype.value == 2 || document.theform.fieldtype.value == 8 ) {
		size_block.style.display="block";
		size_block_space.style.display="block";
	} else {
		size_block.style.display="none";
		size_block_space.style.display="none";
	}

	if( document.theform.fieldtype.value == 3 || document.theform.fieldtype.value == 4 || document.theform.fieldtype.value == 5 ) {
		options_block.style.display="block";
		options_block_space.style.display="block";
		if( document.theform.optionstable.value == 0 ) {
			//optionstext_direction_block.style.display="block";
			optionstext_block.style.display="block";
			optionstext_block_space.style.display="block";
		} else {
			//optionstext_direction_block.style.display="none";
			optionstext_block.style.display="none";
			optionstext_block_space.style.display="none";
		}
	} else {
		options_block.style.display="none";
		options_block_space.style.display="none";
			//optionstext_direction_block.style.display="none";
			optionstext_block.style.display="none";
			optionstext_block_space.style.display="none";
	}
}

function ChangedBizFieldSelectionOptionsOption() {
	if( document.theform.optionstable.value == 0 ) {
		//optionstext_direction_block.style.display="block";
		optionstext_block.style.display="block";
		optionstext_block_space.style.display="block";
	} else {
		//optionstext_direction_block.style.display="none";
		optionstext_block.style.display="none";
		optionstext_block_space.style.display="none";
	}
}

function ChangedSubCatFieldOption( n ) {
	//window.alert( document.getElementById( fieldname ).value );
	//var temp = document.getElementById("field"+n+"_select_block").value;
	document.getElementById("field"+n+"_select_block").value = document.getElementById("field"+n).value;

	if( document.getElementById("field"+n).value > 0 ) {
		document.getElementById("field"+n+"_view_block").style.display="block";
		document.getElementById("field"+n+"_edit_block").style.display="block";
	} else {
		document.getElementById("field"+n+"_view_block").style.display="none";
		document.getElementById("field"+n+"_edit_block").style.display="none";
	}
}

function ViewSubCatFieldPopup( n, contenttype ) {
	var id = document.getElementById("field"+n+"_select").value;
	window.open( "adminSettingsFieldOne.php?s=" + contenttype + "&p=1&v=1&id="+id, "", "toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, copyhistory=yes, width=700, height=600");
}

function EditSubCatFieldPopup( n, contenttype ) {
	var id = document.getElementById("field"+n+"_select").value;
	window.open( "adminSettingsFieldOne.php?s=" + contenttype + "&p=1&v=0&id="+id, "", "toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, copyhistory=yes, width=700, height=600");
}

function ReOrderSubCatField( parentid, subID, fieldNumber, offset, contenttype ) {
	var thisIOrder = document.getElementById("iOrder_field"+fieldNumber+"_block").value;
	var otherFieldNumber;
	var otherIOrder;
	var searchOrder = window.parseInt( thisIOrder, 10 ) + offset;
	for(i=1; i<=JS_SUBCAT_N_FIELDS; i++) {
		if( document.getElementById("iOrder_field"+i+"_block").value == searchOrder ) {
			otherFieldNumber = i;
			otherIOrder = document.getElementById("iOrder_field"+i+"_block").value;
		}
	}

	//window.alert( "switch: field " + fieldNumber + "-" + thisIOrder + " with field " + otherFieldNumber + "-" + otherIOrder );

	document.getElementById("iOrder_field"+otherFieldNumber+"_block").value = thisIOrder;
	document.getElementById("iOrder_field"+fieldNumber+"_block").value = otherIOrder;

	document.theform.action = OneSettingPageUpdateLink( "adminSettingsCatOne.php?s=" + contenttype + "&parent=" + parentid + "&id=" + subID + "&form=1" );
	document.theform.submit();
}


function TableToggleDisplay( id, rowsExp, rowsCol, expandListName ) {
	//window.alert( "hi" );
	var thisCatExpanded = document.getElementById("cat_expanded"+id);
	var thisCatCollapsed = document.getElementById("cat_collapsed"+id);

	var arrRowsExpand = new Array();
	var arrRowsCollapse = new Array();
	arrRowsExpand = rowsExp.split( "|" );
	arrRowsCollapse = rowsCol.split( "|" );

	var currRow;

	var arrExpandList = new Array();
	//arrExpandList = document.theform[ expandListName ].value.split( "," );

	if( thisCatExpanded.style.display == "none" ) {
		thisCatExpanded.style.display = "block";
		thisCatCollapsed.style.display = "none";
		// show
		for( i=0; i<arrRowsExpand.length; i++){
			if( arrRowsExpand[ i ].length > 0 )
				document.getElementById("row"+arrRowsExpand[ i ]).style.display = "block";
		}

		// add to expand list
		if( !IsInArray( arrExpandList, id )) {
			arrExpandList.push( id );
		}
	} else {
		thisCatExpanded.style.display = "none";
		thisCatCollapsed.style.display = "block";
		// hide
		for( i=0; i<arrRowsCollapse.length; i++) {
			if( arrRowsCollapse[ i ].length > 0 ) {
				document.getElementById("row"+arrRowsCollapse[ i ]).style.display = "none";
				currRow = document.getElementById("cat_collapsed"+arrRowsCollapse[ i ]);
				if( currRow != null ) {
					document.getElementById("cat_expanded"+arrRowsCollapse[ i ]).style.display = "none";
					document.getElementById("cat_collapsed"+arrRowsCollapse[ i ]).style.display = "block";
				}
			}
		}

		// remove from expand list
		RemoveFromArray( arrExpandList, id );
	}
	//document.theform[ expandListName ].value = arrExpandList.join( "," );
}

function OneSettingPageUpdateLink( strUrl ) {
	return strUrl + "&cexp=" + document.theform.catExpandList.value + "&fexp=" + document.theform.fieldExpandList.value + "&pexp=" + document.theform.presetExpandList.value;
}
function OneSettingPage( strUrl ) {
	//location.replace( OneSettingPageUpdateLink( strUrl ));
	document.theform.action = OneSettingPageUpdateLink( strUrl );
	document.theform.submit();
}
function OneCategory( strUrl ) {
	OneSettingPage( strUrl );
	//location.replace( strUrl + "&cexp=" + document.theform.catExpandList.value );
}
function OneField( strUrl ) {
	OneSettingPage( strUrl );
	//location.replace( strUrl + "&fexp=" + document.theform.fieldExpandList.value );
}
function OnePreset( strUrl ) {
	OneSettingPage( strUrl );
	//location.replace( strUrl + "&pexp=" + document.theform.presetExpandList.value );
}

function IsInArray( array, value ) {
	for( i=0; i<array.length; i++ ) {
		if( array[ i ] == value ) {
			return true;
		}
	}
	return false;
}
function RemoveFromArray( array, value ) {
	for( i=0; i<array.length; i++ ) {
		if( array[ i ] == value ) {
			array.splice( i, 1 );
			return;
		}
	}
}

function FieldsTableToggleDisplay( id ) {
	//window.alert( "hi" );
	var thisFieldExpand = document.getElementById("fieldexpand"+id);
	var thisFieldCollapse = document.getElementById("fieldcollapse"+id);
	var thisFieldDetails = document.getElementById("fielddetails"+id);

	var arrExpandList = new Array();
	arrExpandList = document.theform.fieldExpandList.value.split( "," );
	//window.alert( "start: " + document.theform.expandList.value );
	//window.alert( arrExpandList.length + " expand items" );

	if( thisFieldExpand.style.display == "block" ) {		// currently collapsed
		thisFieldExpand.style.display = "none";
		thisFieldCollapse.style.display = "block";
		thisFieldDetails.style.display = "block";

		// add to expand list
		if( !IsInArray( arrExpandList, id )) {
			arrExpandList.push( id );
		}
	} else {												// currently expanded
		thisFieldExpand.style.display = "block";
		thisFieldCollapse.style.display = "none";
		thisFieldDetails.style.display = "none";

		// remove from expand list
		RemoveFromArray( arrExpandList, id );
	}
	document.theform.fieldExpandList.value = arrExpandList.join( "," );
}

function PresetsTableToggleDisplay( id ) {
	//window.alert( "hi" );
	var thisPresetExpand = document.getElementById("presetexpand"+id);
	var thisPresetCollapse = document.getElementById("presetcollapse"+id);
	var thisPresetDetails = document.getElementById("presetdetails"+id);

	var arrExpandList = new Array();
	arrExpandList = document.theform.presetExpandList.value.split( "," );
	//window.alert( "start: " + document.theform.expandList.value );
	//window.alert( arrExpandList.length + " expand items" );

	if( thisPresetExpand.style.display == "block" ) {		// currently collapsed
		thisPresetExpand.style.display = "none";
		thisPresetCollapse.style.display = "block";
		thisPresetDetails.style.display = "block";

		// add to expand list
		if( !IsInArray( arrExpandList, id )) {
			arrExpandList.push( id );
		}
	} else {												// currently expanded
		thisPresetExpand.style.display = "block";
		thisPresetCollapse.style.display = "none";
		thisPresetDetails.style.display = "none";

		// remove from expand list
		RemoveFromArray( arrExpandList, id );
	}
	document.theform.presetExpandList.value = arrExpandList.join( "," );
}

function ReOrderPreset( id, increment, contenttype ) {
	if( increment == 1 ) {
		document.theform.actionType.value = "down";
	} else {
		document.theform.actionType.value = "up";
	}
	document.theform.id.value = id;
	document.theform.returnURL.value = OneSettingPageUpdateLink( document.theform.returnURL.value );
	document.theform.action = "adminSettingsPresetOneUpdate.php?s=" + contenttype;
	document.theform.submit();
}

function DeletePreset( id, contenttype )
{
	if( confirm( "This will delete the preset AND all of its items.  This action cannot be undone.  Continue?" ))
	{
		document.theform.actionType.value = "delete";
		document.theform.id.value = id;
		document.theform.returnURL.value = OneSettingPageUpdateLink( document.theform.returnURL.value );
		document.theform.action = "adminSettingsPresetOneUpdate.php?s=" + contenttype;
		document.theform.submit();
	}
}

function ReOrderPresetItem( parentid, id, increment, contenttype ) {
	if( increment == 1 ) {
		document.theform.actionType.value = "down";
	} else {
		document.theform.actionType.value = "up";
	}
	document.theform.id.value = parentid;
	document.theform.itemID.value = id;
	//document.theform.returnURL.value = "adminSettingsPresetOne.php?id=" + parentid + "&form=1";
	document.theform.action = OneSettingPageUpdateLink( "adminSettingsPresetOne.php?s="+ contenttype +"&id=" + parentid + "&form=1" );
	document.theform.submit();
}

function DeletePresetItem( parentid, id, contenttype ) {
	if( confirm( "Are you sure you want delete this preset item?" ))
	{
		document.theform.id.value = parentid;
		document.theform.itemID.value = id;
		document.theform.actionType.value = "delete";
		//document.theform.returnURL.value = "adminSettingsPresetOne.php?id=" + parentid + "&form=1";
		document.theform.action = OneSettingPageUpdateLink( "adminSettingsPresetOne.php?s="+ contenttype +"&id=" + parentid + "&form=1" );
		document.theform.submit();
	}
}

function AddPresetItem( parentid, contenttype ) {
	if( IsEmpty( document.theform.preset_new_item_name )) {
		window.alert( "Please enter a title." );
		document.theform.preset_new_item_name.focus();
		return;
	} 
	document.theform.id.value = parentid;
	document.theform.itemID.value = -1;
	document.theform.actionType.value = "add";
	//document.theform.returnURL.value = "adminSettingsPresetOne.php?id=" + parentid + "&form=1";
	document.theform.action = OneSettingPageUpdateLink( "adminSettingsPresetOne.php?s="+ contenttype +"&id=" + parentid + "&form=1" );
	document.theform.submit();
}

function OneContentPageUpdateLink( strUrl ) {
	return strUrl + "&perPage=" + document.theform.perPage.value + "&cexp_b=" + document.theform.catExpandListBizDir.value + "&cexp_e=" + document.theform.catExpandListEditorial.value;
}
function OneContentPage( strUrl ) {
	//location.replace( OneContentPageUpdateLink( strUrl ));
	if( document.theform.searchstring ) {
		if( document.theform.searchstring.value == "" ) {
			document.theform.action = OneContentPageUpdateLink( strUrl + "&searchstring=" + document.theform.searchstringcurr.value );
		} else {
			document.theform.action = OneContentPageUpdateLink( strUrl + "&searchstring=" + document.theform.searchstring.value );
		}
	} else {
		document.theform.action = OneContentPageUpdateLink( strUrl );
	}
	//document.theform.action = OneContentPageUpdateLink( strUrl );
	document.theform.submit();
}
function OneContentPageSearch( strUrl ) {
	//location.replace( OneContentPageUpdateLink( strUrl ));
	if( document.theform.searchstring ) {
		document.theform.action = OneContentPageUpdateLink( strUrl + "&searchstring=" + document.theform.searchstring.value );
	} else {
		document.theform.action = OneContentPageUpdateLink( strUrl );
	}
	document.theform.submit();
}

function PreviewOneContent( strFieldNumbers, strURL ) {
	document.theform.action = strURL;
	document.theform.target = "_blank";
	document.theform.submit();
}

function SubmitOneContent( updateType, strFieldNumbers, strURL ) {
	if( !CheckContentItemFields( strFieldNumbers )) {
		return;
	}
	document.theform.updateType.value = updateType;
	if( updateType == "p" || updateType == "up" || updateType == "rj" || updateType == "ac" ) {
		document.theform.returnURL.value = document.theform.publishURL.value;
	}
	document.theform.action = strURL;
	document.theform.target = "";
	document.theform.submit();
}

function DeleteContent( id, contenttype, startPage ) {
	if( confirm( "Are you sure you want delete this item?" ))
	{
		document.theform.id.value = id;
		document.theform.action = "adminContentDelete.php?s="+contenttype;
		document.theform.returnURL.value = OneContentPageUpdateLink( document.theform.returnURL.value ) + "&startPage=" + startPage ;
		document.theform.submit();
	}
}

function DeleteField( id, contenttype )
{
	if( confirm( "Deleting a field might invalidate some content data and alter the look of submission forms.  This action cannot be undone.  Continue?" ))
	{
		document.theform.actionType.value = "delete";
		document.theform.id.value = id;
		document.theform.returnURL.value = OneSettingPageUpdateLink( document.theform.returnURL.value );
		document.theform.action = "adminSettingsFieldOneUpdate.php?s=" + contenttype;
		document.theform.submit();
	}
}

// make sure that at least 1 checkbox is selected
function EnforceCheckboxSelection( fieldNumber, iBox ) {
	var nSelected, nCheck, i;

	nCheck = window.parseInt( document.getElementById( "field" + fieldNumber + "_n_checkboxes" ).value, 10 );

	nSelected = 0;
	for( i = 1; i <= nCheck; i++ ) {
		if( document.getElementById( "field"+fieldNumber+"_box"+i ).checked == true ) {
			nSelected++;
		}
	}
	if( nSelected == 0 )
		document.getElementById( "field"+fieldNumber+"_box"+iBox ).checked = true;
}

// should only be text input and textarea fields
function CheckContentItemFields( strFieldNumbers ) {
	if( strFieldNumbers == "" ) {
	    return( true );
	}
	//window.alert( strFieldNumbers );

	var arrFields = new Array();
	arrFields = strFieldNumbers.split( "," );

	for( i=0; i<arrFields.length; i++ ) {
		//window.alert( "field"+arrFields[ i ]);
		//window.alert( document.getElementById( "field"+arrFields[ i ]).value );
		if( IsEmpty( document.getElementById( "field"+arrFields[ i ]))) {
			window.alert( "Please enter all required (*) fields." );
			document.getElementById( "field"+arrFields[ i ]).focus();
			return( false );
		}
	}
	return( true );
}

function NewWindow( newURL ) {
	NewWindow2( newURL, "" );
}
function NewWindow2( newURL, strOptions ) {
	newWin = window.open( newURL, "", strOptions );
	//window.open( previewURL, "Content Preview", "","toolbar=yes,location=yes,directories=no,status=no,menubar=yes,scrollbars=yes,resizable=no,copyhistory=yes,width=400,height=400" );
}

function OnChangeListingViewPerPage( startURL, oldPerPage, currPage ) {
	numPreceding = oldPerPage * ( currPage - 1 )
	if( numPreceding < 0 ) {
		numPreceding = 0;
	}
	temp = document.theform.viewperpage.value;
	newPerPage = parseInt( document.theform.viewperpage.value, 10 );
	numPreviousPages = parseInt( numPreceding / newPerPage, 10 );

	document.theform.returnURL.value = startURL + "&startPage=" + ( newPerPage * numPreviousPages + 1 );
	document.theform.perPage.value = newPerPage;
	document.theform.action = "updatePerPage.php";
	document.theform.submit();
}

function OnChangeFeaturedResources() {
	strURL = document.featured_form.featured_resources.value;
	if( strURL != "" ) {
		MM_jumpMenu( "parent", strURL, 0 );
	}
}

function SetAsHomePage( object, strURL ) {
	if( document.all ) {
		object.style.behavior = 'url(#default#homepage)';
		object.setHomePage( strURL );

		//document.write('<A HREF="javascript:history.go(0);" onClick="this.style.behavior=\'url(#default#homepage)\';this.setHomePage(\'http://www.YourWebSiteHere.com\');">');
		//document.write('<font size="5" color=6699FF face=arial><B>Click Here to Make My Web Page Your Homepage</B></font></a>');
	}
}

function MM_jumpMenu(targ,selObj,restore){ //v3.0
  strURL = selObj.options[selObj.selectedIndex].value;
  if (targ == "parent") {
	eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  }
  else {
  if( strURL != "" ) {
	  //eval(targ+".location='"+strURL+"'");
	  NewWindow( strURL );
  }
  }
  if (restore) selObj.selectedIndex=0;
}


// name - name of the cookie
// value - value of the cookie
// [expires] - expiration date of the cookie (defaults to end of current session)
// [path] - path for which the cookie is valid (defaults to path of calling document)
// [domain] - domain for which the cookie is valid (defaults to domain of calling document)
// [secure] - Boolean value indicating if the cookie transmission requires a secure transmission
// * an argument defaults when it is assigned null as a placeholder
// * a null placeholder is not required for trailing omitted arguments
function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}

// name - name of the desired cookie
// * return string containing value of specified cookie or null if cookie does not exist
function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}

// name - name of the cookie
// [path] - path of the cookie (must be same as path used to create cookie)
// [domain] - domain of the cookie (must be same as domain used to create cookie)
// * path and domain default if assigned null or omitted if no explicit argument proceeds
function deleteCookie(name, path, domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" + 
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

// date - any instance of the Date object
// * hand all instances of the Date object to this function for "repairs"
function fixDate(date) {
  var base = new Date(0);
  var skew = base.getTime();
  if (skew > 0)
    date.setTime(date.getTime() - skew);
}

function SubmitPoll( id, nActualChoices, width, height, bUpdate ) {
	var strURL, strSelectedValue;
	
	strSelectedValue = "";
	for( i = 0; i < nActualChoices; i++ ) {
	    if( document.pollform.poll_choices[ i ].checked ) {
	        strSelectedValue = document.pollform.poll_choices[ i ].value;
			break;
	    }
	}
	//window.alert( "selected value: " + strSelectedValue );
	strURL = "/poll.php?id=" + id + "&n=" + nActualChoices + "&update=" + bUpdate + "";
	if( bUpdate == "1" ) {
	    strURL += "&field=" + strSelectedValue;
	}
    NewWindow2( strURL, 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=no,width=' + width + ',height=' + height + '' );
}

function CheckSMSInput() {
	var mobilestring, emailstring, reg;

	mobilestring = document.smsform.mobile.value;
	reg = /^\d{11,}$/i;
	if( mobilestring.match( reg ) == null ) {
		window.alert( "Please enter a valid international mobile number (do not include punctuations)." );
		document.smsform.mobile.focus();
		return;
	}

	emailstring = document.smsform.email.value;
	reg = /^\S+@\S+.\S+$/i;
	if( emailstring.match( reg ) == null ) {
		window.alert( "Please enter a valid E-mail addresss." );
		document.smsform.email.focus();
		return;
	}

	document.smsform.submit();
}

function textLimit( field, maxlen ) {
	/*
	var result;
	if( field.value.length >= maxlen ) {
		if( event.preventDefault ) {
			event.preventDefault();
		}
		result = false;
	} else {
		result = true;
	}
	*/

	var indicator = document.getElementById( "charsleft" );
	var warning = document.getElementById( "charswarning" );
	var diff = maxlen - field.value.length;
	if( indicator != null ) {
		indicator.value = diff;
	}
	if( diff < 0 ){
		if( indicator != null ) {
			indicator.style.color = "red";
		}
		if( warning != null ) {
			warning.style.display = "inline";
		}
	} else {	
		if( indicator != null ) {
			indicator.style.color = "black";
		}
		if( warning != null ) {
			warning.style.display = "none";
		}
	}	
}

function CommitHomepage() {
	var obj = document.getElementById( 'preview_homepage_msg' );
	if( obj != null ) {
		obj.innerHTML = '';
	}

	if( confirm( "You are about to update the live homepage.  Do you wish to continue?" )) {
		obj = document.getElementById( 'commit_homepage_msg' );
		if( obj != null ) {
			obj.innerHTML = 'Please Wait...';
		}

		document.admin_form.action = 'homepagecommit.php';
		document.admin_form.target = 'admin_iframe';
		document.admin_form.submit();
	}
}
function CommitHomepage_Response( bSuccess ) {
	var obj = document.getElementById( 'commit_homepage_msg' );
	if( obj != null ) {
		if( bSuccess ) {
			obj.innerHTML = 'The homepage has been refreshed.  You may <a class=linksmall target="_blank" href="/">view it here</a>.';
		} else {
			obj.innerHTML = 'The homepage could not be refreshed at this time.';
		}
	}
}

function PreivewHomepage() {
	var obj = document.getElementById( 'commit_homepage_msg' );
	if( obj != null ) {
		obj.innerHTML = '';
	}

	obj = document.getElementById( 'preview_homepage_msg' );
	if( obj != null ) {
		obj.innerHTML = 'Please Wait...';
	}

	document.admin_form.action = 'homepagepreview.php';
	document.admin_form.target = 'admin_iframe';
	document.admin_form.submit();
}
function PreviewHomepage_Response() {
	var obj = document.getElementById( 'preview_homepage_msg' );
	if( obj != null ) {
		obj.innerHTML = 'If the popup window was blocked, please <a class=linksmall target="_blank" href="/index.php?temp=yes">click here</a>.';
	}

	document.admin_form.action = '/index.php?temp=yes';
	document.admin_form.target = '_blank';
	document.admin_form.submit();

	/*
	var link = document.createElement( 'a' );
	if( link != null ) {
		link.target = '_blank';
		link.innerHTML = 'click here';
		link.href = '/index.php?temp=yes';
		obj.appendChild( link );
	}
	*/

}

function ShowElement( strID, strDisplay ) {
	var obj = document.getElementById( strID );
	if( obj != null ) {
	    if( typeof strDisplay != 'undefined' && strDisplay.length > 0 ) {
	        obj.style.display = strDisplay;
	    } else {
	        obj.style.display = '';
	    }
	}
}
function HideElement( strID ) {
    var obj = document.getElementById( strID );
	if( obj != null ) {
		obj.style.display = 'none';
	}
}

function SetFlashBannerStatus( value ) {
	if( confirm( "You are about to change the Flash Banner display status.  Do you wish to continue?" )) {
		var obj = document.getElementById( 'homepage_flashbanner_msg' );
		if( obj != null ) {
			obj.innerHTML = 'Please Wait...';
		}

		document.admin_form.action = 'homepageflashbanner.php?v=' + value;
		document.admin_form.target = 'admin_iframe';
		document.admin_form.submit();
	}
}
function SetFlashBannerStatus_Response( value ) {
	if( typeof value == "undefined" || value == null ) {
		var obj = document.getElementById( 'homepage_flashbanner_msg' );
		if( obj != null ) {
			obj.innerHTML = 'A problem occurred.  Please try again later.';
		}
		return;
	}
	var obj = document.getElementById( 'homepage_flashbanner_status' );
	if( obj != null ) {
		if( value == "1" ) {
			obj.innerHTML = "ON";
			obj.style.color = "green";
		} else {
			obj.innerHTML = "OFF";
			obj.style.color = "red";
		}
	}
	var obj = document.getElementById( 'homepage_flashbanner_link' );
	if( obj != null ) {
		if( value == "1" ) {
			obj.innerHTML = "Turn Off";
			obj.href = "javascript:SetFlashBannerStatus( '0' );";
		} else {
			obj.innerHTML = "Turn On";
			obj.href = "javascript:SetFlashBannerStatus( '1' );";
		}
	}
	var obj = document.getElementById( 'homepage_flashbanner_msg' );
	if( obj != null ) {
		obj.innerHTML = '';
	}
}

/***********************************************
* Bookmark site script-  Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
***********************************************/

function bookmarksite(title, url){
if (document.all)
window.external.AddFavorite(url, title);
else if (window.sidebar)
window.sidebar.addPanel(title, url, "")
}

