//------------------------------------------------------------------------------
// TestOnline(tm) Common JavaScript methods
// Copyright (C) 2001-2005 J. Cunningham & Assoc.  All rights reserved.
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// Form initialization - set the focus and start the response timer.
//------------------------------------------------------------------------------
var responseTimeStart;
function initializeForm() {
responseTimeStart = new Date();
if ( document.form1 != null ) {
if ( document.form1.loginemailaddress != null ) {
if ( document.form1.loginemailaddress.value == '' ) {
var ea = getCookie("emailAddress");
if ( ea != null && ea.length > 0 ) {
document.form1.loginemailaddress.value = ea;
}
}
try {
document.form1.loginemailaddress.focus();
} catch ( ex ) {
}
} else if ( document.form1.userprofileemailaddress != null ) {
document.form1.userprofileemailaddress.focus();
} else if ( document.form1.fillintheblankfield != null ) {
document.form1.fillintheblankfield.focus();
} else if ( document.form1.questionsearchfor != null
&& document.form1.questionsearchfor.value == '' ) {
document.form1.questionsearchfor.focus();
} else if ( document.form1.viewpromotioncodesfrom != null
&& document.form1.viewpromotioncodesfrom.value == '' ) {
document.form1.viewpromotioncodesfrom.focus();
} else if ( document.form1.editpaymentfor != null
&& document.form1.editpaymentfor.value == '' ) {
document.form1.editpaymentfor.focus();
} else if ( document.form1.savepaymentchangesbutton != null
&& document.form1.editpaymentccswipe != null
&& !document.form1.editpaymentccswipe.disabled ) {
document.form1.savepaymentchangesbutton.focus();
document.form1.editpaymentccswipe.focus();
}
if ( document.form1.windowwidth != null ) {
document.form1.windowwidth.value = document.body.clientWidth;
}
if ( document.form1.windowheight != null ) {
document.form1.windowheight.value = document.body.clientHeight;
}
if ( document.form1.gotocomponent != null ) {
document.form1.gotocomponent.value = "";
}
if ( document.form1.resetscoresforusertopic != null ) {
document.form1.resetscoresforusertopic.value = "";
}
if ( document.form1.viewquestions != null ) {
document.form1.viewquestions.value = "";
}
if ( document.form1.edittopic != null ) {
document.form1.edittopic.value = "";
}
if ( document.form1.movetopic != null ) {
document.form1.movetopic.value = "";
}
if ( document.form1.deletetopic != null ) {
document.form1.deletetopic.value = "";
}
if ( document.form1.edituserprofile != null ) {
document.form1.edituserprofile.value = "";
}
if ( document.form1.edituseroptions != null ) {
document.form1.edituseroptions.value = "";
}
if ( document.form1.viewuserstatistics != null ) {
document.form1.viewuserstatistics.value = "";
}
if ( document.form1.inserttopicbefore != null ) {
document.form1.inserttopicbefore.value = "";
}
if ( document.form1.inserttopicafter != null ) {
document.form1.inserttopicafter.value = "";
}
if ( document.form1.inserttopicbefore != null ) {
document.form1.inserttopicbefore.value = "";
}
if ( document.form1.editpromotioncode != null ) {
document.form1.editpromotioncode.value = "";
}
}
}
//------------------------------------------------------------------------------
// Form submission - stop the response timer.
//------------------------------------------------------------------------------
function stopTimer() {
if ( document.form1.responsetime != null ) {
if ( responseTimeStart != null ) {
responseTimeStop = new Date();
document.form1.responsetime.value = (responseTimeStop.valueOf() - responseTimeStart.valueOf());
} else {
document.form1.responsetime.value = 0;
}
}
if ( document.form1 != null && document.form1.windowwidth != null ) {
document.form1.windowwidth.value = document.body.clientWidth;
}
if ( document.form1.windowheight != null ) {
document.form1.windowheight.value = document.body.clientHeight;
}
return true;
}
//------------------------------------------------------------------------------
// Mark or clear a range of topics
//------------------------------------------------------------------------------
function chooseTopicCopyRange( first, last ) {
var chooseTopicArray = document.form1.choosetopic;
var started = false;
for ( ix = 0; ix < chooseTopicArray.length; ix++ ) {
chooseTopicCheckbox = chooseTopicArray[ix];
if ( chooseTopicCheckbox.value == first ) {
started = true;
var newValue = chooseTopicCheckbox.checked;
} else if ( started  ) {
chooseTopicCheckbox.checked = !chooseTopicCheckbox.disabled && newValue;
}
if ( chooseTopicCheckbox.value == last ) {
return;
}
}
}
//------------------------------------------------------------------------------
// Check for ENTER key being pressed.
//------------------------------------------------------------------------------
function checkForEnterKey(e) {
var keyPressed = window.event ? e.keyCode : e.which;
//var keychar = String.fromCharCode(key);
// alert( keyPressed );
if ( keyPressed == 13 ) {
if ( document.getElementById("enterkeypressed") != null ) {
document.getElementById("enterkeypressed").value = 'yes';
}
}
}
//------------------------------------------------------------------------------
// Sets a cookie.
//------------------------------------------------------------------------------
function setCookie( name, value, expire ) {
document.cookie = name + "=" + escape(value) + ((expire == null) ? "" : ("; expires=" + expire.toGMTString()))
}
//------------------------------------------------------------------------------
// Gets a cookie.
//------------------------------------------------------------------------------
function getCookie(Name) {
var searchString = Name + "=";
if (document.cookie.length > 0) {
offset = document.cookie.indexOf(searchString);
if (offset != -1) {
offset += searchString.length;
end = document.cookie.indexOf(";", offset);
if (end == -1) {
end = document.cookie.length;
}
return unescape(document.cookie.substring(offset, end));
} 
}
}
//------------------------------------------------------------------------------
// Determine offsetX and offsetY of an event (like click) in an element.
//------------------------------------------------------------------------------
function getEventOffsetX( evt ) {
var offX;
if ( window.event ) {
offX = window.event.offsetX;
} else if ( evt.target ) {
offX = evt.clientX;
//alert('offX starts at ' + offX);
var elem = evt.target;
do {
offX -= elem.offsetLeft;
//alert('element ' + elem + ': offX - ' + elem.offsetLeft + ' = ' + offX);
} while ( (elem = elem.offsetParent) );
}
return offX;
}
function getEventOffsetY( evt ) {
var offY;
if ( window.event ) {
offY = window.event.offsetY;
} else if ( evt.target ) {
offY = evt.clientY;
//alert('offY starts at ' + offY);
var elem = evt.target;
do {
offY -= elem.offsetTop;
//alert('element ' + elem + ': offY - ' + elem.offsetTop + ' = ' + offY);
} while ( (elem = elem.offsetParent) );
}
return offY;
}
//------------------------------------------------------------------------------
// Open a pop-up help window.
//------------------------------------------------------------------------------
function helpWindow( url ) {
hw = window.open( url, "_help",
"scrollbars=yes, width=750, height=550, resizable=yes, status=no" );
hw.focus();
}
