//In this var we keep the ClientID of the label used in the C_Basket
//that keeps the Count of the items in the cart
var CartItemCount_ID='';
//In this var we keep the ClientID of the label used in the C_Basket
//that keeps the Names of the products in the cart
var CartItemDescription_ID='';

var CartTotalPrice='';
var sSessionString='';

//Keeps the maximun chars of the product description shown in the basket
var MaxBasketProductLength=100;

function RandomNumber()
{
var ranNum= Math.round(Math.random()*4);
return ranNum;
}
//test change image on mouse over

function ChangeImage(Bt, imgUrl){
//document.getElementById(Bt).src = imgUrl;
Bt.src = imgUrl;

}

function ChangeVisibility(Lbl, sBoolean){
if (sBoolean)
	Lbl.style.visibility = '';
else
	Lbl.style.visibility = 'hidden';
}
function SwitchVisibility(Lbl){
	var v=Lbl.style.visibility;
	if (v=='hidden')
		Lbl.style.visibility = '';
	else
		Lbl.style.visibility = 'hidden';
}

function toDecimal(original){
	temp = Math.round(original*100)/100;
	if((original * 100) % 100 == 0)
		return temp + '.00';
	if((original * 10) % 10 == 0)
		return temp + '0';
	return temp
}



function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}

function isValidEmail(emailStr){
	if(emailStr.length== 0){
		return true;
	}
	if(!/^\w+([\.\-\+]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(emailStr))
		return false
	return true	
}

function isValidPhone(phoneStr){
	if(phoneStr.length== 0){
		return true;
	}
	if(!/^[0-9\-\(\)]+$/.test(phoneStr))
		return false
	return true	
}
function isFloat(floatStr){
	if(floatStr.length== 0){
		return true;
	}
	if(!/^[0-9\.]+$/.test(floatStr))
		return false
	return true	
}

var time_reg_format = "[0-9]{1,2}\:[0-9]{2}";
function isTime(timeStr){
	time_reg_format = time_reg_format.replace('([ap]m)', '');
	time_reg_format = time_reg_format.replace('([AP]M)', '');
	if(timeStr.length== 0){
		return true;
	}
	//we now support multiple time formats
	myregexp = new RegExp(time_reg_format)
	if(!myregexp.test(timeStr))
		return false

return true
}

/**
 * Ensures a value submitted in a form is numeric and is in a range
 *
 * @param   object   the form
 * @param   string   the name of the form field to check
 * @param   integer  the minimum authorized value
 * @param   integer  the maximum authorized value
 *
 * @return  boolean  whether a valid number has been submitted or not
 */
function checkFormElementInRange(theForm, theFieldName, min, max)
{
    var theField         = theForm.elements[theFieldName];
    var val              = parseInt(theField.value);

    if (typeof(min) == 'undefined') {
        min = 0;
    }
    if (typeof(max) == 'undefined') {
        max = Number.MAX_VALUE;
    }

    // It's not a number
    if (isNaN(val)) {
        theField.select();
        alert(errorMsg1);
        theField.focus();
        return false;
    }
    // It's a number but it is not between min and max
    else if (val < min || val > max) {
        theField.select();
        alert(val + errorMsg2);
        theField.focus();
        return false;
    }
    // It's a valid number
    else {
        theField.value = val;
    }

    return true;
} // end of the 'checkFormElementInRange()' function

/**
  * Checks/unchecks all options of a <select> element
  *
  * @param   string   the form name
  * @param   string   the element name
  * @param   boolean  whether to check or to uncheck the element
  *
  * @return  boolean  always true
  */
function setSelectOptions(the_form, the_select, do_check)
{
    var selectObject = document.forms[the_form].elements[the_select];
    var selectCount  = selectObject.length;

    for (var i = 0; i < selectCount; i++) {
        selectObject.options[i].selected = do_check;
    } // end for

    return true;
} // end of the 'setSelectOptions()' function

function DisableButton(bt)
{
	bt.disabled=true; 
	bt.className='OrderButtonDisabled';
}
function EnableButton(bt)
{
	bt.disabled=false;
	bt.className='OrderButtonOrder';
}
function AddToCart(bt, sSessionRowID, sProductID, iQuantity)
{
	DisableButton(bt);
	var cmd='AddToCart_Core(\''+bt.id+'\',\''+sSessionRowID+'\',\''+sProductID+'\','+iQuantity+')';
	//'alert(cmd);
	setTimeout(cmd,500);
}

function AddToCart_Core(btId, sSessionRowID, sProductID, iQuantity)
{
    var bt=document.getElementById(btId);
	var v=API.AddToCart(sSessionRowID,sProductID,iQuantity);
	
	if (v==null || v.value==null || v.value==false) 
	{
		alert('Add to cart procedure canceled....'); 
		EnableButton(bt);
		return(false);
	}
	
	UpdateCart(sSessionRowID);
	EnableButton(bt);
}


function UpdateCart(sSessionRowID)
{
	var v;
	v=API.GetCartItemCount(sSessionRowID); 
	if (v!=null && v.value!=null)
	{
		var c=document.getElementById(CartItemCount_ID); 
		if (c!=null) 
		{
			c.innerHTML=''+v.value;
		} 
	}
	v=API.GetCartItemDescription(sSessionRowID,MaxBasketProductLength); 
	if (v!=null && v.value!=null)
	{
		var c=document.getElementById(CartItemDescription_ID); 
		if (c!=null) 
		{
			c.innerHTML=''+v.value;
		} 
	}
	v=API.GetTotalPrice(sSessionString); 
	if (v!=null && v.value!=null)
	{
		var c=document.getElementById(CartTotalPrice); 
		if (c!=null) 
		{
			c.innerHTML=''+v.value+'&euro;';
		} 
	}
}

function AddToCart_Core_BAck(bt, sSessionRowID, sProductID, iQuantity)
{
	bt.disabled=true; 
	var bg=bt.style.backgroundColor; 
	bt.style.backgroundColor='#000000';
	var v=API.AddToCart(sSessionRowID,sProductID,iQuantity);
	
	if (v==null || v.value==null || v.value==false) 
	{
		alert('Η προσθήκη στο καλάθι σας ΔΕΝ πραγματοποιήθηκε'); 
		bt.disabled=false;
		return(false);
	}
	v=API.GetCartItemCount(sSessionRowID); 
	if (v!=null && v.value!=null)
	{
		var c=document.getElementById(CartItemCount_ID); 
		if (c!=null) 
		{
			c.innerText=''+v.value+' products';
		} 
	}
	bt.style.backgroundColor=bg;
	bt.disabled=false;
}


//Hooks the Enter key event for a textbox to a button
function fnTrapKD(btn, event)
{
	if (document.all)
	{
		if (event.keyCode == 13)
		{
			event.returnValue=false;
			event.cancel = true;
			btn.click();
		}
	}
 else 
	if (document.getElementById)
	{
		if (event.which == 13)
		{
			event.returnValue=false;
			event.cancel = true;
			btn.click();
		}
	}
 else 
	if(document.layers)
	{
		if(event.which == 13)
		{
			event.returnValue=false;
			event.cancel = true;
			btn.click();
		}
	}
}
var newwin
function basicwindow(url,w,h) {
var settings = '"toolbar=no, scrollbars=yes, resizable=yes, directories=no, Left = 50, Top = 50,  width='+w+', height='+h+'"';

closewindow();
var newwin = window.open(url, 'newwin' ,settings);
}
function closewindow() {
if (newwin && !newwin.closed) {
newwin.close();
}
}
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


function addBookmark2(title,url) {
if (window.sidebar) {
window.sidebar.addPanel(title, url,"");
} else if( document.all ) {
window.external.AddFavorite( url, title);
} else if( window.opera && window.print ) {
return true;
}
}









