var agt=navigator.userAgent.toLowerCase();
var IE = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));

//	This timer will be used to determine if we want to load ajax pages.
//	We will use it to know if we have used mouse over and mouse out before getting reply.
//	If we move the mouse out we will skip the request
var timer;

function toggle( obj ){

	//	no object was found. do nothing
	if ( !obj ){
		return;
	}

	//	now check to see what is the status of the object
	if ( obj.style.display == "none" ){
		obj.style.display = "block";
	}
	else{
		obj.style.display = "none";
	}

}

function checkMaxLength(){

	var obj = arguments[0];
	var maxLength = arguments[1];

	if ( !obj.value){
		return;
		}

	if (obj.value.length > maxLength){
		obj.value = obj.value.substring( 0, maxLength-1);
		obj.focus();
		}
	else{
		$(obj.name + '_length' ).innerHTML= '<b>' + obj.value.length +'</b>';
	}
}

function checkLengthBounds( obj, minLength, maxLength){

	if (obj.value.length < minLength || obj.value.length > maxLength){
		obj.style.background='red';
		obj.style.color='yellow';
		}
	else{
		obj.style.background='white';
		obj.style.color='black';
		}
	$(obj.name + '_length' ).innerHTML= '<b>' + obj.value.length +'</b>';

}

function displayLength( objId ){
	var obj = document.getElementsByName( objId )[0];
	$(obj.name + '_length' ).innerHTML= '<b>' + obj.value.length +'</b>';
}

String.prototype.trim = function() {
	a = this.replace(/^\s+/, '');
	return a.replace(/\s+$/, '');
	};


var currentLine;
var currentLineClass;

function markLine( src ){

	if ( currentLine ){
        currentLine.className = currentLineClass;
    }

	currentLineClass = src.className;
    src.className = 'trMark' ;
    currentLine= src;

}

function swapSingleSelection( obj ){

	//	get the name of the object
	var objName = obj.name;

	//	get all object with same name
	var elements = document.getElementsByName( objName );

	//	Remove "old" selections
	for (ind in elements){
		elements[ind].checked = false;
	}

	//	Mark the current element
	obj.checked = true;

}

function setValues( fieldsName , value ){

	//	get all object with same name
	var elements = document.getElementsByName( fieldsName );

	//	Reset "old" values
	for (ind in elements){
		elements[ind].value = value;
	}

}

function setScores( obj, resetFieldName, targetFieldId ){
	swapSingleSelection( obj );
	setValues( resetFieldName , '0');
	$( targetFieldId ).value = 1;
}

// Confirm delete of credit card
function confirmCCDelete(ccId, message ){
	if (confirm( message )){
		window.location.href='ccDelete.do?id=' + ccId;
		}
	}

// Confirm default credit card
function confirmDefaultCC( ccId, message ){
	if (confirm( message )){
		window.location.href='ccDefault.do?id=' + ccId;
		}
	}

function checkSubmit(e) {

	var keyCode = e ? e.which: window.event.keyCode;;
	var userName;

	if( keyCode == 13){
		setUserName();
		document.forms[0].submit();
		}
	}

if (document.layers){
	document.captureEvents(Event.KEYPRESS);
}

function setUserName(){
	//	Check to see if we are in the login with phone prefix or not
	if ( $('operatorCode')){
		var userName = $N( 'j_username')[0];
		userName.value = '972' + $('operatorCode').value + $('shortPhone').value;
	}
}

///
// This funtion will fill the end time with 2 hours ahead.
//
function fillEndTime(){
	var startTime = $N( 'startTimeStamp' )[0];

	if (startTime.value.length == 0 ){
		return alert('Please fill start date first ... ');
	}

	//	The end time field
	var endTimeStamp = $N( 'endTimeStamp' )[0];

	//	Process the startTime
	var startTime = startTime.value;
	var date = startTime.split(' ')[0].split('/');
	var time = startTime.split(' ')[1].split(':');

	// The time format is this:
	// dd/MM/yyyy hh:mm:ss
	var endTime = new Date();
	endTime.setDate(date[0]);
	endTime.setMonth(date[1] - 1);
	endTime.setFullYear(date[2]);
	endTime.setHours(time[0]);
	endTime.setMinutes(time[1]);
	endTime.setSeconds(time[2]);

	//	we set the time to be 2 hours ahead.
	var timeDiffrence = 2 * 60 * 60 * 1000;
	endTime.setTime( endTime.getTime() + timeDiffrence);

	$N( 'endTimeStamp' )[0].value = "" +
									(endTime.getDate() < 10 ? '0' : '') +  endTime.getDate() + '/' +
									(endTime.getMonth() < 9 ? '0' : '' ) +  (endTime.getMonth() + 1) + '/' +
									endTime.getFullYear() + ' ' +
									(endTime.getHours() <  10 ? '0' : '') +  endTime.getHours() + ':' +
									(endTime.getMinutes() < 10 ? '0' : '') +  endTime.getMinutes() + ':' +
									(endTime.getSeconds() < 10 ? '0' : '') +  endTime.getSeconds() ;
}

function swapHiddenRows( show ){

	var list = document.getElementsByClassName('hiddenSection');
	var displayValue = 'none';


	// for game type we want to display game options
	if ( show ){
		if (IE)	displayValue = 'block';
		else	displayValue = 'table-row';
	}

	// Show hide the game section
	for (var ind=0; ind < list.length; ind++){
		list[ind].style.display=displayValue;
		}
}
