﻿/* Modified by Davy on 2007-08-08 */

var accounttype = "";
var handle_type = "";
var form_name = "";

function radioFormHandler(field,handletype,fieldchange){

	  handle_type = handletype;
	  form_name = field;
	  var pattern2 = /^\d{1,8}$/;
	  var pattern = /^\d{3}-[a,A,p,P]{1}-\d{1,8}$/;
	  var pattern_ads = /^\d{3}-[a,A]{1}-\d{1,8}$/;
	  var pattern_pub = /^\d{3}-[p,P]{1}-\d{1,8}$/;
	  var uid = field.username;

	  
	  window.status = field.username.value;
	  
	  
	  if (uid.value == ""){
              alert("Please input your Login ID!");			  			  
      } else if (pattern_ads.test(uid.value) == true){
		  accounttype = 'adcenter';
	  } else if (pattern_pub.test(uid.value) == true){
		  accounttype = 'publisher';
	  } else {
		  alert ("Login Error, please check your username!");  
	  }
	  
      if (fieldchange == "login" && accounttype){
		  if (field.password.value == ""){
              alert("Please input your password!");
		  } else { 	
					
		    if(accounttype == 'adcenter'){
			  
			  get_user_network(uid.value,"ads");
			  return;
				  
		    } else {
			  
			  get_user_network(uid.value,"pub");
			  return;
			  
		    }

		    field.submit();
		  }
		   
      } else if (fieldchange == "forgotpassword" && accounttype){
		   if(accounttype=='adcenter')
			   self.location="http://admin.clicksor.com/adcenter/forgot_pw.php";
		   else
	           self.location="http://admin.clicksor.com/publisher/forgot_pw.php";
      }


}


var xmlHttp;

function createXMLHttpRequest() {

    if (window.ActiveXObject) {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");

    }
    else if (window.XMLHttpRequest) {
        xmlHttp = new XMLHttpRequest();

    }
}

function get_user_network(uid, type) {
 
	//var url = "http://admin.clicksor.com/publisher/checklogin.php?uid=" + uid + "&type=" + type;
	var url = "http://"+self.location.host+"/files/checklogin.php?uid=" + uid + "&type=" + type;
    createXMLHttpRequest();
    xmlHttp.open("GET", url, true);
    xmlHttp.onreadystatechange = handleStateChange;
    xmlHttp.send(null);
}

function handleStateChange() {

    if(xmlHttp.readyState == 4) {
       var strreturn=xmlHttp.responseText;  
	   if (strreturn == "error") {
		   alert("Login error, please check your Login ID && Password.");  
		   return;
	   }
           else var strreturn = "admin.clicksor.com";
	   if(accounttype == 'adcenter')
		 form_name.action = 'https://' + strreturn + '/' + accounttype + '/' + handle_type;
	   else
	      form_name.action = 'http://' + strreturn + '/' + accounttype + '/' + handle_type;
	   //alert(form_name.action);	         
	   form_name.submit();
    }
}


function forget_pswd(field){
	
	  form_name = field;

	  var pattern_ads = /^\d{3}-[a,A]{1}-\d{1,8}$/;
	  var pattern_pub = /^\d{3}-[p,P]{1}-\d{1,8}$/;
	  var uid = field.username;
	  
	  if (uid.value == ""){
              alert("Please input your Login ID!");			  			  
      } else if (pattern_ads.test(uid.value) == true){
		  accounttype = 'adcenter';
	  } else if (pattern_pub.test(uid.value) == true){
		  accounttype = 'publisher';
	  } else {
		  alert ("Login Error, please check your username!"); 
	  }

      if (accounttype == 'adcenter'){			  
		  window.location.href='/ad/ad_forgot_pw.php';
		  return;				  
	  } else if (accounttype == 'publisher'){		  
		  window.location.href='/pub/pub_forgot_pw.php';
		  return;			  
	  }
}
