var whitespace = " \t\n\r";

function isValidEmail(s)
{   
	if (isEmpty(s)) return false;
   
	if (isWhitespace(s)) return false;

	var i = 1;
	var sLength = s.length;

	while ((i < sLength) && (s.charAt(i) != "@"))
	{ i++
	}

	if ((i >= sLength) || (s.charAt(i) != "@")) return false;
	else i += 2;

	while ((i < sLength) && (s.charAt(i) != "."))
	{ i++
	}

	if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
	else return true;
}

function isEmpty(s)
{   
	return ((s == null) || (s.length == 0))
}

function isWhitespace(s)
{   
	var i;

    if (isEmpty(s)) return true;

    for (i = 0; i < s.length; i++)
    {   
		var c = s.charAt(i);

		if (whitespace.indexOf(c) == -1) return false;
    }
    return true;
}

function checkForm() { 
	if (isWhitespace(document.subscribeForm.elements['AVALA Name'].value)) {
	document.subscribeForm.elements['AVALA Name'].style.backgroundColor='yellow';
		alert("Please enter a Name!");
		document.subscribeForm.elements['AVALA Name'].focus();
		return false;
	}
	if (isWhitespace(document.subscribeForm.elements['Company_Organization'].value)) {
	document.subscribeForm.elements['Company_Organization'].style.backgroundColor='yellow';
		alert("Please enter a Company!");
		document.subscribeForm.elements['Company_Organization'].focus();
		return false;
	}
	if (!isValidEmail(document.subscribeForm.elements['Email Address'].value)) {
	document.subscribeForm.elements['Email Address'].style.backgroundColor='yellow';
		alert("Please enter a valid Email Address. (name@host.com)");
		document.subscribeForm.elements['Email Address'].focus();
		return false;
	}
}

function dates (){
var d = new Date(); //creates a new date
var dd = d.getDate(); //retrieves the date from the system
var mm = d.getMonth() + 1; //retrieves the month JS starts counting at zero, so add a 1 to start January at 1
var yyyy = d.getFullYear(); //retrieves the year
var now = mm + "/" + dd + "/" + yyyy; //formats the date*/

document.subscribeForm.SubscriberDate.value = now; //puts the value retrieved from the system in the sysDt field
}

function APICall (){
//Triggered send external key from application
var triggeredsendexternalkey = "AVALAMarketingLeadEmail"

//Exact Target account number
var ETaccountnumber = 58037;

//Autoresponder Variables
var API1 = "https://api.dc1.exacttarget.com/integrate.aspx?qf=xml&xml=<exacttarget><authorization><username>avalamarketing</username><password>makemefries1%21</password></authorization><system><system_name>triggeredsend</system_name><action>add</action><TriggeredSend+xmlns:xsi='http://www.w3.org/2001/XMLSchemainstance'+xmlns:xsd='http://www.w3.org/2001/XMLSchema'+xmlns='http://exacttarget.com/wsdl/partnerAPI'><TriggeredSendDefinition><CustomerKey>";
var API2 = "</CustomerKey></TriggeredSendDefinition><Subscribers><EmailAddress>";
var email = document.subscribeForm.elements['Email Address'].value;
var API3 = "</EmailAddress><SubscriberKey>";
var API4 = "</SubscriberKey></Subscribers></TriggeredSend></system></exacttarget>";

//Autoresponder XML Build
document.autoresponder.autoresponder.value = API1 + triggeredsendexternalkey + API2 + email + API3 + ETaccountnumber + API4;
}

function submitTwice(f){
f.action = document.subscribeForm.action;
f.target='_self';
f.submit();
f.action = document.autoresponder.autoresponder.value;
f.target='ifr1';
f.submit();
}