var windowName = null;

function openWindow(URL, w, h, name, external) { 
	if (!w && !h) { //if a width and height are not specified then use these default values
		width = 400;
		height = 450;
		}
	else { //else use the values specified
		width = w;
		height = h;
		}
	if (!name) { //if a name is not specified
		windowName = "PopUpWindow";
		}
	else { //else use the name specified
		windowName = name;
		}

		windowName = window.open(URL,windowName,"width=" + width + ",height=" + height + ",toolbar=0,location=0,directories=0,status=0,menuBar=0,scrollBars=1,resizable=1,top=100,left=100");
	if (!(external)){
		windowName.focus();
	}
} 

function showWindow(URL, w, h, name, external) { 
	if (windowName) { // object exists
		if (!windowName.closed) { // check and see if it has not been closed
			windowName.location.href = URL; // if not, set focus
			}
		else {
			openWindow(URL, w, h, name, external);
			}
		}
	else { // the window was closed, create it again
		openWindow(URL, w, h, name, external);
		}
	if (!(external)){
		windowName.focus();
	}
}


//check for delete on user list page
function checkDelete(frm) {
	var isChecked = false;
	for(i=0; i<frm.elements.length; i++) {
		if(frm.elements[i].type == "checkbox") {
			if(frm.elements[i].checked == true) {
				isChecked = true;
			}
		}
	}
	if(!isChecked) {
		alert("You must choose a user to delete before clicking the button.")
		return false;
	}
	else {
		return true;
	}
}

//admin user form validation **************************************************
function isValidUserForm(frm) {
	if(frm.txtFname.value == "") {
		alert("You must supply a First Name."); frm.txtFname.focus(); return false;
	}
	if(frm.txtLname.value == "") {
		alert("You must supply a Last Name."); frm.txtLname.focus(); return false;
	}
	if(!isEMail(frm.txtEmail)) {
		alert("You must supply a valid email address."); frm.txtEmail.focus(); return false;
	}
	if(frm.txtUsername.value == "") {
		alert("You must supply a username."); frm.txtUsername.focus(); return false;
	}
	if(frm.txtPassword.value == "") {
		alert("You must supply a password."); frm.txtPassword.focus(); return false;
	}
	if(frm.selTypes.value == "") {
		alert("You must choose a user type."); frm.selTypes.focus(); return false;
	}
	if((frm.hidUserType.value.indexOf("3") == -1) && (frm.selTypes.value.indexOf("3") != -1)) {
		if(!confirm("Are you sure you want to make this user an Admin?"))
			return false;
	}
	return true;
}

//Quote form validation **************************************************
function isValidFormQuote(frm) {
	if(frm.Company.value == "") {
		alert("Please supply a Company name."); frm.Company.focus(); return false;
	}
	if(frm.Contact.value == "") {
		alert("Please supply a Contact."); frm.Contact.focus(); return false;
	}
	if(frm.Address.value == "") {
		alert("Please supply an Address."); frm.Address.focus(); return false;
	}
	if(frm.City.value == "") {
		alert("Please supply a City."); frm.City.focus(); return false;
	}
	if(frm.State.options[0].selected) {
		alert("Please choose a State."); frm.State.focus(); return false;
	}
	//if(!checkZip(frm.Zip.value)) {
	//	alert("Please supply a valid Zip Code."); frm.Zip.focus(); return false;
	//}
	if(frm.Zip.value == "") {
		alert("Please supply a Postal Code."); frm.Zip.focus(); return false;
	}
	if(frm.Country.value == "") {
		alert("Please supply a Country."); frm.Country.focus(); return false;
	}
	if(frm.Phone.value == "") {
		alert("Please supply a Phone number."); frm.Phone.focus(); return false;
	}
	if(!isEMail(frm.Email)) {
		alert("Please supply a valid email address."); frm.Email.focus(); return false;
	}
	for(i=0; i<frm.elements.length; i++) {
		if(frm.elements[i].type == "checkbox") {
			if(frm.elements[i].checked == true) {
				if(frm.elements[i+1].value == "") {
					alert("Please specify the level of importance for you choice.")
					frm.elements[i+1].focus(); return false
				}
			}
		}
	}
	return true;
}


// Check for valid e-mail address
function isEMail(s) {   
	if (isWhitespace(s)) return false;
    // there must be >= 1 character before @
    var i = 1;
    var newVal;
    var sLength;
    
    newVal = s.value
    sLength = newVal.length
        
    while ((i < sLength) && (newVal.charAt(i) != "@")) { 
		i++
    }
        
    if ((i >= sLength) || (newVal.charAt(i) != "@")) return false;
    else i += 2;

    while ((i < sLength) && (newVal.charAt(i) != ".")) {
		i++
    }

    // there must be at least one character after the .
    if ((i >= sLength - 1) || (newVal.charAt(i) != ".")) return false;
    else return true;
}


// Returns true if string s is empty or whitespace characters only.
function isWhitespace(s) {
	var i;
    var newVal;
    var objLen;
    var c;
    var whitespace = " \t\n\r";
    
    if (s == null) return true;
    
    newVal = s.value;
    objLen = newVal.length;
    
	// Search string for non-whitespace character.
    for (i = 0; i < newVal.length; i++) {   
     	// Check that current character isn't whitespace.
     	c = newVal.charAt(i);
    	if (whitespace.indexOf(c) == -1) return false;
    }
    // All characters are whitespace.
    return true;
}


function checkZip(str) {
	var regexp = /^(\d{5}|\d{5}-\d{4})$/;
	return regexp.test(str);
}

function setMode(mode,levelID) {
	document.frm.hidMode.value = mode;
	document.frm.hidLevelID.value = levelID;
}

// called onload of admin/data_edit.asp
// set default form elements based on selections in level dropdowns
// populate textbox with selected value in dropdown
// set checkbox value to selected value in dropdown
function setDefaultValues() {
	var selectedValue;
	var selectedText;
	var f = document.frm;
	//if level 1 dropdown exists
	if(f.selL1) {
		// if level2 dropdown exists
		if(f.selL2) {
			if(f.selL2.options[f.selL2.selectedIndex].value.length > 0) {
				selectedValue = f.selL2.options[f.selL2.selectedIndex].value;
				selectedText = f.selL2.options[f.selL2.selectedIndex].text;
			}
			else {
				selectedValue = f.selL1.options[f.selL1.selectedIndex].value;
				selectedText = f.selL1.options[f.selL1.selectedIndex].text;		
			}
		}
		else {
			if(f.selL1.options[f.selL1.selectedIndex].value.length > 0) {
				selectedValue = f.selL1.options[f.selL1.selectedIndex].value;
				selectedText = f.selL1.options[f.selL1.selectedIndex].text;
			}
		}
	}
	if(f.txtDesc && f.cbxDelete) {
		f.txtDesc.value = selectedText;
		f.cbxDelete.value = selectedValue;
	}
	//alert(selectedText);
}

//called on button click events in admin/data_edit.asp
function setFunction(mode) {
	var f = document.frm;
	if(mode == "delete") {
		if(f.cbxDelete.checked == false) {
			alert("You need to check the checkbox before deleting.");
			return;
		}
		if(f.hidLevel.value == "1" && f.selL2) {
			alert("You must delete all Level 2 items before deleting a Level 1 item.");
			f.selL2.focus();
			return;
		}
	}
	f.hidMode.value = mode;
	f.submit();
}


function setTimeSelectedValue(val)
{
	var obj = document.forms(0).selEventTime;
	if(obj)
	{
		for(i=0;i<obj.options.length;i++)
		{
			if(obj.options[i].value == val)
			{
				obj.selectedIndex = i;
				break;
			}
		}
	}
}

function setFocus(obj)
{
	obj.focus();
}