function ConstantSumQs(f){
}
function validateQConstantSum(o, n, t){
 if (isNaN(o.value))
 {
   alert("Please enter a number.");
   o.focus();
   o.value="";
   
 }
 var fN = o.name.split('I')[0];
 var result =0;
  var f=o.form;
 for (var i=1; i<=n; i++){
   result +=Number(f[fN+"I"+i].value);
 }
 
 var tip = "sum_"+fN; 
 //$(tip).text(result);
 //alert($(tip).text());
 //alert("sum_"+fN);

 if (result==t) {
   document.getElementById(tip).innerHTML = t;; 
  return;
 } 
 document.getElementById(tip).innerHTML = (result) ;
// alert((t-result) +" remains to be added to "+t);
 //$(tip).html((t-result) +" remains to be added to "+t); 
 //return true;
}
function validateQRankingOrder(o,n){
  return true;
}
//Used to get query field passbyUrl
function getQueryString(id) {
	var url = document.location+''; // Insures string
	q=url.split('?');
	if (q[1]) {
		var pairs = q[1].split('&');
		for (i=0;i<pairs.length;i++) {
			var keyval = pairs[i].split('=');
			if (keyval[0] == id) { var v = keyval[1]; break; }
		}
	}

	if (v) { return v; } else return "";

}

//use for check or unchecked form fields.
function checkAll( allBox, theFieldName)
{
  var f=allBox.form;
  len = f.elements.length;
  var i=0;
  for( i=0 ; i<len ; i++) {
    if (f.elements[i].name==theFieldName) {
      f.elements[i].checked=allBox.checked;
    }
  }
}

 function showHide(id, isShow){
  o = document.getElementById(id);
  if (isShow)
    o.style.display="";
  else
    o.style.display="none";
 }

//Used to check top number of checkbox. To use, onclick="checkTop(3, this)".


 function checkTop(num, o)

 {
  var n=0;
  for (i = 0; i < o.form.elements[o.name].length; i++){
    if (o.form.elements[o.name][i].checked)
      n++;
  }
  if (n>num)
    o.checked=false;

 }

function selectUniqueOne(aField){
// this function will prevent selections with the same value more than once
// suppose chkname is OGS2Q1I, then any field with a name
// that starts with OGS2Q1I (excluding t.name) will be
// checked.
// For group choice question, drop down list, onchange="selectUniqueOne(this)"

  chkname = aField.name.substr(0, aField.name.lastIndexOf("I")+1);
  fields = aField.form.elements;
  var count = 0;
  num = 0;
  //refvalue = aField.value;

  for (i = 0; i < fields.length; i++) {
      // llok for all the related radio boxes.
      if (fields[i].name.indexOf(chkname, 0) != -1 && fields[i].name != aField.name) {
          // now fields[i] points to a related radio box.
          if (fields[i].selectedIndex==aField.selectedIndex) {
          //  alert('You have already selected this rating. Please select another.');
            fields[i].selectedIndex = 0;
            break;
          }
    }
  }
  return;
}


function pickOne(aField){
// this function will prevent selections with the same value more than once
// suppose chkname is OGS2Q1I, then any field with a name
// that starts with OGS2Q1I (excluding t.name) will be
// checked.

  chkname = aField.name.substr(0, aField.name.lastIndexOf("I")+1);
  fields = aField.form.elements;
  var count = 0;
  num = 0;
  refvalue = aField.value;

  for (i = 0; i < fields.length; i++) {
      // llok for all the related radio boxes.
      if (fields[i].name.indexOf(chkname, 0) != -1 && fields[i].name != aField.name) {
          // now fields[i] points to a related radio box.
          if (fields[i].checked==true && fields[i].value == aField.value) {
          //  alert('You have already selected this rating. Please select another.');
            fields[i].checked = false;
            break;
          }
    }
  }
  return;
}

/******************************************************************
   convert_date()

   Function to convert supplied dates to format - dd/mm/yyyy.
        Valid input dates =
                ddmmyy, ddmmmyy, ddmmyyyy, ddmmmyyyy,
                d/m/yy, dd/m/yy, d/mm/yy, dd/mm/yy, d/mmm/yy, dd/mmm/yy,
                d/m/yyyy, dd/m/yyyy, d/mm/yyyy, dd/mm/yyyy, d/mmm/yyyy, dd/mmm/yyyy
        Valid date seperators =
                '-','.','/',' ',':','_',','

        Calls convert_month()
                        invalid_date()
                        validate_date()
                        validate_year()

*******************************************************************/
function convert_date(field1)
{
var fLength = field1.value.length; // Length of supplied field in characters.
var divider_values = new Array ('-','.','/',' ',':','_',','); // Array to hold permitted date seperators.  Add in '\' value
var array_elements = 7; // Number of elements in the array - divider_values.
var day1 = new String(null); // day value holder
var month1 = new String(null); // month value holder
var year1 = new String(null); // year value holder
var divider1 = null; // divider holder
var outdate1 = null; // formatted date to send back to calling field holder
var counter1 = 0; // counter for divider looping
var divider_holder = new Array ('0','0','0'); // array to hold positions of dividers in dates
var s = String(field1.value); // supplied date value variable

//If field is empty do nothing
if ( fLength == 0 ) {
   return true;
}

// Deal with today or now
if ( field1.value.toUpperCase() == 'NOW' || field1.value.toUpperCase() == 'TODAY' ) {

        var newDate1 = new Date();

                  if (navigator.appName == "Netscape") {
                    var myYear1 = newDate1.getYear() + 1900;
                  }
                  else {
                          var myYear1 =newDate1.getYear();
                  }

        var myMonth1 = newDate1.getMonth()+1;
        var myDay1 = newDate1.getDate();
        field1.value = myDay1 + "/" + myMonth1 + "/" + myYear1;
        fLength = field1.value.length;//re-evaluate string length.
        s = String(field1.value)//re-evaluate the string value.
}

//Check the date is the required length
if ( fLength != 0 && (fLength < 6 || fLength > 11) ) {
        invalid_date(field1);
        return false;
        }

// Find position and type of divider in the date
for ( var i=0; i<3; i++ ) {
        for ( var x=0; x<array_elements; x++ ) {
                if ( s.indexOf(divider_values[x], counter1) != -1 ) {
                        divider1 = divider_values[x];
                        divider_holder[i] = s.indexOf(divider_values[x], counter1);
                   //alert(i + " divider1 = " + divider_holder[i]);
                        counter1 = divider_holder[i] + 1;
                        //alert(i + " counter1 = " + counter1);
                        break;
                }
         }
 }

// if element 2 is not 0 then more than 2 dividers have been found so date is invalid.
if ( divider_holder[2] != 0 ) {
   invalid_date(field1);
        return false;
}

// See if no dividers are present in the date string.
if ( divider_holder[0] == 0 && divider_holder[1] == 0 ) {

                //continue processing
                if ( fLength == 6 ) {//ddmmyy
                   day1 = field1.value.substring(0,2);
                     month1 = field1.value.substring(2,4);
                          year1 = field1.value.substring(4,6);
                          if ( (year1 = validate_year(year1)) == false ) {
                           invalid_date(field1);
                                return false;
                                }
                        }

                else if ( fLength == 7 ) {//ddmmmy
                   day1 = field1.value.substring(0,2);
                          month1 = field1.value.substring(2,5);
                          year1 = field1.value.substring(5,7);
                          if ( (month1 = convert_month(month1)) == false ) {
                           invalid_date(field1);
                                return false;
                                }
                          if ( (year1 = validate_year(year1)) == false ) {
                           invalid_date(field1);
                                return false;
                                }
                        }
                else if ( fLength == 8 ) {//ddmmyyyy
                   day1 = field1.value.substring(0,2);
                          month1 = field1.value.substring(2,4);
                          year1 = field1.value.substring(4,8);
                        }
                else if ( fLength == 9 ) {//ddmmmyyyy
                   day1 = field1.value.substring(0,2);
                          month1 = field1.value.substring(2,5);
                          year1 = field1.value.substring(5,9);
                          if ( (month1 = convert_month(month1)) == false ) {
                           invalid_date(field1);
                                return false;
                                }
                        }

                if ( (outdate1 = validate_date(day1,month1,year1)) == false ) {
                   alert("The value " + field1.value + " is not a vaild date.\n\r" +
                        "Please enter a valid date in the format dd/mm/yyyy");
                        field1.focus();
                        field1.select();
                        return false;
                        }

                field1.value = outdate1;
                return true;// All OK
                }

// 2 dividers are present so continue to process
if ( divider_holder[0] != 0 && divider_holder[1] != 0 ) {
          day1 = field1.value.substring(0, divider_holder[0]);
          month1 = field1.value.substring(divider_holder[0] + 1, divider_holder[1]);
          //alert(month1);
          year1 = field1.value.substring(divider_holder[1] + 1, field1.value.length);
        }

if ( isNaN(day1) && isNaN(year1) ) { // Check day and year are numeric
        invalid_date(field1);
        return false;
   }

if ( day1.length == 1 ) { //Make d day dd
   day1 = '0' + day1;
}

if ( month1.length == 1 ) {//Make m month mm
        month1 = '0' + month1;
}

if ( year1.length == 2 ) {//Make yy year yyyy
   if ( (year1 = validate_year(year1)) == false ) {
           invalid_date(field1);
                return false;
                }
}

if ( month1.length == 3 || month1.length == 4 ) {//Make mmm month mm
   if ( (month1 = convert_month(month1)) == false) {
           alert("month1" + month1);
           invalid_date(field1);
           return false;
   }
}

// Date components are OK
if ( (day1.length == 2 || month1.length == 2 || year1.length == 4) == false) {
   invalid_date(field1);
   return false;
}

//Validate the date
if ( (outdate1 = validate_date(day1, month1, year1)) == false ) {
   alert("The value " + field1.value + " is not a vaild date.\n\r" +
        "Please enter a valid date in the format dd/mm/yyyy");
        field1.focus();
        field1.select();
        return false;
}

// Redisplay the date in dd/mm/yyyy format
field1.value = outdate1;
return true;//All is well

}
/******************************************************************
   convert_month()

   Function to convert mmm month to mm month

   Called by convert_date()

*******************************************************************/
function convert_month(monthIn) {

var month_values = new Array ("JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC");

monthIn = monthIn.toUpperCase();

if ( monthIn.length == 3 ) {
        for ( var i=0; i<12; i++ )
                {
           if ( monthIn == month_values[i] )
                   {
                        monthIn = i + 1;
                        if ( i != 10 && i != 11 && i != 12 )
                                {
                           monthIn = '0' + monthIn;
                                }
                        return monthIn;
                        }
                }
        }

else if ( monthIn.length == 4 && monthIn == 'SEPT') {
   monthIn = '09';
   return monthIn;
        }

else {
        return false;
        }
}
/******************************************************************
   invalid_date()

   If an entered date is deemed to be invalid, invali
   d_date() is called to display a warning message to
   the user.  Also returns focus to the date  in que
   stion and selects the date for edit.

   Called by convert_date()

*******************************************************************/
function invalid_date(inField)
{
inField.focus();
inField.select();
alert("The value " + inField.value + " is not in a vaild date format.\n\r" +
        "Please enter date in the format dd/mm/yyyy");
//inField.focus();
//inField.select();
return true
}
/******************************************************************
   validate_date()

   Validates date output from convert_date().  Checks
   day is valid for month, leap years, month !> 12,.


   Notes: Please feel free to use/edit this script.  If you do please keep my comments and details
   intact and notify me via a quick Email to the address above.  Enjoy!
*******************************************************************/
function validate_date(day2, month2, year2)
{
var DayArray = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
var MonthArray = new Array("01","02","03","04","05","06","07","08","09","10","11","12");
var inpDate = day2 + month2 + year2;
var filter=/^[0-9]{2}[0-9]{2}[0-9]{4}$/;

//Check ddmmyyyy date supplied
if (! filter.test(inpDate))
  {
  return false;
  }
/* Check Valid Month */
filter=/01|02|03|04|05|06|07|08|09|10|11|12/ ;
if (! filter.test(month2))
  {
  return false;
  }
/* Check For Leap Year */
var N = Number(year2);
if ( ( N%4==0 && N%100 !=0 ) || ( N%400==0 ) )
          {
   DayArray[1]=29;
          }
/* Check for valid days for month */
for(var ctr=0; ctr<=11; ctr++)
          {
   if (MonthArray[ctr]==month2)
           {
      if (day2<= DayArray[ctr] && day2 >0 )
        {
        inpDate = day2 + '/' + month2 + '/' + year2;
        return inpDate;
        }
      else
        {
        return false;
        }
           }
   }
   return true;
}
/******************************************************************
   validate_year()

   converts yy years to yyyy
   Uses a hinge date of 10
        < 10 = 20yy
        => 10 = 19yy.

   Called by convert_date() before validate_date().


   Notes: Please feel free to use/edit this script.  If you do please keep my comments and details
   intact and notify me via a quick Email to the address above.  Enjoy!
*******************************************************************/
function validate_year(inYear)
{
if ( inYear < 10 )
        {
   inYear = "20" + inYear;
   return inYear;
        }
else if ( inYear >= 10 )
        {
   inYear = "19" + inYear;
   return inYear;
        }
else
        {
        return false;
        }
}


function isInputEmpty(o,warning){
  if (o.value.length==0){alert(warning);  o.focus(); return true;}
}

function checkNum(o,theMin, theMax){
  if(isNaN(o.value)){alert("Number only please!"); o.value=""; return false;}
  else if (parseFloat(o.value)<theMin || parseFloat(o.value)>theMax){alert("The value given is out of range!");o.value=""; return false;}
}

function getRadioValue(radioObject) {
        var value = null;
        for (var i=0; i<radioObject.length; i++) {
        if (radioObject[i].checked) {
                value = radioObject[i].value;
                break;}
        }
        return value;
}

function gotoURL(url){
  location.hash=url; return true;
}

function isRadioNotChecked(o,warning){
  if(getRadioValue(o)==null){alert(warning);return true;}
}

function isNotSelected(o,warning){
  if(o.selectedIndex==0){alert(warning);return true;}
}

function getCookieVal (offset) {
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1)
    endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
}

function FixCookieDate (date) {
  var base = new Date(0);
  var skew = base.getTime(); // dawn of (Unix) time - should be 0
  if (skew > 0)  // Except on the Mac - ahead of its time
    date.setTime (date.getTime() - skew);
}

function GetCookie (name) {
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  while (i < clen) {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg)
      return getCookieVal (j);
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break;
  }
  return null;
}
function SetCookie (name,value,expires,path,domain,secure) {
  document.cookie = name + "=" + escape (value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
}


function DeleteCookie (name,path,domain) {
  if (GetCookie(name)) {
    document.cookie = name + "=" +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

//
//  Examples
//
var expdate = new Date ();
FixCookieDate (expdate); // Correct for Mac date bug - call only once for given Date object!
expdate.setTime (expdate.getTime() + (24 * 60 * 60 * 1000)); // 24 hrs from now

function isValidEmail(o) {
  var locAt;
  var locPeriod;
  var okEmail;
        if (o.value.length==0) {return false;}
  locAt = o.value.indexOf("@");
  okEmail = ((locAt != -1) &&
         (locAt != 0) &&
         (locAt != (o.value.length - 1)) &&
         (o.value.indexOf("@", locAt + 1) == -1)
        );
  if (okEmail) {
    // so far, so good
    locPeriod = o.value.indexOf(".");
    okEmail = ((locPeriod != -1) && (locPeriod != (o.value.length - 1)));
  }
  if ( okEmail==false ) {
     alert("You misspelled the email?");
      o.focus();
  o.select();
  return false;

   }

  return okEmail;
}

function checkOther(a, name, value) {

  els = a.form[name];

  if (els.type=="select-one") {
    for(i=0;i<els.options.length;i++) {
      if (els.options[i].value==value) {
          els.options[i].checked=true;
          els.options[i].selected = true;
          return;
      }
    }
  }
  else {
    for(i=0;i<els.length;i++) {
      if(els[i].type=="checkbox" || els[i].type=="radio") {
        if (els[i].value==value) {
          els[i].checked=true;
          return;
        }
      }
    }
  }

  return;
}


function setParam(fld){
  var str =  location.search.substring(1,location.search.length);
  document.surveyForm[fld].value = str;
}

// added Jan 23, 2002. Updated Feb. 5, 2002.
function checkOtherMissed(){
  var len = document.surveyForm.length;
  var flds = document.surveyForm.elements;
  for(i=0;i<len;i++) {
      if (flds[i].name.indexOf("C", 0)==0 && flds[i].value=="") {
          // found a check box with blank comment. Now parse its corresponding name and value for the radio/check boxes.

          // look for the name of the corresponding radio/check box; note that it can be multi-valued.
          var aname = null;
          for(j=0;j<len;j++) {
              if (flds[i].name.indexOf(flds[j].name)==1) {
                  aname = flds[j].name;
                  if (flds[i].name.charAt(aname.length+1)=='A') break;
              }
          }

         // now parse the answer value in comment that matches the user-selected value
         if (aname!=null) {
           var valOther = flds[i].name.substring(aname.length+2,  flds[i].name.length);

           // look for the object of the corresponding radio/check box;
           // note that it can be multi-valued.
           for(j=0;j<len;j++) {
            if (flds[j].name==aname && flds[j].value==valOther && flds[j].checked) {
              alert("Please specify.");
                 location.href ="#"+aname;
                 return false;
           }
           }
         }
      }
  }
  return true;
}

function skipConditional(aname, avalue, to, otherwise){
  // function to conditionally skip to a bookmark location "to".
  // The condition is a radio string "aname" at a given value.
  // added Feb. 5, 2002. by Deping
  if (getRadioValue(document.surveyForm[aname])==avalue) {
      location.href=to;
  }
  else if (otherwise!="") {
      location.href=otherwise;
  }
}
function readCookie(name)
{
  var cookieValue = "";
  var search = name + "=";
  if(document.cookie.length > 0)
  { 
    offset = document.cookie.indexOf(search);
    if (offset != -1)
    { 
      offset += search.length;
      end = document.cookie.indexOf(";", offset);
      if (end == -1) end = document.cookie.length;
      cookieValue = unescape(document.cookie.substring(offset, end))
    }
  }
  return cookieValue;
}

function writeCookie(name, value, hours)
{
  var expire = "";
  if(hours != null)
  {
    expire = new Date((new Date()).getTime() + hours * 3600000);
    expire = "; expires=" + expire.toGMTString();
	
  }
  document.cookie = name + "=" + escape(value) + expire+"; path=/" ;
}
function redirect(URLStr) { location = URLStr; }

