/* javascript */

/*
 * Filename   : myajax.js
 * Desc       : Ajax Javascript Funktionen
 * 
 * Department : S(kim)
 * 
 * Author     : Dipl.-Ing. Jan Rademaker
 * E-Mail     : jan.rademaker@hs-owl.de
 * 
 * Datum      : 17.05.2011
 * 
 */
 
 if ( keydown == undefined ) var keydown = 0;

 jQuery( window ).load( function() {
	var myForm;
	if ( document.forms && ( myForm = document.forms["mailform"] ) ) {

		// Username
	   	if ( myForm.elements["user"] ) {
	   		var name = jQuery( "#user" ).val();
	   		jQuery( "#user" ).focus();
	    	SearchName( name );
	    	delete name;
	   	}
	
		// Location
	  	if ( myForm.elements["mailformLocation"] ) {
	  		if ( jQuery( "#mailformLocation" ).val() == "no" ) jQuery( "#mailformLocation" ).focus();
	   	}
	
		// Netzbereich
		if ( myForm.elements["mailformLocation"] ) {
			if ( jQuery( "#mailformLocation" ).val() != "no" ) jQuery( '#suggestions3' ).show();
			else jQuery( '#suggestions4' ).show();
			jQuery.post( "fileadmin/template/ajax/soap_networks.php" , { queryString: ""+jQuery( "#LocationValue" ).val()+"" , queryNet: ""+jQuery( "#NetworkValue"  ).val()+"" } , function( data ) {
		       	if ( data.length > 0 ) {
   		           	jQuery( '#suggestions4'         ).hide();
   		           	jQuery( '#suggestions3'         ).hide();   		           	
		           	jQuery( '#suggestions2'         ).show();
		           	jQuery( '#autoSuggestionsList2' ).html( data );
		   	  		// Weiter oder Reload wenn Location erfasst
		           	if ( 
		   	  			jQuery( "#mailformLocation" ).val() != "no" && 
		   	  			jQuery( "#mailformNetwork"  ).val() == "no" 
		   	  		) jQuery( "#mailformNetwork" ).focus();
		        	}
		    } );
	   	}
	   	
	   	// Rechnername
		if ( myForm.elements["WorkstationName"] ) {
	  		// Weiter oder Reload wenn Location erfasst
           	if ( jQuery( "#mailformLocation" ).val() != "no" && jQuery( "#mailformNetwork"  ).val() != "no" ) jQuery( "#WorkstationName" ).focus();
		   	SearchHost( jQuery( "#WorkstationName" ).val() );
		}
		
		// MAC-Adresse
		if ( myForm.elements["MAC"] ) SearchMAC( jQuery( "#MAC" ).val() );
		
		// SwitchPortAntrag
		if ( myForm.elements["SwitchPort"] ) jQuery( "#SwitchPort" ).focus();
	}
	delete myForm;	
 } );

 // OnPageLoad :: Sucht den Usernamen
 function SearchName( inputString ) {
	var x,y,element,sugbox;
	//
   	element           = document.getElementById( "user"        ); // Input-Type-Text-Box
   	sugbox            = document.getElementById( "suggestions" ); // Aussen-Div-Box
   	sugbox.style.top  = getPositionY( element , "first" ) + "px";
	sugbox.style.left = getPositionX( element           ) + "px";
	jQuery.post( "fileadmin/template/ajax/ldap_user_check.php" , {queryString: ""+inputString+""} , function( data ) {
   	jQuery( '#suggestions'         ).show();
        jQuery( '#autoSuggestionsList' ).html( data );
   	} );
	delete x,y,element,sugbox;
 }
 
 // Form1 Input Type Text onKeyUp :: Sucht den Usernamen
 function get_names( inputString , event ) {
	var x, y, element;
	if ( inputString.length == 0 ) jQuery( '#suggestions' ).hide();
   	else {
		var jrevent  = event || window.event;
		var charCode = jrevent.keyCode;		
		if ( inputString.length > 3 ) {
        	var element       = document.getElementById( "user"        );	// Input-Type-Text-Box
        	var sugbox        = document.getElementById( "suggestions" );	// Aussen-Div-Box
         	sugbox.style.top  = getPositionY( element , "first" ) + "px";
	 		sugbox.style.left = getPositionX( element ) + "px";	 		
	 		//
         	jQuery.post( "fileadmin/template/ajax/ldap_user.php" , {queryString: ""+inputString+""} , function( data ) {
         		if ( data.length > 0 ) { 
         			jQuery( '#suggestions'         ).show();
         			jQuery( '#autoSuggestionsList' ).html( data );
					var listroot = document.getElementById( "toc" );	// UL-Liste         			
         			if ( charCode == 40 ) { // Coursor Down
    					if ( keydown == 0 ) keydown = 1;
						if ( keydown == listroot.childNodes.length ) keydown = 1;
						else keydown++;
						for ( i=0 ; i<listroot.childNodes.length ; i++ ) {
       						var listmember = listroot.childNodes[i];
   							if ( i == (keydown - 1) ) {
   								listmember.focus();   								
   								listmember.className+="aktiv";
   							} else listmember.className.replace("aktiv","");
       					}
         			} else if ( charCode == 38 ) { // Coursor Up
    					if ( keydown == 0 ) keydown = listroot.childNodes.length;
    					if ( keydown == 1 ) keydown = listroot.childNodes.length;
						else keydown--;
						for ( i=0 ; i<listroot.childNodes.length ; i++ ) {
       						var listmember = listroot.childNodes[i];
   							if ( i == (keydown - 1) ) {
   								listmember.focus();   								
   								listmember.className+="aktiv";
   							} else listmember.className.replace("aktiv","");
       					}
         			} else if ( 
         					( charCode == 13 ) || 
         					( charCode == 37 ) || 
         					( charCode == 39 )
         				) { // Enter, Coursor Left, Coursor Right
						for ( i=0 ; i<listroot.childNodes.length ; i++ ) {
       						var listmember = listroot.childNodes[i];
       						if ( i == (keydown - 1) ) {
       							jQuery( '#user'        ).val( listmember.firstChild.data );
   								jQuery( '#suggestions' ).hide();
       							stop();
       						}
       					}						
    				} else {
    					keydown = 0;
	           			for ( i=0 ; i<listroot.childNodes.length ; i++ ) {
	       					var listmember = listroot.childNodes[i];
      						listmember.onmouseover = function() { this.className += "aktiv"; }
       						listmember.onmouseout  = function() { this.className  = this.className.replace ( "aktiv" , "" ); }         					
	       				}
    				}
         		} 
         	} );
		}
	}
   	delete x, y, element;
 } 
 
 // Form 1 Username-LI onCLick or <Enter>
 //
 function get_username( thisValue ) {
	jQuery( '#user' ).val( thisValue );
   	jQuery('#suggestions').hide();
 }
 
 // Form 2 Standort-Select onChange()
 function set_location( inputString , netwString ) {
	if ( inputString.length == 0 ) { jQuery( '#suggestions2' ).hide(); }
	else {
		jQuery( '#suggestions2' ).hide();		
		jQuery( '#suggestions3' ).show();
    	jQuery.post( "fileadmin/template/ajax/soap_networks.php" , { queryString:inputString , queryNet:netwString } , function(data) {
         	if ( data.length > 0 ) {
            	jQuery( '#suggestions3'         ).hide(); 		
            	jQuery( '#suggestions2'         ).show();
            	jQuery( '#autoSuggestionsList2' ).html( data );
   		  		// OnChange Location
            	if ( 
   		  			jQuery( "#mailformLocation" ).val() != "no" &&
   		  			jQuery( "#mailformNetwork"  ).val() == "no" 
   		  		) jQuery( "#mailformNetwork" ).focus();
         	}
      	} );
   	}
 } 

 function NetChange() {
   	if ( 
		jQuery( "#mailformLocation" ).val() != "no" &&
   		jQuery( "#mailformNetwork"  ).val() != "no"   		
   	) jQuery( "#WorkstationName" ).focus();
 }
 
 // Form X Hostname Search <input onkeyup()>
 function SearchHost( inputString ) {
	var x,y,info;
	var element = document.getElementById( "WorkstationName" ); // Input-Type-Text-Box
    var errBoxL = document.getElementById( "errorBox2"       ); // Aussen-Div-Box
	if ( ( jQuery( "#hostname_x_pos" ).val() == "" ) && ( jQuery( "#hostname_y_pos" ).val() == "" ) ) {
		y = getPositionY( element , "second" );
		x = getPositionX( element            );
		jQuery( "#hostname_y_pos" ).val( y );
		jQuery( "#hostname_x_pos" ).val( x );
	} else {
		y = jQuery( "#hostname_y_pos" ).val();
		x = jQuery( "#hostname_x_pos" ).val();							
	}
	errBoxL.style.position = "absolute";
    errBoxL.style.top      = y + "px";         
	errBoxL.style.left     = x + "px";    
   	if( ! inputString.length ) { 
   		jQuery( '#errorBox2'            ).hide();
       	element.style.background      = "#ffffff";   		
   	} else {
    	inputString = inputString.toLowerCase();
    	jQuery( '#WorkstationName' ).val( inputString.toLowerCase() );
    	jQuery( '#WorkstationName' ).val( inputString.replace( /ä/g  , "ae" ) );
    	jQuery( '#WorkstationName' ).val( inputString.replace( /ö/g  , "oe" ) );    	
    	jQuery( '#WorkstationName' ).val( inputString.replace( /ü/g  , "ue" ) );    	
    	jQuery( '#WorkstationName' ).val( inputString.replace( /ß/g  , "ss" ) );    	
    	jQuery( '#WorkstationName' ).val( inputString.replace( /_/g  , "-"  ) );    	
    	jQuery( '#WorkstationName' ).val( inputString.replace( /--/g , "-"  ) );     	
	    //
      	if ( inputString.length > 4 ) {
      		jQuery.post( "fileadmin/template/ajax/soap_search_host_mac.php" , { queryString:inputString , queryType:"HostRecord" } , function( data ) {
   				if ( data ) {
	            	errBoxL.style.background = "#ee0101";
		      		info                     = "Dieser Rechnername wird bereits verwendet!";
	            } else {
	        	   	if ( ! data ) {
		            	errBoxL.style.background = "#33ff33";	        	   		
				   		info                     = "Dieser Rechnername ist frei!";
	            	}
	         	}
				jQuery( '#errorBox2' ).html( info );				
	      	} );
      	} else {
           	errBoxL.style.background = "#ffff00";         	
  		    jQuery( '#errorBox2' ).html( "Dieser Rechnername ist zu kurz (min. 5 Buchstaben)!" );
     	}
	    jQuery( '#errorBox2' ).show(); 
	    //
	    if ( 
			jQuery( "#mailformLocation" ).val() != "no" &&
		    jQuery( "#mailformNetwork"  ).val() != "no" 
		) jQuery( "#WorkstationName" ).focus();
	}
	delete x,y,info,element,errBoxL;
 }

 // MAC-Adresse
 function SearchMAC( inputString ) {
	var x, y, info;
   	var element         = document.getElementById( "MAC"       );	// Input-Type-Text-Box
    var errBoxR         = document.getElementById( "errorBox3" );	// Aussen-Div-Box
    //
   	if ( ( jQuery( "#mac_x_pos" ).val() == "" ) && ( jQuery( "#mac_y_pos" ).val() == "" ) ) {
		y = getPositionY( element , "second" );
		x = getPositionX( element );
		jQuery( "#mac_y_pos" ).val( y );
		jQuery( "#mac_x_pos" ).val( x );
	} else {
		y = jQuery( "#mac_y_pos" ).val();
		x = jQuery( "#mac_x_pos" ).val();							
	}
	//    
    errBoxR.style.top   = y + "px";       
	errBoxR.style.left  = x + "px";
	//
	//
	//regex = /^([0-9A-F]{2}([:-]|$)){6}$/i;
	//
	//
	//regex = /^([0-9a-f]{2}([:-]|$)){6}$/i;
	//
	//
	//regex = /^([0-9a-f]{2}([:-]|$)){1}$/i;	
	//
	//
	//regex = /^([0-9a-f]{2}([:-]|$)){6}$/i;	
	//if ( regex.test( inputString ) ) {    		
	//
	inputString = inputString.toUpperCase();
	inputString = inputString.replace( /:/gi  , "-" );
	inputString = inputString.replace( /;/gi  , "-" );		
	inputString = inputString.replace( /\./gi , "-" );		
	inputString = inputString.replace( /,/gi  , "-" );		
	inputString = inputString.replace( /\ /gi , "-" );		
   	if ( inputString.length == 0 ) { 
   		jQuery( '#errorBox3' ).hide();
       	element.style.background = "#ffffff";
   	} else {
    	jQuery( '#MAC' ).val( inputString );
    	if ( inputString.length == 17 ) {
			regex = /^([0-9a-f]{2}([:-]|$)){6}$/i;
			if ( ! regex.test( inputString ) ) {
				errBoxR.style.background = "#ffff00";
         		jQuery( '#errorBox3' ).html( "Die MAC-Adresse ist syntaktisch falsch!" );		
			} else {
	    		jQuery.post( "fileadmin/template/ajax/soap_search_host_mac.php" , { queryString:inputString , queryType:"MACAddress" } , function(data) {
	            	if ( data ) {
	            		errBoxR.style.background = "#ee0101";
	               		info                     = "Diese MAC-Adresse wird bereits verwendet!";
	            	} else {
	               		if ( ! data ) {
	                  		errBoxR.style.background  = "#33ff33";
	                  		info                      = "Diese MAC-Adresse ist frei!";
	               		}
	            	}
	            	jQuery( '#errorBox3' ).html( info );
					//
			        if (  
			        	jQuery( "#mailformLocation" ).val() != "no" &&
			        	jQuery( "#mailformNetwork"  ).val() != "no" 
			        ) jQuery( "#MAC" ).focus();            	
   		      	} );
			}
  		} else if ( inputString.length < 17 ) {
         	errBoxR.style.background = "#ffff00";
         	jQuery( '#errorBox3' ).html( "Die MAC-Adresse ist noch zu kurz. Bsp.: 00:11:22:33:44:55" );  			
  		} else {
         	errBoxR.style.background = "#ee0101";
         	jQuery( '#errorBox3' ).html( "Die MAC-Adresse is zu lang!" );
      	}
	    jQuery( '#errorBox3' ).show();
	}
	delete x, y, info, element, errBoxR;
 }
 function getPositionY( element , page ) {
	var y = 0;
   	if ( element && typeof element.offsetParent != "undefined" && typeof element.offsetTop == "number" ) { y += element.offsetTop; }
   	element = "";
   	//
   	//if ( navigator.appName.indexOf( "Internet Explorer" ) != -1 ) y = y + 15;   	
   	//
   	if ( page == "first"  ) {
   		if ( navigator.userAgent.indexOf( "Intel Mac" )  != -1 ) {
   			if ( navigator.userAgent.indexOf( "Safari" ) != -1 ) return y + 18;
   			if ( navigator.userAgent.indexOf( "Opera" )  != -1 ) return y + 16;   			
   			else                                                 return y + 20;
		} else if ( navigator.appName.indexOf( "Internet Explorer" ) != -1 ) {
			if      ( navigator.userAgent.indexOf( "MSIE 7" ) != -1 ) return y + 240;
			else if ( navigator.userAgent.indexOf( "MSIE 9" ) != -1 ) return y + 20;
			else                                                      return y + 23;
   		} else return y + 23;
  	} else if ( page == "second" ) {
   		if ( navigator.userAgent.indexOf( "Intel Mac" ) != -1 ) {
   			if ( navigator.userAgent.indexOf( "Safari" ) != -1 ) return y + 52;
   			else                                                 return y + 51;
   		} else if ( navigator.appName.indexOf( "Internet Explorer" ) != -1 ) {
   			if      ( navigator.userAgent.indexOf( "MSIE 6" ) != -1 ) return y + 75;   			
   			else if ( navigator.userAgent.indexOf( "MSIE 7" ) != -1 ) return y + 240;   			
   			else if ( navigator.userAgent.indexOf( "MSIE 8" ) != -1 ) return y + 55;
   			else if ( navigator.userAgent.indexOf( "MSIE 9" ) != -1 ) return y + 55;
   			else                                                      return y + 70;
   		} else if ( navigator.userAgent.indexOf( "Chrome" ) != -1 ) { 
   			return y + 53;   		
   		} else return y + 60;
   	} else if ( page == "third"  ) {
   		if ( navigator.userAgent.indexOf( "Safari" ) != -1 ) return y + 52;
   		else                                                 return y + 60;
   	} else if ( page == "fourth" ) {
   		if ( navigator.userAgent.indexOf( "Safari" ) != -1 ) return y + 23;
   		else                                                 return y + 23;
   	} else {
   		if ( navigator.userAgent.indexOf( "Safari" ) != -1 ) return y + 23;
   		else                                                 return y + 23;
   	}
 }
 function getPositionX( element ) {
	var x = 0;
   	if ( element && typeof element.offsetParent != "undefined" && typeof element.offsetLeft == "number" ) { x += element.offsetLeft; }
   	element = "";
   	return x;
 }

 /*
  * end of myajax.js
  */
 
 /* javascriptend */

