//Global variables for these functions
myrules = function() {
	/* 
	Images
	*/
	$$('img').each(function(e) {
				
         var img = e;
		 
		 /* Make PNG Transparent */
         var imgName = img.src.toUpperCase()
			 if (imgName.substring(imgName.length-3, imgName.length) == "PNG") {
		 		if(/MSIE/.test(navigator.userAgent) && /png_trans/.test(img.className))  {
					var imgID = (img.id) ? "id='" + img.id + "' " : "";
					var imgClass = (img.className) ? "class='" + img.className + "' " : "";
					var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' ";
					var imgStyle = "display:block;float:left";
					if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle;
					new_height = (img.height*1);
					var strNewHTML = '<span ' + imgID + imgClass + imgTitle	+ ' style="width:' + img.width + 'px; height:' + new_height + 'px;' + imgStyle + ';' + 'filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\''+img.src + '\', sizingMethod=\'\');"></span>';
					img.outerHTML = strNewHTML;
				} 
			 } else if(/enlarge/.test(img.className)) {
				 
				 img.style.cursor = 'pointer';
				 img.onclick = function() {
					 
				 	//Get new width and height
					new_height = (img.height *3.33)+10;
					new_width = img.width*3.33;
					 
				 	new popup(e,'/webadmin/view_image.php?img='+img.src,{title:'Image View',width:new_width,height:new_height,posFromElement:true});					
					 
					 return false;
					 
				 }
				 
				 
			 }
			 
	});
	
	
	$$('div').each(function(e) {
		
		if(/no_results/.test(e.className)) {
							 
		new Effect.Highlight(e, {startcolor:'#ffcc00',endcolor:'#FF6600',duration:1});				 
							 
							 
		 }					  
						  
						  
	});
	
	$$('#databaseLinks a').each(function(e) {
		
		Event.observe(e,"mouseover",function() { 
		Element.up(e).style.backgroundColor = '#dddddd'; 
		});
		
		Event.observe(e,"mouseout",function() { 
		Element.up(e).style.backgroundColor = ''; 
		});		
								  
						  
	});	
	
	//Do captchas
	$$('div.captcha_box').each(function(e) {
										 
		if(e.id) {			
							
				current_id = e.id.replace("captcha_box_","")				
				image = e.down('img');
				
				if(!/loading/.test(image.src)) {
				return;	
				}
				
				this.pars = {};
				this.pars.id_image = image.id;		
				this.pars.id_site = current_id;
				this.pars.id_login = $('id_login_'+current_id).value;
				this.pars.id_captcha = $('id_captcha_'+current_id).value;									
				this.pars.url = $('url_'+current_id).value;				
											
				var pars = $H(this.pars).toQueryString();				
				var myAjax = new Ajax.Request('http://'+window.location.hostname+'/app/index.php?action=ajax_get_captcha', 
				{method: 'post', parameters: pars,onComplete:function(t) {
					show_captcha(t.responseText);
				}}); //					

		}									 
										 
										 
    });
	
	//SHow cpatcha
	function show_captcha(t) {
		
		response = eval(t);
		$(response[0].image_id).src = response[0].img_src;
		current_id = response[0].image_id.replace("captcha_image_","");
		$('image_key_'+current_id).value = response[0].image_key;
		console.log($('image_location_'+current_id));
		$('image_location_'+current_id).value = response[0].img_src;
		
	}
	
	//Captchas reload
	$$('a.reload').each(function(e) {
										 			
			Event.observe(e,"click",function(e) {
				
				thelink = Event.findElement(e,'a');							
				image = thelink.up().down('img')
				current_id = image.id.replace("captcha_image_","")
				
				thesrc = image.src + "&rand="+Math.random();
				image.src = "/app/view/images/loading.gif";

				setTimeout(function() {

					this.pars = {};
					this.pars.id_image = image.id;		
					this.pars.id_site = current_id;
					this.pars.id_login = $('id_login_'+current_id).value;
					this.pars.id_captcha = $('id_captcha_'+current_id).value;					
					this.pars.url = $('url_'+current_id).value;									
												
					var pars = $H(this.pars).toQueryString();				
					var myAjax = new Ajax.Request('http://'+window.location.hostname+'/app/index.php?action=ajax_get_captcha', 
					{method: 'post', parameters: pars,onComplete:function(t) {
						show_captcha(t.responseText);
					}}); //	

				},1000);
				

												
			});			
			
			e.href = "#";
			
			e.onclick = function() {
			return false;	
			}
										 
    });	
	
		
	$$('input').each(function(e) {
		
		if(/Delete/.test(e.value)) {
							 
				e.onclick = function() {
					
				//Count the number of ticked checkboxes
				var proceed = count_checkboxes();
				
					if(proceed == false) {
					alert("Please tick a checkbox in the 'Select' column\nto delete that row");	
					return false;
					}
					
					
					conf = confirm("Are you sure?");
					if(!conf) {
					return false;	
					} else {
					return true;	
					}
										
				}
							 
		} else if(/Multi/.test(e.value)) {
			
			e.onclick = function() {
			
				//Count the number of ticked checkboxes
				var proceed = count_checkboxes();
				
					if(proceed == false) {
					alert("Please tick multiple checkboxes in the 'Select' column\nto edit those rows");	
					return false;
					}
			
			}
			
			
		} else if(/get_links/.test(e.id)) {
			
			e.onclick = function() {
			
				//Count the number of ticked checkboxes
				var proceed = count_checkboxes();
				
					if(proceed == false) {
					alert("Please tick multiple checkboxes in the 'Select' column\nto get links for those rows");	
					return false;
					} 
					
					new linkUpdate();
			
			}
			
			
		} 
			
			
						  
	});	
	
	$$('.quick_links').each(function(e) {
									 
			e.onchange = function() {
			
				window.location = e.value;				
				
			}
									 
									 
 	});
	
	$$('select').each(function(e) {
									 
		if(/id_client/.test(e.name)) {
			
			baz = e;
			
			e.onchange = function() {
				
					$$('select').each(function(f) {
											   
							parent1 = (findParent(e,'edit-table'));
						    parent2 = (findParent(f,'edit-table'));											   
											   
							if(/id_product/.test(f.name) && (parent1 == parent2)) {
															
								filter = e[e.selectedIndex].innerHTML;
								if(filter == "----------") {
								return false;	
								}
							
								conf = confirm("Would you like to filter Product by '" + filter + "' ?");
								if(!conf) {
								return false;	
								} else {
								
									var selectIndex;
									$A(f).each(function(g,m) {
										regEx = new RegExp("" + filter + "","i");
										if(!regEx.test(g.innerHTML)) {
										Element.hide(g);
										} else if (!selectIndex) {	
										selectIndex = m; //Get the first non-hidden element
										}
									});
									
									f.selectedIndex = selectIndex;
									
									//Create undo
									undo = Builder.node('a',{href: '#'},'Undo filter');
									
										undo.onclick = function() {											
												$A(f).each(function(g) {
												Element.show(g);
												});	
												f.selectedIndex =0;
												Element.hide(undo);
												return false;
										};
										
										f.parentNode.appendChild(undo);
								
								}
					
							}								 
							
							});					
				
			}
			
		}									 
									 
 	});	
	
		
	$$('a').each(function(e) {					
				
		if(/show-table/.test(e.href) && !/heading_link/.test(e.className)) {
			
			e.onclick = function() {
				
				if($('clicknav_holder')) { //Double click does actual link
				Element.remove($('clicknav_holder'));
				return true;	
				}
								
				new clickNav(this,{
				elements:{
				list_table: {
					href:this.href,
					title:'List All',
					innerHTML:'<img src="/webadmin/view/images/icn_default_list.gif" style="vertical-align:middle;margin-bottom:4px" border="0" /> <span class="underline">List All</span>'
					},
				search_table: {
					href:this.href.replace("show-table","search-table"),
					title:'Search',
					innerHTML:'<img src="/webadmin/view/images/icn_default_search.gif" style="vertical-align:middle;margin-bottom:4px" border="0" /> <span class="underline">Search</span>'
					},
				add_row: {
					href:this.href.replace("show-table","add-row"),
					title:'Add Row',
					innerHTML:'<img src="/webadmin/view/images/icn_default_add.gif" style="vertical-align:middle;margin-bottom:4px" border="0" /> <span class="underline">Add</span>'					
					},
				add_rows: {
					href:this.href.replace("show-table","add-rows"),
					title:'Add Rows',
					innerHTML:'<img src="/webadmin/view/images/icn_default_add_multi.gif" style="vertical-align:middle;margin-bottom:4px" border="0" /> <span class="underline">Add Rows</span>'					
					}
				},				
				left:0,
				top:10,
				style:{
					position:'absolute',
					zIndex:1000,
					display:'none'	
				},
				duration:0.3,
				onEnd: function() {
				Rounded("div.clickNav_outer","all","transparent","#fff","border #999");					
				}
				});
				
				return false;
							
			}
			

		} else if(/ibrowser/.test(e.href)) {
				
			e.target = '_self';
			e.href = '#';			
			//Get the field to pass back to
				Event.observe(e,"click",function() { 
				
					return_id = Element.previous(e).id;
					new popup(e,'/libs/php/ibrowser/ibrowser.php?return_id='+return_id,{title:'Image Management',width:'540',height:'360'});											
								
				});
				
				e.onclick = function() {
					return false;
				}
			
			
		} else if(/select_table/.test(e.href)) {			
			
			e.href = '#';			
						
			//Get the field to pass back to
				Event.observe(e,"click",function() { 
					
					if(e.id) {
						selectClass = e.id.replace('select_table_','');
						if(selectClass == 'select_table') {
						findClass = 'selectAble';	
						} else {
						findClass = selectClass;		
						} 
					} else {
					findClass = 'selectAble';							
					}
										
					if(!/checked/.test(e.className)) {
					Element.addClassName(e,'checked');
					masterElement = 'checked';
				    } else {
					Element.removeClassName(e,'checked');	
					masterElement = '';
					}
					
					
					
						$$('.'+findClass).each(function(element) {
						element.checked = masterElement;
						});
							
						return false;													
								
				});
			
				e.onclick = function() {
				
				return false;
					
				}
			
			
		} else if(/insert_calendar/.test(e.href)) {
				
			e.href = '#';			

				Event.observe(e,"click",function() { 					
											
				 dateButton = (Element.up(e).id).replace("tableColumn","dateButton");
				 dateField = (Element.up(e).id).replace("tableColumn","dateField");
				 
				 dateButton = $(dateButton);
				 dateField = $(dateField);
				 				 												 
				 showCalendar(dateButton, dateField, calCallback,'lc_');
				 
												
				});
				
				e.onclick = function() {
							return false;
				}
			
			
		}
				

	});
	
};

function count_checkboxes() {
	
	//Count the number of ticked checkboxes
	var proceed = false;
	$$('.selectAble').each(function(element) {
		if(element.checked) {
		proceed = true;
		}
	});
	
	return proceed;	
	
}