/* 
dhtml_utilities.js: library of javascript to support common animation / branding effects
    david.kelly@fanore.com (http://www.fanore.com/website_design.htm)
    
    
    
*/

//generic "get an item by reference" which can handle either a getElementbyID or via the IE All collection
// note that "parent" argument is optional
// this is supplanted by the $ function built into prototype-1.4.0.js
function get_el(elem,parent) {
	if (document.layers) {
	    if (parent) {
	     return eval("document."+parent+".document."+elem);
	      }
	    else {
		return eval("document."+elem);
		     }
		
	  } 
	    else if (document.all) {
		return eval("document.all."+elem);
	} 
	    else if (document.getElementById) {
		return eval("document.getElementById('"+elem+"')");
	}
	//alert("here");
}

/* parameters for open new window
        url: url to be opened
        n: name to assign to new window
        f: use default settings? f=0 implies use custom settings
        w: factor to use when calculating window width e.g. w=2 ==> 50% of screen width
        h: factor to use when calculating window height e.g. h=2 ==> 50% of screen height
        t: top in pixels
        l: left position in pixels
        
*/
function open_new_window(url, n, f, w,h,t,l){
// default values for settings variables
var url_to_open="";
var name_of_window="";
var window_width=0;
var window_height=0;
var screen_width=screen.width;
var screen_height=screen.height;
var window_left=0;
var window_top=0;
var def_settings=false; //==> do not use default window settings

//          check that a valid url has been passed
            url_to_open=url;
            if (url_to_open.length==0){
                return void 0;
            }
//    alert(url_to_open);

//          check that a valid window name has been passed
            name_of_window=n;
            if (name_of_window.length==0){
                    name_of_window="NON"
                }    
//    alert(f);
            if (f!=0){
                def_settings=true;
            }   
             
            if (def_settings){
//              default settings are half screen size window                
                window_width=(screen_width/2);
                window_height=(screen_height/2);
                window_left=(screen_width-window_width)/2; // centered
                window_top=(screen_height-window_height)/2; //vertically centered
            }
            else{
//              use the users parameters
                if (parseInt(w) > 5){
                    window_width=w;
                    window_height=h;
                }
                else{
                    window_width=(screen_width/w);
                    window_height=(screen_height/h);
                
                }
                window_left=l
                window_top=t
                               
            }
    var settings='Height=' + window_height + ',Width=' + window_width + ',Top=' + window_top + ',left=' + window_left + ',scrollbars=yes,resizable=1, location=1,status=1'
	var w=window.open(url_to_open,name_of_window,settings);
	if (w.opener==null)w.opener=window;
}


function close_window(parent_url){
    if (arguments.length!=0){
       
        self.parent.location.href=parent_url;
    }
    self.close();
}

/* Loads the new FCK Editor  */
var oFCKeditor=false
function load_editor(){
//alert("load_editor");
    var datasource_name = 'fd_' + $('FCKDATASOURCE').value;
    var prefix=$('fck_base_url').value;
    
    //var sBasePath = document.location.pathname.substring(0,document.location.pathname.lastIndexOf('common')) ;
    var sBasePath = prefix + '/fck/fckeditor/';
    //alert(sBasePath);
    oFCKeditor = new FCKeditor( 'FCKeditor1' ) ;
    //alert(oFCKeditor);
    oFCKeditor.BasePath	= sBasePath ;
    oFCKeditor.Height	= 300 ;
    //oFCKeditor.Value	= 'This is some <strong>sample text<\/strong>. You are using <a href="http://www.fckeditor.net/">FCKeditor<\/a>.' ;
    oFCKeditor.Value = $(datasource_name).value
    var toolbar_set=$('html_editor_to_use').value
    
    oFCKeditor.ToolbarSet = toolbar_set;
    oFCKeditor.Config['LinkUpload'] = false;
    oFCKeditor.Config['LinkBrowser'] = false;
        
    oFCKeditor.Create() ;
    
}



/* for user with mutli-part forms (enhanced_form.inc) - performs much the same function as the show_save_progress function but optimised for multipart data*/
function display_progress(){
var el=get_el('progress_bar_url')
var progress_bar_url=el.value;

   var el=get_el("but_save");
   if (el){
        $('but_save').setOpacity(0.1)
       //Element.hide('but_save'); 
   }
   var el=get_el("but_close");
   if (el){
    $('but_close').setOpacity(0.1)
       //Element.hide('but_close'); 
   }
    var el=get_el('FCKDATASOURCE');
    if (el){    
        var oEditor = FCKeditorAPI.GetInstance('FCKeditor1') ;
    
    	// Get the editor contents in XHTML.
        //var debug_size=oEditor.GetXHTML( true )
    	//alert(debug_size.length) ;		// "true" means you want it formatted.
        //var debug_eile=oEditor.GetHTML()
        //alert(debug_eile)
        var datasource_name = 'fd_' + el.value;
        //$(datasource_name).value= oEditor.GetXHTML( true );  
        //$(datasource_name).value= oEditor.GetHTML();  
       $(datasource_name).value= oEditor.GetXHTML(true);  
    }       



// display a window indicating save progress 
            var w=380;
            var h=50;
            var winLeft=Math.round((screen.width - w)/2);
            var winTop=Math.round((screen.height - h)/2);
            var sVersion = navigator.appVersion;
             
           if (sVersion.indexOf('MSIE') != -1 && sVersion.substr(sVersion.indexOf('MSIE')+5,1) > 4)
          {
        		var settings="dialogWidth:380px; dialogHeight:50px; center:yes";
        		var x=window.showModelessDialog(progress_bar_url+ '&b=IE',null,settings);
            }
            else
            {
        		//var x=window.open(progress_bar_url+ '&b=NN','','width=370,height=115', true);
                var x=window.open(progress_bar_url+ '&b=NN','','width=380px,height=50', true);
        
           }
           
            x.focus()
   
   

            

    //Event.observe(window, 'unload', function(event){
    //  window.opener.location.reload();
    //  }, false);
    return true;


}


/** ----- confirm an action - if confirmed then redirect to the url passed -- **/

function confirm_action(text,url,winflag,ajax_url,ajax_params,ajax_target,ajax_function, debug_target){
/* winflag indicates one of three options
    0 implies that we redirect to the URL
    1 implies that we open a new window
    2 implies that we use ajax to open the url requested
   */
    if (confirm(text)){
        if (winflag==1){
            // if winflag = 1 ==> open new window
            	var k=window.open(url,"","Height=480,Width=500,Top=20,left=40,scrollbars=yes,resizable=1");
        }
        
        else{
            if (winflag==2){
                // to use option 2 the url is passed as an array in this format
                // url|params|target|function(s) to run when complete
                //var ajax_data=url.split("|");
                //alert(ajax_data[0]);
                //alert(ajax_data[1]);
                //alert(ajax_data[2]);
                //alert(ajax_data[3]);
                
                //alert(ajax_url);
                //alert(ajax_params);
                //alert(ajax_target);
                //alert(ajax_function);
               // ajax_get_url(ajax_data[0].toString(),ajax_data[1].toString(),ajax_data[2].toString(), ajax_data[3].toString());
                if (arguments.length==8){
                    // debug the ajax result
                    ajax_get_url(ajax_url,ajax_params,'ajax_debug_panel', ajax_function);
                }
                else{
                    ajax_get_url(ajax_url,ajax_params,ajax_target, ajax_function);
                }
            }
            else{
                window.location.href=url;
            }
        }
    }
}


function string_syntax(svalue, bmail){
    var str=svalue;
    var sinvalid="!,£,\$,%,',\&";
    
    if (bmail==true){
    	var reg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)|(')|(%)|(")/; // not valid
    	var reg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/; // valid
    }
    else{
    	var reg1 = /(\.\.)|(^\.)|(')|(%)|(")/; // not valid
    	var reg2 = /([a-zA-Z0-9])|([a-zA-Z])|([0-9])/; // valid
    
    }
    if (!reg1.test(str) && reg2.test(str)) { // if syntax is valid
    	return true;
    	}
return false;

}

//password_conforms: validate that password entered by user conforms to password policy
function password_conforms(value){
    var alpha_chars="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    var numeric_chars="1234567890";
    var has_alpha=false;
    var has_number=false;
    for (var i=0;i<value.length;i++){
        //alert(alpha_chars.indexOf(value.substr(i,1)));
        if (alpha_chars.indexOf(value.substr(i,1))>-1){
            has_alpha=true;
        }
    }
   // alert(has_alpha);
    for (var i=0;i<value.length;i++){
        if (numeric_chars.indexOf(value.substr(i,1))>-1){
            has_number=true;
        }
    }
    if ((has_alpha)&&(has_number)){
        return true;
    }
    else{
        return false;
    }
    
}





