// ---------------------------------------------------------------------------------------
// RENLOCK SOFTWARE
// ---------------------------------------------------------------------------------------
// Page 				: functions.js
// Description			: Contains all major JavaScript functions
// Creation Date		: 02/26/2006
// Author				: Renlock Software
// ---------------------------------------------------------------------------------------
// Modified By			: Cristi Rusu
// Modified Date		: 12/06/2007
// Description			: Added functions for newsletter, copyBilltoShip2, passwordStrength
// Version				: 1.0.0
// ---------------------------------------------------------------------------------------
// 2006. all rights reserved
var shippingPopupIframe = null;
function ResetScrollPosition()
{
   var scrollX = document.getElementById('__SCROLLPOSITIONX');
   var scrollY = document.getElementById('__SCROLLPOSITIONY');

   if(scrollX && scrollY)
   {
      scrollX.value = 0;
      scrollY.value = 0;
   }
}

function ResizeContainers(div1, div2)
{
    var leftSide = document.getElementById(div1);
    var heightL = leftSide.offsetHeight;
    var rightSide = document.getElementById(div2);
    var heightR = rightSide.offsetHeight;
    var height = 0;
    if(heightL >= heightR)
    {
        rightSide.style.height = heightL+"px";
        rightSide.style.minHeight = heightL+"px";
        
        leftSide.style.height = heightL+"px";
        leftSide.style.minHeight = heightL+"px"
    }
    else
    {
        rightSide.style.height = heightR+"px";
        rightSide.style.minHeight = heightR+"px";
    
        leftSide.style.height = heightR+"px";
        leftSide.style.minHeight = heightR+"px";
    }        
}
function HideContainer(id)
{
    var container = document.getElementById(id);
    if(container)
    {
        container.style.display = 'none';
    }
}
function ShowContainer(id)
{
    var container = document.getElementById(id);
    if(container)
    {
        container.style.display = 'block';
    }
}
function GetPage(dropdown)
{
    if(dropdown)
    {
        var index = dropdown.selectedIndex;
        var value = dropdown.options[index].value;
        if(value != null)
        {
            window.location.href = value;
        }
    }
}

function Subscribe()
{
    //first check if the element actually exists
    var subscribe = document.getElementById('subscribe');
    var unSubscribe = document.getElementById('unsubscribe');
    var subscribeF = document.getElementById('subscribef');
    var unSubscribeF = document.getElementById('unsubscribef');
    if (subscribe)
    {
	    if (subscribe.checked == true)
	    {
		    unSubscribe.checked = false;
	    }
	    else
	    {
		    unSubscribe.checked = true;
	    }
	}
	
	if (subscribeF)
	{
	    if (subscribeF.checked == true)
	    {
		    unSubscribeF.checked = false;
	    }
	    else
	    {
		    unSubscribeF.checked = true;
	    }
	}
}
function Unsubscribe()
{
    var subscribe = document.getElementById('subscribe');
    var unSubscribe = document.getElementById('unsubscribe');
    var subscribeF = document.getElementById('subscribef');
    var unSubscribeF = document.getElementById('unsubscribef');
    if (unSubscribe)
    {
	    if (unSubscribe.checked == true)
	    {
		    subscribe.checked = false;
	    }
	    else
	    {
		    subscribe.checked = true;
	    }
	}
	if (unSubscribeF)
	{
	    if (unSubscribeF.checked == true)
	    {
		    subscribeF.checked = false;
	    }
	    else
	    {
		    subscribeF.checked = true;
	    }
	}
}

function check(element)
{
    var hid = document.getElementById('ctl00_chkHidden');
    if (element.checked)
    {
        hid.value = 'on';
    }
    else
    {
        hid.value = 'off';
    }
}

function PostASPForm (action){
    aspnetForm.ctl00$InnerArea$act.value = action;
    aspnetForm.submit ();
}

//for compare products - search page
function checkCompare(separator, productId, postbackURL)
{
    var chkId=separator+productId;
    var chk = document.getElementById(chkId);
    if(chk)
    {
        chk.checked = true;
        CrossPostASPForm(postbackURL);
    }    
}
//set the links from a popup page to redirect to the main window
function SetLocation(url, setFocus)
{
    var mainWindow = window.opener;
    if(setFocus)
    {
        mainWindow.focus();
    }
    mainWindow.location.href = url;
}
//for compare products popup page -- reset the page width
function setDimension(count)
{
    if(count == 0)
    {
        window.close();
        window.opener.setFocus();
    }
    else
    {
        var width = 260;
        width+=count*180;
        window.resizeTo(width,600);
    }
}

function CrossPostASPForm (action)
{
    var aspForm = document.getElementById('aspnetForm');
    if(aspForm)
    {
        aspForm.action = action;
        aspForm.submit();
    }        
}

//for newsletter interests checkBox
function checkAll(chkBoxNo)
{
//debugger
    if(document.getElementById('chkAll').checked)
    {
        var i=0;
        for (i=1; i<=chkBoxNo;i++)
        {
            chkId = 'ctl00_InnerArea_interestChk_'+i;
            document.getElementById(chkId).checked = true;
        }
    }
    else
    {
        var i=0;
        for (i=1; i<=chkBoxNo;i++)
        {
            chkId = 'ctl00_InnerArea_interestChk_'+i;
            document.getElementById(chkId).checked = false;
        }
    }
}

///clear the text from a textbox on click
function clearText(txtBox, text)
{
////debugger
    if (text!='')
    {
        if(txtBox.value == "")
        {
            txtBox.value=text;
        }
    }
    else
    {
        txtBox.value=text;
    }
}

function setVisible2(showDiv, hideDiv,chkBox)
{
    var aux;
    var shipImg = document.getElementById("shipImg");
    if (!document.getElementById(chkBox).checked)
    {
        aux = hideDiv;
        hideDiv = showDiv;
        showDiv = aux;
    }
    if (showDiv)
        document.getElementById(showDiv).style.display = 'block';
    if (hideDiv)
        document.getElementById(hideDiv).style.display = 'none';
}

function setVisible(showDiv, hideDiv,chkBox)
{
    var aux;
    if (!document.getElementById(chkBox).checked)
    {
        aux = hideDiv;
        hideDiv = showDiv;
        showDiv = aux;
    }
//    if(showDiv == "companyBilling")
//    {
//        document.getElementById('companyBillingExtra').style.display = 'block';
//        document.getElementById('billingExtra').style.display = 'none';
//    }
//    else
//    {
//        document.getElementById('companyBillingExtra').style.display = 'none';
//        document.getElementById('billingExtra').style.display = 'block';
//    }
    if (showDiv)
        document.getElementById(showDiv).style.display = 'block';
    if (hideDiv)
        document.getElementById(hideDiv).style.display = 'none';
}

//password strength
function passwordChanged(passTxt) {
var strength = document.getElementById("strength");
var strongRegex = new RegExp("^(?=.{8,})(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*\\W).*$", "g");
var mediumRegex = new RegExp("^(?=.{7,})(((?=.*[A-Z])(?=.*[a-z]))|((?=.*[A-Z])(?=.*[0-9]))|((?=.*[a-z])(?=.*[0-9]))).*$", "g");
var enoughRegex = new RegExp("(?=.{6,}).*", "g");
var pwd = document.getElementById(passTxt);

if (pwd.value.length==0) {
strength.innerHTML = "Introduceti cel putin 6 caractere";
} else if (false == enoughRegex.test(pwd.value)) {
strength.innerHTML = "Mai multe caractere";
} else if (strongRegex.test(pwd.value)) {
strength.innerHTML = "<span style=\"color:green\">Tare!</span>";
} else if (mediumRegex.test(pwd.value)) {
strength.innerHTML = "<span style=\"color:orange\">Mediu!</span>";
} else {
strength.innerHTML = "<span style=\"color:red\">Slab!</span>";
}
}
//end password strength

//add to favorites

/***********************************************
* Bookmark site script- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
***********************************************/

/* Modified to support Opera */
function bookmarksite(title,url){
if (window.sidebar) // firefox
	window.sidebar.addPanel(title, url, "");
else if(window.opera && window.print){ // opera
	var elem = document.createElement('a');
	elem.setAttribute('href',url);
	elem.setAttribute('title',title);
	elem.setAttribute('rel','sidebar');
	elem.click();
} 
else if(document.all)// ie
	window.external.AddFavorite(url, title);
}
//end add to favorites

function CopyBillToShip(){

	aspnetForm.ctl00$InnerArea$shpFirstName.value = aspnetForm.ctl00$InnerArea$billFirstName.value;
	aspnetForm.ctl00$InnerArea$shpLastName.value = aspnetForm.ctl00$InnerArea$billLastName.value;
	aspnetForm.ctl00$InnerArea$shpStreet1.value = aspnetForm.ctl00$InnerArea$billStreet1.value;
	aspnetForm.ctl00$InnerArea$shpCity.value = aspnetForm.ctl00$InnerArea$billCity.value;
	aspnetForm.ctl00$InnerArea$shpZip.value = aspnetForm.ctl00$InnerArea$billZip.value;
	aspnetForm.ctl00$InnerArea$shpCountry.value = aspnetForm.ctl00$InnerArea$billCountry.value;
	aspnetForm.ctl00$InnerArea$shpPhone.value = aspnetForm.ctl00$InnerArea$billPhone.value;
	
	aspnetForm.ctl00$InnerArea$shpPhone2.value = aspnetForm.ctl00$InnerArea$billPhone2.value;
	aspnetForm.ctl00$InnerArea$shpProvince.value = aspnetForm.ctl00$InnerArea$billProvince.value;
}
//CopyBilltoShip2
//works for Firefox also
// added functionality: if the users checks and then unchecks the checkBox
//all the values in the form are reseted
function CopyBillToShip2()
{
    var chkBox = "chkCopyBillToShip";
    if (document.getElementById(chkBox).checked)
    {
        setValues("ctl00_InnerArea_shpFirstName", "ctl00_InnerArea_billFirstName", false);
        setValues("ctl00_InnerArea_shpLastName", "ctl00_InnerArea_billLastName", false);
        setValues("ctl00_InnerArea_shpStreet1", "ctl00_InnerArea_billStreet1", false);
        setValues("ctl00_InnerArea_shpCity", "ctl00_InnerArea_billCity", true);
        setValues("ctl00_InnerArea_shpZip", "ctl00_InnerArea_billZip", false);
        setValues("ctl00_InnerArea_shpCountry", "ctl00_InnerArea_billCountry", false);
        setValues("ctl00_InnerArea_shpPhone", "ctl00_InnerArea_billPhone", false);        
        setValues("ctl00_InnerArea_shpPhone2", "ctl00_InnerArea_billPhone2", false);
        setValues("ctl00_InnerArea_shpProvince", "ctl00_InnerArea_billProvince", false);
    }
    else
    {
        clearValues("ctl00_InnerArea_shpFirstName");
        clearValues("ctl00_InnerArea_shpLastName");
        clearValues("ctl00_InnerArea_shpStreet1");
        clearValues("ctl00_InnerArea_shpCity");
        clearValues("ctl00_InnerArea_shpZip");
        clearCountry("ctl00_InnerArea_shpCountry");
        clearValues("ctl00_InnerArea_shpPhone");
        
        clearValues("ctl00_InnerArea_shpPhone2");
        clearValues("ctl00_InnerArea_shpProvince");

    }
    
}
/**********************
field - the field you want to fill
value - the field containing the value
reset - used for combo box to reset the old values and insert the new ones
*********************/
function setValues (field, value, reset)
{
    var destination = document.getElementById(field);
    var source = document.getElementById(value);
    if(reset == false)
    {
        if(destination.tagName == "SELECT" && source.tagName == "SELECT")
        {
            destination.selectedIndex = source.selectedIndex;
        }
        else
        {
            destination.value = source.value; 
        }
    }
    else
    {
        if(destination.tagName == "SELECT" && source.tagName == "SELECT")
        {
            //reset the combo values
            destination.options.length = 0;
            for(var i = 0; i<source.options.length; i++)
            {
                destination.options[i] = new Option(source.options[i].text,source.options[i].value);
            }
            destination.selectedIndex = source.selectedIndex;
        }
        else
        {
            destination.value = source.value;
        }
    }
}
function clearValues (field)
{
    document.getElementById(field).value = "";
}
function clearCountry (field)
{
    document.getElementById(field).value = -1;
}
//End copyBilltoShip2 functions

function OpenPopup(url) 
{
    window.open(url, 'newWin','toolbar=no,location=no left=40 right=100,scrollbars=yes,width=350,height=500');
}

function ProductImageOpenWnd(link, title)
{
    var strFeatures;
    var p_strPage;
    p_strPage = link;
    strFeatures = "status=no,toolbar=no,menubar=no,location=no,scrollbars=no,resizable=no,width=500,height=500";
    oWindow = window.open(p_strPage, title, strFeatures);
    oWindow.focus();
}

//show/hide review
function ShowHideReview()
{
    var review = document.getElementById("review");
    var reviewsContainer = document.getElementById("reviewsContainer");
    var reviewLink = document.getElementById("reviewLink");
    var reviewLink2 = document.getElementById("reviewLink2");
    if (document.getElementById("review"))
    {
        if (review.style.display == "none")
        {
            review.style.display = "block";
            if(reviewsContainer)
            {
                reviewsContainer.style.display = "none";
            }
        }
        else
        {
            review.style.display = "none";
            if(reviewsContainer)
            {
                reviewsContainer.style.display = "block";
            }
        }
    }
}
//fills the review form with the name, title, comment and vote
function FillReviewForm(name, title, comment, vote)
{
    document.getElementById('ctl00_InnerArea_ReviewsProducts_rr').value = name;
    document.getElementById('ctl00_InnerArea_ReviewsProducts_rt').value = title;
    document.getElementById('ctl00_InnerArea_ReviewsProducts_rd').value = comment;
    document.getElementById('ctl00_InnerArea_ReviewsProducts_rv_'+(vote-1)).checked = true;
}

//
// Image Gallery change images (if we get images from DB)
//
function changeDbImage(container, imageId, siteRoot, productCode, size)
{
    //var zoomBtn = document.getElementById('zoom');
    document.getElementById(container).src=siteRoot+'/processors/ProcGalleryItem.aspx?imgId='+imageId+'&size='+size;
//    if(zoomBtn)
//    {
//        //siteRoot+"/GalleryPopup/"+productCode+"/"+imageId+".aspx'"+,520,600,true"
//        //zoomBtn.onclick = function(){CreateWnd(''+siteRoot+'/ProductGallery/'+productCode+'/'+imageId+'.aspx',520,600,true)};
//    }
}

//
//Image gallery change images (if the images are loaded from disk)
//
function changeImage(container,imageSrc, imagePath)
{
    var zoomBtn;
    if(document.getElementById('zoom'))
    {
        zoomBtn = document.getElementById('zoom').parentNode;
    }
    var imageContainer = document.getElementById(container);
    imageContainer.src=imageSrc;
    if(zoomBtn)
    {
        zoomBtn.onclick = function(){CreateWnd(''+imagePath,880,780,true); return false;};
        if(zoomBtn.tagName == "A")
        {
            zoomBtn.href=imagePath;
        }
    }
    var imageLink = imageContainer.parentNode;
    if(imageLink)
    {
        imageLink.onclick = function(){CreateWnd(''+imagePath,880,780,true); return false;};
        if(imageLink.tagName=="A")
        {
            imageLink.href=imagePath;
        }
    }
}

//
//Print Page
//
function printPage(btnId)
{
    document.getElementById(btnId).style.display = "none";
    window.print();
}

function showPayment (paymentId)
{
    var cardBtn = document.getElementById("CardBtn");
    var rambursBtn = document.getElementById("RambursBtn");
    if (document.getElementById(paymentId))
    {
        document.getElementById(paymentId).style.display = 'block';
        if (paymentId == 'PaymentFormCard')
        {
            hidePayment('PaymentFormRamburs');
            hidePayment('PaymentFormOP');
        }
        else if(paymentId == 'PaymentFormRamburs')
            {
                hidePayment('PaymentFormCard');
                hidePayment('PaymentFormOP');
            }
            else if(paymentId == 'PaymentFormOP')
                {
                    hidePayment('PaymentFormCard');
                    hidePayment('PaymentFormRamburs');
                }
    }
}
function hidePayment(paymentId)
{
    if (document.getElementById(paymentId))
    {
        document.getElementById(paymentId).style.display = 'none';
    }
}

function submitEPayment(actionURL)
{
    var formDiv = document.getElementById('formDiv');
    var aspForm = document.getElementById('aspnetForm');
    //check if the ePayment form has been created
    if(formDiv && formDiv.getElementsByTagName("input"))
    {
        if(aspForm)
        {
            aspForm.action = actionURL;
            aspForm.submit();
        }
    }
}

//for email cart
function showForm(id)
{
    var showForm = document.getElementById(id);
    if(showForm)
    {
        showForm.style.display = 'block';
    }
}
function showHide(show, hide)
{
    var showElement = document.getElementById(show);
    var hideElement = document.getElementById(hide);
    
    if(showElement)
        showElement.style.display = 'block';
    if(hideElement)
        hideElement.style.display = 'none';        
}
function SetSortByHidden(sortDDL, hiddenBottomId, hiddenTopId)
{
    var hiddenBottomInput = document.getElementById(hiddenBottomId);
    var hiddenTopInput = document.getElementById(hiddenTopId);
    if(hiddenBottomInput && sortDDL && hiddenTopInput)
    {
        var sortMethod = sortDDL.options[sortDDL.selectedIndex].value.split("-");
        hiddenTopInput.value = sortMethod[1];
        hiddenBottomInput.value = sortMethod[1];
        sortDDL.options[sortDDL.selectedIndex].value = sortMethod[0];
    }
}
function ShowSpecialProduct(selectedProduct, selectedPrefix,productListId,selectedDivId)
{
    if(selectedProduct && selectedProduct.id != "")
    {
        var listDiv = document.getElementById(productListId);
        var selectedList = document.getElementById(selectedDivId);
        
        if(listDiv)
        {
            var productsList = listDiv.getElementsByTagName("div");
            if(productsList && productsList.length > 0)
            {
                for(var i=0; i<productsList.length; i++)
                {
                    if(productsList[i].id != "")
                    {
                        if(productsList[i].id != selectedProduct.id)
                        {
                            productsList[i].className = "specialListProduct floatleft";
                        }
                        else
                        {
                            productsList[i].className = "specialListProduct floatleft selectedSpecialProduct";
                        }
                    }
                }
            }
        }
        if(selectedList)
        {
            var productsList = selectedList.getElementsByTagName("div");
            if(productsList && productsList.length > 0)
            {
                for(var i=0; i<productsList.length; i++)
                {
                    if(productsList[i].id != "")
                    {
                        if(productsList[i].id != selectedPrefix+selectedProduct.id)
                        {
                            productsList[i].className = "category_specials main_center_container hide";
                        }
                        else
                        {
                            productsList[i].className = "category_specials main_center_container";
                        }
                    }                    
                }
            }
        }
    }
}
function HideUsingClass(containerId)
{
    var el = document.getElementById(containerId);
    if(el)
    {
        el.className = el.className.replace("hideDiv","").replace("showDiv","") + " hideDiv ";
    }
}
function ShowUsingClass(containerId)
{
    var el = document.getElementById(containerId);
    if(el)
    {
        el.className = el.className.replace("hideDiv","showDiv");
    }
}
function ShowShippingPopup(containerId)
{
    var container = document.getElementById(containerId);
    if(container)
    {
        var parent = null;
        if(typeof container.parentElement != 'undefined' && container.parentElement !=null)
        {
            parent = container.parentElement;
        }
        else
        {
            parent = container.parentNode;
        }
        if(parent && parent.tagName != 'BODY')
        {
            parent.removeChild(container);
            document.body.appendChild(container);
        }
        container.className = container.className.replace("hideDiv","");
        var arrayPageSize = getPageSize();
        var arrayPageScroll = getPageScroll();
        container.style.top = (arrayPageSize[3] - 150)/2+arrayPageScroll[1]+'px';
        container.style.left = (arrayPageSize[2]+arrayPageScroll[0] - 500)/2+15+'px';
        if(IsIE())
        {
            var iFrame = document.createElement("IFRAME");
            iFrame.setAttribute("src", "javascript:'<html></html>'");
            iFrame.setAttribute("border","0");
            iFrame.className = container.className;
            iFrame.style.top = container.style.top;
            iFrame.style.left = container.style.left;
            
            iFrame.style.zIndex = 0;
            document.body.appendChild(iFrame);
            shippingPopupIframe = iFrame;
        }
        container.style.position = 'absolute';
        container.style.zIndex = 10;
        //container.style.left = (arrayPageSize[2]+arrayPageScroll[0]-500)/2+'px';
    }
}
function RemoveIframe(idChildContainer)
{
    if(shippingPopupIframe!=null)
    {
        var container = document.getElementById(idChildContainer);
        container.parentElement.removeChild(shippingPopupIframe);
        shippingPopupIframe = null;
    }
}

// getPageSize()
// Returns array with page width, height and window width, height
function getPageSize(){	
	var xScroll, yScroll;	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}
//
// getPageScroll()
// Returns array with x,y page scroll values.
function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {
		yScroll = document.body.scrollTop;
	}
	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}
function IsIE()
{
    return ( navigator.appName=="Microsoft Internet Explorer" );
}
