﻿// JScript File
var emailValidation = new Boolean(true);
var emailMatch = new Boolean(true);


function SubmitContactDetails_Wrapped(_args, _context, m_controlMode)
{
	try
	{
		// Perform form verification before the callback
		if(ContactDetails_FormVerification(m_controlMode))
		{
			SetInnerText(document.getElementById("FormError"),"");
			ContactDetails_CallbackTrigger(m_controlMode);
		}
		else
		{
			//tell the user to fill out the form properly
			var messageDiv = document.getElementById("FormError");
			if(!emailValidation)
			  SetInnerText(messageDiv,"* Please enter a valid Email Address");
			else if (!emailMatch)
				SetInnerText(messageDiv,"* Email Addresses are not the same");
			else
				SetInnerText(messageDiv,"* Please Fill in All the Required Fields");
			emailValidation = true;
			emailMatch = true;
		}
	}
	catch(e)
	{
		alert('ERROR: Sorry, this form is unavailable at this time');
	}
}

function ContactDetails_FormVerification(_controlMode)
{
	// ensure that the necessary fields are filled in
	var digit = 0;
	var tb;
	var email = '';
	for(digit=0; (tb = document.getElementsByTagName("input")[digit]); digit++) 
	{
		if((tb.className == "contactFormTb" || tb.className == "contactFormTbSmall") && tb.value == "")
			return false
		if(tb.id == "ContactDetails1_emailTextBox" || 
		  (_controlMode == 'Communities' && tb.id =='TCC_ctl01_ctl01_ContactDetails1_emailTextBox') )
		{
			// validate as an email
			if(!echeck(tb.value))
			{
				emailValidation = false;
				return false
			}
			email = tb.value;
		}
		if(tb.id == "ContactDetails1_confirmEmailTextBox" ||
		  (_controlMode == 'Communities' && tb.id =='TCC_ctl01_ctl01_ContactDetails1_confirmEmailTextBox') )
		{
			if(tb.value != email)
			{
				emailMatch = false;
				return false
			}
		}
		
	}
	return true;
}

function ContactDetails_CallbackTrigger_Wrapped(cbreference, _controlMode)
{
	try
	{
		var org = "N/A";
		var name = "N/A";
		var email = "N/A";
		var tel = "N/A";
		var desc = "N/A";
		
		// new fields for private space offer form
		var firstname = "N/A";
		var lastname = "N/A";
		var numusers = "N/A";
		var nummonths = "N/A";
		
		//clear the textbox values
		if (_controlMode == 'Communities')
		{
		  if(document.getElementById("TCC_ctl01_ctl01_ContactDetails1_orgTextBox"))
			  org = document.getElementById("TCC_ctl01_ctl01_ContactDetails1_orgTextBox").value;
  			
		  if(document.getElementById("TCC_ctl01_ctl01_ContactDetails1_nameTextBox"))
			  name = document.getElementById("TCC_ctl01_ctl01_ContactDetails1_nameTextBox").value;
  			
		  if(document.getElementById("TCC_ctl01_ctl01_ContactDetails1_emailTextBox"))
			  email = document.getElementById("TCC_ctl01_ctl01_ContactDetails1_emailTextBox").value;
  			
		  if(document.getElementById("TCC_ctl01_ctl01_ContactDetails1_telTextBox"))
			  tel = document.getElementById("TCC_ctl01_ctl01_ContactDetails1_telTextBox").value;
 		}
		else
		{
		  if(document.getElementById("ContactDetails1_orgTextBox"))
			  org = document.getElementById("ContactDetails1_orgTextBox").value;
  			
		  if(document.getElementById("ContactDetails1_nameTextBox"))
			  name = document.getElementById("ContactDetails1_nameTextBox").value;
  			
		  if(document.getElementById("ContactDetails1_emailTextBox"))
			  email = document.getElementById("ContactDetails1_emailTextBox").value;
  			
		  if(document.getElementById("ContactDetails1_telTextBox"))
			  tel = document.getElementById("ContactDetails1_telTextBox").value;
  			
		  if(document.getElementById("ContactDetails1_descTextBox"))
			  desc = document.getElementById("ContactDetails1_descTextBox").value;
  			
		  // new fields for private space offer form
		  if(document.getElementById("ContactDetails1_firstNameTextBox"))
			  firstname = document.getElementById("ContactDetails1_firstNameTextBox").value;

		  if(document.getElementById("ContactDetails1_lastNameTextBox"))
			  lastname = document.getElementById("ContactDetails1_lastNameTextBox").value;

		  if(document.getElementById("ContactDetails1_numberOfUsersTextBox"))
			  numusers = document.getElementById("ContactDetails1_numberOfUsersTextBox").value;

		  if(document.getElementById("ContactDetails1_numberOfMonthsTextBox"))
			  nummonths = document.getElementById("ContactDetails1_numberOfMonthsTextBox").value;
		}
		
		// For Premium Services
		if (_controlMode == 'PremiumServices')
		{
			desc = "Interested in: ";
			for(digit=0; (tb = document.getElementsByTagName("input")[digit]); digit++) 
			{
				if(tb.parentElement.className == "checkboxServices")
				{
					if(tb.checked == true)
					{
						desc += tb.parentElement.parentElement.innerText + " - ";
					}
				}
			}
		}
		
		// build the args string to handle in the C# ProcessAJAXCallback function
		var args = 'Telephone='+tel+'|Email='+email+'|Name='+name+'|Organization='+org+'|Description='+desc+'|FirstName='+firstname+'|LastName='+lastname+'|NumUsers='+numusers+'|NumMonths='+nummonths;
		
		eval(cbreference);
	}
	catch(e)
	{
	}
}

function ContactDetails_CallbackDone_Wrapped(_args, _context)
{
	try
	{
	  // Check for a session time out before completing the callback
	  if (CheckSessionTimeout())
      return;
    
		var digit = 0;
		var tb = 0;
		for(digit=0; (tb = document.getElementsByTagName("input")[digit]); digit++) 
		{
			if(tb.className == "contactFormTb" || tb.className == "contactFormTbSmall" || tb.className == "contactFormTbNotRequired" || tb.className == "contactFormTbNotRequiredSmall")
				tb.value = "";
		}
		
		if(document.getElementById("ContactDetails1_descTextBox"))
			document.getElementById("ContactDetails1_descTextBox").value = "";
			
		// notify user
		SetInnerText(document.getElementById("FormError"),"Thank you " + _args + ", your details are submitted");
	}
	catch(e)
	{
	}
}

function ContactDetails_ProcessCallBackError_Wrapped(_args, _context, client)
{
}

function echeck(str) 
{
	// simple javascript email validation...
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1)
	{
		 return false
	}
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
	{
		 return false
	}
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
	{
			return false
	}
	if (str.indexOf(at,(lat+1))!=-1)
	{
			return false
	}
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
	{
			return false
	}
	if (str.indexOf(dot,(lat+2))==-1)
	{
			return false
	}
	if (str.indexOf(" ")!=-1)
	{
			return false
	}
	return true					
}