var emailListHtml = "";

function multiEmail(inputTextArea, submitFormName) {
	var valid = true;

	 var emailsString = document.getElementById(inputTextArea).value;
  
	   if (emailsString != null && emailsString.length > 0) {
	    emailsString = emailsString.replace(/\s*|\r*|\n*/g,'');
	    if (emailsString.length > 0) {
	      var email = emailsString.split(',');
	      for (var i = 0; i < email.length; i++) {
		if (!validateEmail(email[i], 1, 0)) {
		  alert('one or more email addresses entered is invalid.  please separate each with a comma.');
		  valid = false;
		  break;
		}
		}
		    }
	   }
        
	var submitForm = document.getElementById(submitFormName);
	if (valid && submitForm != null)
	{
		
		if ((Form.getInputs(submitFormName, "checkbox","invitedFriends[]").pluck("checked").any()) || ($(inputTextArea).getValue().length > 0) || (Form.getInputs(submitFormName, "checkbox","chkEmails[]").pluck("checked").any())) {
			
		    _gaq.push(['_trackPageview', 'createcompinvite/friendoremailorimport']);
		}
		submitForm.submit();
	}
	return valid;
	
}

function invitePopup(url)
{
	window.open( url, 'Invite', 'width=850, height=520, resizable=yes, scrollbars=yes, status=yes' );
}

function checkImport()
{
	if ($('import_username').value == '')
	{
		alert('please enter a username');
		return false;
	}
	
	if ($('import_password').value == '')
	{
		alert('please enter a password');
		return false;
	}
	
	return true;
}

function checkAddresses()
{
	if ($('emails').value == '')
	{
		alert('please enter at least one email address');
		return false;
	}
	if (!multiEmail('emails', null))
	{
		return false;
	}
	return true;
}

function sendInvitesContacts()
{
	getImportedEmails('chkEmails[]','txtRecipientList');
	var users = document.getElementsByName('chkIbyUsers[]');
	var userList = createList(users);
	if (userList == '' && $('txtRecipientList').value == '')
	{
		alert('you gotta select someone before you send an invite');
		return false;
	}
	$('chkUsers').value = userList;
	return true;
}

function sendInvitesFriends()
{
	var invitedFriends = document.getElementsByName('invitedFriends[]');
	var friendsList = createList(invitedFriends);
	var faveFriends = (document.getElementById('faveFriends') == null ? 0 : (document.getElementById('faveFriends').checked ? 1 : 0));
	var groupId = (document.getElementsByName('invitedGroup') == null ? 0 : getRadioButtonValue(document.getElementsByName('invitedGroup')));
	if (friendsList == "" && faveFriends == 0 && groupId == 0)
	{
	    alert('you gotta select someone before you send an invite');
	    return false;
	}
	$('invitedFriends').value = friendsList;
	return true;
}

function sendInvitesToNewComp(actionUrl)
{
    var recipientList = getImportedEmails('chkEmails[]', false);
    if (recipientList == "")
    {
	    alert('you gotta select someone before you send an invite');
	    return;
    }
    document.getElementById('inviteDiv').style.display = "none";
    document.getElementById('email_list').style.display = "none";
    document.getElementById('inviteWait').style.display = "block";
    
    txtRecipientList = document.getElementById('txtRecipientList');
    if (txtRecipientList != null)
    {
	txtRecipientList.value = recipientList;
    }
    
    document.create.submit();
}

function createList(listControl)
{
    var list = "";
    if (listControl)
    {
	    for (i =0; i < listControl.length; i++)
		{
			if (listControl[i].checked)
			{
				list += listControl[i].value + ",";
			}
		} 
	    list = 	list.substring(0, list.length-1);
    }
    return list;
}

function getRadioButtonValue(control)
{
	for (i = 0; i < control.length; i++)
	{
		if (control[i].checked)
		{
			return control[i].value;
		}
	}
	return 0;
}

function getImportedEmails(checkBoxName, hiddenFieldName)
{
	var check = document.getElementsByName(checkBoxName);
	var emails = "";
	for (i =0; i < check.length; i++)
	{
		if (check[i].checked)
		{
			emails += check[i].value + ",";
		}
	}
	emails =  (emails.length > 0 ? emails.substring(0, emails.length-1) : emails);
	var hiddenField = document.getElementById(hiddenFieldName);
	if (hiddenField != null)
	{
		hiddenField.value = emails;
	}
	return emails;
}


function validateEmail(addr,man,db) {
if (addr == '' && man) {
   if (db) alert('email address is mandatory');
   return false;
}
if (addr == '') return true;
var invalidChars = '\/\'\\ ";:?!()[]\{\}^|';
for (i=0; i<invalidChars.length; i++) {
   if (addr.indexOf(invalidChars.charAt(i),0) > -1) {
      if (db) alert('email address contains invalid characters');
      return false;
   }
}
for (i=0; i<addr.length; i++) {
   if (addr.charCodeAt(i)>127) {
      if (db) alert("email address contains non ascii characters.");
      return false;
   }
}

var atPos = addr.indexOf('@',0);
if (atPos == -1) {
   if (db) alert('email address must contain an @');
   return false;
}
if (atPos == 0) {
   if (db) alert('email address must not start with @');
   return false;
}
if (addr.indexOf('@', atPos + 1) > - 1) {
   if (db) alert('email address must contain only one @');
   return false;
}
if (addr.indexOf('.', atPos) == -1) {
   if (db) alert('email address must contain a period in the domain name');
   return false;
}
if (addr.indexOf('@.',0) != -1) {
   if (db) alert('period must not immediately follow @ in email address');
   return false;
}
if (addr.indexOf('.@',0) != -1){
   if (db) alert('period must not immediately precede @ in email address');
   return false;
}
if (addr.indexOf('..',0) != -1) {
   if (db) alert('two periods must not be adjacent in email address');
   return false;
}
var suffix = addr.substring(addr.lastIndexOf('.')+1);
if (suffix.length != 2 && suffix != 'com' && suffix != 'net' && suffix != 'org' && suffix != 'edu' && suffix != 'int' && suffix != 'mil' && suffix != 'gov' & suffix != 'arpa' && suffix != 'biz' && suffix != 'aero' && suffix != 'name' && suffix != 'coop' && suffix != 'info' && suffix != 'pro' && suffix != 'museum') {
   if (db) alert('invalid primary domain in email address');
   return false;
}
return true;
}
