/* Modified by Davy on 2007-06-19 */

var accounttype = "";
var handle_type = "";

 
function radioFormHandler(field,handletype,fieldchange,myacctype,currenthost,network){

	  handle_type = handletype;
	  
	  if (!myacctype){	  
	      for (var i=0;field.radiobutton && i < field.radiobutton.length; i++){
	            if (field.radiobutton[i].checked){
	                accounttype = field.radiobutton[i].value
					//if(accounttype=='advertisers' && field.username.value>7330)
					if(accounttype=='advertisers') //start using adcenter
						accounttype = 'adcenter';
	            }
	      }
	  } else{
		accounttype = myacctype;
	  }
	  
	  if (!currenthost) currenthost = 'www.clicksor.com';
	  if (!network) network = 100;
	  
	  window.status = field.username.value;
 
      var pattern2 = /^\d{1,8}$/;
	  var pattern = /^\d{3}-[a,A,p,P]{1}-\d{1,8}$/;
	  
	  var uid = field.username;
	  
       if (accounttype == ""){
			  alert("Please choose a type of account")
		} else if (fieldchange == "login" ){
		  if (uid.value == ""){
              alert("Please input your Login ID!");			  			  
          } else if (pattern.test(uid.value) == false){
			  
			if (pattern2.test(uid.value) == true){  
			  if (accounttype == 'adcenter')
			      alert("Dear Advertiser:" + '\n' + "Clicksor system is being upgraded. " + '\n' + "Please login with your new and unique Account ID:" + '\n\n' + network + "-A-" + uid.value);
			  else
                  alert("Dear Publisher:" + '\n' + "Clicksor system is being upgraded. " + '\n' + "Please login with your new and unique Account ID:" + '\n\n' + "100-P-" + uid.value);
			 } else
			   alert("Login Error, please check your username or password!");	
			 
		  } else 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;
			  
		    }

            if(accounttype == "agencies"){
               document.login.action = 'http://admin.clicksor.com/refpro/' + handletype;
            }
		    field.submit();
		  }
		   
      } else if (fieldchange == "signup"){
		   if(accounttype=='adcenter')
			   self.location="http://" + currenthost + "/advertisers_choose.php";
		   else
	           self.location="http://" + currenthost + "/publishers_presignup.php";
      } else if(fieldchange == "forgotpassword"){
		   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 = "./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;
	   }
       
	   if(accounttype == 'adcenter')
		  document.login.action = 'https://' + strreturn + '/' + accounttype + '/' + handle_type;
	   else
	      document.login.action = 'http://' + strreturn + '/' + accounttype + '/' + handle_type;
		         
	   document.login.submit();
    }
}


