/apps/userauthportal/static_www/cp.js

https://github.com/bigswitch/snac · JavaScript · 117 lines · 100 code · 10 blank · 7 comment · 33 complexity · 7447da459a5f73137b13396cbc1c049b MD5 · raw file

  1. function redirectParent( url ) {
  2. //TODO: more compatible redirect
  3. opener.location.replace(url);
  4. }
  5. function redirect(url) {
  6. //TODO: more compatible redirect
  7. document.location.replace(url);
  8. }
  9. function returnObjById( id )
  10. {
  11. if (document.getElementById)
  12. var returnVar = document.getElementById(id);
  13. else if (document.all)
  14. var returnVar = document.all[id];
  15. else if (document.layers)
  16. var returnVar = document.layers[id];
  17. return returnVar;
  18. }
  19. function lopu(form, pu_url, parent_url) {
  20. var puname = 'lopu';
  21. var win = window.open(pu_url, puname,
  22. 'width=306,height=200,scrollbars=no,resizable=yes');
  23. if (!(win.focus && win.opener) || win == null || typeof(win) == "undefined")
  24. {
  25. //alert("pu blocked:"+win.focus);
  26. return true;
  27. }
  28. win.opener.focus()
  29. win.opener.location.replace(parent_url);
  30. form.pu.value="1";
  31. form.havepu.value="1";
  32. form.target=puname;
  33. form.submit();
  34. return false;
  35. }
  36. if( typeof XMLHttpRequest == "undefined" ) {
  37. //TODO: Add window.onerror to handle browsers that don't suppor try/catch
  38. // see http://tinyurl.com/6xbjfp
  39. XMLHttpRequest = function() {
  40. try { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); } catch(e) {};
  41. try { return new ActiveXObject("Msxml2.XMLHTTP.3.0"); } catch(e) {};
  42. try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) {};
  43. try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {};
  44. throw new Error("XMLHttpRequest or XMLHTTP not supported.");
  45. };
  46. }
  47. function watchForAuth(check_url, redir_url, next_check_to) {
  48. var form = returnObjById("login");
  49. if (form == null) {
  50. return null;
  51. }
  52. try {
  53. var data = formData("login");
  54. var req = new XMLHttpRequest();
  55. req.open("POST", check_url, true);
  56. req.setRequestHeader("Content-Type",
  57. "application/x-www-form-urlencoded");
  58. req.onreadystatechange = function () {
  59. if (req.readyState
  60. && req.readyState == 4
  61. && req.status == 200
  62. && req.responseText)
  63. {
  64. if (req.responseText == 1) {
  65. if (form.rurl !=null
  66. && typeof(form.rurl)!="undefined"
  67. && form.rurl.value != "")
  68. {
  69. redirect(form.rurl.value);
  70. }
  71. else {
  72. redirect(redir_url);
  73. }
  74. }
  75. else {
  76. setTimeout('watchForAuth("'+check_url+'", "'+
  77. redir_url+'", "'+next_check_to+'")',
  78. next_check_to);
  79. }
  80. }
  81. };
  82. req.send(data);
  83. } catch(e) {
  84. //alert("exception:"+e); //XXX
  85. //not supported - do nothing
  86. }
  87. }
  88. function formData(formId) {
  89. formId=returnObjById(formId);
  90. if (formId == null) {
  91. return null;
  92. }
  93. var postStr= '';
  94. for (i = 0; i < formId.elements.length; i++) {
  95. formElem = formId.elements[i];
  96. switch (formElem.type) {
  97. case 'text':
  98. case 'select-one':
  99. case 'hidden':
  100. case 'password':
  101. case 'textarea':
  102. postStr += formElem.name + '=' + encodeURIComponent(formElem.value) + '&'
  103. break;
  104. }
  105. }
  106. return postStr;
  107. }