/*
 * Addon Function using Jquery
 *
 * Copyright (c) 2007 Aprian
 * GPL (GPL-LICENSE.txt) licenses.
 *
 * $Date: 2007-10-22 11:06:24 -0400 (Thu, 22 Oct 2007) $
 * $Rev: 1 $
 */


/*
 * DROP DOWN MENU
 * Class: dropThis, xsubmenu, menuon
 */
function dropdownmenu(menuID) {
	
	/* hide submenu & subsubmenu*/
	$(".dropThis").find("ul").hide();
	
	/* onclick menu */
	$(".dropThis > h3").click( function() {
		
		var $theLink = $(this).children('a'); // a
		var $theSubMenu = $(this).next(); // a ul
		var $theParentSiblings = $(this).parent().siblings(); // div
		
		/* if href="#" show submenu, else go to link */
		if($theLink.attr("href") == '#' || $theLink.attr("href") === '') {
					
			/* if there're another siblings */
			if($theParentSiblings.length) {
				
				/* loop over the menu to hide visible menu */
				$(this).parent().siblings().each(function() {
					
					if($(this).children("ul:visible").size() > 0) {
						$(this).children("ul:visible").slideUp();
					}
						
				});
				
			}
			
	
			$theSubMenu.toggle();
			return false;
		}

		
	});
	
	/* onclick submenu & subsubmenu */
	$(".dropThis" + "> ul").find("a").click( function() {
				
		if($(this).attr("href") == '#' || $(this).attr("href") === '') {
			
			/* our parent & their siblings */
			var $thisParent = $(this).parent(); // li
			var $thisParentSiblings = $(this).parent().siblings(); // other li
			var $thisParentMenu = $(this).parent().next('ul'); // ul
			
			if($thisParentSiblings.length) {
				
				/* loop over the siblings and hide them */
				$(this).parent().siblings().each(function() { // li
					
					$siblingChildrens = $(this).children("ul:visible"); // ul ul
					
					/* hide our sibling */
					$siblingChildrens.slideUp();
					
					if($siblingChildrens.length) {
						
						/* hide our sibling children's */
						var $siblingGrandsons = $(this).children("ul:visible").children("li").children("ul:visible"); //ul ul ul
						
						if($siblingGrandsons.length) {
							$siblingGrandsons.slideUp();
						}
						
					}
				});
				
			}
			
			/* hide parent menu */
			$thisParentMenu.slideUp();			
			
			/* if href="#" show submenu, else go to link */
			$(this).next('ul').toggle();
			return false;
		}
		
		

	}
	);
	
	/* styling the menu */
	$(".dropThis").find("a").next("ul").prev("a").addClass("xsubmenu");
	
	/* show parents for active menu */
	$(".dropThis").find("a.menuon").parents().show();

}

/*
 * CLOSE BUTTON
 * Bind close function for element with ID btn_close (#btn_close)
 * ID : #btn_close
*/

function closePopup() {
	$("input#btn_close").each( function() {
		$(this).click(function() {
			self.close();
		});
	});
}

/*
 * POPUP WINDOW
 * Bind open new window function for element with ID openPopup (#openPopup)
 * Class: openPopup pop_[Width]x[Height]
 * To create popup 800x600, use class: openPopup pop_800x600
*/

function openPopup() {
	$("a.openPopup").each( function() {
		$(this).click(function() {
			
			var winpop;
			var xurl = $(this).attr('href');
			var xtarget = '_blank';
			var thisClass = $(this).attr('class').split(' ');
			var re = new RegExp('^(pop_)');
			for(i=0;i<thisClass.length;i++) {
				if(thisClass[i].match(re)) {
					popSizes = thisClass[i].split('_');
					popSize = popSizes[1].split('x');
					popWidth = popSize[0];
					popHeight = popSize[1];
				}
			}
			
			winpop = window.open(xurl, xtarget,'width=' + popWidth + ',height=' + popHeight + ',resizable=0,scrollbars=yes,menubar=no,status=no' );
			winpop.focus();
			return false;
		});
	});
}

/*
 * CONFIRM MESSAGE
 * Bind confirm function for element with CLASS popConfirm (.popConfirm)
 * Class: .popConfirm
 * Confirm message will be taken from attributes 'TITLE';
*/
function popConfirm() {
	$("a.popConfirm").each( function(){
		$(this).click( function() {
			var popMessage = $(this).attr('title');
			confirm(popMessage);
		});
	});
}

/*
 * CHECK ALL CHECKBOX
 * Check all checkbox in the group
 * Class: .checkAll, .checkThis
 * Give the group of checkbox the same class name. For the checkbox that have value ALL , give CLASS name = "checkAll" and ID name = [class for groups checkbox]
 */
function checkThisAll(checkboxClasses) {

	$("input[@type='checkbox'].checkAll").bind('click', function() {
		checkState = $(this).attr('checked');
		checkClass = $(this).attr('id');
		if(checkboxClasses) {
			var cbx = checkboxClasses.split(',');
			for(i=0;i<cbx.length;i++) {
				if(cbx[i] != checkClass) {
					/* remove other checked checkbox */ 
					$("input[@type='checkbox']." + cbx[i]).each( function() { this.checked = ''; });
					/* remove checkbox 'ALL' */
					$("input[@type='checkbox']#" + cbx[i]).each( function() { this.checked = ''; });		
				}
			}
			
		}
		$("input[@type='checkbox']." + checkClass).each( function() { this.checked = checkState; });
	});
	
}

function printOptions(printAreaClasses, checkboxClasses){
	if(printAreaClasses) {
		$("input[@name='print_area']").bind('click', function() {
			var pax = printAreaClasses.split(',');
			var cbx = checkboxClasses.split(',');
			for(i=0;i<pax.length;i++) {
				var cbx_new = cbx;
				if($(this).attr('class') == pax[i]) {
					var cbx = cbx.splice(i,1);
					for(x=0;x<cbx.length;x++) {
						/* remove other checked checkbox */ 
						$("input[@type='checkbox']." + cbx_new[x]).each( function() { this.checked = ''; });
						/* remove checkbox 'ALL' */
						$("input[@type='checkbox']#" + cbx_new[x]).each( function() { this.checked = ''; });		
					}
				}
			}	
		});
	}
}

function validateCheckboxOne(obj, msg) {
	var checkNum = 0;
	$("input[@name='" + obj + "']").each( function() {
		if($(this).attr('checked') === true) { checkNum = checkNum + 1; }
	});

	if(checkNum === 0) {
		alert(msg);
		return false;
	}
}
/*
 * REMOVE RADIO / CHECKBOX BORDER
 * 
 */
function removeBorder() {
	$('input[@type="checkbox"]').css("border","none");
	$('input[@type="radio"]').css("border","none");
}

/*
 * OPEN WINDOW
 */
function OpenCart(url,width,height) {
	Win = window.open(url,"cartWindow",'width=' + width + ',height=' + height + ',toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no' );
	Win.focus();
}


/*
 * VALIDATE INPUT INTEGER & REQUIRED ONE
 * Use = required_one(form_name group_className);
*/
	/* obj = form_name group_className */
	
function required_one(obj) {
	var obj = obj.split(' ');
	var form_name = obj[0];
	var input_group = obj[1];
	
	$("form[@name='" + form_name + "']").submit(function(){
		var check = 0;
		var msg = '';
		var id = '';

		$("form[@name='" + form_name + "'] input" + input_group).each(function(i){
			var checkVal = $(this).val();
			if(checkVal != '') {
				var checkInt = parseInt(checkVal);
				if(checkInt < 0 || isNaN(checkInt)  == true) {
					msg = $(this).attr('title');
					id = $(this).attr("id");
				} else{
					check = check + 1;
				}
				
			} else {
				if($(this).attr('title')) {
					msg = $(this).attr('title');	
				} else {
					msg = 'Please enter some value';
				}
				id = $(this).attr("id");
			}
		});
		if(check == 0){
			alert(msg);
			$("input#" + id).focus();
			return false;
		}
	});
}