// common.js

var	g_rootPath = "";


function	g_setRootPath( path )
{
	g_rootPath = path;
}




// jquery dialog stuff
function	jqDisableButton( dlgClass, buttonLabel )
{
	var button = getDialogButton( dlgClass, buttonLabel );
	if ( button )
	{
//		button.attr( 'disabled', 'disabled' ).addClass( 'ui-state-disabled' );
		button.attr( 'disabled', true ).addClass( 'ui-state-disabled' );
	}
}



function	jqEnableButton( dlgClass, buttonLabel )
{
	var button = getDialogButton( dlgClass, buttonLabel );
	if ( button )
	{
//		button.attr( 'disabled', '' ).removeClass( 'ui-state-disabled' );
		button.attr( 'disabled', false ).removeClass( 'ui-state-disabled' );
	}
}




function	enableButton( id )
{
//	alert( 'en but ' + id );
//	alert( $(id).attr( 'disabled') );
	
//	$( id ).attr( 'disabled', '' ).removeClass( 'ui-state-disabled' );
	$( id ).attr( 'disabled', false ).removeClass( 'ui-state-disabled' );
}



function	disableButton( id )
{
//	alert( 'dis but ' + id);
	$( id ).attr( 'disabled', true ).addClass( 'ui-state-disabled' );
//	$( id ).attr( 'disabled', 'disabled' ).addClass( 'ui-state-disabled' );
}




/*  requires a class in the dialog options */
function getDialogButton( dialog_selector, button_name )
{
	var 	jButton;
	var buttons = $( '.' + dialog_selector + ' .ui-dialog-buttonpane button' );

	for ( var i = 0; i < buttons.length; ++i )
	{
		jButton = $( buttons[i] );
		if ( jButton.text() == button_name )
		{
			return jButton;
		}
	} 

	return null;
}




	function	isValidEmail( str )
	{
		var at="@";
		var dot=".";

		var lat=str.indexOf(at);
		var lstr=str.length;
		var ldot=str.indexOf(dot);
		
		if (str.indexOf(at)==-1)
		   return false;
		
		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
			return false;
		
		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
		    return false;
		
		 if (str.indexOf(at,(lat+1))!=-1)
		    return false;

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
		    return false;

		 if (str.indexOf(dot,(lat+2))==-1)
		    return false;
		 		
		 if (str.indexOf(" ")!=-1)
		     return false;
 		 
		 return true;					
	}


	
	
	
/************************** errors & info box ***********************/	
	
	

	function	Erb_showErrorBox( )
	{
		$( '#errorReport' ).show( 'slow' );	
	}
	
	
	function	Erb_showErrors( errs )
	{
		$( '#errlist' ).empty();
		$( '#errorReport' ).show( 'slow' );
		$( '#errlist' ).append( '<ul>' );

		for ( er in errs )
		{
			$( '#errlist' ).append( '<li>' + errs[er] + '</li>' );
		}
		
		$( '#errlist' ).append( '</ul>' );

	}	

	function	Erb_showError( msg )
	{
		$( '#errlist' ).empty();
		$( '#errorReport' ).show( 'slow' );
		$( '#errlist' ).append( '<ul>' );

		$( '#errlist' ).append( '<li>' + msg + '</li>' );
		$( '#errlist' ).append( '</ul>' );
	}	
	

	function	Erb_clearErrors()
	{
		$( '#errorReport' ).hide();
	}
	
	
	
	
	
	function	Erb_showInfo( msg )
	{
		$( '#infolist' ).empty();
		$( '#infoReport' ).show( 'slow' );
		$( '#infolist' ).append( '<ul>' );

		$( '#infolist' ).append( '<li>' + msg + '</li>' );
		$( '#infolist' ).append( '</ul>' );
	}	

	

/*********************** Base Page calls ********************************/

	function	selectRow( key )
	{
		get( 'txtSelect' ).value = key;
		get( 'action' ).value = 'selectrow';
		document.forms[ '_frm' ].submit(); 
		
	}

	
	

	function	selectProduct( key )
	{
//		get( 'txtSelect' ).value = key;
//		get( 'txtViewTargetPage' ).value = 'product.jsp';
//		get( 'action' ).value = 'showproduct';
//		document.forms[ '_frm' ].submit();
		
		
		
		
		
		//document.location.href = g_rootPath + 'product.jsp' + '?txtSelect=' + key;
		document.location.href = g_rootPath + 'core/product/' + key;
	}


	

	function	showBasket()
	{
		//get( 'action' ).value = 'showbasket';
		//document.forms[ '_frm' ].submit(); 
		document.location.href = g_rootPath + 'core/basket';
	}

	function	addToBasket( keyProduct )
	{
		get( 'action' ).value = 'addtobasket';
		get( 'txtSelect' ).value = keyProduct;
		document.forms[ '_frm' ].submit(); 
	}


	// submitted from image, so no need to submit here
	function	recalculate()
	{
		$( '#action' ).val( 'recalculate' );
	}


	// submitted from image, so no need to submit here
	function	proceedToCheckout()
	{
		$( '#action' ).val( 'proceedtocheckout' );
	}



	function	search()
	{
		$( '#action' ).val( 'search' );
		document.forms[ '_frm' ].submit(); 
	}

	

	function	gohome()
	{
		document.location.href = g_rootPath + 'core/home';
	}

	
	function	goSignin()
	{
		document.location.href = g_rootPath + 'core/signin';
	}

	
	
	
	
	
function	get( id )
{
	return document.getElementById( id );
}



function	wget( wnd, id )
{
	return wnd.document.getElementById( id );
}









AV_gen = 
{


	isNumber:				function( value )
	{
//		alert( fld.value );
//		var anum=/(^\d+$)|(^\d+\.\d+$)/
//		if ( anum.test( fld.value ) )
//			return true;
//	
//		return false;
		
		return !isNaN(value);
	},




	trim:					function ( s )
	{
		// remove trailing
		s = s.replace(/\s+$/g,'');
		// and leading
		s = s.replace(/^\s+/g,'');
	
		return s;
	},



	//	adds the class name to the existing 'class=' entry for the passed ctrl name
	addClass:				function ( ctlName, newClass )
	{
	//alert( 'get class for ' + ctlName );
	//alert( get( ctlName ) );

		var		current = get( ctlName ).className;
		
		if ( current.indexOf( newClass ) < 0 )
		{
			 get( ctlName ).className = current + ' ' + newClass; 
		}
//		alert( 'added - ' + get( ctlName ).className );
	},



	removeClass:			function( ctlName, oldClass )
	{
		var		current = get( ctlName ).className;
	
		current = current.replace( oldClass, '' );

		get( ctlName ).className = current;
		//alert( 'removed - ' + get( ctlName ).className );
	
	},
	


	hasClass:			function( ctlName, whatClass )
	{
		var		current = get( ctlName ).className;
	
		if ( current != null )
			return ( current.indexOf( whatClass ) != -1 );
		else
			return false;
	},
	
	
	
	// get the abs Y pos of the passed element
 	findPosY:			function( element )
  	{
		var top = 0;

	    // Try because sometimes errors on offsetParent after DOM changes.
    	try
    	{
        	while (element.offsetParent)
        	{ 	// While we haven't got the top element in the DOM hierarchy
            
            	// Add the offsetTop
            	top += element.offsetTop;
         	   // If my parent scrolls, then subtract the top scroll position
            	if ( element.offsetParent.scrollTop ) 
				{ 
					top -= element.offsetParent.scrollTop; 
				}

	            // Grab
    	        element = element.offsetParent;
        	}
    	}
    	catch (e)
    	{
        	// Do nothing
    	}

	    // Add the top element topoffset and the windows topscroll and subtract the body's client top position.
    	top += element.offsetTop + document.body.scrollTop - document.body.clientTop;

		return top;
	},
		

	show:	function( ctlname )
	{
		AV_gen.removeClass( ctlname, 'invisible' );
//		get( ctlname ).className = '';
	},


	
	hide:	function( ctlname )
	{
		AV_gen.addClass( ctlname, 'invisible' );
//		get( ctlname ).className = 'invisible';
	}



}	// end class




function	slashquotes( str )
{
	str = str.replace( "'", "\'" );
	return str.replace( '"', '\"');
}


