
function points_total_points(){
var frm = document.points_order_form;
frm.POINTS_QTY.value = frm.points.value;
}


function points_total_order(){
var frm = document.order_form;
frm.POINTS_QTY.value = frm.points.value;
}

var reg1 = /^\w+((-|,|\.|\'|\s)*\w+)*$/;	
var reg_dig = /(\d+|_)/;
var reg_num = /^[0-9]*$/;
var reg_alpha = /^(\w*[0-9]*)*$/;

function formcheck1() {
	var frm = document.order_form;
	var msg_choose_any_product = "Error:\nYou must choose any product to buy it";
	var msg_POINTS_QTY_less = "Error:\nThe minimum quantity of the calling credit is $25.";
	var msg_valid_email = "Error:\nPlease enter a valid E-mail";
	var msg_select_cntry_b = "Error:\nPlease select the country in the billing info";
	var msg_select_cntry_s = "Error:\nPlease select the country in the shipping info";
	var msg_payment = "Error:\nPlease choose the payment method";

	if (frm.ATA10D_QTY.value.length == 0 && frm.ATA10L_QTY.value.length == 0 && frm.ATACOMBO_QTY.value.length == 0 && frm.ATA20L_QTY.value.length == 0 && frm.ATA11L_QTY.value.length == 0 && frm.ATA02L_QTY.value.length == 0 && frm.ATAGSM_QTY.value.length == 0){
		alert(msg_choose_any_product);
		frm.ATA10D_QTY.focus();
		return false;
	}

	if (frm.POINTS_QTY.value > 0 && frm.POINTS_QTY.value < 25){
		alert(msg_POINTS_QTY_less);
		frm.POINTS_QTY.focus();
		frm.POINTS_QTY.select();
		return false;
	}

	//Products Qty. number check
	var aryOrdNumCheck = new Array("ATA10D_QTY","ATA10L_QTY","ATACOMBO_QTY","ATA20L_QTY","ATA11L_QTY","ATA02L_QTY","ATAGSM_QTY","POINTS_QTY");	
	var aryOrdNumCheckLabels = new Array("the product ATA-10D","the product ATA-10L","the product ATA-Combo","the product ATA-20L","the product ATA-11L","the product ATA-02L","the product ATA-GSM","the Calling Credit");
	for ( var i = 0; i < aryOrdNumCheck.length; i ++ ){
		fld = eval("frm." + aryOrdNumCheck[i]);
		if ( fld && fld.value != "" && !reg_num.test(fld.value)) {
			var value = aryOrdNumCheckLabels[i];
			alert('Error:\nThe quantity of ' + value + ' can contain only numbers with no spaces.');
			fld.focus();
			fld.select();
			return false;
		}
	}

	// check for empty fields
	var aryFields = new Array("b_firstname","b_surname","b_email","b_phone","b_mobile","b_city","b_address","s_firstname","s_surname","s_email","s_phone","s_mobile","s_city","s_address");
	var aryLabels = new Array("first name","last name","E-mail","phone","mobile","city","full address","first name","last name","E-mail","phone","mobile","city","full address");
	var aryParts = new Array("billing info","billing info","billing info","billing info","billing info","billing info","billing info","shipping info","shipping info","shipping info","shipping info","shipping info","shipping info","shipping info");
	var icnt = aryFields.length;
	for(var i = 0; i < icnt; i++) {
		fld = eval("frm." + aryFields[i]);
		if (fld.value == "" || fld.value == 0) {
			var value = aryLabels[i];
			var partvalue = aryParts[i];
			alert('Error:\nPlease, enter a value for the ' + value + ' in the ' + partvalue + '.');
			fld.focus();
			return false;
		}
	}

	//len check
	var aryLenCheck = new Array("b_firstname","b_surname","b_phone","b_mobile","b_city","b_address","s_firstname","s_surname","s_phone","s_mobile","s_city","s_address");
	var aryLenCheckLabels = new Array("first name","last name","phone","mobile", "city","full address","first name","last name","phone","mobile", "city","full address");
	var aryMaxLen = new Array( 20, 20, 20, 20, 25, 100, 20, 20, 20, 20, 25, 100);
	var aryMinLen = new Array( 2, 2, 5, 5, 2, 2, 2, 2, 5, 5, 2, 2);
	for (var i = 0; i < aryLenCheck.length; i ++) {
		fld = eval("frm." + aryLenCheck[i]);
		if ( fld && fld.value && fld.value != "" && ( fld.value.length > aryMaxLen[i] ||  fld.value.length < aryMinLen[i] ) ) {
			fld_name_len_check = aryLenCheckLabels[i];
			min_len = aryMinLen[i];
			max_len = aryMaxLen[i];
			if (  aryMaxLen[i] != 0 &&  aryMinLen[i] != 0 ) {
				alert("Error:\n" + fld_name_len_check + " must be between " + min_len + " and "  + max_len + ""); 
			} else if (  aryMinLen[i] != 0 ) {
				alert("Error:\n" + fld_name_len_check + " must be longer than " + min_len +""); 
			} else {
				alert("Error:\n" + fld_name_len_check + " must be no longer than " + max_len +""); 
			}
			fld.focus();
			fld.select();
			return false;
		}	
	}
	//Valid Email
	var reg_email = /^[a-zA-Z0-9.@_-]+[@][a-zA-Z0-9_-]+[.][a-zA-Z0-9._-]+$/;
	fld = frm.b_email;
	if (!reg_email.test(fld.value)) {
		alert(msg_valid_email + ' in the billing info');
		fld.focus();
		fld.select();
		return false;
	}
	fld = frm.s_email;
	if (!reg_email.test(fld.value)) {
		alert(msg_valid_email + ' in the shipping info');
		fld.focus();
		fld.select();
		return false;
	}
	// check country
	fld = frm.b_country;
	if ( !fld || fld.value == "") {
		alert(msg_select_cntry_b + '.');
		fld.focus();
		return false;
	}
	fld = frm.s_country;
	if ( !fld || fld.value == "") {
		alert(msg_select_cntry_s + '.');
		fld.focus();
		return false;
	}
	//check payment method
	if( frm.payment[0].checked == false && frm.payment[1].checked == false && frm.payment[2].checked == false && frm.payment[3].checked == false && frm.payment[4].checked == false){ 
		alert(msg_payment);
		return false;
	}
	return true;
}

function sameinfo(){
	frm = document.order_form;
	if ( frm.same.checked == true ){
		frm.s_firstname.value = frm.b_firstname.value;
		frm.s_surname.value = frm.b_surname.value;
		frm.s_email.value = frm.b_email.value;
		frm.s_country.value = frm.b_country.value;
		frm.s_phone.value = frm.b_phone.value;
		frm.s_mobile.value = frm.b_mobile.value;
		frm.s_city.value = frm.b_city.value;
		frm.s_address.value = frm.b_address.value;
		frm.s_zipcode.value = frm.b_zipcode.value;
		var dialPrefix = document.getElementById('b_dialPrefix').innerHTML;
		sSCountry(dialPrefix.replace("+",""));
	}
	if ( frm.same.checked == false ){
		frm.s_firstname.value = frm.s_surname.value = frm.s_email.value = frm.s_country.value = frm.s_phone.value = frm.s_mobile.value = frm.s_city.value = frm.s_address.value = frm.s_zipcode.value = document.getElementById('s_dialPrefix').innerHTML = document.getElementById('s_dialPrefix2').innerHTML = '';
	}
}

function in_array(needle, haystack){
	var key = ''; 
	for(key in haystack){
		if(haystack[key] == needle){
			return true;
		}
	}
	return false;
}

function formcheck2(type) {
	var frm = ((type == 2)?document.renewForm:document.points_order_form);
	var msg_deviceid_empty = "Error:\nYou must enter your device ID.";
	var msg_deviceid_length = "Error:\nDevice ID must contain 8 digits.";
	var msg_deviceid_error = "Error:\nDevice ID is invalid\nPlease enter valid device ID.";
	var msg_POINTS_QTY_empty = "Error:\nYou must enter a quantity for the Calling Credit.";
	var msg_POINTS_QTY_less = "Error:\nThe minimum quantity of the Calling Credit is $25.";
	var msg_valid_email = "Error:\nPlease enter a valid E-mail.";
	var msg_select_cntry_b = "Error:\nPlease select the country.";
	var msg_payment = "Error:\nPlease choose the payment method";
	var devicesQtyEmptyMsg = "Error:\nPlease select the quantity of devices to renew them.";
	var BESTipIDsSuffix = new Array('10','20','21','28','30','66','51','61','62','63','64','65','69','67');
	var fld='';

	fld = frm.deviceid;
	if (fld.value.length == 0){
		alert(msg_deviceid_empty);
		fld.focus();
		return false;
	}
	if (fld.value.length < 8 || fld.value.length > 8){
		alert(msg_deviceid_length);
		fld.focus();
		fld.select();
		return false;
	}
	if(!reg_num.test(fld.value) || !in_array(fld.value.substr(0,2),BESTipIDsSuffix)){
		alert(msg_deviceid_error);
		fld.focus();
		fld.select();
		return false;
	}
	if(type == 2){
		fld = frm.devicesQty;
		if (!fld || fld.value == "" || fld.value > 5){
			alert(devicesQtyEmptyMsg);
			fld.focus();
			return false;
		}
	}else{
		fld = frm.POINTS_QTY;
		if (fld.value.length == 0){
			alert(msg_POINTS_QTY_empty);
			fld.focus();
			fld.select();
			return false;
		}
		if (fld.value < 25){
			alert(msg_POINTS_QTY_less);
			fld.focus();
			fld.select();
			return false;
		}
	}

	//Products Qty. number check
	var aryOrdNumCheck = new Array("deviceid","POINTS_QTY");
	var aryOrdNumCheckLabels = new Array("The device ID","The quantity of the Calling Credit");
	for ( var i = 0; i < aryOrdNumCheck.length; i ++ ) {
		fld = eval("frm." + aryOrdNumCheck[i]);
		if ( fld && fld.value != "" && !reg_num.test(fld.value)) {
			var value = aryOrdNumCheckLabels[i];
			alert('Error:\n' + value + ' can contain only numbers with no spaces.');
			fld.focus();
			fld.select();
			return false;
		}
	}

	// check for empty fields
	var aryFields = new Array("b_firstname","b_surname","b_email","b_phone","b_mobile","b_city","b_address");
	var aryLabels = new Array("first name","last name","E-mail","phone","mobile","city","full address");
	var icnt = aryFields.length;
	for(var i = 0; i < icnt; i++) {
		fld = eval("frm." + aryFields[i]);
		if (fld.value == "" || fld.value == 0) {
			var value = aryLabels[i];
			alert('Error:\n Please enter a value for the ' + value + '.');
			fld.focus();
			return false;
		}
	}

	//len check
	var aryLenCheck = new Array("b_firstname","b_surname","b_phone","b_mobile","b_city","b_address");
	var aryLenCheckLabels = new Array("first name","last name","phone","mobile","city","full address");
	var aryMaxLen = new Array( 20, 20, 20, 20, 25, 100);
	var aryMinLen = new Array( 2, 2, 5, 5, 2, 2);
	for (var i = 0; i < aryLenCheck.length; i ++) {
		fld = eval("frm." + aryLenCheck[i]);
		if ( fld && fld.value && fld.value != "" && ( fld.value.length > aryMaxLen[i] ||  fld.value.length < aryMinLen[i] ) ) {
			fld_name_len_check = aryLenCheckLabels[i];
			min_len = aryMinLen[i];
			max_len = aryMaxLen[i];
			if (  aryMaxLen[i] != 0 &&  aryMinLen[i] != 0 ) {
				alert("Error:\n" + fld_name_len_check + " must be between " + min_len + " and "  + max_len + ""); 
			} else if (  aryMinLen[i] != 0 ) {
				alert("Error:\n" + fld_name_len_check + " must be longer than " + min_len +""); 
			} else {
				alert("Error:\n" + fld_name_len_check + " must be no longer than " + max_len +""); 
			}
			fld.focus();
			fld.select();
			return false;
		}	
	}
	//Valid Email
	var reg_email = /^[a-zA-Z0-9.@_-]+[@][a-zA-Z0-9_-]+[.][a-zA-Z0-9._-]+$/;
	fld = frm.b_email;
	if (!reg_email.test(fld.value)) {
		alert(msg_valid_email);
		fld.focus();
		fld.select();
		return false;
	}
	//check country
	fld = frm.b_country;
	if ( !fld || fld.value == "") {
		alert(msg_select_cntry_b + '.');
		fld.focus();
		return false;
	}
	//check payment method
	if( frm.payment[0].checked == false && frm.payment[1].checked == false && frm.payment[2].checked == false && frm.payment[3].checked == false && frm.payment[4].checked == false){ 
		alert(msg_payment);
		return false;
	}
	return true;
}

function sBCountry(prefix,type) {
	elementID = ((type==2)?'s':'b')+'_dialPrefix';
	if(document.all){
		element = document.all(elementID);
		element2 = document.all(elementID+'2');
	}else{
		element = document.getElementById(elementID);
		element2 = document.getElementById(elementID+'2');
	}
	element.innerHTML = (prefix)?'+'+prefix:'';
	element2.innerHTML = (prefix)?'+'+prefix:'';
}
function sSCountry(prefix){
	sBCountry(prefix,2);
}
