//This function is used to open the url to a new window
function openSizedNamedWindow(url, winname, width, height) {
	popupWin = window.open(url, winname, 'scrollbars,resizable=no,width=' + width + ',height=' + height);
}

//This function is used to verify whether or not a string is a valid date.
//String must be in mm-dd-yyyy format.
function isDate(dateStr) {
    var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
    var matchArray = dateStr.match(datePat); // is the format ok?
    if (matchArray == null) {
        //alert("Please enter date as either mm/dd/yyyy or mm-dd-yyyy.");
        return false;
    }
    month = matchArray[1]; // parse date into variables
    day = matchArray[3];
    year = matchArray[5];
    if (month < 1 || month > 12) { // check month range
        //alert("Month must be between 1 and 12.");
        return false;
    }
    if (day < 1 || day > 31) {
        //alert("Day must be between 1 and 31.");
        return false;
    }
    if ((month==4 || month==6 || month==9 || month==11) && day==31) {
        //alert("Month "+month+" doesn't have 31 days!")
        return false;
    }
    if (month == 2) { // check for february 29th
        var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
        if (day > 29 || (day==29 && !isleap)) {
            //alert("February " + year + " doesn't have " + day + " days!");
            return false;
        }
    }
    return true; // date is valid
}


//Checks the upload document form to see if the document getting uploaded is not an invalid 
//filetype, and to see if a display name has been specified. 
function CheckUploadForm(){
	var errorMsg = "";
	if (document.upload.document.value.length==0){ errorMsg += "\nYou need to choose a document to upload."; }
	if (document.upload.docname.value.length==0){ errorMsg += "\nYou need to specify a document name."; } 
	if (document.upload.document.value.indexOf(".asp") != "-1") { errorMsg += "\nYou cannot upload ASP files."; };
	if (document.upload.document.value.indexOf(".exe") != "-1") { errorMsg += "\nYou cannot upload EXE files."; };
	if (document.upload.document.value.indexOf(".bat") != "-1") { errorMsg += "\nYou cannot upload BAT files."; };
	if (document.upload.document.value.indexOf(".cgi") != "-1") { errorMsg += "\nYou cannot upload CGI files."; };
	if (document.upload.document.value.indexOf(".idx") != "-1") { errorMsg += "\nYou cannot upload IDX files."; };
	if (document.upload.document.value.indexOf(".htaccess") != "-1") { errorMsg += "\nYou cannot upload htaccess files."; };
	return checkErrorMsg(errorMsg);
}


//Checks the upload graphic form to see if the graphic getting uploaded is not an invalid filetype.
function CheckUploadGraphicForm(){
	var errorMsg = "";
	var blImgFound = 0;
	var strDocName = document.upload.document.value.toLowerCase();
	if (strDocName.length==0){ errorMsg += "\nYou need to choose an image to upload."; }
	if (strDocName.indexOf(".asp") != "-1" && strDocName.indexOf(".asp") != -1) { errorMsg += "\nYou cannot upload ASP files."; };
	if (strDocName.indexOf(".exe") != "-1" && strDocName.indexOf(".exe") != -1) { errorMsg += "\nYou cannot upload EXE files."; };
	if (strDocName.indexOf(".bat") != "-1" && strDocName.indexOf(".bat") != -1) { errorMsg += "\nYou cannot upload BAT files."; };
	if (strDocName.indexOf(".cgi") != "-1" && strDocName.indexOf(".cgi") != -1) { errorMsg += "\nYou cannot upload CGI files."; };
	if (strDocName.indexOf(".idx") != "-1" && strDocName.indexOf(".idx") != -1) { errorMsg += "\nYou cannot upload IDX files."; };
	if (strDocName.indexOf(".htaccess") != "-1" && strDocName.indexOf(".htaccess") != -1) { errorMsg += "\nYou cannot upload htaccess files."; };
	if (strDocName.indexOf(".png") != "-1" && strDocName.indexOf(".png") != -1) { blImgFound = 1; };
	if (strDocName.indexOf(".gif") != "-1" && strDocName.indexOf(".gif") != -1) { blImgFound = 1; };
	if (strDocName.indexOf(".jpg") != "-1" && strDocName.indexOf(".jpg") != -1) { blImgFound = 1; };
	if (blImgFound != 1 && strDocName.length!=0) { errorMsg += "\nThe selected file is not a valid image."; };
	return checkErrorMsg(errorMsg);
}


//Checks the AddPage form to see if the directory name contains valid characters, and
//the directory name has been specified, and a page name has been specified.
function CheckAddPageForm() {
	var errorMsg = "";
	var re = /^\w*$/;
	if (!re.test(document.formAdd.directory.value)) { errorMsg += "\nThe Directory Name field contains invalid characters."; }
	if (document.formAdd.directory.value.length==0) { errorMsg += "\nThe Directory Name is blank."; }
	if (document.formAdd.title.value.length==0) { errorMsg += "\nThe Page Name field is blank."; }
	return checkErrorMsg(errorMsg);
}


//Checks the Edit Page form to see if the page name has been specified.
function CheckEditPageForm() {
	var errorMsg = "";
	if (document.formEdit.title.value.length==0) { errorMsg += "\nThe Page Name field is blank."; }
	return checkErrorMsg(errorMsg);
}

//Checks the Add/Edit Article form to make sure article title, article date, 
//and article body have been entered.
function CheckArticleForm(TryCount) {
	var errorMsg = "";
	if (document.formArticle.title.value.length==0){ errorMsg += "\nThe Article Title field is blank."; }
	if (document.formArticle.message.value.length==0){ errorMsg += "\nThe Article Body field is blank."; }
	if (document.formArticle.date.value.length==0){ errorMsg += "\nThe Article Date field is blank."; }
	if (!isDate(document.formArticle.date.value) && document.formArticle.date.value.length>0){ errorMsg += "\nThe Article Date field is not a valid format."; }
	return checkErrorMsg(errorMsg);
}

//Checks the Article Settings form to make sure article list title is not blank.
function CheckArticleSettingsForm() {
	var errorMsg = "";
	if (document.formArticleSettings.list_title.value.length==0){ errorMsg += "\nThe Article List Title field is blank."; }
	return checkErrorMsg(errorMsg);
}

//Checks the length of the errorMsg string.  If it is not an empty string, an error
//message alert is displayed, otherwise the form is submitted.
function checkErrorMsg(errorMsg) {
	if (errorMsg != ""){
		msg = "___________________________________________________________________\n\n";
		msg += "Your document can not be uploaded because there are problems with the form.\n";
		msg += "Please correct the problem(s) and re-submit the form.\n";
		msg += "___________________________________________________________________\n\n";
		msg += "The following problem(s) need to be corrected: -\n";
		msg += errorMsg;
		errorMsg += alert(msg);
		return false;
	}	
	return true;
}


//this function moves options in a select box up and down.  The select box symbolizes
//the navigation of the current page.
function moveNav(strDir) {
	var mySelect = document.frmNav.mynav;
	if (mySelect.selectedIndex != "-1") {
		intTotal = mySelect.options.length;
		intMe = mySelect.selectedIndex;
		intPrev = mySelect.selectedIndex-1;
		intNext = mySelect.selectedIndex+1;
		if (intPrev == -1) intPrev = intTotal-1;
		if (intNext == intTotal) intNext = 0;
		strTempText = mySelect.options[intMe].text;
		strTempValue = mySelect.options[intMe].value;		
		if (strDir == "up") {
			mySelect.options[intMe].text = mySelect.options[intPrev].text;
			mySelect.options[intMe].value = mySelect.options[intPrev].value;
			mySelect.options[intPrev].text = strTempText;
			mySelect.options[intPrev].value = strTempValue;
			mySelect.selectedIndex = intPrev;
		}else if (strDir == "down") {
			mySelect.options[intMe].text = mySelect.options[intNext].text;
			mySelect.options[intMe].value = mySelect.options[intNext].value;
			mySelect.options[intNext].text = strTempText;
			mySelect.options[intNext].value = strTempValue;
			mySelect.selectedIndex = intNext;		
		}
	}
}

//Writes the order of the select box to a hidden text field so that the new nav file can be written.
function submitNavForm() {
	document.frmNav.txtNav.value = "";
	for (i=0;i<document.frmNav.mynav.options.length;i++) {
		document.frmNav.txtNav.value += document.frmNav.mynav.options[i].value + "\n";
	}
	return true;
}
