function $(id) {
    return document.getElementById(id);
}

function ajax() {
    var ro;
    var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer"){
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    }else{
        ro = new XMLHttpRequest();
    }
    return ro;
}

function formatCurrency(amount) {
    var i = parseFloat(amount);
    if(isNaN(i)) { i = 0.00; }
    var minus = "";
    if(i <0) { minus = '-'; }
    i = Math.abs(i);
    i = parseInt((i + .005) * 100);
    i = i / 100;
    s = new String(i);
    if(s.indexOf('.') <0) { s += '.00'; }
    if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
    s = minus + s;
    return s;
}

function IsNumeric(sText) {
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
}

function account_radio_checker() {
	
	var radio_choice = false;
	
	if(document.acctfinder.household.length == undefined) {
		if (document.acctfinder.household.checked) radio_choice = true;
	} else {
		for (i=0;i<document.acctfinder.household.length;i++) {
			if (document.acctfinder.household[i].checked) radio_choice = true; 
		}
	}
	if (radio_choice != true) {
		alert("Please select an address to verify your account.")
		return (false);
	} else {
		return (true);
	}
}

function checkConfirmForm() {
	var radio = false;
	
	if(document.complain.subnum.length == undefined) {
		if (document.complain.subnum.checked) radio = true;
	} else {
		for (i=0;i<document.complain.subnum.length;i++) {
			if (document.complain.subnum[i].checked) radio = true; 
		}
	}
	
	if (!radio) {
		alert("Please select the subscription that has a problem.")
		return (false);
	} else {
		
		if(document.complain.problem.selectedIndex == 0	) {
			alert("Please select the problem you are experiencing.");
			return false;
		} else {
			return true;	
		}
	}
}

function checkNewSub() {
	var radio = false;
	
	if(document.newsub.sub.length == undefined) {
		if (document.newsub.sub.checked) radio = true;
	} else {
		for (i=0;i<document.newsub.sub.length;i++) {
			if (document.newsub.sub[i].checked) radio = true; 
		}
	}
	
	if (!radio) {
		alert("Please select a newspaper delivery option.")
		return (false);
	} else {
		return true;	
	}
}

function household_radio_checker() {
	
	var radio_choice = false;
	
	if(document.housefinder.house.length == undefined) {
		if (document.housefinder.house.checked) radio_choice = true;
	} else {
		for (i=0;i<document.housefinder.house.length;i++) {
			if (document.housefinder.house[i].checked) radio_choice = true; 
		}
	}
	if (!radio_choice) {
		alert("Please select an address to verify your household.")
		return (false);
	}
	return (true);
}



function findAccount() {
	
	var anumber = $('account').value;
	var pnumber = $('phonenum').value;
	var hnumber = $('housenum').value;
	var zip = $('zip').value;
	
	if(anumber != '') {
		type = 'account';	
	} else if(anumber == '' && pnumber != '') {
		type = 'phone';	
	} else {
		type = 'house';	
	}
	
	if(type == 'house' && hnumber == '' || type == 'house' && zip == '') {
		alert("You must enter a house number AND a zip code");	
	} else {
		$('results').innerHTML = '<img src="images/indicator.gif">';
		var ajx = new ajax;
		ajx.open('get', 'script/ajax.php?action=findaccount&type=' + type + '&anumber=' + anumber + '&pnumber=' + pnumber + '&hnumber=' + hnumber + '&zip=' + zip, true);
		ajx.onreadystatechange = function() {
		   if(ajx.readyState == 4) {
				//alert(ajx.responseText);
				response = ajx.responseText.split('||');
				if(response[1] == 0) {
					$('results').innerHTML = 'We could not find your account.  Please try your search again or contact customer support at 801.204.6100 or 1.800.662.9076<br><br><a href="household.php?sub=no">CLICK HERE IF YOU DO NOT HAVE AN ACCOUNT</a>';	
				} else {
					form = '<form name="acctfinder" method="post" action="confirmaccount.php" onsubmit="return account_radio_checker()">';
					form = form + response[0];
					form = form + '<br><input type="submit" value="confirm household >>" style="padding:0px 10px;">';
					$('results').innerHTML = form;
				}
		   }
		}
		ajx.send(null);
	}
}
function findHousehold() {
	
	var hnumber = $('housenum').value;
	var zip = $('zip').value;
	
	if(hnumber == '' || zip == '') {
		alert("You must enter a house number AND a zip code");		
	} else {
		$('results').innerHTML = '<img src="images/indicator.gif">';
		var ajx = new ajax;
		ajx.open('get', 'script/ajax.php?action=findhousehold&hnumber=' + hnumber + '&zip=' + zip, true);
		ajx.onreadystatechange = function() {
		   if(ajx.readyState == 4) {
				//alert(ajx.responseText);
				response = ajx.responseText.split('||');
				if(response[1] == 0) {
					$('results').innerHTML = 'We could not find your household.  Please search again or...<br><br><a href="household.php?sub=yes">CLICK HERE IF YOU ALREADY HAVE AN ACCOUNT</a>';	
				} else {
					form = '<form name="housefinder" method="post" action="functions/site.php?action=household0&manual=0" onsubmit="return household_radio_checker()">';
					form = form + response[0];
					form = form + '<br><input type="submit" value="confirm household >>" style="padding:0px 10px;">';
					$('results').innerHTML = form;
				}
				$('manualAddress').style.display = 'block';
		   }
		}
		ajx.send(null);
	}
}

function showManualAdd() {
	if($('manualForm').style.display == 'none') {
		$('manualForm').style.display = 'block';	
	} else {
		$('manualForm').style.display = 'none';		
	}
}

function checkManualForm() {
	if($('manaddress').value == '') {
		alert("Please enter your address");
		$('manaddress').focus();
		return false;
	} else if($('mancity').value == '') {
		alert("Please enter your city");
		$('mancity').focus();
		return false;
	} else if($('manzip').value == '') {
		alert("Please enter your zip code");
		$('manzip').focus();
		return false;
	} else {
		return true;	
	}
}

function checkConfirmAcct() {
	
	num = $('numaccounts').value;
	for(i=1;i<num;i++) {
		if($('account' + i).checked == true) {
			return true;
		}
	}
	alert("You must select an account to proceed");
	return false;

}

function processAccount(code,na) {
	var ajx = new ajax;
	ajx.open('get', 'functions/processaccount.php', true);
	ajx.onreadystatechange = function() {
	   if(ajx.readyState == 4) {
		    response = ajx.responseText;
		   	if(code != '' || na == 1) {
				window.location = 'newsub.php';	
			} else {
				if(response == 0) {
		   			window.location = 'main.php';
				} else {
					window.location = 'ebilling.php';
				}
			}
	   }
	}
	ajx.send(null);	
}

function paymentPrice() {
	
	payaccount = $('payment').value * 1;
	paynie = $('niedonation').value * 1;
	paytip = $('carriertip').value * 1;
	
	totalpay = (payaccount + paynie + paytip);
	
	totalpay = formatCurrency(totalpay);
	
	$('amount').value = totalpay;
	
	$('totalPay').innerHTML = '$' + totalpay;
}
function checkVacationForm2() {
	startdate = document.getElementById('startdate').value;
	enddate = document.getElementById('enddate').value;
	
	startdateformat = new Date(startdate);
	enddateformat = new Date(enddate);
	
	if(startdateformat > enddateformat) {
		alert("Your restart date is before your stop date");
		return false;
	}
	
	//alert(startdate + ' ' + enddate);
	
	if(confirm("Are you sure you want to put a vacation hold on your subscriptions from " + startdate + " to " + enddate + "?")) {
		return true;		   
	} else {
		return false;	
	}
}
function checkVacationForm() {
	var currentTime = new Date()
	var month = currentTime.getMonth() + 1
	var day = currentTime.getDate()
	var year = currentTime.getFullYear()
	var today = year + '-' + month + '-' + day;

	var pStart;
	var pEnd;
	var startDates = startCal.getSelectedDates();
	var endDates = endCal.getSelectedDates();
	
	if(startDates == '') {
		alert("Please Select a Start Date for your Vacation Hold");	
		return false;
	} else if(endDates == '') {
		alert("Please Select a End Date for your Vacation Hold");	
		return false;
	}
	
	if(startDates != '') {
		for (var i = 0; i < startDates.length; ++i) {
			var startdate = startDates[i];
		}
		var startMonth = startdate.getMonth() + 1;
		var startYear = startdate.getFullYear();
		var startDay = startdate.getDate();
		
		theStartDate = startYear + '-' + startMonth + '-' + startDay;
		
		$('startdate').value = startYear + '-' + startMonth + '-' + startDay;
		var pStart = new Date();
		pStart.setFullYear(startYear,startMonth,startDay);
	}
	
	
	if(endDates != '') {
		for (var i = 0; i < endDates.length; ++i) {
			var enddate = endDates[i];
		}
		var endMonth = enddate.getMonth() + 1;
		var endYear = enddate.getFullYear();
		var endDay = enddate.getDate();
		
		theEndDate = endYear + '-' + endMonth + '-' + endDay;
		
		$('enddate').value = endYear + '-' + endMonth + '-' + endDay;
		var pEnd = new Date();
		pEnd.setFullYear(endYear,endMonth,endDay);
	}
	
	if(theEndDate <= theStartDate) {
		alert("Your End Date is before your Start Date");
		return false;
	} else {
		return true;
	}

	
}

function setNewSubPrice() {
	$('totalPrice').innerHTML = '<img src="images/indicator.gif">';
	subPrice = $('subprice').value * 1;	
	carrierTip = $('cTip').value;
	if(!IsNumeric(carrierTip)) {
		carrierTip = 0;	
	}
	carrierTip = carrierTip * 1;
	donateLit = $('dLit').value;
	if(!IsNumeric(donateLit)) {
		donateLit = 0;	
	}
	donateLit = donateLit * 1;
	
	total = subPrice + carrierTip + donateLit;
	
	$('totalPrice').innerHTML = '$' + formatCurrency(total);	
}

function whatcomplaint() {
	problemindex = $('problem').selectedIndex;
	p = $('problem')[problemindex].value;

	if(p == 'PL' || p == 'O') {
		$('detailsDiv').style.display = 'block';	
	} else {
		document.complain.details.value = '';
		$('detailsDiv').style.display = 'none';
	}
}

function tdPrice() {
	carrierTip = document.tipdonate.tip.value;
	if(!IsNumeric(carrierTip)) {
		carrierTip = 0;	
	}
	carrierTip = carrierTip * 1;
	document.tipdonate.tip.value = formatCurrency(carrierTip);
	
	donateLit = document.tipdonate.nie.value;
	if(!IsNumeric(donateLit)) {
		donateLit = 0;	
	}
	donateLit = donateLit * 1;
	document.tipdonate.nie.value = formatCurrency(donateLit);
	
	totaltd = carrierTip + donateLit;
	
	$('totalPrice').innerHTML = '$' + formatCurrency(totaltd);
	document.tipdonate.amount.value = formatCurrency(totaltd);
}

function checkTD() {
	if(document.tipdonate.amount.value == 0) {
		alert("Please enter a tip amount or donation amount.");
		return false;
	} else {
		return true;	
	}
}

function checkoutComplete(type,transid) {
	//alert("Working");
	var ajx = new ajax;
	ajx.open('get', 'script/checkoutcomplete.php?type=' + type + '&transid=' + transid, true);
	ajx.onreadystatechange = function() {
	   if(ajx.readyState == 4) {		   
		   if(ajx.responseText != '') {
				//alert(ajx.responseText);   
		   }
	   }
	}
	ajx.send(null);	
}

