
function getBasePath ()
{
	var site_url = document.location.href;
	
	if	(site_url.search(/yalla\.co\.il/i) == -1)
	{
		return '/yalla_shops/';
		
	}
	return '../yalla_shops/';
}

function addbookmark(title,url)
{
	if (window.sidebar)
	{
		window.sidebar.addPanel(title, url,"");
	}
	else if( window.external )
	{
		window.external.AddFavorite( url, title);
	}
	else if(window.opera && window.print)
	{
		return true;
	}
}

function displayArea (area_id)
{	
	area_elm = document.getElementById(area_id);
	
	if	(area_elm.style.display == 'none')
	{
		area_elm.style.display = '';
	}
	else
	{
		area_elm.style.display = 'none';
	}
}

function alertSize()
{
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
	//Non-IE
	myWidth = window.innerWidth;
	myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
	//IE 6+ in 'standards compliant mode'
	myWidth = document.documentElement.clientWidth;
	myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
	//IE 4 compatible
	myWidth = document.body.clientWidth;
	myHeight = document.body.clientHeight;
	}
	
	if	(myWidth < 1100)
	{
		if	(document.getElementById('divTopLeft'))
			document.getElementById('divTopLeft').style.display = 'none';
		
		if	(document.getElementById('adv16'))
			document.getElementById('adv16').style.display = 'none';			
	}
	else
	{
		if	(document.getElementById('divTopLeft'))
			document.getElementById('divTopLeft').style.display = '';
			
		if	(document.getElementById('adv16'))
			document.getElementById('adv16').style.display = '';			
	}
}

if(typeof HTMLElement!='undefined'&&!HTMLElement.prototype.click)
HTMLElement.prototype.click=function(){
var evt = this.ownerDocument.createEvent('MouseEvents');
evt.initMouseEvent('click', true, true, this.ownerDocument.defaultView, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
this.dispatchEvent(evt);
}

function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if (node == null)
		node = document;
	if (tag == null)
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if (pattern.test(els[i].className) ) {
		  classElements[j] = els[i];
		  j++;
		}
	}
	return classElements;
}

/*
 * Count down => מקבל זמן והופך אותו לטיימר
 * countDownTime format: day-hour-minuts-second
 */
function countDown (dd,hh,ii,ss,divId)
{
	elm = document.getElementById(divId);
	ss 	= parseInt(ss);
	ii 	= parseInt(ii);
	hh 	= parseInt(hh);
	dd 	= parseInt(dd);
	
	if	(!(ss == 1 && ii == 0 && hh == 0 && dd == 0))
	{
		ss--;
		if	(ss <= 0)
		{
			ss = 59; ii--;
			if	(ii <= 0)
			{
				ii = 59; hh--;
				if	(hh <= 0)
				{
					hh = 59; dd--;
					if	(dd <= 0)
					{
						dd = 0;
					}
				}
			}
		}
		
		show_dd = (dd < 10) ? '0' + dd : dd;
		show_hh = (hh < 10) ? '0' + hh : hh;
		show_ii = (ii < 10) ? '0' + ii : ii;
		show_ss = (ss < 10) ? '0' + ss : ss;
		
		elm.innerHTML = show_dd + ":" + show_hh + ":" + show_ii + ":" + show_ss;
		
		setTimeout("countDown("+dd+","+hh+","+ii+","+ss+",'"+divId+"')",1000);
	}
	else
	{
		elm.innerHTML = '00:00:00:00';
		window.location.reload();
	}
}

// בדיקה אם ערך הוא מספר

function isInteger(n) {
    var IsFound = /^-?\d+$/.test(n);
    
    return IsFound;
}

// תוספות ושידרוגים של מוצר

function updatePrice (productPrice,productId)
{
	var moreSum = 0;
	
	if	(productId) productId = '_' + productId; else productId = '';
    
	for (i = 1; i < 10; i++)
	{
		elm = document.getElementById('more_' + i + productId);
		
		if	(elm)
		{
			if (elm.options[document.getElementById('more_' + i + productId).selectedIndex].id > 0) {
                if (document.getElementById('more_count_'+i).type != "select-one") {
                    ih = '<select name="more_count_'+i+'" id="more_count_'+i+'" style="width: 50px;" onchange="updatePrice('+productPrice+')">';
                    for (l = 1; l <= elm.options[document.getElementById('more_' + i + productId).selectedIndex].id; l++) {
                        ih = ih + ' <option value="'+l+'">'+l+'</option>';
                    }
                    ih = ih + '</select>';
                    document.getElementById('mcd_'+i).innerHTML = ih;
             
                } else {
                    if (elm.options[document.getElementById('more_' + i + productId).selectedIndex].id != document.getElementById('more_count_'+i).length) {
                        ih = '<select name="more_count_'+i+'" id="more_count_'+i+'" style="width: 50px;" onchange="updatePrice('+productPrice+')">';
                        for (l = 1; l <= elm.options[document.getElementById('more_' + i + productId).selectedIndex].id; l++) {
                            ih = ih + ' <option value="'+l+'">'+l+'</option>';
                        }
                        ih = ih + '</select>';
                        document.getElementById('mcd_'+i).innerHTML = ih;
                    }
                }
                
                if (elm.options[document.getElementById('more_' + i + productId).selectedIndex].id == 1) {
                    document.getElementById('lpt_'+i).style.display = 'inline';
                } else {
                    document.getElementById('lpt_'+i).style.display = 'none';
                }
            } else {
                if (document.getElementById('more_count_'+i).type != "text") {
                    document.getElementById('mcd_'+i).innerHTML = '<input type="text" name="more_count_'+i+'" id="more_count_'+i+'" value="1" style="width: 50px;"  onkeyup="updatePrice('+productPrice+')" onkeydown="updatePrice('+productPrice+')" onchange="updatePrice('+productPrice+')" />';
                }
                
                document.getElementById('lpt_'+i).style.display = 'none';
            }
            
            if (elm.options[document.getElementById('more_' + i + productId).selectedIndex].title == 0) {
                document.getElementById('qpt_'+i).style.display = 'none';
                document.getElementById('mcd_'+i).innerHTML = '<input type="hidden" name="more_count_'+i+'" id="more_count_'+i+'" value="0">';
            } else {
                document.getElementById('qpt_'+i).style.display = 'inline';
            }
            
            if (!isInteger(parseInt(document.getElementById('more_count_' + i + productId).value))) {
                document.getElementById('more_count_' + i + productId).value = '0';
            }
            
            price = parseInt(elm.options[document.getElementById('more_' + i + productId).selectedIndex].title);
			count = parseInt(document.getElementById('more_count_' + i + productId).value);
			if	(!price) price = 0;
			
			moreSum += (price * count);
			
			if	(document.getElementById('more_' + i + productId).selectedIndex != 0 && count == 0)
			{
				document.getElementById('more_count_' + i + productId).value = 1;
			}            
		}
	}
	
	document.getElementById('more_sum' + productId).innerHTML = moreSum;
	document.getElementById('product_sum' + productId).innerHTML = moreSum + productPrice;
}

function getMoreSelections (productId)
{
	var selections = '';
	
	if	(productId) productId = '_' + productId; else productId = '';
	
	for (i = 1; i < 10; i++)
	{
		elm 	= document.getElementById('more_' + i + productId);
		count 	= document.getElementById('more_count_' + i + productId);
		
		if	(elm)
		{
			if	(elm.value != '0')
			{
				selections += ',more' + i + ':' + ((elm.value) ? elm.value : 0) + 'x' + count.value;
			}
		}
	}
	
	return selections;
}

function getSubProducts()
{
	var selections = '';
    
    for (i = 0; i < document.getElementsByName('subProductGroup').length; i++)
	{
	    groupID = document.getElementsByName('subProductGroup')[i].id;
        productID = document.getElementsByName('subProductGroup')[i].value;
		
		if	(productID > 0)
		{
			selections += groupID + '$$$' + productID + '$$$';
		}
	}
	
	return selections;
}

function upgradesList (storeid,id,pNum)
{
	myWindow=window.open(getBasePath() + 'ajax.flyingCart.upgrades.php?storeid='+ storeid +'&id=' + id + '&pNum=' + pNum,'','width=580,height=400')
	myWindow.document.title = 'רשימת התוספות';
	myWindow.focus();
}

// תגובות

function addComment (page,id,storeId)
{
	var newWin = window.open(getBasePath() + 'addComment.php?storeid=' + storeId + '&tbl=' + page + '&id=' + id, 'addComment', 'menubar=0,resizable=0,scrollbars=1,top=100,left=200,width=500,height=400');
}

function Comment (id)
{
	var elm = document.getElementById('comment_' + id);
	
	if	(elm.style.display == 'none')
	{
		elm.style.display = 'block'
	}
	else
	{
		elm.style.display = 'none'
	}
}

// שלח לחבר

function send2f (page,id,storeId)
{
	var newWin = window.open(getBasePath() + 'send2f.php?storeid=' + storeId + '&tbl=' + page + '&id=' + id, 'send2f', 'menubar=0,resizable=0,scrollbars=1,top=100,left=200,width=600,height=400');
}

// גירסה להדפסה

function printVersion (page,id,storeId,pType)
{
	var newWin = window.open(getBasePath() + 'printVersion.php?storeid=' + storeId + '&tbl=' + page + '&id=' + id + '&pType=' + pType, 'printVersion', 'menubar=0,resizable=1,scrollbars=1,top=100,left=200,width=700,height=500');
}

// שכחתי סיסמא

function forgotPass (yallaUrl,storeId)
{
	var newWin = window.open(yallaUrl + '/?view=forgotpass&disableHeader=true&storeid=' + storeId, 'forgotPass', 'menubar=0,resizable=0,scrollbars=1,top=100,left=200,width=400,height=300');
}

// מכרזים

function michrazim (yallaUrl,storeId,userId,price,esroni,productId)
{
	var newWin = window.open(yallaUrl + '/ssl/michrazim.php?storeid=' + storeId + '&userId=' + userId + '&productId=' + productId + '&price=' + price + '&esroni=' + esroni, 'michrazim', 'menubar=0,resizable=0,scrollbars=1,top=100,left=200,width=500,height=350');
}

function michrazimPacks (yallaUrl,storeId,userId,packId)
{
	var newWin = window.open(yallaUrl + '/ssl/michrazim.packs.php?storeid=' + storeId + '&userId=' + userId + '&packId=' + packId, 'michrazimPacks', 'menubar=0,resizable=0,scrollbars=1,top=100,left=200,width=500,height=350');
}

function openMifrat (storeId,productId)
{
	var newWin = window.open(getBasePath() + 'mifrat.php?storeid=' + storeId + '&productId=' + productId, 'mifrat', 'menubar=0,resizable=0,scrollbars=1,top=100,left=200,width=500,height=350');
}

function passStrength (pass,langKey)
{
	rank = 0;
	
	if	(pass.length > 8) rank++;
	
	var nums 		= '1234567890';
	var letters 	= 'abcdefghijklmnopqrstuvwhyz';
	var letters2 	= 'ABCDEFGHIJKLMNOPQRSTUVWHYZ';
	var isNumbers 	= false;
	var isLowerCase = false;
	var isUpperCase = false;
	var isSymbols 	= false;
	
	for (i = 0; i < pass.length; i++)
	{
		lowerCase 	= letters.indexOf(pass.charAt(i));
		upperCase 	= letters2.indexOf(pass.charAt(i));
		numbers 	= nums.indexOf(pass.charAt(i));
		
		if	(lowerCase != -1)
		{
			isLowerCase = true;
		}
		if (upperCase != -1)
		{
			isUpperCase = true;
		}
		if (numbers != -1)
		{
			isNumbers = true;
		}
		if	(lowerCase == -1 && upperCase == -1 && numbers == -1)
		{
			isSymbols = true;
		}
	}
	
	if	(isNumbers) rank++;
	if	(isLowerCase) rank++;
	if	(isUpperCase) rank++;
	if	(isSymbols) rank++;
	
	precent = (rank / 5) * 100;
	
    ranks_txt        = new Array();
	ranks_txt['_HE'] = new Array('חלש','סביר','טוב','מצויין');
    ranks_txt['_EN'] = new Array('weak','nice','good','strong');
	ranks_colors 	 = new Array('#FF0000','#FFD400','#55AAFF','#48C81E');
	
	if	(precent <= 25)
	{
		document.getElementById('passStrengthGraph_msg').innerHTML = ranks_txt[langKey][0];
		document.getElementById('passStrengthGraph').style.background = ranks_colors[0];
		document.getElementById('passStrengthGraph').style.width = '25%';
	}
	if	(precent >= 25 && precent <= 50)
	{
		document.getElementById('passStrengthGraph_msg').innerHTML = ranks_txt[langKey][1];
		document.getElementById('passStrengthGraph').style.background = ranks_colors[1];
		document.getElementById('passStrengthGraph').style.width = '50%';
	}
	if	(precent >= 50 && precent <= 75)
	{
		document.getElementById('passStrengthGraph_msg').innerHTML = ranks_txt[langKey][2];
		document.getElementById('passStrengthGraph').style.background = ranks_colors[2];
		document.getElementById('passStrengthGraph').style.width = '75%';
	}
	if	(precent >= 75 && precent <= 100)
	{
		document.getElementById('passStrengthGraph_msg').innerHTML = ranks_txt[langKey][3];
		document.getElementById('passStrengthGraph').style.background = ranks_colors[3];
		document.getElementById('passStrengthGraph').style.width = '100%';
	}
}

function checkRegForm(langKey) {
    rf = document.regForm;
    ue = rf.userEMail;
    uf = rf.userFN;
    ul = rf.userLN;
    up = rf.userPass;
    urp = rf.userRePass;
    
    errors = new Array();
    errors['_HE'] = new Array();
    errors['_HE'][0] = 'השם הפרטי קצר מדי';
    errors['_HE'][1] = 'שם המשפחה קצר מדי';
    errors['_HE'][2] = 'מינימום 4 תווים';
    errors['_HE'][3] = 'הסיסמאות אינן תואמות';
    errors['_EN'] = new Array();
    errors['_EN'][0] = 'The first name is too short';
    errors['_EN'][1] = 'The last name is too short';
    errors['_EN'][2] = 'Minimum 4 characters';
    errors['_EN'][3] = 'The passwords doe\'s not match';
    
    s = 1;
    
    ed = '<div style="font-size: 11px; font-family: arial; color: #CC0000; display: inline"><b>';
    eed = '</b></div>';
    
    if (ue.value.length < 6) {
        ue.style.borderColor = '#CC0000';
        document.getElementById('userEMail').style.display = 'inline';
        s = 0;
    } else {
        ue.style.borderColor = '#8A8A8A';
        document.getElementById('userEMail').style.display = 'none';
    }
    
    if (uf.value.length < 2) {
        uf.style.borderColor = '#CC0000';
        document.getElementById('userFN').style.display = 'inline';
        document.getElementById('regUserFN').innerHTML = ed+errors[langKey][0]+eed;
        s = 0;
    } else {
        uf.style.borderColor = '#8A8A8A';
        document.getElementById('userFN').style.display = 'none';
        document.getElementById('regUserFN').innerHTML = '';
    }
    
    if (ul.value.length < 2) {
        ul.style.borderColor = '#CC0000';
        document.getElementById('userLN').style.display = 'inline';
        document.getElementById('regUserLN').innerHTML = ed+errors[langKey][1]+eed;
        s = 0;
    } else {
        ul.style.borderColor = '#8A8A8A';
        document.getElementById('userLN').style.display = 'none';
        document.getElementById('regUserLN').innerHTML = '';
    }
    
    if (up.value.length < 4) {
        up.style.borderColor = '#CC0000';
        document.getElementById('userPass').style.display = 'inline';
        document.getElementById('regUserPass').innerHTML = ed+errors[langKey][2]+eed;
        s = 0;
    } else {
        up.style.borderColor = '#8A8A8A';
        document.getElementById('userPass').style.display = 'none';
        document.getElementById('regUserPass').innerHTML = '';
    }
    
    if (up.value != urp.value) {
        urp.style.borderColor = '#CC0000';
        document.getElementById('userRePass').style.display = 'inline';
        document.getElementById('regUserRePass').innerHTML = ed+errors[langKey][3]+eed;
        s = 0;
    } else {
        urp.style.borderColor = '#8A8A8A';
        document.getElementById('userRePass').style.display = 'none';
        document.getElementById('regUserRePass').innerHTML = '';
    }
    
    if (s == 1) {
        return true;
    } else {
        return false;
    }
}

function load_outer_pictures() {
    $('.outer-pic').each(function() {
        var imgObj = $(this);
        var newWidth = 150;
        var newHeight = 150;
        if (typeof(imgObj.attr('resizewidth')) != 'undefined') {
            newWidth = imgObj.attr('resizewidth');
            newHeight = imgObj.attr('resizeHeight');
        }
        var newImg = new Image();

        newImg.onload = function() {
            var height = newImg.height;
            var width = newImg.width;
            var newSizes = getThumbnailSize( width,height,newWidth,newHeight );
            imgObj.css({
                'width': newSizes[0],
                'height': newSizes[1]
            });
            
            if (imgObj.hasClass('outer-pic-center')) {
                imgObj.css({
                    'position': 'absolute',
                    'top': '50%',
                    'left': '50%',
                    'margin-top': '-'+(newSizes[1]/2)+'px',
                    'margin-left': '-'+(newSizes[0]/2)+'px'
                });
            }
            
            imgObj.show();
        }
        
        newImg.src = imgObj.attr('src');
    });
}

function getThumbnailSize( width,height,maxWidth,maxHeight ) {
    var newSize = new Array();
    if (width > maxWidth || height > maxHeight) {
        if (width > height) {
            newSize[0] = maxWidth;
            newSize[1] = (maxWidth/width)*height;
        } else if (height >= width) {
            newSize[1] = maxHeight;
            newSize[0] = (maxHeight/height)*width;
        }
    } else {
        newSize[0] = width;
        newSize[1] = height;
    }
    
    if (newSize[0] > maxWidth) {
        newSize[1] = (maxWidth/newSize[0])*newSize[1];
        newSize[0] = maxWidth;
    } else if (newSize[1] > maxHeight) {
        newSize[0] = (maxHeight/newSize[1])*newSize[0];
        newSize[1] = maxHeight;
    }
    
    return newSize;
}
