﻿function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function ValidateStringLength(theString) {
	var theTrimString = trim(theString);
	if (theTrimString.length == '0') {
		return false;	
	} else {
		return true;
	}	
}

function isNumeric(sText) {
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char;
   for (i = 0; i < sText.length && IsNumber == true; i++) { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) {
         IsNumber = false;
	  }
   }
   return IsNumber;
}

function ValidateStringSpecialChars(theString) {
	
	var iChars = "#$^=[]\\\'{}|\"<>";
	
	for (var i = 0; i<theString.length; i++) {
	  if (iChars.indexOf(theString.charAt(i)) != -1) {
		return false;
	  }
    }

	return true;
}

function OnCheckAll( theForm, theName, theChecked ) {
// used to mark all check boxes in a list as "checked"
	for( var i=0; i<theForm.length; i++ ) {
        var input = theForm[i];
        if( input.nodeName == 'INPUT' )
            if( input.type == 'checkbox' && input.name == theName && ! input.disabled )
                if( input.checked != theChecked )
                    input.checked = theChecked;
    }
}

function ChangeGroupActionAndSubmit( theForm, theValue ) {
	theForm.MM_GroupAction.value=theValue;
	theForm.submit();
}

function ModulePrintPage() {
  lochref = '' + this.location;
	if (lochref.indexOf('?') > 0) {
		lochref = '?' + lochref.substring((lochref.indexOf('?')) + 1);
	} else {
		lochref = '';
	}

	w_width = 790;
	w_height = 500;
	if (screen) {
		w_left = (screen.availWidth - w_width)/2;
		w_top = ((screen.availHeight - w_height)/2)-20;
	}
	theWindowParam = 'width='+w_width+',height='+w_height+',left='+w_left+',top='+w_top+',status=no,resizable=no,scrollbars=yes';

	fparam = 'print_mod.php'+lochref;
  window.showPrintWindow = open(fparam, 'showPrintWindow', theWindowParam);
	setTimeout('if(showPrintWindow&&!showPrintWindow.closed)showPrintWindow.focus()',100);
}

function ValidateForm_SendEmail_General(theForm) {
  var fname = theForm.fname.value;
  var femail = theForm.femail.value;
  var name = theForm.name.value;
  var email = theForm.email.value;
	
	if (ValidateStringLength(fname) == false) {
		alert("Πρέπει να γράψετε το όνομα του φίλου σας.");
		return false;
	} else if (ValidateStringLength(femail) == false) {
		alert("Πρέπει να γράψετε το email του φίλου σας.");
		return false;
	} else if ((femail.indexOf('@') == -1) || (femail.indexOf('.') == -1)) {
		alert("Λάνθασμένος τύπος email του φίλου σας.");
		return false;
	} else if (ValidateStringLength(name) == false) {
		alert("Πρέπει να γράψετε το όνομα σας.");
		return false;
	} else if (ValidateStringLength(email) == false) {
		alert("Πρέπει να γράψετε το email σας.");
		return false;
	} else if ((email.indexOf('@') == -1) || (email.indexOf('.') == -1)) {
		alert("Λάνθασμένος τύπος του email σας.");
		return false;
	} else {
		return true;
	}
}

function CountCheckedRecs( theForm, theName ) {
// used to mark all check boxes in a list as "checked"
	var Checked = 0;
    for( var i=0; i<theForm.length; i++ ) {
        var input = theForm[i];
        if( input.nodeName == 'INPUT' )
            if(input.type == 'checkbox' && input.name == theName && input.checked) {
				Checked++;	
			}
    }
	return Checked;
}

function ShowImageNormalSize(theImage) {
	w_width = 100;
	w_height = 100;
	if (screen) {
		w_left = 10;
		w_top = 10;
	}
	theWindowParam = 'width='+w_width+',height='+w_height+',left='+w_left+',top='+w_top+',status=no,resizable=yes,scrollbars=yes';
	
	fparam = 'admin/modules/ws_mod_pcm_01/tools/show_normal_size.php?img=' + theImage;
  window.ShowImageNormalSizeWindow = open(fparam, 'ShowImageNormalSizeWindow', theWindowParam);
	setTimeout('if(ShowImageNormalSizeWindow&&!ShowImageNormalSizeWindow.closed)ShowImageNormalSizeWindow.focus()',100);
}

/* Calendar functions */
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 stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function findPosY(obj) {
  var curtop = 0;
  if(obj.offsetParent)
	  while(1)
	  {
		curtop += obj.offsetTop;
		if(!obj.offsetParent)
		  break;
		obj = obj.offsetParent;
	  }
  else if(obj.y)
	  curtop += obj.y;
  return curtop;
}

function findPosX(obj) {
  var curleft = 0;
  if(obj.offsetParent)
	  while(1) 
	  {
		curleft += obj.offsetLeft;
		if(!obj.offsetParent)
		  break;
		obj = obj.offsetParent;
	  }
  else if(obj.x)
	  curleft += obj.x;
  return curleft;
}

function getScrollWidth()
{
   var w = window.pageXOffset ||
           document.body.scrollLeft ||
           document.documentElement.scrollLeft;
           
   return w ? w : 0;
} 

function getScrollHeight()
{
   var h = window.pageYOffset ||
           document.body.scrollTop ||
           document.documentElement.scrollTop;
           
   return h ? h : 0;
}

function setCharAt(str,index,chr) {
	if(index > str.length-1) return str;
	return str.substr(0,index) + chr + str.substr(index+1); 
} 

/* MODULE FUNCTIONS */
function ReturnAjaxObj() {
	var ajaxRequest;
	try {
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try {
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Something is wrong with your browser!");
				return false;
			}
		}
	}
	return ajaxRequest;
}

function ChangeSectionsMenuImg(SctId, Case) {
	if (Case == '1') {
		$("#sct_"+SctId+"_on").css("display","none");	
		$("#sct_"+SctId+"_over").css("display","block");	
	} else {
		$("#sct_"+SctId+"_on").css("display","block");	
		$("#sct_"+SctId+"_over").css("display","none");	
	}
}

function ChangeCatMenuBg(theCase, theId) {

	if (theCase == '1') {
		$('td[title=catmenu_' + theId + ']').each(function(){
			$(this).removeClass().addClass("submenusel");
			$("#catmenu_" + theId + "_bg").css("backgroundColor","#F84101");
		});	
	} else if (theCase == '2') {
		$('td[title=catmenu_' + theId + ']').each(function(){
			$(this).removeClass().addClass("submenuon");
			$("#catmenu_" + theId + "_bg").css("backgroundColor","#FFFFFF");
		});	
	}
}

function FilteringClear(BlockNum) {
	if (BlockNum == 'A') {
	  $("input[name='Block1Boxes']").attr('checked', false);
	  //$("input[name='Block1Selector']").attr('checked', true);
	  $("input[name='Block2Boxes']").attr('checked', false);
	  //$("input[name='Block2Selector']").attr('checked', true);
	} else {
	  $("input[name='Block"+BlockNum+"Boxes']").attr('checked', false);
	  //$("input[name='Block"+BlockNum+"Selector']").attr('checked', true);
	}
	FilteringDisplayRecs();	
}

function FilteringShowAllRecs(BlockNum) {
	$("input[name='Block"+BlockNum+"Boxes']").attr('checked', false);
	FilteringDisplayRecs();
}

function FilteringDisplayRecs() { // GET
	
	var theQueryStringParam = $('#qrystring').val();
	var theSelBrands, theSelSizes;
	/*Brands*/
	var theSelIdsBlock1 = [];
	$("input[name=Block1Boxes]").each( function() {
		if( $(this).is(':checked') ) {
			theSelIdsBlock1.push($(this).val());   
		}
	});
	theSelBrands = theSelIdsBlock1.join('|');
	/*****/
	
	/*Sizes*/
	var theSelIdsBlock2 = [];
	$("input[name=Block2Boxes]").each( function() {
		if( $(this).is(':checked') ) {
			theSelIdsBlock2.push($(this).val());   
		}
	});
	theSelSizes = theSelIdsBlock2.join('|');
	/*****/
	
	var queryString = "";
	if (theQueryStringParam) {
		queryString = "?"+theQueryStringParam;
	}
	
	/*WITH checkbox all*/
	/*
	if (theSelBrands) {
		$("input[name='Block1Selector']").attr('checked', false);
		queryString += "&brands="+theSelBrands;
	} else {
		$("input[name='Block1Selector']").attr('checked', true);
	}
	*/
	if (theSelBrands) {
		queryString += "&brands="+theSelBrands;
	}
	
	if (theSelSizes) {
		queryString += "&sizes="+theSelSizes;
	}
	if (queryString.substr(0,1) == "&") {
		queryString = setCharAt(queryString,0,'?'); 
	}
	UpdateRecordsBlockBody(queryString);
}

function DisplayFilterSizes(theQueryString) {
	var ajaxRequest = ReturnAjaxObj();
	
	if (theQueryString.substr(0,1) != "?") {
	  if (theQueryString.substr(0,1) == "&") {
		  theQueryString = setCharAt(theQueryString,0,'?'); 
	  } else {
		  theQueryString = "?"+theQueryString; 
	  }
	}
	
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4) {
			var ajaxDisplay = document.getElementById("FBL2_block");
			ajaxDisplay.innerHTML = ajaxRequest.responseText;
		}
	}
	
	ajaxRequest.open("GET", "modules/ws_mod_pcm_01/blocks/filtering/get_sizes.php"+theQueryString, true);
	ajaxRequest.send(null); 
}

function ManFilterBlocks(BlockName, theQryString) {
	if ($('#'+BlockName+'_close_btn').is(":hidden")) {
	  $('#'+BlockName+'_close_btn').css("display","block");
	  $('#'+BlockName+'_open_btn').css("display","none");
	  $('#'+BlockName+'_block').css("display","none");
	  $('#'+BlockName+'_clear_btn').css("display","none");
	} else {
	  $('#'+BlockName+'_close_btn').css("display","none");
	  $('#'+BlockName+'_open_btn').css("display","block");
	  $('#'+BlockName+'_block').css("display","block");
	  $('#'+BlockName+'_clear_btn').css("display","block");
	  if (BlockName == 'FBL2') {
		  DisplayFilterSizes(theQryString);
	  }
	}
}
function PagingDisplayRecs(thePageNum, theQueryString, theLang) { // GET
	
	var queryString;
	if (theQueryString != '') {
		queryString = "?"+theQueryString+"&pagenum="+thePageNum;
	} else {
		queryString = "?pagenum="+thePageNum;
	}
	queryString = queryString+"&lang="+theLang;
	
	UpdateRecordsBlockBody(queryString);
}



function UpdateRecordsBlockBody(theQueryString) {

	/* setup loading block */
	var new_block_h = parseInt(document.getElementById("RecsListBlock").scrollHeight, 10);
	if ((!new_block_h)||(new_block_h <= 30)) { new_block_h = 50; }
	var new_block_w = parseInt(document.getElementById("RecsListBlock").scrollWidth, 10);
	new_block_h = new_block_h-30;
	
	document.getElementById("RecsListBlock_loading").style.width = new_block_w+"px";
	document.getElementById("RecsListBlock_loading").style.height = new_block_h+"px";
	
	document.getElementById("RecsListBlock_loading").style.display = "block";
	setTimeout('document.images["RecsListBlock_loading_img"].src = "layout/images/loading.gif"', 10); 
	document.getElementById("RecsListBlock_loading_img").style.marginTop = ((new_block_h/2)-32);
	/* */
	
	var ajaxRequest = ReturnAjaxObj();
	//updating - no cache
	//queryString = queryString+"&sid="+Math.random();
	
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4) {
			var ajaxDisplay = document.getElementById("RecsListBlock");
			ajaxDisplay.innerHTML = ajaxRequest.responseText;
			$("#printbtn").attr("href", "Javascript:printPage('index.php"+theQueryString+"');");
			$("#emailbtn").attr("href", "Javascript:emailPage('index.php"+theQueryString+"');");
			$.scrollTo( '#PrdsTop', {duration:600} );
		}
	}

	ajaxRequest.open("GET", "modules/ws_mod_pcm_01/products/update_records.php"+theQueryString, true);
	ajaxRequest.send(null); 
}

/*New Arrivals Block*/
/* FA 04 Functions */
function NewPrdsBlockNavButtons(BlockPageNum, MaxBlockRecs, QueryString){

	/* setup loading block */
	var new_block_h = parseInt(document.getElementById("HBlockContentArea").scrollHeight, 10);
	var new_block_w = parseInt(document.getElementById("HBlockContentArea").scrollWidth, 10);
	//new_block_h = new_block_h; // paging row height
	document.getElementById("h_block_loading").style.width = new_block_w + "px";
	document.getElementById("h_block_loading").style.height = new_block_h + "px";
	
	document.getElementById("h_block_loading").style.display = "block";
	// setTimeout('document.images["block_"' + BlockNum + '"_loading_img"].src = "layout/images/loading.gif"', 10); 
	document.getElementById("h_block_loading_img").style.marginTop = ((new_block_h / 2) - 16);
	/* */
	var ajaxRequest = ReturnAjaxObj(); 
	
	var queryString = "?blocknum=" + BlockPageNum + "&maxrecs=" + MaxBlockRecs + QueryString;
	
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4) {
			var ajaxDisplay = document.getElementById("HBlockContentArea");
			ajaxDisplay.innerHTML = ajaxRequest.responseText;
		}
	}
	ajaxRequest.open("GET", "modules/ws_mod_pcm_01/blocks/new_arrivals/update_block.php" + queryString, true);
	ajaxRequest.send(null); 
}

function DisplayPrdImage(Id, ImgW, ImgH, Case, Sizes, event) {
	if (Case == '1') {
		var PhotoSrc = $("#img_a_"+Id).attr('name');
		if (PhotoSrc) {
		  var ShowHtml = '<table cellspacing="0" cellpadding="0" border="0" class="ProductsPage"><tr><td><img src="'+PhotoSrc+'" border="0" width="'+ImgW+'"></td></tr>';
		  if (Sizes != "") {
			  ShowHtml = ShowHtml+"<tr><td class='title' height='24' valign='bottom'>Available Sizes: "+Sizes+"</td></tr>";
		  }
		  ShowHtml += "</table>";
		  $("#ImgPreview").html(ShowHtml);
		  
		  /*PUT IN POSITION*/
		  var btn_pos = $("#img_"+Id).offset();
		  BtnLeftPos = btn_pos.left+16;
		  BtnTopPos = btn_pos.top+5;
		  
		  var RightAvailableImgSpace = $(window).width()-BtnLeftPos;
		  if (ImgW > RightAvailableImgSpace) {
			BtnLeftPos = BtnLeftPos-((ImgW-RightAvailableImgSpace)+30);
		  }
		  
		  var BottomAvailableImgSpace = $(window).height()-(BtnTopPos-$(document).scrollTop());
		  if (ImgH > BottomAvailableImgSpace) {
			BtnTopPos = BtnTopPos-((ImgH-BottomAvailableImgSpace)+48);
		  }
		  
		  $("#ImgPreview").css("marginLeft",(BtnLeftPos)+"px");
		  $("#ImgPreview").css("marginTop",(BtnTopPos)+"px");
		  //$("#ImgPreview").show(200);
		  $("#ImgPreview").css("display","block");
		}
	} else if(Case=='2') {
		HidePrdImg(event);
	}
}

function HidePrdImg(event) {
		var toElement = null;
	
		if (event.relatedTarget) {
			toElement = event.relatedTarget;
		} else if (event.toElement) {
			toElement = event.toElement;
		}
		
		while (toElement && toElement.id != "ImgPreview") {
			toElement = toElement.parentNode;
		}
		if (!toElement) {
		  $("#ImgPreview").html('<img src="layout/images/loading_2.gif" border="0">');
		  $("#ImgPreview").css("display","none");
		}
}


