/* Nexus6 Manual Validation Library - for use exclusively with N6::JS */

function CheckPasswords(string, pwd1, pwd2)
{
    if (string == 'add')
    {
        if (pwd1 == "" || pwd2 == "")
        {
            alert("You must enter a value in both password fields");
            return false;
        }
    }
    if (pwd1 != pwd2)
    {
        alert("The password fields do not match, please re-enter your password");
        return false;
    }
    return true;
}

function AlertIfNotChecked(element, msg)
{
	if(!element.checked)
	{
		alert(msg);
		return false;
	}
	return true;
}

function AlertIfNotMatch(fld1, fld2, msg)
{
	if(fld1.value != fld2.value)
	{
		alert(msg);
		fld2.focus();
		return false;
	}
	return true;
}

function AlertIfCheckedAndNoValue(chkBx, fld1, msg)
{
    if(chkBx.checked)
    {
        if(fld1.value == '')
        {
            alert(msg);
            fld1.focus();
            return false;
        }
    }
    return true;
}

function AlertIfCheckedAndNotMatch(chkBx, fld1, fld2, msg)
{
    if(chkBx.checked)
    {
	    if(fld1.value != fld2.value)
	    {
    		alert(msg);
		    fld2.focus();
		    return false;
	    }
	}
	return true;
}

function AlertIfSelectedIndex(fld, index, msg)
{
	if(fld.options.selectedIndex == index)
	{
		alert(msg);
		fld.focus();
		return false;
	}
	return true;
}

function AlertIfSelectedValue(fld, val, msg)
{
	if(fld.options[fld.options.selectedIndex].value == val)
	{
		alert(msg);
		fld.focus();
		return false;
	}
	return true;
}

function AlertIfNotSelected(fld, msg)
{
	var isSelected = false;
	for(var i=0; i<fld.length; i++)
	{
		if(fld.options[i].selected)
		{
			isSelected = true;
		}
	}
	if(!isSelected)
	{
		alert(msg);
		fld.focus();
		return false;
	}
	else
	{
		return true;
	}
}

function AlertIfValueExists(fldArray, msg)
{
	//Loop through array, and extract values
	var values = new Array();
	for(var i=0; i<fldArray.length(); i++)
	{
		values[i] = fldArray[i].value;
	}

	//Loop through values array, and check if each value is unique
	for(var i=0; i<values.length(); i++)
	{
		for(var j=0; j<values.length(); j++)
		{
			if((i != j) && (values[i] != '') && (values[i] == values[j]))
			{
				alert(msg);
				return false;
			}
		}
	}
	return true;
}

function AlertIfValueExistsForm(frm, fldName, msg)
{
	var fldArray = new Array();
	var fldCount = 0;
	//Loop through form, extracting fields into fldArray
	for(var i=0; i<frm.elements.length; i++)
	{
		if(frm.elements[i].name == fldName)
		{
			fldArray[fldCount++] = frm.elements[i];
		}
	}

	//Loop through array, and extract values
	var values = new Array();
	for(var i=0; i<fldArray.length; i++)
	{
		values[i] = fldArray[i].value;
	}

	//Loop through values array, and check if each value is unique
	for(var i=0; i<values.length; i++)
	{
		for(var j=0; j<values.length; j++)
		{
			if((i != j) &&
			   (values[i] == values[j]) &&
			   (values[i] != '') &&
			   (values[j] != '') &&
			   (values[i] != null) &&
			   (values[j] != null))
			{
				alert(msg);
				return false;
			}
		}
	}
	return true;
}

function AlertIfFieldEmptyForm(frm, fldNameArray, msgArray)
{
	var fldArray = new Array();
    var fldCount = 0;
    //Loop through form, extracting fields into fldArray
    for(var i=0; i<frm.elements.length; i++)
    {
		for(var j=0; j<fldNameArray.length; j++)
		{
	        if(frm.elements[i].name == fldNameArray[j])
	        {
	            fldArray[fldCount++] = new Array(frm.elements[i], j);
	        }
    	}
	}

    //Loop through array and check if the value is empty
    for(var i=0; i<fldArray.length; i++)
    {
        if((fldArray[i][0].value == "") || (fldArray[i][0].value.length == 0))
		{
			alert(msgArray[fldArray[i][1]]);
			fldArray[i][0].focus();
			return false;
		}
    }
	return true;
}

function AlertIfNotCheckedForm(frm, fldName, msg)
{
	var found = false;
	for(i=0; i<frm.elements.length;i++)
	{
		if (frm.elements[i].name == fldName)
		{
			if(frm.elements[i].checked)
			{
				found = true;	
			}
		}
	}

	if(!found) 
	{
		alert(msg);
		return false;
	}
	return true;
}

function AlertIfNotAlphaNumeric(fld, msg)
{
    var charpos = fld.value.search("[^A-Za-z0-9\_]");
	if(charpos >= 0)
	{
		fld.focus();
        alert(msg);
        return false;
    }
    return true;
}

function AlertIfNotAlphaNumericUnderscore(fld, msg)
{
    var charpos = fld.value.search("[^A-Za-z0-9_]");
	if(charpos >= 0)
	{
		fld.focus();
        alert(msg);
        return false;
    }
	return true;
}

function LowerCase(fld)
{
	fld.value = fld.value.toLowerCase();
	return true;
}

function AlertIfCheckedAndFieldEmpty(chkBox, fld, msg)
{
	if(chkBox.checked)
	{
		if(fld.value.length == 0)
		{
			alert(fld)
			fld.focus();
			return false;
		}
	}
	return true;
}

function AlertIfRadioCheckedAndFieldEmpty(chkBox, val, fld, msg)
{
    // Determine which button is checked
    for(var i=0; i<chkBox.length; i++)
    {
        if(chkBox[i].checked && chkBox[i].value == val && fld.value.length == 0)
        {
            alert(msg);
            fld.focus();
            return false;
        }
    }
    return true;
}

function AlertIfItemInArray(initialVal, item, itemArray, msgStart, msgEnd, itemBefore, itemSeperator)
{
	for(var i=0; i<itemArray.length; i++)
	{
		if(item.value != initialVal && item.value == itemArray[i])
		{
			var msgStr = msgStart;
			for(var i=0; i<itemArray.length; i++)
			{
				msgStr += itemBefore + itemArray[i] + itemSeperator + " ";
			}
			msgStr += msgEnd;
			item.focus();
			alert(msgStr);
			return false;
		}
	}
	return true;
}

function AlertIfInvalidFileType(validTypesArray, fld, radioGrp, msg, ifNotBlank)
{
    if(ifNotBlank && fld.value.length == 0)
    {
        return true;
    }

    //Get file type for selected item
    var fileIndex = -1;
    for(var i=0; i<radioGrp.length; i++)
    {
        if(radioGrp[i].checked)
        {
            for(var j=0; j<validTypesArray.length; j++)
            {
                if(validTypesArray[j][0] == radioGrp[i].value)
                {
                    fileIndex = j;
                }
            }
        }
    }

    // Check for when we have no restrictions
    if(fileIndex == -1)
    {
        return true;
    }

    // Get the last index of '.' and check characters after that to make
    // sure they are a valid type
    var fldIndex = fld.value.lastIndexOf('.');
    var fileType = fld.value.substr(fldIndex+1);
    fileType = fileType.toLowerCase();
    var found = false;
    var validTypeString = "";
    for(var i=1; i<validTypesArray[fileIndex].length; i++)
    {
        if(validTypesArray[fileIndex][i] == fileType)
        {
            found = true;
        }
        validTypeString += "."+validTypesArray[fileIndex][i]+ " ";
    }
    if(!found) 
    {
        alert(msg + validTypeString);
        return false;
    }
    return true;
}

function AlertIfNotVal1(fld1, fld2, msg)
{
    if(fld1 && !fld2 && fld1.value.length == 0)
    {
        alert(msg);
        return false;
    }
    else if(fld1 && fld2 && fld1.value.length == 0 && fld2.value.length == 0)
    {
        alert(msg);
        return false;
    }
    return true;
}

function AlertIfNoValue(fld1, msg)
{
    if(fld1 && fld1.value.length == 0)
    {
        alert(msg);
        return false;
    }
    return true;
}
