function stepKey(nextfield, chars, currfield){
	x = currfield.value.length;
	if (x == chars) {
		o = document.getElementById(nextfield); //changed, not get the webwork's form_id
		if ( o != null)	o.focus();
	}
}

function checkYellowCheckBox(obj) {
    var error = false;
    var msg = '';
	var checked = document.getElementById( obj.id + '_terms');
    if ( checked.checked==false) {
        error = true;
    }
    if (error) {
       document.getElementById('alert').style.display = '';
       return false;
    }
    disPopup();
}

function checkRegister(obj, mobileMsg, operatorMsg, alertMsg1, alertMsg2) {

    var error = false;
    var msg = '';

	if (checkPhone(obj) == false) {
	    msg = msg + "- " + mobileMsg + "\n";
	    error = true;
	}
	if (!checkOperator(obj)) {
		msg = msg + "- " + operatorMsg + "\n";
		error = true;
	}
    if (error) {
        alert(alertMsg1 + "\n\n" + msg + "\n" + alertMsg2);
        return false;
    } else return true;
    disPopup();
}

function checkPhone(obj) {
    var p = obj.elements['msisdn.phone'].value.length;
    var q = obj.elements['msisdn.phone'].value.charAt(0);
    phoneValue = obj.elements['msisdn.phone'].value;
    pattern = /(^[0-9]{5,18})/;

    if (!pattern.test(phoneValue)) { 
        return false;
    } else {
        return true;
    }
}

function checkOperator(obj) {
	var o = obj.elements['operatorId'].value;
	
	if(o>4) {
		return true;
	}
	return false;
}



var secs
var timerID = null
var timerRunning = false
var delay = 1000

function InitializeTimer()
{
    // Set the length of the timer, in seconds
    secs = 15
    StopTheClock()
    StartTheTimer()
}

function StopTheClock()
{
    if(timerRunning)
        clearTimeout(timerID)
    timerRunning = false
}

function hidediv() {
	if (document.getElementById) { // DOM3 = IE5, NS6
			document.getElementById('timermsg1').style.display = 'none';
			document.getElementById('timermsg2').style.display = '';
		}
		else {
		if (document.layers) { // Netscape 4
		document.timermsg1.display = 'none';
		document.timermsg2.display = '';
		}
		else { // IE 4
		document.all.timermsg1.style.display = 'none';
		document.all.timermsg2.style.display = '';
		}
	}
}

function StartTheTimer()
{
    if (secs==0)
    {
        StopTheClock()
		hidediv();
        //alert("You have just wasted 10 seconds of your life.")
    }
    else
    {
        secs = secs - 1
        timerRunning = true
        timerID = self.setTimeout("StartTheTimer()", delay)
    }
}

function isSecondPage(){
	if(document.getElementById('timermsg1') != null){
		InitializeTimer();
	}
}

function unloadPopup(link) {
    //var Popup = window.open('/popup/countdown.html','','height=420,width=760,status=0,toolbar=0,menubar=0,location=0');
    var Popup = window.open(link,'','height=420,width=760,status=0,toolbar=0,menubar=0,location=0');
    Popup.focus();
}

function disPopup(){
	document.body.onunload = "";
	window.onunload= "";
}

function checkPhoneLength(obj) {
//    phoneValue = obj.value;
//    if (phoneValue!="" && phoneValue.charAt(0) == '0') {
//        obj.maxLength = 12;
//    }
//    else{
//    	obj.maxLength = 11;
//    }
}
