function window_close_now() {
    window.setTimeout("window.close()", 1);
      top.window.close();
      window.close();
}

function check_open()
{
  changeScreenSize(660,800) ;
    return_code = true ;
    now = new Date();
     localtime = now.toString() ;
     hours = now.getHours();
     minutes = now.getMinutes() ;
     weekdaypos = now.getDay() ;  // is a number between 0 and 6. Sunday is 0, Monday is 1 and so on.
     local_time = hours + minutes / 60 ;
     delta_time  =  Math.abs( local_time -  document.data_passer.server_time.value    )  ;
    //document.getElementById('contact_us_after_hours').style.visibility='hidden';
    
    document.frm_goto_chat_system.timestamp_client.value =  now ; // hours * 100 + minutes  ;
    

    //
    // - determine if KC or SAC customer
    //
    if ( delta_time < .5 ) {  // kc customer
        //document.frm_goto_chat_system.REASON_TAG.value =  "KCCUSTOMERSUPPORTWCDATA" ;
        document.data_passer.region_code.value =  "KC" ;
        document.getElementById('PHONE_SAMPLE_TEXT').innerHTML = document.data_passer.kc_phone.value ;
        //document.frm_goto_chat_system.__gTranscriptEmailFrom.value  = document.data_passer.kc_care_email.value ;

    }
    else {  // SAC customer
        //document.frm_goto_chat_system.REASON_TAG.value = "SACCUSTOMERSUPPORTWCDATA" ;
        document.data_passer.region_code.value = "SAC" ;
        document.getElementById('PHONE_SAMPLE_TEXT').innerHTML = document.data_passer.sac_phone.value ;
        //document.frm_goto_chat_system.__gTranscriptEmailFrom.value   = document.data_passer.sac_care_email.value ;
    }



    // - determine KC or SAC center is open and if not tell customer
    //
    someone_open = false ;
    display_hours_open = "";

     if ( document.data_passer.region_code.value == "SAC"  )  {
          if ( document.data_passer.sac_care_open.value == 0  ) {
              display_hours_open = display_hours_open + document.data_passer.sac_care_hrs.value + "\r\n";
              document.getElementById('CARE_CLOSED').innerHTML = "<img src='assets/images/radio-grey.gif' /> Customer Service is currently closed."
                  + "<u><span onClick='alert(" +'"' + document.data_passer.sac_care_hrs.value +'"'+ ")';> Customer Service Chat Hours  </span>"  ;
              }
              else { someone_open = true ; }
          if ( document.data_passer.sac_tech_open.value == 0  ) {
              display_hours_open = display_hours_open + document.data_passer.sac_tech_hrs.value + "\r\n";
              document.getElementById('TECH_CLOSED').innerHTML = "<img src='assets/images/radio-grey.gif' /> Technical Support is currently closed."
                + "<u><span onClick='alert(" +'"' + document.data_passer.sac_tech_hrs.value +'"'+ ")';> Technical Support Chat Hours  </span>"  ;
              }
              else {
              someone_open = true ; }
          if ( document.data_passer.sac_sales_open.value == 0  ) {
              display_hours_open = display_hours_open + document.data_passer.sac_sales_hrs.value + "\r\n";
              document.getElementById('SALES_CLOSED').innerHTML = "<img src='assets/images/radio-grey.gif' /> Sales is currently closed."
                  + "<u><span onClick='alert(" +'"' + document.data_passer.sac_sales_hrs.value +'"'+ ")';> Sales Chat Hours  </span>"  ;
              }
              else {
              someone_open = true ; }

        }  // if SAC
     else {
          if ( document.data_passer.kc_care_open.value == 0  ) {
              display_hours_open = display_hours_open + document.data_passer.kc_care_hrs.value + "\r\n";
              document.getElementById('CARE_CLOSED').innerHTML = "<img src='assets/images/radio-grey.gif' /> Customer Service  is currently closed. "
                        + "<u><span onClick='alert(" +'"' + document.data_passer.kc_care_hrs.value +'"'+ ")';>Customer Service Chat Hours  </span></u>"  ;
              }
              else {
              someone_open = true ; }
          if ( document.data_passer.kc_tech_open.value == 0  ) {
              display_hours_open = display_hours_open + document.data_passer.kc_tech_hrs.value + "\r\n";
              document.getElementById('TECH_CLOSED').innerHTML = "<img src='assets/images/radio-grey.gif' /> Technical Support is currently closed. "
                    + "<u><span onClick='alert(" +'"' + document.data_passer.kc_tech_hrs.value +'"'+ ")';>Technical Support Chat Hours  </span>"  ;
              }
              else {
              someone_open = true ; }
          if ( document.data_passer.kc_sales_open.value == 0  ) {
              display_hours_open = display_hours_open + document.data_passer.kc_sales_hrs.value + "\r\n";
              document.getElementById('SALES_CLOSED').innerHTML = "<img src='assets/images/radio-grey.gif' /> Sales is currently closed. "
                  + "<u><span onClick='alert(" +'"' + document.data_passer.kc_sales_hrs.value +'"'+ ")';>Sales Chat Hours  </span>"  ;
              }
              else {
              someone_open = true ; }

        }  // if KC

    if ( someone_open == false ) {
        alert ( "Currently all SureWest chat support is closed. \r\n" + display_hours_open ) ;
        document.getElementById('form_button').style.visibility='hidden';
        }

if ( document.frm_goto_chat_system.client_browser.value.indexOf("APPLEWEBKIT") > 0 ) {
        tmp1 = "SureWest Chat is optimized for use with Internet Explorer and Firefox.  " +
               "You can still chat but you will need to enable your caps lock for a better experience.  " +
               "Sorry for any inconvenience. \r\n\r\n-SureWest Support" ;
         alert( tmp1 ) ;
        }
        

     return return_code  ;

}


function validate_form() {
  return_code = true ;

// - remove spaces
  document.frm_goto_chat_system.NAME.value = trim( document.frm_goto_chat_system.NAME.value ) ;
  document.frm_goto_chat_system.EMAILADDRESS.value = trim( document.frm_goto_chat_system.EMAILADDRESS.value ) ;
  document.frm_goto_chat_system.PHONE.value = trim( document.frm_goto_chat_system.PHONE.value ) ;
  document.frm_goto_chat_system.ACCOUNTID.value = trim( document.frm_goto_chat_system.ACCOUNTID.value ) ;


//  - format check
  document.frm_goto_chat_system.PHONE.value = return_num_only( document.frm_goto_chat_system.PHONE.value ) ;
  document.frm_goto_chat_system.ACCOUNTID.value= return_num_only( document.frm_goto_chat_system.ACCOUNTID.value ) ;



  return_alert_text = "Please enter the following: \r\n";

   //alert("in function - validate_form ");
  if (document.frm_goto_chat_system.NAME.value.length < 3 )
    {
    return_alert_text = return_alert_text  + "* your name \r\n";
    return_code =  false;
    }
    
  if (document.frm_goto_chat_system.EMAILADDRESS.value.length < 5 )
    {
    return_alert_text = return_alert_text  + "* your email address \r\n";
    return_code =  false;
    }

  if ( (document.frm_goto_chat_system.ACCOUNTID.value.length > 0 ) &&
      ((document.frm_goto_chat_system.ACCOUNTID.value == 0 ) || (document.frm_goto_chat_system.ACCOUNTID.value.length < 2 ) ) )
    {
    return_alert_text = return_alert_text  + "* Invalid account was detected. \r\n";
    return_code =  false;
    }

  if ( (document.frm_goto_chat_system.PHONE.value.length > 0 ) &&
        ((document.frm_goto_chat_system.PHONE.value == 0 ) || (document.frm_goto_chat_system.PHONE.value.length < 10 ) ) )
      {
      return_alert_text = return_alert_text  + "*  Invalid phone number was detected. \r\n";
      return_code =  false;
      }



  if (document.frm_goto_chat_system.ACCOUNTID.value == document.frm_goto_chat_system.PHONE.value )
    {
    return_alert_text = return_alert_text  + "* phone or account number \r\n";
    return_code =  false;
    }


    if (typeof document.frm_goto_chat_system.ROUTING1[0] == 'undefined' ) {
        // alert ( " using radio button of single one for  routing");
         routing1 = document.frm_goto_chat_system.ROUTING1.value}
     else {
          //alert ( " using array routing");
          routing1 = get_radio_value() ; }
          
          //alert ( "ROUTING1 = " + routing1)    ;

  document.frm_goto_chat_system.REASON_TAG.value = "" ;   // REASON_TAG = routing to chat queue
  
    if ( document.data_passer.region_code.value ==  "SAC"  ) {

        if  (routing1 == "CustomerService" )   {
          document.frm_goto_chat_system.REASON_TAG.value =   document.data_passer.sac_care_queue.value ;
          document.frm_goto_chat_system.__gTranscriptEmailFrom.value  = document.data_passer.sac_care_email.value ; }
        else if  (routing1  == "TechnicalSupport" ) {
          document.frm_goto_chat_system.REASON_TAG.value =   document.data_passer.sac_tech_queue.value  ;
          document.frm_goto_chat_system.__gTranscriptEmailFrom.value  = document.data_passer.sac_tech_email.value ; }
        else if  (routing1  == "Sales") {
          document.frm_goto_chat_system.REASON_TAG.value =   document.data_passer.sac_sales_queue.value ;
         document.frm_goto_chat_system.__gTranscriptEmailFrom.value  = document.data_passer.sac_sales_email.value ; }
         else {
          return_code =  false;
          return_alert_text = return_alert_text  + "* area of assistance \r\n";
          }
      }
    else {
        if  (routing1 == "CustomerService" )   {
          document.frm_goto_chat_system.REASON_TAG.value =  document.data_passer.kc_care_queue.value  ;
          document.frm_goto_chat_system.__gTranscriptEmailFrom.value  = document.data_passer.kc_care_email.value ; }
        else if  (routing1  == "TechnicalSupport" ) {
          document.frm_goto_chat_system.REASON_TAG.value =   document.data_passer.kc_tech_queue.value  ;
          document.frm_goto_chat_system.__gTranscriptEmailFrom.value  = document.data_passer.kc_tech_email.value ; }
        else if  (routing1  == "Sales") {
          document.frm_goto_chat_system.REASON_TAG.value =   document.data_passer.kc_sales_queue.value  ;
          document.frm_goto_chat_system.__gTranscriptEmailFrom.value  = document.data_passer.kc_sales_email.value ; }
        else {
          return_code =  false;
          return_alert_text = return_alert_text  + "* area of assistance \r\n";
          }
      }



    if ( return_code != true ) {
      alert(return_alert_text)    ;
    } else {
      setcookie(document.frm_goto_chat_system.ACCOUNTID.value,
                document.frm_goto_chat_system.PHONE.value,
                document.frm_goto_chat_system.REASON_TAG.value
      ) ;
    }


    return return_code ;
}

function changeScreenSize(w,h) {
  window.resizeTo( w,h )
}
     
     
function setcookie(account_id, phone_num, queue) {

  phone = escape(phone_num);

  phone_num = phone.lpad("0",10);

  account = escape(account_id);
  var date = new Date();
  date.setTime(date.getTime()+86400);
  var expires = "; expires="+date.toGMTString();

  document.cookie ="c=" + account + phone_num + expires + "; domain=.surewestchat.com ; path=/";
  document.cookie ="q=" + queue + expires + "; domain=.surewestchat.com ; path=/";
}

function return_num_only( temp_var ) {

  var temp_var = new String(temp_var);
  var temp_var = temp_var.replace(/[^0-9]/g, '');

  return ( temp_var ) ;

}

//pads left
String.prototype.lpad = function(padString, length) {
	var str = this;
    while (str.length < length)
        str = padString + str;
    return str;
}   // function

//pads right
String.prototype.rpad = function(padString, length) {
	var str = this;
    while (str.length < length)
        str = str + padString;
    return str;
}   // function

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}  // function

function get_radio_value() {
 //alert ( " length= " + document.frm_goto_chat_system.ROUTING1.length )   ;
  for (var i=0; i < document.frm_goto_chat_system.ROUTING1.length; i++) {
    //  alert ( i + " | " + document.frm_goto_chat_system.ROUTING1.length )   ;
    if (document.frm_goto_chat_system.ROUTING1[i].checked) {
      var rad_val = document.frm_goto_chat_system.ROUTING1[i].value;
    }
  } // for loop
   
   return rad_val ;
   
}   // function

//--------------------------------------------------------------
//
//
//
//--------------------------------------------------------------
function CheckMessages() {
  if (typeof document.frm_goto_chat_system.ROUTING1[0] == 'undefined' ) {
    routing1 = document.frm_goto_chat_system.ROUTING1.value
  } else {
    routing1 = get_radio_value() ; 
  }

  //alert ( "ROUTING1 = " + routing1)    ;

  var ChatMessage = "";

  if (document.data_passer.region_code.value == "SAC") {
    if (routing1 == "CustomerService" ) {
      if (document.data_passer.sac_care_msg.value != "") {
       ChatMessage += document.data_passer.sac_care_msg.value;
      }

      if (document.data_passer.all_care_msg.value != "") {
       ChatMessage += document.data_passer.all_care_msg.value;
      }
    } else if (routing1  == "TechnicalSupport" ) {
      if (document.data_passer.sac_tech_msg.value != "") {
       ChatMessage += document.data_passer.sac_tech_msg.value;
      }

      if (document.data_passer.all_tech_msg.value != "") {
       ChatMessage += document.data_passer.all_tech_msg.value;
      }
    } else if  (routing1  == "Sales") {
      if (document.data_passer.sac_sales_msg.value != "") {
       ChatMessage += document.data_passer.sac_sales_msg.value;
      }

      if (document.data_passer.all_sales_msg.value != "") {
       ChatMessage += document.data_passer.all_sales_msg.value;
      }
    } 
  } else {
    if (routing1 == "CustomerService" ) {
      if (document.data_passer.kc_care_msg.value != "") {
       ChatMessage += document.data_passer.kc_care_msg.value;
      }

      if (document.data_passer.all_care_msg.value != "") {
       ChatMessage += document.data_passer.all_care_msg.value;
      }
    } else if (routing1  == "TechnicalSupport" ) {
      if (document.data_passer.kc_tech_msg.value != "") {
       ChatMessage += document.data_passer.kc_tech_msg.value;
      }

      if (document.data_passer.all_tech_msg.value != "") {
       ChatMessage += document.data_passer.all_tech_msg.value;
      }
    } else if (routing1  == "Sales") {
      if (document.data_passer.kc_sales_msg.value != "") {
       ChatMessage += document.data_passer.kc_sales_msg.value;
      }

      if (document.data_passer.all_sales_msg.value != "") {
       ChatMessage += document.data_passer.all_sales_msg.value;
      }
    } 
  }

  if (ChatMessage != "") {
    alert(ChatMessage);
  }
} //end function CHeckMessages()


