/* BEGINNING Cookies get/set/delete */

function Set_Cookie( name, value, expires, path, domain, secure ) 
{
// set time, it's in milliseconds
  var today = new Date();
  today.setTime( today.getTime() );

/*
if the expires variable is set, make the correct 
expires time, the current script below will set 
it for x number of days, to make it for hours, 
delete * 24, for minutes, delete * 60 * 24
*/
  if ( expires )
  {
    expires = expires * 1000 * 60 * 60 * 24;
  }
  
  var expires_date = new Date( today.getTime() + (expires) );

  document.cookie = name + "=" +escape( value ) +
    ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
    ( ( path ) ? ";path=" + path : "" ) + 
    ( ( domain ) ? ";domain=" + domain : "" ) +
    ( ( secure ) ? ";secure" : "" );
}

// this function gets the cookie, if it exists
function Get_Cookie( name ) {
	
    var start = document.cookie.indexOf( name + "=" );
    var len = start + name.length + 1;

    if ( ( !start ) &&  ( name != document.cookie.substring( 0, name.length ) ) )
    {
        return null;
    }

    if ( start == -1 ) return null;

    var end = document.cookie.indexOf( ";", len );

    if ( end == -1 ) end = document.cookie.length;

    return unescape( document.cookie.substring( len, end ) );
}
	
// this deletes the cookie when called
function Delete_Cookie( name, path, domain ) 
{
  if ( Get_Cookie( name ) )   
    document.cookie = name + "=" + ( ( path ) ? ";path=" + path : "") + ( ( domain ) ? ";domain=" + domain : "" ) + ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

/* END Cookies get/set/delete */

/* BEGINNING Formatting of fields for Online forms */

function formatPostalcode(field) {
    field.value = trim(field.value.toUpperCase());   
    
    var ov = field.value;
    
    if (ov.length == 6 && ov.search(/^[a-zA-Z]\d[a-zA-Z]\d[a-zA-Z]\d$/) != -1) {
        field.value = ov.substring(0, 3) + " " + ov.substring(3, 6);
    }
    else if (ov.length == 7 && ov.search(/^[a-zA-Z]\d[a-zA-Z]\s\d[a-zA-Z]\d$/) != -1) {
        return true;
    }
    else if (ov.length > 0){
        alert("Invalid Postal Code format. Please use X1X 1X1");
    }
}

function formatPhone (field) {
    field.value = trim(field.value);

    var ov = field.value;
    var v = "";
    var x = -1;

    // is this phone number 'escaped' by a leading plus?
    if (0 < ov.length && '+' != ov.charAt(0)) { // format it
        // count number of digits
        var n = 0;
        if ('1' == ov.charAt(0)) {  // skip it
            ov = ov.substring(1, ov.length);
        }

        for (i = 0; i < ov.length; i++) {
            var ch = ov.charAt(i);

            // build up formatted number
            if (ch >= '0' && ch <= '9') {
                if (n == 0) v += "(";
                else if (n == 3) v += ") ";
                else if (n == 6) v += "-";
                v += ch;
                n++;
            }
            // check for extension type section;
            // are spaces, dots, dashes and parentheses the only valid non-digits in a phone number?
            if (! (ch >= '0' && ch <= '9') && ch != ' ' && ch != '-' && ch != '.' && ch != '(' && ch != ')') {
                x = i;
                break;
            }
        }
        // add the extension
        if (x >= 0) v += " " + ov.substring(x, ov.length);

        // if we recognize the number, then format it
        if (n == 10 && v.length <= 40) field.value = v;
    }
    return true;
}

function trim(st) {
    var len = st.length
    var begin = 0, end = len - 1;
    while (st.charAt(begin) == " " && begin < len) {
        begin++;
    }
    while (st.charAt(end) == " " && begin < end) {
        end--;
    }
    return st.substring(begin, end+1);
}

/* END Formatting of fields for Online forms */

/* BEGINNING of Form validation */
/* moved to TP Online
function validateProfileFormOnSubmit(theForm) {
    var reason = "";

    reason += validateEmpty(theForm.FNAME, "First Name");   
    reason += validateEmpty(theForm.LNAME, "Last Name");
    
    if (theForm.ORGPARENT_NO.options[theForm.ORGPARENT_NO.selectedIndex].value == 'Other'){
        reason += validateEmpty(theForm.ORGNAME, "Organization");
        }
    else {
        reason += validateEmpty(theForm.ORG_NO, "Organization");
        }
        
    reason += validateEmpty(theForm.POSITIONTITLE, "Position");    
    reason += validateEmail(theForm.USERNAME, "Email");    
    reason += validateEmail(theForm.EMAIL, "Confirm Email");
    reason += validateMatchingFields(theForm.USERNAME, theForm.EMAIL, "Email");
    reason += validateEmpty(theForm.PHONE1, "Phone");   
    reason += validateEmpty(theForm.ADDRESS1, "Mailing Address");   
    reason += validateEmpty(theForm.CITY, "City");   
    reason += validateEmpty(theForm.PROV, "Province");   
    reason += validateEmpty(theForm.POSTALCODE, "Postal Code");   
    reason += validateEmpty(theForm.COUNTRY, "Country");   
    
    if (reason != "") {
        document.getElementById('ERRORMSG').innerHTML = "Error:<ul>" + reason + "</ul>";
        return false;
    }

    return true;
}



function validateProfileFormEditOnSubmit(theForm) {
    var reason = "";
    reason += validateEmpty(theForm.FNAME, "First Name");   
    reason += validateEmpty(theForm.LNAME, "Last Name");
    
    if (theForm.ORGPARENT_NO.options[theForm.ORGPARENT_NO.selectedIndex].value == 'Other'){
        reason += validateEmpty(theForm.ORGNAME, "Organization");
        }
    else {
        reason += validateEmpty(theForm.ORG_NO, "Organization");
        }
    reason += validateEmpty(theForm.POSITIONTITLE, "Position");    
    reason += validateEmail(theForm.USERNAME, "Email");    
    reason += validateEmail(theForm.EMAIL, "Confirm Email");
    reason += validateMatchingFields(theForm.USERNAME, theForm.EMAIL, "Email");
    reason += validateEmpty(theForm.PHONE1, "Phone");
    reason += validateEmpty(theForm.ADDRESS1, "Mailing Address");   
    reason += validateEmpty(theForm.CITY, "City");   
    reason += validateEmpty(theForm.PROV, "Province");   
    reason += validateEmpty(theForm.POSTALCODE, "Postal Code");   
    reason += validateEmpty(theForm.COUNTRY, "Country");   
    
        
if (reason != "") {
    document.getElementById('ERRORMSG').innerHTML = "Error:<ul>" + reason + "</ul>";
    return false;
}  
return true;
}
*/
/* END form validation */

/* BEGINNING form validation functions */

function validateRadioButton(fld, fieldname) {
    var error = "<li>" + fieldname + "</li>\n"
    
    for (x=0; x < fld.length; x++) {
        if (fld[x].checked){
            error = "";
        }
    }

    return error;  
}

function validateMatchingFields(fld, fld2, fieldname) {
    var error = "";
    
    if (fld.value.length == 0 || fld.value != fld2.value) {
        fld.style.background = '#FDED2F'; 
        error = "<li>" + fieldname + "</li>\n"        
    } else {
        fld.style.background = 'White';
    }    
    return error;  
}

function validateEmpty(fld, fieldname) {
    var error = "";
    
    if (fld.value.length == 0 || fld.value.indexOf("Please click") == 0) {
        fld.style.background = '#FDED2F'; 
        error = "<li>" + fieldname + "</li>\n"        
    } else {
        fld.style.background = 'White';
    }    
    return error;  
}

function validatePassword(fld) {
    var error = "";
    var illegalChars = /[\W_]/; // allow only letters and numbers 
 
    if (fld.value == "") {
        fld.style.background = '#FDED2F';
        error = "You didn't enter a password.</li>\n";
    } else if ((fld.value.length < 7) || (fld.value.length > 15)) {
        error = "The password is the wrong length. It must be great than 6 characters and less than 15.</li>\n";
        fld.style.background = '#FDED2F';
    } else if (illegalChars.test(fld.value)) {
        error = "The password contains illegal characters.</li>\n";
        fld.style.background = '#FDED2F';
    } else if (!((fld.value.search(/(a-z)+/)) && (fld.value.search(/(0-9)+/)))) {
        error = "The password must contain at least one numeral.</li>\n";
        fld.style.background = '#FDED2F';
    } else {
        fld.style.background = 'White';
    }
   return error;
}   

function validateEmail(fld, fieldname) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
   
    if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = '#FDED2F';
        error = "<li>" + fieldname + "</li>\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = '#FDED2F';
        error = "<li>" + fieldname + "</li>\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validatePostalcode(fld, fieldname) {        
    var ov = trim(fld.value.toUpperCase()); 
    
    if (ov.length == 6 && ov.search(/^[a-zA-Z]\d[a-zA-Z]\d[a-zA-Z]\d$/) != -1) {
        field.value = ov.substring(0, 3) + " " + ov.substring(3, 6);
    } 
    else if (ov.length == 7 && ov.search(/^[a-zA-Z]\d[a-zA-Z]\s\d[a-zA-Z]\d$/) != -1) {
        error = "";
    }
    else if (ov.length > 0){
        error = "<li>" + fieldname + "</li>\n";
    }
    return error;
}

function validatePhone(fld, fieldname) {        
    var ov = trim(fld.value); 
    
    if (ov.search(/[a-zA-Z]/) != -1){
        error = "<li>" + fieldname + "</li>\n";
    }
    else {
        error = "";
    }
    return error;
}
/* END form validation functions */


/* BEGINNING Dynamic Organization dropdown */

function Lookup(parentId, value, name)
{
    this.parentId = parentId;
    this.value = value;
    this.name = name;
}

function changed(sel)
{
    var  ddOrg = document.forms[1].elements["ORG_NO"];
   
    if (!ddOrg)
    {
        return; //error
    }

    var selectedValue = sel.options[sel.selectedIndex].value;
    
    if (selectedValue == 'Other') {        
        document.getElementById('ORG_NO').style.display = 'none';
        document.getElementById('ORGNAME').style.display = 'block';
    }
    else {
        document.getElementById('ORG_NO').style.display = 'block';
        document.getElementById('ORGNAME').style.display = 'none';
        ddOrg.options.length = 0;
        var option = document.createElement('OPTION');
        option.value = "";
        option.innerHTML = "";
        ddOrg.appendChild(option);
        
        for (var i = 0; i < lookups.length; ++i)
        {
            var lookup = lookups[i];
            if (lookup.parentId == selectedValue)
            {
                var option = document.createElement('OPTION');
                option.value = lookup.value;
                option.innerHTML = lookup.name;
                ddOrg.appendChild(option);
            }
        }  
    }  
}


/* END Dynamic Organization dropdown */

function hidepanel(imageid, panelid)
{  
  if (document.getElementById(panelid).style.display == 'none') {
    document.getElementById(panelid).style.display = 'inline';
    document.getElementById(imageid).src = '/TPOnline/Images/navtreeopen.gif'
    document.getElementById(imageid).title = 'Hide description'
  }
  else{
    document.getElementById(panelid).style.display = 'none';
    document.getElementById(imageid).src = '/TPOnline/Images/navtreeclose.gif'    
    document.getElementById(imageid).title = 'Show description'
  }  
}

