// JavaScript Document
function validateForm(){

    var canSubmit = true;
    canSubmit = canSubmit
                          & validatePhoneHome()
						  & validateEmail()
						  & validateProfession()
						  & validateCountry()
						  & validateCity()
						  & validateMailingAddress()
                          & validateFullName()
						  & valradiopayment( document.airtrans ) 
						  & valradiocard( document.airtrans );


    if(canSubmit){
       document.airtrans.submit();
    }

    return false;
}

function valradiopayment(thisform) {
	// place any other field validations that you require here
	// validate myradiobuttons
	myOption = -1;
	for (i=thisform.payment.length-1; i > -1; i--) {
		if (thisform.payment[i].checked) {
			myOption = i; i = -1;
		}
	}

	if (myOption == -1) {		
		 var content = getElementById('divpayment');
		 content.setAttribute('class', 'error');
      	 var errorFullName = getElementById('divpayment');
		 if(errorFullName) {
			errorFullName.innerHTML = 'You must select a payment amount';
		 }	else {
			errorFullName = document.createElement('LABEL');
			errorFullName.setAttribute('id','divpayment');
			errorFullName.setAttribute('class','errorMsg');
			errorFullName.innerHTML = 'You must select a payment amount';
			content.appendChild(errorFullName);
		}
		return false;		
	} else {
		var content = getElementById('divpayment');
		content.setAttribute('class', '');
		var errorFullName = getElementById('divpayment');
		if(errorFullName) {
		    errorFullName.parentNode.removeChild(errorFullName);
		}
	}
	return true;
}

function valradiocard(thisform) {
	// place any other field validations that you require here
	// validate myradiobuttons
	myOption = -1;
	for (i=thisform.radiopago.length-1; i > -1; i--) {
		if (thisform.radiopago[i].checked) {
			myOption = i; i = -1;
		}
	}

	if (myOption == -1) {
		
		 var content = getElementById('divradiopago');
		 content.setAttribute('class', 'error');
      	 var errorFullName = getElementById('divradiopago');
		 if(errorFullName) {
			errorFullName.innerHTML = 'You must select a payment method';
		 }	else {
			errorFullName = document.createElement('LABEL');
			errorFullName.setAttribute('id','divradiopago');
			errorFullName.setAttribute('class','errorMsg');
			errorFullName.innerHTML = 'You must select a payment method';
			content.appendChild(errorFullName);
		}
		return false;
		
	} else {
		var content = getElementById('divradiopago');
		content.setAttribute('class', '');
		var errorFullName = getElementById('divradiopago');
		if(errorFullName) {
		    errorFullName.parentNode.removeChild(errorFullName);
		}
	}
	
	return true;
}


function validateIsEmpty(inputFieldID, contentID, labelErrorID, errorMessage)  {
    var action = false;
    var inputField = getElementById(inputFieldID);

	   var textFullName = inputField.value;

    action = isEmpty(textFullName)
    if (action) {
		     var content = getElementById(contentID);
		     content.className= 'error';

       var errorFullName = getElementById(labelErrorID);
		     if(errorFullName) {
           errorFullName.innerHTML = errorMessage;
       }
       else {
           errorFullName = document.createElement('LABEL');
           errorFullName.setAttribute('id',labelErrorID);
           errorFullName.className='errorMsg';
           errorFullName.innerHTML = errorMessage;
           content.appendChild(errorFullName);
       }

		     inputField.focus();
    }
    else {
        var content = getElementById(contentID);
		      content.className='';
		      var errorFullName = getElementById(labelErrorID);
        if(errorFullName) {
            errorFullName.parentNode.removeChild(errorFullName);
        }
    }
    return !action;
}

function validateIsZipCode(inputFieldID, contentID, labelErrorID, errorMessage)  {
    var action = false;
    var inputField = getElementById(inputFieldID);

	   var textFullName = inputField.value;

    action = !isPostalCode(textFullName)
    if (action) {
		     var content = getElementById(contentID);
		     content.setAttribute('class', 'error');

       var errorFullName = getElementById(labelErrorID);
		     if(errorFullName) {
           errorFullName.innerHTML = errorMessage;
       }
       else {
           errorFullName = document.createElement('LABEL');
           errorFullName.setAttribute('id',labelErrorID);
           errorFullName.setAttribute('class','errorMsg');
           errorFullName.innerHTML = errorMessage;
           content.appendChild(errorFullName);
       }

		     inputField.focus();
    }
    else {
        var content = getElementById(contentID);
		      content.setAttribute('class', '');
		      var errorFullName = getElementById(labelErrorID);
        if(errorFullName) {
            errorFullName.parentNode.removeChild(errorFullName);
        }
    }
    return !action;
}

function validateIsPhoneHome(inputFieldID, contentID, labelErrorID, errorMessage)  {
    var action = false;
    var inputField = getElementById(inputFieldID);

	   var textFullName = inputField.value;

    action = !isPhoneNumber(textFullName)
    if (action) {
		     var content = getElementById(contentID);
		     content.setAttribute('class', 'error');

       var errorFullName = getElementById(labelErrorID);
		     if(errorFullName) {
           errorFullName.innerHTML = errorMessage;
       }
       else {
           errorFullName = document.createElement('LABEL');
           errorFullName.setAttribute('id',labelErrorID);
           errorFullName.setAttribute('class','errorMsg');
           errorFullName.innerHTML = errorMessage;
           content.appendChild(errorFullName);
       }

		     inputField.focus();
    }
    else {
        var content = getElementById(contentID);
		      content.setAttribute('class', '');
		      var errorFullName = getElementById(labelErrorID);
        if(errorFullName) {
            errorFullName.parentNode.removeChild(errorFullName);
        }
    }
    return !action;
}

function validateIsEmail(inputFieldID, contentID, labelErrorID, errorMessage)  {
    var action = false;
    var inputField = getElementById(inputFieldID);

	var textFullName = inputField.value;

    action = !isEmail(textFullName)
    if (action) {
		     var content = getElementById(contentID);
		     content.setAttribute('class', 'error');

       var errorFullName = getElementById(labelErrorID);
		     if(errorFullName) {
           errorFullName.innerHTML = errorMessage;
       }
       else {
           errorFullName = document.createElement('LABEL');
           errorFullName.setAttribute('id',labelErrorID);
           errorFullName.setAttribute('class','errorMsg');
           errorFullName.innerHTML = errorMessage;
           content.appendChild(errorFullName);
       }

		     inputField.focus();
    }
    else {
        var content = getElementById(contentID);
		      content.setAttribute('class', '');
		      var errorFullName = getElementById(labelErrorID);
        if(errorFullName) {
            errorFullName.parentNode.removeChild(errorFullName);
        }
    }
    return !action;
}

function validateIsEcualsEmail(inputFieldID, inputFieldID2, contentID, labelErrorID, errorMessage)  {
    var action = false;
    var inputField = getElementById(inputFieldID);
	var inputField2 = getElementById(inputFieldID2);

	var textFullName = inputField.value;
	var textFullName2 = inputField2.value;

    action = !(textFullName == textFullName2)

    if (action) {
		     var content = getElementById(contentID);
		     content.setAttribute('class', 'error');

       var errorFullName = getElementById(labelErrorID);
		     if(errorFullName) {
           errorFullName.innerHTML = errorMessage;
       }
       else {
           errorFullName = document.createElement('LABEL');
           errorFullName.setAttribute('id',labelErrorID);
           errorFullName.setAttribute('class','errorMsg');
           errorFullName.innerHTML = errorMessage;
           content.appendChild(errorFullName);
       }

		     inputField2.focus();
    }
    else {
        var content = getElementById(contentID);
		      content.setAttribute('class', '');
		      var errorFullName = getElementById(labelErrorID);
        if(errorFullName) {
            errorFullName.parentNode.removeChild(errorFullName);
        }
    }
    return !action;
}

function validateFullName() {
    var inputFieldID = 'fullName';
    var contentID = 'contentFullName';
    var labelErrorID = 'errorFullName';
    var errorMessage = 'Please input your full name.';

    return validateIsEmpty(inputFieldID, contentID, labelErrorID, errorMessage);
}

function validateMailingAddress() {
    var inputFieldID = 'mailingAddress';
    var contentID = 'contentMailingAddress';
    var labelErrorID = 'errorMailingAddress';
    var errorMessage = 'Please input your mailing address.';

    return validateIsEmpty(inputFieldID, contentID, labelErrorID, errorMessage);
}

function validateCity() {
    var inputFieldID = 'city';
    var contentID = 'contentCity';
    var labelErrorID = 'errorCity';
    var errorMessage = 'Please input your city.';

    return validateIsEmpty(inputFieldID, contentID, labelErrorID, errorMessage);
}
function validateCountry() {
    var inputFieldID = 'country';
    var contentID = 'contentCountry';
    var labelErrorID = 'errorCountry';
    var errorMessage = 'Please input your country.';

    return validateIsEmpty(inputFieldID, contentID, labelErrorID, errorMessage);
}

function validateProfession() {
    var inputFieldID = 'profession';
    var contentID = 'contentProfession';
    var labelErrorID = 'errorProfession';
    var errorMessage = 'Please input your profession.';

    return validateIsEmpty(inputFieldID, contentID, labelErrorID, errorMessage);
}

function validatePhoneHome() {
    var inputFieldID = 'phoneHome';
    var contentID = 'contentPhoneHome';
    var labelErrorID = 'errorPhoneHome';
    var errorMessage = 'Please input your Phone Home.';

    action = validateIsEmpty(inputFieldID, contentID, labelErrorID, errorMessage);

	//if(action){
	//	errorMessage = 'Please input your valid Phone Home.';
	//	return validateIsPhoneHome(inputFieldID, contentID, labelErrorID, errorMessage);
	//}
	//else{
		return action;
	//}
}

function validateZipCode() {
    var inputFieldID = 'zipCode';
    var contentID = 'contentZipCode';
    var labelErrorID = 'errorZipCode';
    var errorMessage = 'Please input your postal code.';

    action = validateIsEmpty(inputFieldID, contentID, labelErrorID, errorMessage);

	if(action){
		errorMessage = 'Please input a valid postal code';
		return validateIsZipCode(inputFieldID, contentID, labelErrorID, errorMessage);
	}
	else{
		return action;
	}
}

function validateComfirmEmail(){
	var inputFieldID = 'email';
	var inputFieldID2 = 'confirmEmail';
    var contentID = 'contentConfirmEmail';
    var labelErrorID = 'errorConfirmEmail';
    var errorMessage = 'The emails must be the same.';

	return validateIsEcualsEmail(inputFieldID, inputFieldID2, contentID, labelErrorID, errorMessage);
}



function validateEmail() {
    var inputFieldID = 'email';
    contentID = 'contentEmail';
    labelErrorID = 'errorEmail';
    errorMessage = 'Please input your Email.';

    action = validateIsEmpty(inputFieldID, contentID, labelErrorID, errorMessage);
	
	if(action){
		errorMessage = 'Please input your valid Email.';
		validation = validateIsEmail(inputFieldID, contentID, labelErrorID, errorMessage);
		if(validation){
			return validateComfirmEmail()
		}
		else{
			return validation;
		}
	}
	else{
		return action;
	}
}


function getElementById(id){
    var element = document.getElementById? document.getElementById(id) : document.layers? document.layers[id] : document.all? document.all[id] : null;
    return element;
}