//## <AJAX>
var working;
var ind_url = 'http://collegetreasure.com/index.php?';

//===============================================================================
function getElementByMy(name) {
   if (document.getElementById) {
      curElement = document.getElementById(name);
   } else if (document.getElementByName) {
      curElement = document.getElementByName(name);
   } else {
      curElement = document.all[name];
   }

   if (! curElement) {
      //curElement = document.all[name];
      //curElement = document.frm[name];
   }


   return curElement;
}
//========================================================================================
function ParceResponse(str) {
   //v(str);
   mr = /<(.+?)>(.+?)<\/(.+?)>/gi;
   var res = new Array;
   //#myc !!! certain count
   for (a=0; a<17; a++) {
      arr = mr.exec(str);
      if (arr) {
         if (arr[1].indexOf('|')>-1) {
            atypes = arr[1].split("|");
            arr[1] = atypes[0];
            ctype = atypes[1];
         } else {
            ctype = '';
         }
         //v(arr[1] + ctype);
         curElement = getElementByMy(arr[1]);
         //alert(arr[1] + curElement);
         if ((! ctype) || (ctype=='select')) {
            res[arr[1]] = arr[2].split("|");
            FillSelect(curElement, res[arr[1]]);
         } else if (ctype=='sel_index') {
            curElement.selectedIndex = arr[2];
         } else {
            curElement.innerHTML = arr[2];
         }
      }
   }
}
//========================================================================================
function FillSelect(sel, arr) {
   for (var i = 0; i < sel.options.length; i++) {
      sel.options[i] = null;
   }
   sel.options.count = 0;
   //sel.options =null;
   var tmp = '';
   //c(sel.options.count);
   for (var i = 0; i < arr.length; i=i+2) {
      var a=i/2;
      //c(a);
      sel.options[a] = new Option(arr[i+1], arr[i]);
      tmp = tmp + i/2 + '|';
   }
   c(sel.name);
}
//========================================================================================
function StdJsonProcess(str) {

   //alert(str);

   var resp = eval('(' + str + ')');

   if (resp['div']) {
      cdiv = getElementByMy(resp['div']);
      cdiv.innerHTML = resp['cont'];
   }

}
//========================================================================================
function WidgetCheckInputGetData(frm) {
   var myArray=0;
   var tstates = '';

   for (var i=0; i< frm.elements["states[]"].options.length; i++) {
      if (frm.elements["states[]"].options[i].selected == true) {
         tstates = tstates + "&states[]=" + frm.elements["states[]"].options[i].value;
         myArray += 1;
      }
   }

   if(myArray > 5) {
      alert('You can not select more than five states');
      return false;
   } else if (myArray == 0) {
      alert('Please select some states');
      return false;
   }

   if ((!frm.sat.value) && (! frm.act.value)) {
      alert('Please select SAT or ACT.');
      return  false;
   }


   turl = ind_url + "step=sm_widget&sat=" + frm.sat.value
      + "&act=" + frm.act.value + "&gpa=" + frm.gpa.value + tstates;


   cdiv = getElementByMy('m_result_div');
   thtml = '<IFRAME id="m_frame" width="370px" height="570px" src="' + turl
      + '&uid=' + frm.uid.value + '" frameborder=0 name="bianca">';

   cdiv.style.display = 'block';

   //position: (['center', 'center']),
   var $dialog = $('.w_widget_result_div')
      .html(thtml)
		.dialog({
         minWidth: 390,
         title: ''
		});

   return false;
}
//========================================================================================
function WidgetCheckDataPostEmail(frm) {
   var myArray=0;
   var tstates = '';

   var check_fields = new Array('fname', 'lname', 'student', 'phone', 'email', 'year');

   var error = false;
   var turl = '';
   for (var i=0; i< check_fields.length; i++) {
      if (frm.elements[check_fields[i]].value == '') {
         error = true;
      }
      //turl += "&" + check_fields[i] + "=" + frm.elements[check_fields[i]].value;
   }

   if (error) {
      alert('Please enter all required fields.');
      return false;
   }

   turl = ind_url + "&step=sm_widget";
   c(turl);

   /* permission denied
   cdiv = parent.document.getElementById('m_result_div');
   cdiv.style.height = '30px';
   cdiv.style.width="1270px";
   */

   frm.submit();

   //var cdiv = document.getElementById('m_frame').document.getElementById('w_result_div');
   //cdiv = document.getElementById('m_form_div');
   //cdiv.innerHTML = 'Your data submitted successfully...';

   //tfd = getElementByMy('m_form_div');
   //majaxGetUrl(turl, 'StdJsonProcess');

   return false;
}
//========================================================================================
function majaxGetUrl(url, fnc) {
   var http;

   http = get_http();
   if (! http){
      alert('err XMLHTTP err!')
   }

   // get XMLHTTPRequest object
   if(!this.http){
      this.http = get_http();
      working = false;
   }
   if ((! working) && this.http) {
      var http = this.http;

      this.http.open("GET", url, true);
      this.http.onreadystatechange = function() {
         // Data ready
         if (http.readyState == 4) {
            working = false;

             //alert(http.responseText);
            eval(fnc + "(http.responseText);");
         } else {
            //data wait
         }
      }
      working = true;
      this.http.send(null);
   }
   if(!this.http){
      alert('err XMLHTTP object!')
   }
}
//========================================================================================
function get_http() {
  if (window.XMLHttpRequest) {
        // Other Browsers
        requestObj = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        //IE
        requestObj = new ActiveXObject("Msxml2.XMLHTTP");
        if (!requestObj) {
            requestObj = new ActiveXObject("Microsoft.XMLHTTP");
        };
    };
    return requestObj;
};
//========================================================================================
//## <OTHER FUNC>
//========================================================================================
function CheckFieldsForm(mform, ta) {
   d = ' | ';
   err = 0;
   for (var i=0;i<ta.length;i++) {
      var e = ta[i];
      tInp = mform.elements[e];

      // username not an input skip
      if (! tInp) {continue;}

      //v(e +'|'+  tInp.name +'|'+ typeof(tInp) +'|'+ tInp.type +'|'+ tInp.checked +'|'+ tInp.value);

      if (typeof(tInp) != 'undefined') {
         if (tInp.type == 'checkbox') {
            if (tInp.checked == false) {
               err=1;
            }
         } else {
            if (typeof(tInp.value) != 'undefined') {
               if (tInp.value.length<1) {
                  err=1;
               }
            } else {
               a = '0';
               tn = e + a;
               tInp1 = mform.elements[e + '0'];
               if ((typeof(tInp1) != 'undefined') && (tInp1.type == 'checkbox')) {
                  if (err!=1) {
                     err=1;
                     for (a=0; a<130; a=a+2) {
                        tn = e + a;
                        tInp1 = document.getElementById(tn);
                        if (tInp1) {
                           //v(tn +'|'+  tInp1.name +'|'+ typeof(tInp1) +'|'+ tInp1.type +'|'+ tInp1.checked +'|'+ tInp1.value);
                           if (typeof(tInp1) != 'undefined') {
                              if (tInp1.checked == true) {
                                 err=0;
                                 break;
                              }
                           }
                        }
                     }
                  }
               }
            }
         }
      }
   }

   if (err>0) {
      alert("Please fill all required (*) fields");
      return false;
   } else {
      return true;
   }
}
//========================================================================================
function CheckFields(ta) {
   d = ' | ';
   err = 0;
   for (var i=0;i<ta.length;i++) {
      var e = ta[i];
      //alert(e);
      tInp = document.getElementById(e);
      if (tInp) {
         if (tInp.type == 'checkbox') {
            //alert(tInp.name + d + tInp.type + d + tInp.value + d  + tInp.value.length + d + tInp.checked);
            tInp1 = document.getElementById(tInp.name + '0');
            if (tInp1) {
               if (err!=1) {
                  err=1;
                  for (a=0; a<100; a=a+2) {
                     tInp1 = document.getElementById(tInp.name + a);
                     if (tInp1) {
                        //alert(tInp.name + d + tInp.type + d + tInp.value + d  + tInp.value.length + d + tInp.checked);
                        if (tInp1.checked == true) {
                           err=0;
                           break;
                        }
                     }
                  }
               }
            } else {
               if (tInp.checked == false) {
                  err=1;
               }
            }
         } else {
            //alert(tInp.name + d + tInp.type + d + tInp.value + d  + tInp.value.length + d + tInp.checked);
            if (tInp.value.length<1) {
               err=1;
               //alert('err');
               //htmo = document.getElementById(e.toString());
            }
         }
      }
   }

   //alert(err);
   //return false;

   if (err>0) {
      alert("Please fill in all required (*) fields");
      return false;
   } else {
      return true;
   }
}
//========================================================================================
function SubmitActionIdf(frm, act, id, fn) {
   frm.substep.value = act;

   var frm_count = frm.elements.length;
   for (var i = 0; i < frm_count; i++) {
      if (frm.elements[i].name == fn) {
         frm.elements[i].value = id;
      }
   }

   if ((act=='del') && (window.confirm('Delete item ?') != true)) {
      return;
   }

   frm.submit();
}
//========================================================================================
function SubmitAction(act, id) {
   frm.substep.value = act;
   frm.id.value = id;
   frm.submit();
}
//========================================================================================
function QstDel(link) {
   if (window.confirm('Delete item ?') == true) { // + link
      window.location=link;
   }
   return false;
}
//========================================================================================
function SubmitAction1(act, id) { //, id, cpos_null
   frm1.substep.value = act;
   frm1.id.value = id;
   if ((act=='del') && (window.confirm('Delete item ?') != true)) {
      return;
   }
   frm1.submit();
}
//========================================================================================
function SubmitAction2(act, id) { //, id, cpos_null
   frm1.substep.value = act;
   frm1.id.value = id;
   frm1.cpos.value = 0;
   if ((act=='del') && (window.confirm('Delete item ?') != true)) {
      return;
   }
   frm1.submit();
}
//========================================================================================
function CheckAll() {
   frm=frm1;
   for (var i=0;i<frm.elements.length;i++) {
      var e = frm.elements[i];
      if ((e.name != 'allbox') && (e.type=='checkbox')) {
         e.checked = frm.allbox.checked;
      }
   }
}
//========================================================================================
function ChangeContinent() {

   var SelectedContinent = frm.continent.options[frm.continent.selectedIndex].value;
   var oldSelectedCountry = frm.country.options[frm.country.selectedIndex].value;


   // (clear)
   var k = frm.country.options.length;
   for (var i = 0; i < k; i++) {
      frm.country.options[0] = null;
   }

   var result = '';
   var k=0;
   frm.country.options[k] = new Option('All', 'all');
   for (var i in ctr_cnt_h) {
      if ((ctr_cnt_h[i] == SelectedContinent) || (SelectedContinent == 'all')) {
         k++;
         frm.country.options[k] = new Option(i, i);
         if (oldSelectedCountry == i) frm.country.selectedIndex = k;
         result += i;
      }
   }
   //alert(result);
}
//========================================================================================
//## <DEBUG>
//========================================================================================
function v(str) {
   alert(str);
}
//========================================================================================
function c(str) {
   var debug = getElementByMy('debug');
   if (debug) {
      //debug.innerHTML += str + '<br/>';
   }
}
//========================================================================================

