
var changesMade = false;

function checkDisable(title) {
	if(title == 'ot') document.giftCertForm.additional.disabled = false;
	else document.giftCertForm.additional.disabled = true;
}

function copyBillingInfo() {
	setValue( "billAddress1", getValue( "sendAddress1" ) );
	setValue( "billAddress2", getValue( "sendAddress2" ) );
	setValue( "billCity", getValue( "sendCity" ) );
	setValue( "billState", getValue( "sendState" ) );
	setValue( "billZip", getValue( "sendZip" ) );
	setValue( "billCountry", getValue( "sendCountry" ) );
	setValue( "billPhone", getValue( "sendPhone" ) );
}

function getFormProblems() {
	var errorList = "";
	if( getValue( "additional" ).trim().length > 0 ) {
		if( !getValue( "additional" ).trim().isNumeric() ) errorList += "&bull; Additional amount for certificate must be numeric<br />";
	}
	if( !getValue( "sendEmail" ).isValidEmail() ) errorList += "&bull; Sender email address is invalid<br />";
	// recipient info
	if( getValue( "recvName" ).trim().length == 0 ) errorList += "&bull; Recipient name cannot be blank<br />";
	if( getValue( "recvAddress1" ).trim().length == 0 ) errorList += "&bull; Recipient address cannot be blank<br />";
	if( getValue( "recvCity" ).trim().length == 0 ) errorList += "&bull; Recipient city cannot be blank<br />";
	if( getValue( "recvState" ).trim().length == 0 ) errorList += "&bull; Recipient state cannot be blank<br />";
	if( getValue( "recvZip" ).trim().length == 0 ) errorList += "&bull; Recipient zip code cannot be blank<br />";
	// sender info
	if( getValue( "sendName" ).trim().length == 0 ) errorList += "&bull; Sender name cannot be blank<br />";
	if( getValue( "sendAddress1" ).trim().length == 0 ) errorList += "&bull; Sender address cannot be blank<br />";
	if( getValue( "sendCity" ).trim().length == 0 ) errorList += "&bull; Sender city cannot be blank<br />";
	if( getValue( "sendState" ).trim().length == 0 ) errorList += "&bull; Sender state cannot be blank<br />";
	if( getValue( "sendZip" ).trim().length == 0 ) errorList += "&bull; Sender zip code cannot be blank<br />";
	// card and billing info
	if( getValue( "ccName" ).trim().length == 0 ) errorList += "&bull; Shipping name cannot be blank<br />";
	if( !isChecked( "ccSame" ) ) {
		// if the card billing info is not the same as the sender info, verify the fields
		if( getValue( "billAddress1" ).trim().length == 0 ) errorList += "&bull; Card address cannot be blank<br />";
		if( getValue( "billCity" ).trim().length == 0 ) errorList += "&bull; Card city cannot be blank<br />";
		if( getValue( "billState" ).trim().length == 0 ) errorList += "&bull; Card state cannot be blank<br />";
		if( getValue( "billZip" ).trim().length == 0 ) errorList += "&bull; Card zip/postal code cannot be blank<br />";
		if( getValue( "billPhone" ).trim().length == 0 ) errorList += "&bull; Card phone cannot be blank<br />";
		if( getSelectedValue( "billCountry" ).trim().length == 0 ) errorList += "&bull; Card country must be selected<br />";
	}
	if( !getValue( "ccNumber" ).isValidCCNumber() ) errorList += "&bull; Card number is not valid<br />";
	if( getValue( "ccCode" ).trim().length == 0 || !getValue( "ccCode" ).isNumeric() ) errorList += "&bull; Card verification code is invalid<br />";
	return errorList;
}

function validateForm() {
	// first, if the shippingSame checkbox is checked, make sure shipping fields are dups of billing
	if( isChecked( "ccSame" ) ) copyBillingInfo();
	// now, verify fields are OK
	var errorList = "";
	// a field called this, so something was messed with
	changesMade = true;
	// call the actual field validator function
	errorList = getFormProblems();
	if( errorList.length == 0 ) {
		//enableElement( "submitButton" );
	} else {
		//disableElement( "submitButton" );
	}
	return errorList.length == 0;
}

function toggleSame() {
	if( !isElementVisible( 'ccBillInfo' ) ) {
		// copy info over before making visible
		copyBillingInfo();
	}
	toggleElementVisibility( 'ccBillInfo' );
}

function submitGiftRequest() {
	document.getElementById( 'giftCertForm' ).submit();
}