﻿var _licenseNumber = 1;

PageControls.add_ready(function(){ 

    if (PageControls.ddlLicenseNumber != null)
    {
        licenseNumberChanged();
        
        isSecondaryEmailRequired(PageControls.lblSecondaryEmails);
        isSecondaryEmailRequired(PageControls.Label1);
    }
});

function isSecondaryEmailRequired(el) {
	var els = el.getElementsByTagName("em");
	if (els[0] != null)
	{
        els[0].style.display = 'inline';	
	}
}

function IsEmailsDuplicate()
{
    result = false;
    
    document.getElementById("ctl00_ctl00_globalMainContent_mainContent_ctl00_SecondaryEmail2Validator").innerHTML = "";
    document.getElementById("ctl00_ctl00_globalMainContent_mainContent_ctl00_SecondaryEmail1Validator").innerHTML = "";            
     
    if (_licenseNumber == 2)
    {
        if (PageControls.SecondaryEmail1.value == PageControls.Email.value)
        {
            result = true;
            document.getElementById("ctl00_ctl00_globalMainContent_mainContent_ctl00_SecondaryEmail1Validator").innerHTML = "This E-mail address is duplicate!";
        }
    }
    else if (_licenseNumber == 3)
    {
        if (PageControls.SecondaryEmail1.value == PageControls.Email.value ||
            PageControls.SecondaryEmail1.value == PageControls.SecondaryEmail2.value)
        {
            result = true;
            document.getElementById("ctl00_ctl00_globalMainContent_mainContent_ctl00_SecondaryEmail1Validator").innerHTML = "This E-mail address is duplicate!";
        }   
        
        if (PageControls.SecondaryEmail2.value == PageControls.Email.value ||
            PageControls.SecondaryEmail2.value == PageControls.SecondaryEmail1.value)
        {
            result = true;
            document.getElementById("ctl00_ctl00_globalMainContent_mainContent_ctl00_SecondaryEmail2Validator").innerHTML = "This E-mail address is duplicate!";
        }         
    } 
    
    return result;
}

function secondaryEmail1Validate(sender, args)
{
    args.IsValid = true;

    if (_licenseNumber >= 2) 
    {
        if (args.Value.length != 0)
        {
            if (isEmail(args.Value))
            {
                args.IsValid = !IsEmailsDuplicate();
            }
            else
            {
                document.getElementById(sender.id).innerHTML = "Not a valid E-mail address!";        
                args.IsValid = false;
            }
        }
        else
        {
            document.getElementById(sender.id).innerHTML = "Field 'Secondary E-mail 1' cannot be empty!";        
            args.IsValid = false;
        }        
    }
}

function secondaryEmail2Validate(sender, args)
{
    args.IsValid = true;

    if (_licenseNumber >= 3) 
    {
        if (args.Value.length != 0)
        {
            if (isEmail(args.Value))
            {
                args.IsValid = !IsEmailsDuplicate();
            }
            else
            {
                document.getElementById(sender.id).innerHTML = "Not a valid E-mail address!";        
                args.IsValid = false;
            }
        }
        else
        {
            document.getElementById(sender.id).innerHTML = "Field 'Secondary E-mail 2' cannot be empty!";        
            args.IsValid = false;
        }     
    }
}

function isEmail(string)
{
    return (string.toLowerCase().search(/^[+a-z0-9_='%\.-]+\@[a-z0-9_\.-]+\.[a-z]{2,4}$/) != -1);
}

function licenseNumberChanged()
{
	_licenseNumber = parseInt(PageControls.ddlLicenseNumber.options[PageControls.ddlLicenseNumber.selectedIndex].value);
	
	switch(_licenseNumber)
	{
	    default:
	        document.getElementById("secondaryEmail1").style.display = "none";
	        document.getElementById("secondaryEmail2").style.display = "none";
	        break;
	        
	    case 2:
	        document.getElementById("secondaryEmail1").style.display = "block";
	        document.getElementById("secondaryEmail2").style.display = "none";
	        break; 
	        
	    case 3:
	        document.getElementById("secondaryEmail1").style.display = "block";
	        document.getElementById("secondaryEmail2").style.display = "block";
	        break; 	        
	}
}

function platformValidate(sender, args)
{
   if (PageControls.ddlAQtimePLatform.options[PageControls.ddlAQtimePLatform.selectedIndex].value == "")
     args.IsValid = false;
   else
     args.IsValid = true;
}
