/*
	
*/
var testing = false;
var appName = 'signUp';
var whichType = 'dealer';

function loadApplication(appNameSet,queryString){
	if(appNameSet.length > 5){
		appName = appNameSet;
	}
	if(document.getElementById('TestOutput')){
		var siteBase = baseURL();
		var divToWrite = '';
		var urlToRun = '';
		if(typeof(queryString)=='undefined'){
			queryString = '';
		}
		urlToRun = '/Applications/emailReminder/' + window.appName + '.aspx' + queryString;
		siteBase = siteBase.toLowerCase();
		xmlhttpPost(urlToRun,'TestOutput');
	}else{
		setTimeout('loadApplication(\'\')',250);
	}
}

loadApplication('');

function setAppNav(){
	if(document.getElementById('appNav')){
		document.getElementById('appNav').style.background = "transparent url(/images/interface/emailReminderNav_" + window.appName + ".gif) no-repeat 0px 0px";
	}else{
		setTimeout('setAppNav()',550);
	}	
}
function signUpInitial(){
	if(testing){
		document.formLoaded.emailId.value = 'gary.ricke@orbisdesign.com';
		document.formLoaded.emailRecipient.value = 'gary@orbisdesign.com';
		document.formLoaded.first_name.value = 'Gary test';
		document.formLoaded.last_name.value = 'Ricke test';
	}
}
function addReminder(){
	var emailId = document.formLoaded.emailId.value;
	var emailRecipient = document.formLoaded.emailRecipient.value;
	var first_name = document.formLoaded.first_name.value;
	var last_name = document.formLoaded.last_name.value;
	var filter = document.formLoaded.filter.options[document.formLoaded.filter.selectedIndex].value;
	var reminderTime = "";
	for(var i = 0; i < document.formLoaded.reminderTime.length; i++) {
		if(document.formLoaded.reminderTime[i].checked == true){
			reminderTime = document.formLoaded.reminderTime[i].value;
		}
	}
	emailId = emailId.toLowerCase();
	emailRecipient = emailRecipient.toLowerCase();
	
	var ValidateVars = '';
		ValidateVars = ValidateVars + 'emailvalidation=emailId=Your email address is required or does not appear to be a valid email|';
		ValidateVars = ValidateVars + 'emailvalidation=emailRecipient=A valid email address is required or does not appear to be a valid email|';
		ValidateVars = ValidateVars + 'emptyvalidation=first_name=First name is required|';
		ValidateVars = ValidateVars + 'emptyvalidation=last_name=Last name is required|';
		ValidateVars = ValidateVars + 'emptyvalidation=filter=Filter choice is required|';
	
	var canWeSend = formvalidation(ValidateVars,'formLoaded');

	if(canWeSend){
		setKookie('emailFilterEmail',emailId)
		var sendData = 
			'emailId=' + emailId + '|' + 
			'emailRecipient=' + emailRecipient + '|' + 
			'first_name=' + first_name + '|' + 
			'last_name=' + last_name + '|' + 
			'filter=' + filter + '|' +
			'reminderTime=' + reminderTime; 	
		
		var queryString = '?var1=' + sendData + '&runFunction=createFilter';
		loadApplication('signUp',queryString)
	}	
}
function addReminderIsDealer(){
	var isDealer;
	for(var i = 0; i < document.formLoaded.visitorType.length; i++) {
		if(document.formLoaded.visitorType[i].checked == true){
			isDealer = document.formLoaded.visitorType[i].value;
		}
	}
	if(isDealer == 'homeOwner'){
		document.getElementById('recipientLabel').innerHTML = "Your Email Address";	
		document.getElementById('dealerEmailLabel').innerHTML = "&nbsp;";
		document.getElementById('emailId').style.visibility = 'hidden';
		document.getElementById('copyDealer').style.display = 'none';
		document.getElementById('copyHomeOwner').style.display = 'block';
		whichType = 'homeOwner';
	}else{
		document.getElementById('recipientLabel').innerHTML = "Recipient Email address";
		document.getElementById('dealerEmailLabel').innerHTML = "Dealer Email address";
		document.getElementById('emailId').style.visibility = 'visible';
		document.getElementById('copyDealer').style.display = 'block';
		document.getElementById('copyHomeOwner').style.display = 'none';		
		whichType = 'dealer';
	}
	reDrawSifr();
}
function deleteReminder(recordId){
	var sendData = recordId;
	var queryString = '?var1=' + sendData + '&runFunction=deleteFilter';
	
	var agree = confirm("Delete this reminder?");
	if (agree){
		loadApplication('viewReminders',queryString);
	}else{
		return false ;
	}
}
function coordinateEmails(){
	if(window.whichType == 'homeOwner'){
		document.formLoaded.emailId.value = document.formLoaded.emailRecipient.value;
	}
}
function viewReminderPreset(){
	var cook = getCookie('emailFilterEmail');
	if(cook != "null"){
		document.formLoaded.emailId.value = cook;
	}	
}
function searchReminders(){
	var searchEmail = document.formLoaded.emailId.value;
	searchEmail = searchEmail.toLowerCase();
	if(searchEmail.length > 3){
		setKookie('emailFilterEmail',searchEmail)
		var sendData = 
		'searchEmail=' + searchEmail;
		var queryString = '?var1=' + sendData + '&runFunction=searchFilters';
		loadApplication('viewReminders',queryString)
	}
}
function formvalidation(ValidateVars,thisform){
	var string = "";
	var els = document.forms[thisform].elements;
	for(var no=0;no<els.length;no++){
	    string+="Name: "+els[no].name + ", Value: "+els[no].value + "\n";
	}
	var ValidateVarsArray = ValidateVars.split('|');
	with (thisform)
	{
		for (var i=0;i<ValidateVarsArray.length-1;i++) {
			var ValidateVarPieces = ValidateVarsArray[i].split('=');
			if(typeof(eval('document.' + thisform + '.' + ValidateVarPieces[1])) != 'undefined'){			
				if(eval(ValidateVarPieces[0] + '(document.' + thisform + '.' + ValidateVarPieces[1] + ',"' + ValidateVarPieces[2] + '")')==false){
					eval('document.' + thisform + '.' + ValidateVarPieces[1]).focus();
					return false;
				}
			}else{
				alert('FormValidationVars ERROR: \r Trying to validate >>>' + ValidateVarPieces[1] + '<<< which is not in this form \r \r Check that the validation var is spelled correctly  \r Or \r Remove the validation check \r Or \r Add the form element that is missing');
				return false;
			}
		}
		return true;
	}	
}