changed_flag = false;
function is_changed() {
	if(changed_flag) return;
	var element = document.getElementById('change-detector');
	if(element) {
		element.value=1;
		element.disabled=false;
	}
	changed_flag = true;
}
function wipe_bad_input(id) {
	var current_class = document.getElementById(id).className;
	if(current_class == "bad-input") {
		document.getElementById(id).className = ""
	}
}
function toggle_org_name() {
	var to_be_disabled;
	var select_element = document.getElementById('org-type-select-0') || document.getElementById('org-type-select');
	if(select_element.value=="company") {
		to_be_disabled = false;
	} else {
		to_be_disabled = true;
	}
	document.getElementById('org-name-input-0').disabled=to_be_disabled;
}
function check_domain_input() {
	var domain_val = document.getElementsByName('domain');
	if (domain_val[0].value.length > 0) {
                return true;
        }
        alert('Please enter a domain name to search for.');
        return false;
}
function popUp(URL) {
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=500,height=500,left = 262,top = 134');");
}
function validate_month_field(field_name) {
	var mm_val = document.getElementById(field_name).value;
	mm_val = mm_val.replace(/\D+/g, '');
	if (!mm_val.match(/01|02|03|04|05|06|07|08|09|10|11|12/)) {
		alert("Invalid card details.");
		return false;
	} else {
		document.getElementById(field_name).value = mm_val;
	}
	return true;
}
function validate_cc_fields() {
	var have_startdate = false;
	var have_issuenumber = false;
	var cctype_val = document.getElementById('cctype').value;
	if (!cctype_val.match(/Visa|Visa Electron|Visa Debit|Maestro|Mastercard|Solo/)) {
		alert("Invalid card details.");
		return false;
	}
	var acct_val = document.getElementById('acct').value;
	acct_val = acct_val.replace(/\D+/g, '');
	if (!acct_val.match(/\d+/)) {
		alert("Invalid card details.");
		return false;
	} else {
		document.getElementById('acct').value = acct_val;
	}
	if (validate_month_field('ccexpmm') == false) {
		return false;
	}
	var ccexpyyyy_val = document.getElementById('ccexpyyyy').value;
	ccexpyyyy_val = ccexpyyyy_val.replace(/\D+/g, '');
	if (!ccexpyyyy_val.match(/\d{2}/)) {
		alert("Invalid card details.");
		return false;
	} else {
		document.getElementById('ccexpyyyy').value = ccexpyyyy_val;
	}
        var cvv2_val = document.getElementById('cvv2').value;
        if (cvv2_val != "" && !cvv2_val.match(/\d{3,4}/)) {
                alert("Invalid card details.");
                return false;
        }
	if (validate_month_field('ccstartmm') == false) {
		return false;
	}
        var ccstartyyyy_val = document.getElementById('ccstartyyyy').value;
        ccstartyyyy_val = ccstartyyyy_val.replace(/\D+/g, '');
        if (ccstartyyyy_val != "" && !ccstartyyyy_val.match(/\d{2}/)) {
                alert("Invalid card details.");
                return false;
        } else {
                document.getElementById('ccstartyyyy').value = ccstartyyyy_val;
                if (ccstartyyyy_val != "") {
                        have_startdate = true;
                }
        }
        var issuenumber_val = document.getElementById('issuenumber').value;
        issuenumber_val = issuenumber_val.replace(/\D+/g, '');
        if (issuenumber_val != "" && !issuenumber_val.match(/\d{1,2}/)) {
                alert("Invalid card details.");
                return false;
        } else {
                document.getElementById('issuenumber').value = issuenumber_val;
                if (issuenumber_val != "") {
                        have_issuenumber = true;
                }
        }
        if (cctype_val.match(/Maestro|Solo/)) {
                if (!(have_startdate || have_issuenumber)) {
                        alert("Invalid card details.");
                        return false;
                }
        }
        return true;
}
function append_new_hidden_input_to_form(name, value, form) {
        var new_input = document.createElement('input');
        new_input.type = "hidden";
        new_input.name = name;
        new_input.value = value;
        form.appendChild(new_input);
}
