
//<!--

function ShowAlert(message)
{
	var alert = document.getElementById("alert");
	var alertGif = document.getElementById("alertGif");
	alertGif.innerHTML = "<img src='../../Graphics/error_alert.gif' />";
	alert.innerHTML=message;
}

function IsValid_HasName(message){ 
	
	return IsValid_Return((GetName().length==0), message) + IsValidInput("Application Name: ", GetName()); 
}

function IsValid_HasItemName(message)
{ 	
	return IsValid_Return((GetItemName().length==0), message) + IsValidInput("Item Name: ", GetItemName());
}

function IsValid_AtLeastOneField(message)
{ 	
	return IsValid_Return((GetFieldNames().length==0), message); 
}

function IsValid_Description(message){
	
	return IsValid_Return((GetDescription().length>3500), message) + IsValidInput("Description: ", GetDescription()); 
}

function IsValid_AtLeastOneRequiredField(message)
{ 
	var isvalid = false;
	for (var key in CustomFields){
		if (CustomFields[key].IsRequired == true)
			isvalid = true;
	}
	return IsValid_Return(!isvalid, message); 
}

function IsValid_AtLeastOneListHeader(message)
{ 
	var isvalid = false;
	for (var key in CustomFields){
		if ((CustomFields[key].IsRequired == true || CustomFields[key].IsRequired == 'true')
		&& (CustomFields[key].IsListHeader == true || CustomFields[key].IsListHeader == 'true'))
			isvalid = true;
	}
	return IsValid_Return(!isvalid, message); 
}

function IsValid_HasPickOptions(message)
{
	isValid = true;
	
	for (var key in CustomFields){
		if (CustomFields[key].Type.Name == "Pick List"){
			if (CustomFields[key].PickListOptions.length == 0)
			 isValid = false;
		}
	}		
	
	return IsValid_Return(!isValid, message)
}


function IsValid_HasStatusOptions(message)
{
	isValid = true;
	
	for (var key in CustomFields){
		if (CustomFields[key].Type.Name == "Status"){
			if (CustomFields[key].StatusOptions.length == 0)
			 isValid = false;
		}
	}		
	
	return IsValid_Return(!isValid, message)
}

function IsValid_IsOldConvertible()
{
	var isvalid = true;
	var message = "";
	var fID, oldType, newType;
	
	
	for (var key in CustomFields){
		if (CustomFields[key].ObjectID.length > 0){
			newType = CustomFields[key].Type.Name;
			oldType = GetOldType(CustomFields[key].ObjectID);			
			if (!IsConvertable(oldType, newType)) {
				message += "'" + key + "' cannot be converted from '" + oldType + "' to '" + newType +"'.<br />";
				isvalid = false;
			}	
		}
	}
	
	return IsValid_Return(!isvalid, message)
}


function IsConvertable(oldType, newType)
{
	var i;
	if (oldType == newType)
		return true;
	else
	{
		for (i = 0; i<convertableTypeMatrix.length; i++)
		{
			if ((convertableTypeMatrix[i].split(',')[0]==oldType) && (convertableTypeMatrix[i].split(',')[1]==newType))
			{
				return true;
			}
		}
	}
	return false;
}
function GetOldType(fID)
{
	var i; 
	for (i = 0; i<oldIDs.split(',').length; i++)
	{
		if (oldIDs.split(',')[i] == fID)
		{
			return oldTypes.split(',')[i];
		}
	}
	return "";
}


function IsValid_AllCalcFieldsAreDefined(message) {

	for (var key in CustomFields){
		if (CustomFields[key].Type.Name == 'Calculation'){
			if (CustomFields[key].FormulaElements.length < 1)
				return message;
			
		}
	}	
	return "";
}


function IsValid_Return(bool, message)
{
	if (bool)
		return message;
	else
		return "";
}

function Validate_IsFieldBeingReferenced(oldName){
	try {
		if (ReferencingEntities[oldName].length > 0)
			return "This field is being referenced by: " + ReferencingEntities[oldName].join(',') + ". You may not make any changes to this field.";

		return "";
	}
	catch (e)
	{ return ""; }
}

function Validate_DoesAssigneeExist(oldName)
{
	for (var key in CustomFields){
		if (key != oldName){
			if (CustomFields[key].Type.Name == "Assignee")
				return "* You may define only one 'Assignee' type field per application.\n";
		}
	}
	return "";		
}

function Validate_DoesStatusExist(oldName)
{
	for (var key in CustomFields){
		if (key != oldName){
			if (CustomFields[key].Type.Name == "Status")
				return "* You may define only one 'Status' type field per Dynamic application.\n";
		}
	}
	return "";
}

function Validate_ValidCrossRef()
{
	if (List_SelectedIndex('applicationList') == -1 || List_SelectedIndex('refCandidateList') == -1)
		return "You must select an application type and field name to reference.";
		
	return "";
}

function Validate_IsValidConversion(name, type){
	if (CustomFields[name].Type.Name == 'Cross Reference' && type != 'Cross Reference')
		 return "You may not change any option of a previously defined 'Cross Reference' field.";		
	else if (CustomFields[name].Type.Name == 'Calculation' && type != 'Calculation')
		return "You may not change the data type of a previously defined 'Calculation' field.";
		
	return "";
}

function Validate_IsValidEditCrossRef(oldName, newName){
	if (CustomFields[oldName].CrossRefAppID != List_GetSelectedOptionValue('applicationList') ||
		CustomFields[oldName].CrossRefFieldID != List_GetSelectedOptionValue('refCandidateList') ||
		oldName != newName)							
		return "You may not change any option of a previously defined 'Cross Reference' field.";
		
	if (Control_GetCheckValue('defineAppInstance') == true && List_SelectedIndex('appInstanceWSList') != -1 &&
		CustomFields[oldName].CrossRefAppInstanceID != List_GetSelectedOptionValue('appInstanceWSList'))
		return "You may not change any option of a previously defined 'Cross Reference' field.";
		
	if (Control_GetCheckValue('defineAppInstance') == true && CustomFields[oldName].CrossRefAppInstanceID == "")
		return "You may not change any option of a previously defined 'Cross Reference' field.";
		
	return "";
}

function Validate_DuplicateValues(list, name, currIndex)
{
	var error = "";
	var tempList = document.getElementById(list);
	
	for ( var i = 0; i < tempList.length; i++)
	{	
		if (i != currIndex){
			if (List_GetOptionText(list, i).toUpperCase() == name.toUpperCase())
				error = "* You may not add a duplicate field.\n";
		}
	}
	if (ExternalFields[name])
		error = "*You may not name a custom field the same as another calculable field.";
	
	return error;
}

function IsValidInput(prefix, arg) {
	//No pipe, parenthesis, +, ", ',  \, ;  .
	
	var re = /^[^\|"()+';]{0,}$/g;
	var result = re.exec(arg);	
	
	if (result == null)
		return "* " + prefix + " Does not allow:   |, \", (, ), +, ', ;  \n";
	
	return "";
}

function ContainsInvalidChars(defaultValue) {
	//No comma allowed in default value
	var re = /,/g;
	var result = re.exec(defaultValue);
	
	if (result == null)
		return false;
		
	return true;
}



function Validate_DefaultValue(type, defaultValue, maxlengthvalue)
{
	var error = "";
	if (defaultValue != ""){
				
		
		switch (type)
		{
			case "Create Date":
				break;
			case "Creator":
				break;
			case "Currency":
				if (!IsCurrency(defaultValue))
					error = "* You must set the default value for a 'Currency' field to a number or you entered an amount that is too large or small (no $).";
				break;
			case "Decimal":
				if (!IsDecimal(defaultValue))
					error = "* You must set the default value for a 'Decimal' field to a number or you entered an amount that is too large or small.";
				break;
			case "Hyperlink":
				if (!IsValidHyperlink(defaultValue))
					error = "* The default number of characters for a 'Hyperlink' field may not be greater than 100 and must be in the format: 'http://..' or 'mailto:..' or 'https://..' or '\\\\UNCName...'";
				break;
			case "Integer":
				if (!IsInteger(defaultValue))
					error = "* You must set the default value for an 'Integer' field to a whole number or you entered an amount that is too large or small.";
				break;
			case "Item Number":
				break;
			case "Long Date":
				if (!IsLongDate(defaultValue))
					error = "* You must set the default value for a 'Long Date' field to a valid date in this format: MM/DD/YYYY HH:MM AM ";			
				break;
			case "Long Text":
				if (!IsWebForm){
					var maxlengthval = document.getElementById('maxLength').value;
	
					if (maxlengthval.length == 0){
						document.getElementById('maxLength').value = 3500;
						maxlengthval = 3500;
					}	
								
					if (!IsLongText(defaultValue, maxlengthval)){								
						var maxlengthval = document.getElementById('maxLength').value;
						error = "* You may not have more than " + maxlengthval + " characters as the default value for a 'Long Text' field.";
					}
			    }
			    else 
			    {	//PUT WEBFORM LONGTEXT VALIDATION HERE.
					if (!IsLongText(defaultValue, maxlengthvalue)){
						error = "* You may not have more than " + maxlengthvalue + " characters as the default value for a 'Long Text' field.";
					}			    
			    }
				break;
			case "Members":
				break;
			case "Percent":
				if (!IsPercent(defaultValue))
					error = "* You must set the default value for a 'Percent' field to a number between 0 and 100.";
				break;
			case "Pick List":
				break;
			case "Short Date":
				if (!IsValidDate(defaultValue))
					error = "* You must set the default value for a 'Short Date' field to a valid date in this format: MM/DD/YYYY ";
				break;
			case "Text":
				if (!IsText(defaultValue))
					error = "* You may not have more than 100 characters as the default value for a 'Text' field.";
				break;
			case "Yes No":
				if (!IsYesNo(defaultValue))
					error = "* You must set the default value for a 'Yes / No' field to either 'Yes' or 'No'.";
				break;
		}
 	}

	return error;
}

function IsYesNo(defaultValue)
{
	if (defaultValue.toUpperCase() != "YES" && defaultValue.toUpperCase() != "NO")
		return false;	
	
	return true;
}

function IsPercent(defaultValue)
{
	var temp;
		
	try
	{
		temp = parseFloat(defaultValue);
		
		if (isNaN(temp))
			return false;
			
		if (temp >= 0 && temp <= 100)
			return true;
		
		return false;
	}
	catch (e)
	{
		return false;
	}
}

function IsInteger(defaultValue)
{
	var temp;
	
	try
	{
		temp = parseFloat(defaultValue, 10);		
		if (temp % 1 == 0 && (temp >= -2147483648 && temp <= 2147483647))
			return true;
		
		return false;
	}
	catch (e)
	{
		return false;
	}	
}

function IsValidHyperlink(defaultValue)
{
	if (defaultValue.length > 100)
		return false;
		
	var re = /^http:\/\/.+|https:\/\/.+|mailto:.+@.{1,}\..{1,}|\\\\[^\\].+$/;
	re.ignoreCase = true;
	if (re.exec(defaultValue) == null)
		return false;

	return true;
}

function IsDecimal(defaultValue)
{
	var temp;
	
	try
	{		
		temp = parseFloat(defaultValue);
		
		if(temp >= -79228162514246337593950335 && temp <= 79228162514246337593950335)
			return true;
		
		return false;
	}
	catch (e)
	{
		return false;		
	}
}

function IsCurrency(defaultValue)
{
	return IsDecimal(defaultValue);
}

function IsText(defaultValue) {
	if (defaultValue.length > 100)
		return false;
		
	return true;
}

function IsLongText(defaultValue, maxlengthvalue) {	
	
	if (defaultValue.length > maxlengthvalue)
		return false;
		
	return true;
}


function IsLongDate(defaultValue)
{	//Only accepts date/time values in the format:  MM/DD/YYYY HH:MM AM
	//through the years 1000 to 9999 and checks for valid February end-of-month
	//dates (leap year checking).	
	
	try
	{		
		if (defaultValue.length != 19)
			return false;
			
		var ldateParts = defaultValue.split(' ');	
					
		var timeParts = ldateParts[1].split(':');
		var hour = timeParts[0];
		var minutes = timeParts[1];		
		var amPM = ldateParts[2];
		
		//Date Check
		if (!IsValidDate(ldateParts[0]))
			return false;				
		
		//Hour check
		re = /^0[1-9]|1[0-2]$/;
		if (re.exec(hour) == null)
			return false;
			
		//Minutes check
		re = /^[0-5][0-9]$/;
		if (re.exec(minutes) == null)
			return false;
		
		//AM - PM check
		re = /^[AMam]|[PMpm]$/;  //needs to be redone to make more exact. The way this is now, you could pass with Aa.
		re.ignoreCase = true;
		if (re.exec(amPM) == null)
			return false;
			
	}
	catch (e)
	{
		return false;
	}
	
	
	return true;
}

function IsValidDate(defaultValue){
	//Only accepts date values in the format:  MM/DD/YYYY
	//through the years 1000 to 9999 and checks for valid February end-of-month
	//dates (leap year checking).
	
	try
	{
		if (defaultValue.length != 10)
			return false;
			
		var dateParts = defaultValue.split('/');
		var month = dateParts[0];
		var day = dateParts[1];
		var year = dateParts[2];
		
		
		//Month check
		var re = /^0[1-9]|1[0-2]$/;	
		var monthValue = re.exec(month);	
		if (monthValue == null)
			return false;
		
		
		//Year check
		if (year < parseInt(1000, 10) || year > parseInt(9999, 10))
			return false; 		
		
		
		//Day check
		if (monthValue == "04" || monthValue == "06" || monthValue == "09" || monthValue == "11")
			re = /^0[1-9]|[1|2][0-9]|30$/;				//up to 30 days
		else if (monthValue == "02" && (year/4) != Math.floor(year/4))		//check for leap year on February
			re = /^0[1-9]|[1|2][0-8]$/;					//up to 28 days
		else if (monthValue == "02" && (year/4) == Math.floor(year/4))
			re = /^0[1-9]|[1|2][0-9]$/;					//up to 29 days
		else 
			re = /^0[1-9]|[1|2][0-9]|3[0-1]$/;	//up to 31 days	
		
			
		if (re.exec(day) == null)
			return false;
		
	}
	catch (e)
	{	
		return false;
	}
	
	return true;
}

//-->
