// JavaScript Document

function get_cc_input(cc_validate) {
	var cc_content;
	if(cc_validate == null) {
		cc_validate = true;	
	}
	var m;
	cc_content = '<div id="creditcard_input">';
	cc_content += '<h2>Make Payment and complete the transaction</h2>';
	cc_content += '<span id="cc_error"></span>';
	cc_content += '<form name="frm_cc" id="frm_cc" action="'+page_name()+'" method="post" onsubmit="return false;">';
	cc_content += '<table border="1">';
	cc_content += '<tr>';
	cc_content += '<td>Card Holder Name:</td>';
	cc_content += '<td><input type="text" name="CC_name" id="CC_name" maxlength="16" /></td>';
	cc_content += '</tr>';
	cc_content += '<tr>';
	cc_content += '<td>Card Type: </td>';
	cc_content += '<td>';
	cc_content += '<select name="CC_type" id="CC_type">';
	cc_content += '<option value="">--Select--</option>';
	cc_content += '<option value="MasterCard">MasterCard</option>';
	cc_content += '<option value="Visa">Visa</option>';
	cc_content += '<option value="AmEx">AmEx</option>';
	cc_content += '<option value="Discover">Discover</option>';
	cc_content += '</select>';
	cc_content += '</td>';
	cc_content += '</tr>';
	cc_content += '<tr>';
	cc_content += '<td>Card Expiry Month: </td>';
	cc_content += '<td>';
	cc_content += '<select name="CC_month" id="CC_month">';
	cc_content += '<option value="">--Month--</option>';
	for(i=1; i < 13; i++) {
		m = i;
		if(i < 10) {
			m = '0'+i;	
		}
		cc_content += '<option value="'+i+'">'+m+'</option>';	
	}
	cc_content += '</select>';
	cc_content += '<select name="CC_year" id="CC_year">';
	cc_content += '<option value="">--Year--</option>';
	y = new Date();
	this_year = y.getYear();
	this_year = parseInt(this_year);
	to_year = this_year + 20;
	for(i = this_year; i < to_year; i++) {
		cc_content += '<option value="'+i+'">'+i+'</option>';		
	}
	cc_content += '</select>';
	cc_content += '</td>';
	cc_content += '</tr>';
	cc_content += '<tr>';
	cc_content += '<td>Card Code: </td>';
	cc_content += '<td><input type="text" name="CC_code" id="CC_code" size="3" maxlength="4" /></td>';
	cc_content += '</tr>';
	cc_content += '<tr colspan="2">';
	if(!cc_validate) {
		cc_content += '<td><input type="submit" name="CC_submit" id="CC_submit" value="Checkout" /></td>';
	} else {
		cc_content += '<td><input type="button" name="CC_submit" id="CC_submit" value="Checkout" onclick="validate_cc()" /></td>';
	}
	cc_content += '</table>';
	cc_content += '<input type="hidden" name="cc_submit" id="cc_submit" value="normal" />';
	cc_content += '</form>';
	cc_content += '<iframe name="cc_iframe" id="cc_iframe" style="display:none"></iframe>';
	cc_content += '</div>';
	return cc_content;
}

function validate_cc() {
/*	for(i=0; i < document.forms.length; i++) {
		alert(document.forms[i].name);
	}*/
	var cc_name = document.frm_cc.CC_name.value;
	var cc_number = document.frm_cc.CC_number.value;
	var cc_type = document.frm_cc.CC_type.value;
	var cc_month = document.frm_cc.CC_month.value;
	var cc_year = document.frm_cc.CC_year.value;
	var cc_code = document.frm_cc.CC_code.value;
	
	var err_flag = 0;
	if(trim(cc_name) == '') {
		$('cc_name_error').innerHTML = 'Please input Card holders Name.';	
		err_flag = 1;
	} else {
		$('cc_name_error').innerHTML = '';
	}
	
	if(trim(cc_number) == '') {
		$('cc_number_error').innerHTML = 'Please input the Credit Card Number';	
		err_flag = 1;
	} else if(!IsNumeric(cc_number)) {
		$('cc_number_error').innerHTML = 'Card number should be only numeric';
		err_flag = 1;
	} else {
		$('cc_number_error').innerHTML = '';	
	}
	
	if(cc_type == '') {
		$('cc_type_error').innerHTML = 'Please select the Credit card type.';	
		err_flag = 1;
	} else {
		$('cc_type_error').innerHTML = '';	
	}
	
	if(cc_month == '' || cc_year == '') {
		$('cc_expiry_error').innerHTML = 'Please select the card expiry month and year.';	
		err_flag = 1;
	} else {
		$('cc_expiry_error').innerHTML = '';
	}
	
	if(cc_code == '') {
		$('cc_code_error').innerHTML = 'Please input the code behind the card';	
		err_flag = 1;
	} else if(!IsNumeric(cc_code)) {
		$('cc_code_error').innerHTML = 'Card code should be only numeric';
		err_flag = 1;
	} else {
		$('cc_code_error').innerHTML = '';	
	}
	
	if(err_flag == 0) {
		if(checkCreditCard(cc_number, cc_type)) {
			document.getElementById('cc_error').innerHTML = '';
			document.frm_cc.target = 'cc_iframe';
			document.getElementById('CC_processing').style.display = 'block';
			document.frm_cc.CC_submit.disabled = true;
			document.frm_cc.submit();
		} else {
			document.getElementById('cc_error').innerHTML = ccErrors[ccErrorNo];
			return false;
		}
	} else {
		return false;	
	}
}