
function ChangeLanguage(lang)
{
       var actualLink  = '' ;
       var tmpLink = document.location ;
	   var regex = /.language=../g;
	
       tmpLink = tmpLink.toString();

       var tmp = tmpLink.substring(1*tmpLink.length-1,tmpLink.length);

       if(tmp == '#')
       {
               actualLink = tmpLink.substring(0,1*tmpLink.length-1);
       }
       else
       {
               actualLink = tmpLink ;
       }

       if( actualLink.indexOf('?') == -1 )
               actualLink = actualLink+'?' ;

       actualLink = actualLink.replace(regex, '')
	   actualLink = actualLink+'&language='+ lang;

       document.location = actualLink ;
}




function OpenLinkInNewWindow(link,title)
{
	window.open(link , title , 'menubar=no,toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=no,fullscreen=no,channelmode=no,width=630,height=600')
}

function addopinion(Id)
{
	window.open('http://uzyrafy.pl/controlers/addopinion.inc.php?productid='+Id, 'opinion', 'menubar=no,toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=no,fullscreen=no,channelmode=no,width=600,height=650');	
}

function addreview(Id)
{
	window.open('http://uzyrafy.pl/controlers/addreview.inc.php?productid='+Id, 'review', 'menubar=no,toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=no,fullscreen=no,channelmode=no,width=600,height=650');	
}

function addimage(Id)
{
	window.open('http://uzyrafy.pl/controlers/addimage.inc.php?productid='+Id, 'image', 'menubar=no,toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=no,fullscreen=no,channelmode=no,width=600,height=650');	
}



function subscriptionClick()
{

/*
	if( document.getElementById('subscription').checked == true )
	{
		document.getElementById('gamecategory1').checked = true ; 
		document.getElementById('gamecategory2').checked = true ; 
		document.getElementById('gamecategory3').checked = true ; 
		document.getElementById('gamecategory4').checked = true ; 
		document.getElementById('gamecategory5').checked = true ; 
		document.getElementById('gamecategory6').checked = true ; 
		document.getElementById('gamecategory7').checked = true ; 
		document.getElementById('gamecategory8').checked = true ; 
		document.getElementById('gamecategory9').checked = true ; 
		document.getElementById('gamecategory10').checked = true ; 
		document.getElementById('subscriptionsubcategory').style.display = '' ; 
	}
	else
	{
		document.getElementById('subscriptionsubcategory').style.display = 'none' ; 
		document.getElementById('gamecategory1').checked = false ; 
		document.getElementById('gamecategory2').checked = false ; 
		document.getElementById('gamecategory3').checked = false ; 
		document.getElementById('gamecategory4').checked = false ; 
		document.getElementById('gamecategory5').checked = false ; 
		document.getElementById('gamecategory6').checked = false ; 
		document.getElementById('gamecategory7').checked = false ; 
		document.getElementById('gamecategory8').checked = false ; 
		document.getElementById('gamecategory9').checked = false ; 
		document.getElementById('gamecategory10').checked = false ; 
		
	}
	*/
}

function ShowInDescriptionWindow(tag)
{
	document.getElementById('productdescriptionlitewindow_opinion').style.visibility = 'hidden' ;  
	document.getElementById('productdescriptionlitewindow_opinion').style.display = 'none' ;
	
	document.getElementById('productdescriptionlitewindow_comment').style.visibility = 'hidden' ;  
	document.getElementById('productdescriptionlitewindow_comment').style.display = 'none' ;
	
	document.getElementById('productdescriptionlitewindow_mailtofriend').style.visibility = 'hidden' ;  
	document.getElementById('productdescriptionlitewindow_mailtofriend').style.display = 'none' ;
	
	document.getElementById('productdescriptionlitewindow_question').style.visibility = 'hidden' ;  
	document.getElementById('productdescriptionlitewindow_question').style.display = 'none' ;
	
	document.getElementById('productdescriptionlitewindow_'+tag).style.visibility = 'visible' ; 
	document.getElementById('productdescriptionlitewindow_'+tag).style.display = 'inline' ;
	
}

function ReloadWithVariable(name,value)
{	
	document.getElementById('var').name 	= name ; 
	document.getElementById('var').value 	= value; 
	
	document.mForm.submit();
}

function LoadProductDetail(src)
{
	document.getElementById('productFrame').src=src ; 
}

function setCurrentPage(value)
{
	document.getElementById('currentPage').value= value; 
	document.mForm.submit();
}

function send()
{
	document.getElementById('sendquery').value = 'yes' ;  
	document.mForm.submit();
}


function SendSearchTerm()
{
	serchterm = document.getElementById('searchtermvalue').value ; 
	serchterm = serchterm.replace('+','');
	
	if(serchterm == '')
		document.location = 'http://uzyrafy.pl/zaawansowane.html';
	else
	{
		document.mForm.action = 'http://uzyrafy.pl/szukaj-searchterm='+serchterm+'.html' ;
		document.mForm.submit();
	}
}

function SearchBySearchTerm(serchterm)
{

	//serchterm = document.getElementById('searchtermvalue').value ; 
	serchterm = serchterm.replace('+','');

	if(serchterm == '')
		document.location = 'http://uzyrafy.pl/zaawansowane.html';
	else
	{
		document.mForm.action = 'http://uzyrafy.pl/szukaj-searchterm='+serchterm+'.html' ;
		document.mForm.submit();
	}
}



function SearchProducts()
{ 
	searchterm 	= document.getElementById('searchadvanced_searchterm').value ;  
	category 	= document.getElementById('searchadvanced_category').value ;  
	producent 	= document.getElementById('searchadvanced_producent').value ;  
	maxprice 	= document.getElementById('searchadvanced_maxprice').value ;  
	news 		= document.getElementById('searchadvanced_news') ;  
	giraffe 	= document.getElementById('searchadvanced_giraffe') ;  
	
	
	var query = '?' ; 
	
	if(searchterm != '')
		query += 'searchterm='+searchterm+'&' ; 
	
	if(category != '')
		query += 'category='+category+'&' ; 
	
	if(producent != '')
		query += 'producent='+producent+'&' ; 
	
	if(maxprice != '')
		query += 'maxprice='+maxprice+'&' ; 
	
	if(news.checked == true )
		query += 'news=1&' ; 
		
	if(giraffe.checked == true )
		query += 'giraffe=1&' ; 
	
	document.mForm.action = 'http://uzyrafy.pl/szukaj.html'+query;
	document.mForm.submit();
}

function toggleImageUpload()
{
	if(document.getElementById('accountuploaddiv').style.display == 'none')
		document.getElementById('accountuploaddiv').style.display = 'inline' ; 
	else
		document.getElementById('accountuploaddiv').style.display = 'none' ; 	
		
	


}

function SelectSearchRadio(value)
{
	document.getElementById('searchadvanced_maxprice').value  = value;  
	
	
}

function Login(path)
{
	document.getElementById('login_check').value = 'yes' ; 
	document.mForm.action = path+'logowanie';
	document.mForm.submit();	
}

function LoginPartner()
{
	document.getElementById('login_partner_check').value = 'yes' ; 
	document.mForm.submit();	
}

function changeMainPicture(r)
{
	document.getElementById('mainPicture').src = r.src ; 
}

function CheckOrder()
{
	document.getElementById('new_user_check').value = 'yes' ;
	document.mForm.submit();
}

function CheckRegistration()
{
	document.getElementById('new_user_check').value = 'yes' ;
	document.mForm.submit();
}

function Search(action)
{
	document.mForm.action = window.location+action;
	document.mForm.submit();	
}


function NextResult(next,sorting,category)
{	
	//document.getElementById('currentPage').value = next ;	
	document.mForm.action = '?page=search&currentPage='+next+'&sorting='+sorting+'&category='+category ;
	document.mForm.submit();
}

function PreviousResult(previous,sorting,category)
{
	//document.getElementById('currentPage').value = previous ;	
	document.mForm.action = '?page=search&currentPage='+previous+'&sorting='+sorting+'&category='+category ;
	document.mForm.submit();
}

function ShowDescription(id)
{
	document.mForm.action="index.php?page=description&productId="+id;
	document.mForm.submit();
}

function ReloadCart(action)
{
	document.mForm.action = action;
	document.mForm.submit();
}


function SubmitAction(action)
{
	document.mForm.action = action;
	document.mForm.submit();
}


function SearchByCategory(category)
{	

	//document.getElementById('filter_category').value = category ;
	document.mForm.action = 'index.php?page=search&category='+category;
	document.mForm.submit();	
}




function setMenuPointer(theRow, RowNr, theAction)
{
    var theDefaultClass = 'MenuList';
    var thePointerClass = 'MenuListHigh';
    var theMarkClass    = 'MenuListMark';    

	switch(theAction)
	{
	  case "over" :
		  if(theRow.className != theMarkClass)
		    theRow.className = thePointerClass ;
	  break;
	  case "out" :
		  if(theRow.className != theMarkClass)
		    theRow.className = theDefaultClass ;
	  break;
	  case "click" :
		
		  if(theRow.className != theMarkClass)
		    theRow.className = theMarkClass ;
	      else theRow.className = thePointerClass ;
	  break;
	}
}


function setPointer(theRow, RowNr, theAction)
{

    var theCells = null;

    var theDefaultClass = 'listTableRowLow';
    var thePointerClass = 'listTableRowHigh';
    var theMarkClass    = 'listTableRowMark';    

	switch(theAction)
	{
	  case "over" :
		  if(theRow.className != theMarkClass)
		    theRow.className = thePointerClass ;
	  break;
	  case "out" :
		  if(theRow.className != theMarkClass)
		    theRow.className = theDefaultClass ;
	  break;
	  case "click" :
		
		  if(theRow.className != theMarkClass)
		    theRow.className = theMarkClass ;
	      else theRow.className = thePointerClass ;
	  break;
	}
}



function SelectAllCheckBox(name)
{
	for (i=0;i< document.mForm.elements.length;i++) 
	{
		if(document.mForm.elements[i].type=='checkbox' && document.mForm.elements[i].name.substring(0,name.length) == name)
		{
			document.mForm.elements[i].checked = true;
		}
		
	}  
}



function ShowImage(id)
{
	var imagePointer = 'image['+id+']' ; 
	var imageName = document.getElementById(imagePointer).value ; 
	//alert(imageName)
	ChangeImage(imageName) ; 
}
 
	
function ChangeImage(name)
{
	var tab				= GetPositionTable();
	var currently = GetCurrentlyPosition(name);
	var tableName = 'imagetable['+currently+']' ; 
	
	var tmpTableName ; 

	for( var t = 0 ; t < tab.length ; t ++ )
	{
		tmpTableName = 'imagetable['+t+']' ; 
		//document.getElementById(tmpTableName).border = 0 ;
	}

	//document.getElementById(tableName).border = 1 ;
	document.getElementById('mainimage').src = name ;
}

function GetPositionTable()
{	
	var tab = new Array(100) ; 
	var k = 0 ; 

	for(var t = 0 ; t < document.mForm.elements.length ; t++)
	{
		if(document.mForm.elements[t].name.substring(0,5) == 'image')
		{
			tab[k] = document.mForm.elements[t].value ; 
			k ++ ;
		}
	}

	var newTab = new Array(k);

	for( var t = 0 ; t < k ; t++ )
	{
		newTab[t] = tab[t] ; 
	}

	return newTab ; 
}



function GetCurrentlyPosition(name)
{
	var tab = GetPositionTable() ; 

	for(var t = 0 ; t < tab.length ; t++)
	{
		if(tab[t] == name)
		{
			return t ; 
		}
	}
}



function ShowNext()
{
	var tab = GetPositionTable() ; 
	var name = document.getElementById('mainimage').src ; 

	var currently = GetCurrentlyPosition(name) ; 

	if( currently+1 <= tab.length -1 )
		ChangeImage(tab[currently+1]) ; 		
	else 
		ChangeImage(tab[0]) ; 
}



function ShowPrevious()
{
	var tab = GetPositionTable() ; 
	var name = document.getElementById('mainimage').src ; 
	var currently = GetCurrentlyPosition(name) ; 

	if( currently-1 >= 0 )
		ChangeImage(tab[currently-1]) ;
	else 
		ChangeImage(tab[tab.length-1]) ;
}



function ShowFirstImage()
{
	var imageTable =  GetPositionTable();
	ChangeImage(imageTable[0]) ;
}



function ShowLastImage()
{
	var imageTable =  GetPositionTable();
	ChangeImage(imageTable[imageTable.length-1]) ;
}



function CallSlideShow()
{
	if(document.getElementById('slideFlag').value == 0)
	{
		document.getElementById('slideFlag').value = 1 ;
		SlideShow() ;
	}
	else
	{
		document.getElementById('slideFlag').value = 0 ;
	}
}


function UnselectAllCheckBox(name)
{
	for (i=0;i< document.mForm.elements.length;i++) 
	{
		if(document.mForm.elements[i].type=='checkbox' && document.mForm.elements[i].name.substring(0,name.length) == name)
		{
			document.mForm.elements[i].checked = false;
		}
		
	}  
}

function ReverseCheckBox(name)
{
	for (i=0;i< document.mForm.elements.length;i++) 
	{
		if(document.mForm.elements[i].type=='checkbox' && document.mForm.elements[i].name.substring(0,name.length) == name)
		{
			if(document.mForm.elements[i].checked)
				document.mForm.elements[i].checked = false;
			else 
				document.mForm.elements[i].checked = true ; 
		}
		
	}  
}


function SlideShow() 
{
	if(document.getElementById('slideFlag').value == 0)
		return ;

	var delay = 1; 
	
	ShowNext();
	
	setTimeout("SlideShow()",delay*2000);
}



function ChangeTotalPrice()
{
	var totalPriceField = document.getElementById('totalPriceField').value ;
	
	/* Delivery */
	var deliveryType = document.getElementById('order_deliverytype') ; 
	var deliveryId = deliveryType[deliveryType.selectedIndex].value ; 
	
	var tmpPrice = "deliveryPrices_"+deliveryId ; 
	var tmpName = "deliveryName_"+deliveryId ; 
	
	var deliveryPriceField = document.getElementById(tmpPrice).value ; 
	var deliveryNameField = document.getElementById(tmpName).value ; 
	
	/* Payment */
	var paymentType = document.getElementById('order_paymenttype') ; 
	var paymentId = paymentType[paymentType.selectedIndex].value ; 
	
	var tmpPrice = "paymentPrices_"+paymentId ; 
	var tmpName = "paymentName_"+paymentId ; 
	
	var paymentPriceField = document.getElementById(tmpPrice).value ; 
	var paymentNameField = document.getElementById(tmpName).value ; 
		 
	/* Discount */
//		 discountPrices_
		 obj = document.getElementsByTagName("input") ; 
		 
		 
		 var totalDiscount = 0  ;
		 
		 for(var t = 0 ; t < obj.length; t++)
		 {
		 		if(obj[t].id.substring(0,14) == 'discountValue_')
		 		{
		 			id 		= obj[t].id.substring(14,obj[t].id.length) ; 
		 			type 	= document.getElementById('discountKind_'+id).value ; 
		 			value = obj[t].value ; 
		 			
		 			if(type == 'percentage')
		 			{
		 				totalDiscount +=  totalPriceField*value/100 ; 
		 				document.getElementById('discountPriceField_'+id).innerHTML = sprintf("-%.2f",totalPriceField*value/100) ;  
		 			}
		 			else
		 			{
		 				totalDiscount +=  totalPriceField ;
		 				document.getElementById('discountPriceField_'+id).innerHTML = sprintf("-%.2f",totalPriceField) ;  
		 			}		 					
		 		}		 		
		 }
		 
	var paymentType = document.getElementById('order_paymenttype') ; 
	var paymentId = paymentType[paymentType.selectedIndex].value ; 
	
	var tmpPrice = "paymentPrices_"+paymentId ; 
	var tmpName = "paymentName_"+paymentId ; 
	
	var paymentPriceField = document.getElementById(tmpPrice).value ; 
	var paymentNameField = document.getElementById(tmpName).value ; 
		 
		 
	//document.getElementById('deliveryTypeField').innerHTML = deliveryNameField ; 
	document.getElementById('deliveryPriceField').innerHTML = sprintf("%.2f",deliveryPriceField) ;  
	document.getElementById('paymentPriceField').innerHTML = sprintf("%.2f",paymentPriceField) ;  
	
	document.getElementById('allPriceField').innerHTML =  sprintf("%.2f", 1*totalPriceField + 1*deliveryPriceField + 1*paymentPriceField - totalDiscount) ; 
}

function OpenAdditionalWindow(id)
{
	if(document.getElementById(id).style.display == 'none')
		document.getElementById(id).style.display = 'inline';		
	else 
		document.getElementById(id).style.display = 'none';		
}

function PrintOrderDetail(id)
{
	window.open('controlers/printorderdetail.inc.php?edit_order_id='+id, 'printOrderDetail', 'menubar=no,toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=no,fullscreen=no,channelmode=no,width=700,height=600')
}

function AddInquiry()
{
	window.open('addinquiry', 'Inquiry', 'menubar=no,toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=yes,fullscreen=no,channelmode=no,width=400,height=300');
	document.mForm.action = 'addinquiry' ;
	tmp = document.mForm.target ;  
	document.mForm.target = 'Inquiry' ; 
	document.mForm.submit();
	document.mForm.target = tmp ; 		
}

function OpenCmpareWindow()
{
	window.open('compareproducts', 'CompareProduct', 'menubar=no,toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=yes,fullscreen=yes,channelmode=no,width=900,height=700');
	document.mForm.action = 'compareproducts' ;
	tmp = document.mForm.target ;  
	document.mForm.target = 'CompareProduct' ; 
	document.mForm.submit();
	document.mForm.target = tmp ; 		
}


function ReloadPage(action)
{
	document.getElementById('myaction').value = action ;
	document.mForm.submit();	
}




sprintfWrapper = {

    init : function () {

        if (typeof arguments == 'undefined') { return null; }
        if (arguments.length < 1) { return null; }
        if (typeof arguments[0] != 'string') { return null; }
        if (typeof RegExp == 'undefined') { return null; }

        var string = arguments[0];
        var exp = new RegExp(/(%([%]|(\-)?(\+|\x20)?(0)?(\d+)?(\.(\d)?)?([bcdfosxX])))/g);
        var matches = new Array();
        var strings = new Array();
        var convCount = 0;
        var stringPosStart = 0;
        var stringPosEnd = 0;
        var matchPosEnd = 0;
        var newString = '';
        var match = null;

        while (match = exp.exec(string)) {
            if (match[9]) { convCount += 1; }

            stringPosStart = matchPosEnd;
            stringPosEnd = exp.lastIndex - match[0].length;
            strings[strings.length] = string.substring(stringPosStart, stringPosEnd);

            matchPosEnd = exp.lastIndex;
            matches[matches.length] = {
                match: match[0],
                left: match[3] ? true : false,
                sign: match[4] || '',
                pad: match[5] || ' ',
                min: match[6] || 0,
                precision: match[8],
                code: match[9] || '%',
                negative: parseInt(arguments[convCount]) < 0 ? true : false,
                argument: String(arguments[convCount])
            };
        }
        strings[strings.length] = string.substring(matchPosEnd);

        if (matches.length == 0) { return string; }
        if ((arguments.length - 1) < convCount) { return null; }

        var code = null;
        var match = null;
        var i = null;

        for (i=0; i<matches.length; i++) {

            if (matches[i].code == '%') { substitution = '%' }
            else if (matches[i].code == 'b') {
                matches[i].argument = String(Math.abs(parseInt(matches[i].argument)).toString(2));
                substitution = sprintfWrapper.convert(matches[i], true);
            }
            else if (matches[i].code == 'c') {
                matches[i].argument = String(String.fromCharCode(parseInt(Math.abs(parseInt(matches[i].argument)))));
                substitution = sprintfWrapper.convert(matches[i], true);
            }
            else if (matches[i].code == 'd') {
                matches[i].argument = String(Math.abs(parseInt(matches[i].argument)));
                substitution = sprintfWrapper.convert(matches[i]);
            }
            else if (matches[i].code == 'f') {
                matches[i].argument = String(Math.abs(parseFloat(matches[i].argument)).toFixed(matches[i].precision ? matches[i].precision : 6));
                substitution = sprintfWrapper.convert(matches[i]);
            }
            else if (matches[i].code == 'o') {
                matches[i].argument = String(Math.abs(parseInt(matches[i].argument)).toString(8));
                substitution = sprintfWrapper.convert(matches[i]);
            }
            else if (matches[i].code == 's') {
                matches[i].argument = matches[i].argument.substring(0, matches[i].precision ? matches[i].precision : matches[i].argument.length)
                substitution = sprintfWrapper.convert(matches[i], true);
            }
            else if (matches[i].code == 'x') {
                matches[i].argument = String(Math.abs(parseInt(matches[i].argument)).toString(16));
                substitution = sprintfWrapper.convert(matches[i]);
            }
            else if (matches[i].code == 'X') {
                matches[i].argument = String(Math.abs(parseInt(matches[i].argument)).toString(16));
                substitution = sprintfWrapper.convert(matches[i]).toUpperCase();
            }
            else {
                substitution = matches[i].match;
            }

            newString += strings[i];
            newString += substitution;

        }
        newString += strings[i];

        return newString;

    },

    convert : function(match, nosign){
        if (nosign) {
            match.sign = '';
        } else {
            match.sign = match.negative ? '-' : match.sign;
        }
        var l = match.min - match.argument.length + 1 - match.sign.length;
        var pad = new Array(l < 0 ? 0 : l).join(match.pad);
        if (!match.left) {
            if (match.pad == '0' || nosign) {
                return match.sign + pad + match.argument;
            } else {
                return pad + match.sign + match.argument;
            }
        } else {
            if (match.pad == '0' || nosign) {
                return match.sign + match.argument + pad.replace(/0/g, ' ');
            } else {
                return match.sign + match.argument + pad;
            }
        }
    }
}

sprintf = sprintfWrapper.init;
		
		
		

NS4 = (document.layers) ? true : false;

function checkEnter(event)
{     
    var code = 0;
    
    if (NS4)
        code = event.which;
    else
        code = event.keyCode;
    if (code==13)
       SendSearchTerm();
}

