/**********************************************************
* Required field(s) validation v1.10- By NavSurf          *
* Visit Nav Surf at http://navsurf.com                    *
* Visit http://www.dynamicdrive.com/ for full source code *
* Modified by Jeff Craft for Nexicom Inc.                 *
***********************************************************/
// Original code staticly programmed the fields right in this 
// function. I modified it to accept the Required fields and 
// descriptions as a passed array variable;
function formCheck(formobj,fieldRequired,fieldDescription){
	// Enter name of mandatory fields
	//var fieldRequired = Array("fname", "lname");
	// Enter field description to appear in the dialog box
	//var fieldDescription = Array("First Name", "Last Name");
	// dialog message
	var alertMsg = "Please complete the following fields:\n";
	
	var l_Msg = alertMsg.length;
	
	for (var i = 0; i < fieldRequired.length; i++){
		var obj = formobj.elements[fieldRequired[i]];
		if (obj){
			switch(obj.type){
			case "select-one":
				if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == ""){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			case "select-multiple":
				if (obj.selectedIndex == -1){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			case "text":
			case "password":
			case "textarea":
				if (obj.value == "" || obj.value == null){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			default:
			}
			if (obj.type == undefined){
				var blnchecked = false;
				for (var j = 0; j < obj.length; j++){
					if (obj[j].checked){
						blnchecked = true;
					}
				}
				if (!blnchecked){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
			}
		}
	}

	if (alertMsg.length == l_Msg){
		return true;
	}else{
		alert(alertMsg);
		return false;
	}
}

/******************************************************************
* Email Validation script- � Dynamic Drive (www.dynamicdrive.com) *
* This notice must stay intact for legal use.                     *
* Visit http://www.dynamicdrive.com/ for full source code         *
*******************************************************************/
var emailfilter=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i

function checkmail(e){
	var returnval=emailfilter.test(e.value)
	if (returnval==false){
		alert("Please enter a valid email address.")
		e.select()
	}
	return returnval
}

function areYouSure(delValue)
{
  var choice;
  var promptStr = "Are you sure you wish to delete '"+delValue+"'?"
  choice = confirm(promptStr)
  if (choice == true)
	{
    return true;
  }
	else
	{
    return false;
  }
}

function forum_areYouSure(delValue)
{
  var choice;
  var promptStr = "***WARNING*** You are about to delete a forum which will include all topics and posts contained within this forum. Are you sure you wish to delete '"+delValue+"'?"
  choice = confirm(promptStr)
  if (choice == true)
	{
    return true;
  }
	else
	{
    return false;
  }
}

function category_areYouSure(delValue)
{
  var choice;
  var promptStr = "***WARNING*** You are about to delete a category which will include all forums, topics and posts contained within this forum. Are you sure you wish to delete '"+delValue+"'?"
  choice = confirm(promptStr)
  if (choice == true)
	{
    return true;
  }
	else
	{
    return false;
  }
}

function thread_areYouSure(delValue)
{
  var choice;
  var promptStr = "***WARNING*** You are about to delete a thread which will include all posts contained within this thread. Are you sure you wish to delete '"+delValue+"'?"
  choice = confirm(promptStr)
  if (choice == true)
	{
    return true;
  }
	else
	{
    return false;
  }
}

function s_areYouSure()
{
  var choice;
  var promptStr = "Are you sure?"
  choice = confirm(promptStr)
  if (choice == true)
	{
    return true;
  }
	else
	{
    return false;
  }
}

function checkpost()
{
	var msgbody = document.getElementById("message").value;
	
	if(msgbody.length <= 5)
	{
		alert("Please enter a message longer than 5 characters.");
		return false;
	}
	else
	{
		return true;
	}
}

// Ajax function to send a request to a php page
// to query CMS if the username entered exists or not
// and displays the result.
function chkUsername(UserNM, OutputHere){
	var chkUsername;  // The variable that makes Ajax possible!

	try{
		// Opera 8.0+, Firefox, Safari
		chkUsername = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			chkUsername = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				chkUsername = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	chkUsername.onreadystatechange = function(){
		if(chkUsername.readyState == 4){
			document.getElementById(OutputHere).innerHTML = chkUsername.responseText; // Put the result back to the page
		}
	}

	// PHP script to query CMS for username
	var openURL = "includes/chkusername.php?un="+UserNM;
	chkUsername.open("GET", openURL, true);
	chkUsername.send(null); 
}

function chkPwd()
{
	var Pwd1 = document.getElementById("Pwd").value;
	var Pwd2 = document.getElementById("ConfirmPwd").value;
	var outputObj = document.getElementById("pwd_msg");
	
	if(Pwd1 != Pwd2)
	{
		outputObj.innerHTML = "<span style='color: #FF0000; font-family: arial; font-weight: bold; font-size: 10pt;'><br />Passwords do not Match</span>"
	}
	else
	{
		outputObj.innerHTML = "";
	}
}

function clrMsgs()
{
	if(s_areYouSure())
	{
		document.getElementById("pwd_msg").innerHTML = "";
		document.getElementById("un_msg").innerHTML = "";
		return true;
	}
	else
	{
		return false;
	}
}

// Gets the user's screen resolution
function getResolution()
{
	var resEleW = document.getElementById("RES_W");
	var resEleH = document.getElementById("RES_H");
	resEleW.value = screen.width;
	resEleH.value = screen.height;
	
	return;
}

function pageSelect(file,page)
{
	var url;
	url = file+"&p="+page;
	
	window.location = url;
	return true;
}

function p_pageSelect(file,page)
{
	var url;
	url = file+"?p="+page;
	
	window.location = url;
	return true;
}

function request_posted()
{
	alert("Your prayer request has been submitted and is now pending approval from the Administrator.");
}

function findCaretPos(txtObj)
{
    var pos = doGetCaretPosition(txtObj);
    var outText = document.getElementById("curPos");
    
    outText.value = pos;
}

function doGetCaretPosition(ctrl){
	var CaretPos = 0;
	// IE Support
	if (document.selection)
    {
        if(ctrl.type == 'text') // textbox
        {
    		ctrl.focus();
            var Sel = document.selection.createRange();
            var SelLength = document.selection.createRange().text.length;
            Sel.moveStart ('character', -ctrl.value.length);
            CaretPos = Sel.text.length - SelLength;
        }
        else // textarea
        {
            //save off the current value to restore it later,
            var sOldText = ctrl.value;

            //create a range object and save off it's text
            var objRange = document.selection.createRange();
            var sOldRange = objRange.text;

            //set this string to a small string that will not normally be encountered
            var sWeirdString = '#%~';

            //insert the weirdstring where the cursor is at
            objRange.text = sOldRange + sWeirdString; objRange.moveStart('character', (0 - sOldRange.length - sWeirdString.length));

            //save off the new string with the weirdstring in it
            var sNewText = ctrl.value;

            //set the actual text value back to how it was
            objRange.text = sOldRange;

            //look through the new string we saved off and find the location of
            //the weirdstring that was inserted and return that value
            for (i=0; i <= sNewText.length; i++) {
                var sTemp = sNewText.substring(i, i + sWeirdString.length);
                if (sTemp == sWeirdString) {
                    var cursorPos = (i - sOldRange.length);
                    return cursorPos;
                }
            }
        }
	}
	// Firefox support
	else if (ctrl.selectionStart || ctrl.selectionStart == '0')
		CaretPos = ctrl.selectionStart;

	return (CaretPos);

}

function insert_bbcode()
{
    var pos = document.getElementById("curPos").value;
    var mainBody = document.getElementById("message").value;
    var mainLink = document.getElementById("linkUrl").value;
    var mainText = document.getElementById("linkText").value;
    
    var pretext = mainBody.substring(0,pos);
    var posttext = mainBody.substring(pos,mainBody.length);
    
    var outputtext = pretext + '[URL="' + mainLink + '"]' + mainText + '[/URL]' + posttext;
    document.getElementById("message").value = outputtext;
    
    document.getElementById("linkUrl").value = "";
    document.getElementById("linkText").value = "";

    popup('popUpDiv',100,100);
}

