function checkLoginPasswordForm(form) {

	var rx = /^.*$/

	if(form.elements['login'].value.match(rx) && form.elements['password'].value != '')
		return true;
	else
		return false;
}

function checkLoginForm(form) {
	var quad2 = form.elements['quad2'];
	var quad3 = form.elements['quad3'];
	var quad4 = form.elements['quad4'];
	var rx = /^\d\d\d\d$/
	if(quad2.value.match(rx) &&quad3.value.match(rx) && quad4.value.match(rx)) {
		form.elements['cardNo'].value = '6393'+quad2.value+quad3.value+quad4.value;
		return true;
	}
	else return false;
}

function changeLoginInput(e, o) {
	var elems = o.form.elements;
	var q2 = elems.quad2, q3 = elems.quad3, q4 = elems.quad4;
	var code = e.which==null ? event.keyCode : e.which;
	var codes = {'BKSP':8, 'TAB':9, 'ENTER':13, 'LEFT':37, 'UP':38, 'RIGHT':39, 'DOWN':40, 'DEL':46}

	var atStart = (getSelectionStart(o)==0);
	var atEnd = (o.value.length - getSelectionEnd(o)==0);

	if(o.id=='quad2'){
		if(atEnd && (code==codes.RIGHT || code==codes.DEL || (o.value.length==4 && code!=codes.LEFT && code!=codes.TAB))){
			q3.focus();
			if(code==codes.RIGHT)
				return false;
		}
	}
	else if(o.id=='quad3'){
		if(atStart && (code==codes.BKSP || code==codes.LEFT)){
			q2.focus();
			q2.value = q2.value; // move to the end (for MSIE)
			if(code==codes.LEFT)
				return false;
		}
		if(atEnd && (code==codes.RIGHT || code==codes.DEL || (o.value.length==4 && code!=codes.LEFT && code!=codes.TAB))){
			q4.focus();
			if(code==codes.RIGHT)
				return false;
		}
	}
	else if(o.id=='quad4'){
		if(atStart && (code==codes.BKSP || code==codes.LEFT)){
			q3.focus();
			q3.value = q3.value; // move to the end (for MSIE)
			if(code==codes.LEFT)
				return false;
		}
	}
	return true;
}

function getSelectionStart(o) {
	if(o.createTextRange) {
		var r = document.selection.createRange().duplicate();
		r.moveEnd('character', o.value.length);
		if (r.text == '')
			return o.value.length;
		return o.value.lastIndexOf(r.text);
	}
	else
		return o.selectionStart;
}

function getSelectionEnd(o) {
	if(o.createTextRange) {
		var r = document.selection.createRange().duplicate();
		r.moveStart('character', -o.value.length);
		return r.text.length;
	 }
	 else
	 	return o.selectionEnd;
}

//6393001022223333
function pasteLoginInput(e, o){
	var txt = '', quad2 = '', quad3 = '', quad4 = '';
    if(window.clipboardData)
		txt = window.clipboardData.getData('Text');
	else             
		if(e.clipboardData)
			txt = e.clipboardData.getData('text/plain');
/*
		else
			if (netscape){
				try{
					alert("Now you will be asked for additional privileges to enable Copy/Paste func in Firefox");
					netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
					netscape.security.PrivilegeManager.enablePrivilege("XPCComponentsClasses");

					var clip = Components.classes["@mozilla.org/widget/clipboard;1"].getService(Components.interfaces.nsIClipboard);
					var trans = Components.classes["@mozilla.org/widget/transferable;1"].createInstance(Components.interfaces.nsITransferable);

					if(clip && trans){
						trans.addDataFlavor("text/unicode");  
						clip.getData(trans, clip.kGlobalClipboard);
						var str       = new Object();  
						var strLength = new Object();  

						trans.getTransferData("text/unicode", str, strLength);
						if(str)
							str = str.value.QueryInterface(Components.interfaces.nsISupportsString);  
						if(str)
							txt = str.data.substring(0, strLength.value / 2);  
					}
				} catch(exc){
					console.log(exc);
					return false;
				}
		}
*/
	txt = txt.replace(/[^\d]/g, '');
	if(txt.match(/^639300\d{10}/)){
		quad2 = txt.substr(4, 4);
		quad3 = txt.substr(8, 4);
		quad4 = txt.substr(12, 4);
	}
	else
		if(txt.match(/^\d{8}$/)){
			quad2 = '0000';
			quad3 = txt.substr(0, 4);
			quad4 = txt.substr(4, 4);
		}

	if(quad3!='' && quad4!=0){
		$('quad2').value = quad2;
		$('quad3').value = quad3;
		$('quad4').value = quad4;
		return false; // cancel default action
	}
	return true;
}
