// File Name: Validation.js
// Author: Chaman Ipalawatta [chamani@sabretch.com]
// Creation Date: 28-August-2008
// Description: Validates forms for empty values, not a number, email fields etc...

// Special Note: Following Sabre .NET development standards. Minor changes are made!

//////////////////////
//Revision Version  //
//////////////////////
//	Date of Rel	|	Created by	|	Changes
//------------------------------------------
//	28-08-2008		Chaman			Created



//Check whether given html controls value is empty or not...
function isEmpty(controlId, errControlId)
{
	value = true;
	if(controlId.value != '')
	{
		value = false;
	}
	else
	{
		errControlId.innerHTML ="Feild cannot be empty.";
		controlId.focus();
	}
	return value;
}

function isSelected(controlId, errControlId)
{
	value = true;
	if(!controlId.checked)
	{
		errControlId.innerHTML ="Feature must be selected.";
		value = false;
	}
	return value;
}

function isRequired(controlId, errControlId, displayMessage)
{
	if(trim(document.getElementById(controlId).value))
	{		
		document.getElementById(errControlId).innerHTML = "";
		return 0;
	}
	else
	{
		document.getElementById(errControlId).innerHTML = displayMessage;
		return 1;
	}
		
}

function requiredItems(controlArray, errorControlArray, messageArray, validationType)
{
	var count = 0;
	var i = 0;
	var arrayLength = controlArray.length;
	for(i=0; i< arrayLength; i++)
	{
		if(validationType[i] == 1)
		{
			count += isRequired(controlArray[i], errorControlArray[i], messageArray[i]);
		}
		else if(validationType[i] == 2)
		{
			count += emailValidation(controlArray[i], errorControlArray[i], messageArray[i]);
		}
		else if(validationType[i] == 3)
		{
			count += compareValues(controlArray[i], controlArray[i + 1], errorControlArray[i], messageArray[i], messageArray[i + 1]);
			i++;
		}
		else if(validationType[i] == 4)
		{
			count += websiteValidation(controlArray[i], errorControlArray[i], messageArray[i]);
		}
		else if(validationType[i] == 5)
		{			
			count += validateComboBoxes(controlArray[i], errorControlArray[i], messageArray[i]);
		}
		else if(validationType[i] == 6)
		{
			count += validateEmailWithConfirmation(controlArray[i], controlArray[i + 1], errorControlArray[i], messageArray[i], messageArray[i + 1]);
		}		
			
	}
	
	
	if(count > 0)
		return false;
	else
		return true;
}

function emailValidation(controlId, errControlId, displayMessage)
{
	//var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	var reg = /^[a-z0-9_\-]+(\.[_a-z0-9\-]+)*@([_a-z0-9\-]+\.)+([a-z]{2}|aero|arpa|biz|com|coop|edu|gov|info|int|jobs|mil|museum|name|nato|net|org|pro|travel)$/;
	var address = trim(document.getElementById(controlId).value);
	if(address == '')
	{
		document.getElementById(errControlId).innerHTML = displayMessage;
		return 1;
	}
	else if(reg.test(address) == false)
	{
		document.getElementById(errControlId).innerHTML = "Invalid email address";
		return 1;
	}
	else
	{
		document.getElementById(errControlId).innerHTML = "";
		return 0;
	}
}

function websiteValidation(controlId, errorControlId, displayMessage)
{
	//var reg = /(http(s)?:\/\/)?[A-Za-z0-9\.-]{3,}\.[A-Za-z]{3}/;
	var reg = /^((https?|ftp|news):\/\/)?([a-z]([a-z0-9\-]*\.)+([a-z]{2}|aero|arpa|biz|com|coop|edu|gov|info|int|jobs|mil|museum|name|nato|net|org|pro|travel)|(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))(\/[a-z0-9_\-\.~]+)*(\/([a-z0-9_\-\.]*)(\?[a-z0-9+_\-\.%=&amp;]*)?)?(#[a-z][a-z0-9_]*)?$/;
	var value = trim(document.getElementById(controlId).value);
	if(value != "")
	{
		if(reg.test(value) == false)
		{
			document.getElementById(errorControlId).innerHTML = "Invalid website";
			return 1;
		}
		else
		{
			document.getElementById(errorControlId).innerHTML = "";
			return 0;
		}
	}
//	else
//	{
//		document.getElementById(errorControlId).innerHTML = displayMessage;
//		return 1;
//	}
}

function validateEmailWithConfirmation(controlId1, controlId2, errorControlId,displayMessage1,displayMessage2)
{
	if(emailValidation(controlId1,errorControlId,displayMessage1) == 0)
	{
		return	compareValues(controlId1, controlId2, errorControlId, displayMessage1, displayMessage2);
	}
	return 1;
}

function compareValues(controlId1, controlId2, errorControlId, displayMessage1, displayMessage2)
{
	var value1 = trim(document.getElementById(controlId1).value);
	var value2 = trim(document.getElementById(controlId2).value);
	
	if(value1 == '')
	{
		document.getElementById(errorControlId).innerHTML = displayMessage1;
		return 1;
	}
	else if(value1 != value2)
	{		
		document.getElementById(errorControlId).innerHTML = displayMessage2;
		return 1;
	}
	else if(value1 == value2)
	{
		document.getElementById(errorControlId).innerHTML = "";
		return 0;
	}
}

function trim(stringToTrim) 
{
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}


function selectedCount(controlId)
{
    count = 0;
    
    for(i=0 ; i<document.getElementsByName(controlId).length ; i++)
    {
        if(document.getElementsByName(controlId)[i].checked==true)
        {
            count=count+1;
        }
    } 
    
    return count;
}


function validateComboBoxes(controlId, errControlId, displayMessage)
{
	var sel = document.getElementById(controlId);	
	if(sel.options[sel.selectedIndex].value == '0')
	{
		document.getElementById(errControlId).innerHTML = displayMessage;
		return 1;
	}
	else
	{
		document.getElementById(errControlId).innerHTML = '';
		return 0;
	}
		
	//alert(document.getElementById(errControlId).innerHTML);
}
