function call_CheckEmail(email) {
	agent.call('', 'CheckEmail', 'callback_CheckEmail', email);
}

function callback_CheckEmail(result) {
	if (result == 'FALSE') {
	  	alert('This email address has been registered before.\nPlease go to "Login" page to recover your information.');
		document['img_email'].src = 'cross.gif';
		document.registration.email1.value = '';
		document.registration.email2.value = '';
		document.registration.email1.focus();
		return false;
	} else {
		document['img_email'].src = 'tick.gif';
	  	return true;
	}
}

function callback_CheckThumbDrive(result) {
	if (result == 'FALSE1') {
		document.registration.serial.value = '';
		document.registration.tdverifycode.value = '';
		document['img_serial'].src = 'cross.gif';
		document['img_verification'].src = 'cross.gif';
	} else if (result == 'FALSE2') {
	 	document.registration.model.value = '23';
		document.registration.serial.value = '';
		document.registration.tdverifycode.value = '';
		document['img_model'].src = 'cross.gif';
		document['img_serial'].src = 'cross.gif';
		document['img_verification'].src = 'cross.gif';
		alert('Your ThumbDrive USB Flash Drive has been registered before.\nPlease write email to administrator@thumbdrive.com if you have any problem.');
	} else {
	 	document.registration.serial.value = result[0];
		document.registration.tdverifycode.value = result[1];
		document['img_serial'].src = 'tick.gif';
		document['img_verification'].src = 'tick.gif';
	}
}

function ModelChange() {
	var box = document.registration.model;
	var number = box.options[box.selectedIndex].value;
	document.registration.serial.value = '';
	document.registration.tdverifycode.value = '';
	document['img_serial'].src = 'cross.gif';
	document['img_verification'].src = 'cross.gif';

	if (number != 19 && number != 20 && number != 21 && number != 27) {
		if (number == 23) {
			document.registration.serial.disabled = true;
			
			if (document.TDLocator) {
				document.TDLocator.setButton(false);				
			}
		} else {
			document.registration.serial.disabled = true;
			
			if (document.TDLocator) {
				document.TDLocator.setButton(true);
			}
		}
	} else {
		document.registration.serial.disabled = false;
		
		if (document.TDLocator) {
			document.TDLocator.setButton(false);
		}
	}
}


function isBlank(str, img) {
	if (str == '') {
		document[img].src = 'cross.gif';
		return false;
	}
	
	document[img].src = 'tick.gif';
	return true;
}

function isSelect(str, img) {
	if (str == '') {
		document[img].src = 'cross.gif';
		return false;
	}
	
	document[img].src = 'tick.gif';
	return true;
}

function isTDSelect(str, img) {
	if (str == '23') {
		document[img].src = 'cross.gif';
		return false;
	}
	
	document[img].src = 'tick.gif';
	return true;
}

function isPassMatch(str1, str2, img) {
	if (str1 == '' || str2 == '') {
		document[img].src = 'cross.gif';
		document.registration.password1.focus();
		return false;
	}
	
	if (str1.length < 8 || str2.length < 8) {
		alert('Your password must be at least 8 characters long.');
		document[img].src = 'cross.gif';
		return false;	
	}

	if (str1.indexOf(' ') > -1 || str2.indexOf(' ') > -1) {
		alert('White space are not allowed.');
		document[img].src = 'cross.gif';
		return false;
	}

	if (str1 != str2) {
		alert('Both passwords don\'t match...');
		document[img].src = 'cross.gif';
		return false;
	}

	document[img].src = 'tick.gif';
	return true;
}

function isEmailMatch(str1, str2, img) {
	if (str1 == '') {
		document[img].src = 'cross.gif';
		return false;
	} else {
		if (isEmail(str1) == false) {
			alert('Invalid email address.');
			document[img].src = 'cross.gif';
			return false;
		}
	}

	if (str2 == '') {
		document[img].src = 'cross.gif';
		return false;
	} else {
		if (isEmail(str2) == false) {
			alert('Invalid email address.');
			document[img].src = 'cross.gif';
			return false;
		}
	}

	if (str1 != str2) {
		alert('Both email addresses don\'t match...');
		document[img].src = 'cross.gif';
		return false;
	}
	
	document[img].src = 'tick.gif';
	return true;
}

function isAlphabet(str, img) {
	if (str == '') {
		document[img].src = 'cross.gif';
		return false;
	}

	for (var i = 0; i < str.length; i++) {
		var alpha = str.charAt(i);
		var code = alpha.charCodeAt(0);

		if ((code > 64 && code < 91) || (code > 96 && code < 123) || (code == 32)) {
			
		} else {
			alert('Please input alphabet and white space only.');
			document[img].src = 'cross.gif';
			return false;
		}
	}
	
	document[img].src = 'tick.gif';
	return true;
}

function isAlphanumeric(str, img) {
	if (str == '') {
		document[img].src = 'cross.gif';
		return false;
	}

	for (var i = 0; i < str.length; i++) {
		var alpha = str.charAt(i);
		var code = alpha.charCodeAt(0);

		if ((code > 64 && code < 91) || (code > 96 && code < 123) || (code > 47 && code < 58) || (code == 95)) {
		
		} else if (code == 32) {
			alert('Whitespace is not allowed.');
			document[img].src = 'cross.gif';
			return false;
		} else {
			alert('Please input alphabet and numeric only.');
			document[img].src = 'cross.gif';
			return false;
		}
	}

	document[img].src = 'tick.gif';
	return true;
}

function isEmail(str) {
	if (str == '') {
		return false;
	}

	var at = '@';
	var dot = '.';
	var lat = str.indexOf(at);
	var lstr = str.length;
	var ldot = str.indexOf(dot);
	
	if (str.indexOf(at) == -1) {
		return false;
	}

	if (str.indexOf(at) == -1 || str.indexOf(at) == 0 || str.indexOf(at) == lstr) {
		return false;
	}

	if (str.indexOf(dot) == -1 || str.indexOf(dot) == 0 || str.indexOf(dot) == lstr) {
		return false;
	}

	if (str.indexOf(at, (lat + 1)) != -1) {
		return false;
	}

	if (str.substring(lat - 1, lat) == dot || str.substring(lat + 1, lat + 2) == dot) {
		return false;
	}

	if (str.indexOf(dot, (lat + 2)) == -1) {
		return false;
	}
	
	if (str.indexOf(" ") != -1) {
		return false;
	}

	call_CheckEmail(str);

	return true;
}

var count = 0;

function validate() {
  	if (isEmailMatch(document.registration.email1.value, document.registration.email2.value, 'img_email') == false) return false;
  	if (call_CheckEmail(document.registration.email1.value) == false) return false;  
	if (isPassMatch(document.registration.password1.value, document.registration.password2.value, 'img_password') == false) return false;
  	
  	if (document.registration.squestion.disabled) {
		if (isSelect(document.registration.squestions.value, 'img_squestion') == false) return false;
	} else {
	    if (isBlank(document.registration.squestion.value, 'img_squestion') == false) return false;
	}
	
	if (isBlank(document.registration.sanswer.value, 'img_sanswer') == false) return false;
	if (isAlphabet(document.registration.fullname.value, 'img_fullname') == false) return false;
	if (isSelect(document.registration.country.value, 'img_country') == false) return false;

	var box = document.registration.model;
	var number = box.options[box.selectedIndex].value;

	if (number != 23) {
		if (isTDSelect(document.registration.model.value, 'img_model') == false) return false;
		if (isAlphanumeric(document.registration.serial.value, 'img_serial') == false) return false;
		if (isSelect(document.registration.pcountry.value, 'img_pcountry') == false) return false;
		
		if (number != 19 && number != 20 && number != 21) {
			if (isBlank(document.registration.tdverifycode.value, 'img_verification') == false) return false;
			
			document.registration.serial.disabled = false;
			document.registration.tdverifycode.disabled = false;		
		}
	}

	if (count == 0) {
		count++;
		document.registration.submit();
	} else {
	 	alert("Please wait, the system is processing your request.");
	}
}

function resetform() {
 	document['img_email'].src = 'cross.gif';
 	document['img_password'].src = 'cross.gif';
 	document['img_squestion'].src = 'cross.gif';
 	document['img_sanswer'].src = 'cross.gif';
 	document['img_fullname'].src = 'cross.gif';
	document['img_country'].src = 'cross.gif';
	document['img_model'].src = 'cross.gif';
	document['img_serial'].src = 'cross.gif';
	document['img_pcountry'].src = 'cross.gif';
	document['img_verification'].src = 'cross.gif';

	document.registration.reset();
}
