// egirl.js

// gallery popup
function showPicture(picture_path)
{
	winparams="toolbar=no,location=no,directories=no,menubar=no,status=yes,resizable=no,scrollbars=no";
	winparams+=",width=630, height=510";
	popup=window.open("/action/gallery/popup?picture="+escape(picture_path),"egirl_gallery", winparams);
	popup.focus();
}
// gallery popup

// search begin 
function egirl_doSearch(fname)
{
	$("searchForm_"+fname).submit();
}

function egirl_searchOrder(v)
{
	$("pform").sort.value=v;
	$("pform").submit();
}
// search end


// swf load begin
function loadswf(imgsrc, imgwidth, imgheight, imglink, swfcontainer)
{
	var fid = 1;
	while (document.getElementById("flashcontent_" + fid) != null) fid++;
	if (swfcontainer == "")
	{
		swfcontainer = "egirl_swf_" + fid;
	}

	var so = new SWFObject("swf/img_holder.swf", swfcontainer, imgwidth, imgheight, "8", "#336699");
	so.addParam("wmode","transparent");
	so.addParam("scale", "noscale");
	so.addParam("salign", "tl");
	so.addVariable('imgwidth',imgwidth);
	so.addVariable('imgheight',imgheight);
	so.addVariable('imgsrc', imgsrc);
	
	document.write("<div id='flashcontent_" + fid + "'></div>");
	so.write("flashcontent_" + fid);
}

function changeImage(imgcontainer, imgsrc, imglink, txtid)
{
	var m = getMovie(imgcontainer);
	getMovie(imgcontainer).newData(imgsrc, imglink);
	
	var s = document.getElementById("txtflash");
	var t = document.getElementById(txtid);
	s.innerHTML = t.innerHTML;
}

function changeImageHTML(imgcontainer, imgobj, imglink, txtid, cr_top_id, cr_bot_id)
{
	var img = document.getElementById(imgcontainer);
	img.src = imgobj.src;
	
	var s = document.getElementById("txtflash");
	var t = document.getElementById(txtid);
	s.innerHTML = t.innerHTML;
	
	document.getElementById("foto01").href = imglink;
	document.getElementById("imgtxt_link").href = imglink;

	var cr_id;
	for (var i=1; i<=4; i++)
	{
		cr_id = "cr_top_" + i;
		//document.getElementById(cr_id).style.display = (cr_id == cr_top_id) ? "none" : "";
		document.getElementById(cr_id).className = (cr_id == cr_top_id) ? "thumb_cr_top_hover" : "thumb_cr_top";
		cr_id = "cr_bot_" + i;
		//document.getElementById(cr_id).style.display = (cr_id == cr_bot_id) ? "none" : "";
		document.getElementById(cr_id).className = (cr_id == cr_bot_id) ? "thumb_cr_bot_hover" : "thumb_cr_bot";
	}

}

function topart_selectImage(idx)
{
	for (var i=1; i<=4; i++)
	{
		obj_taimg = document.getElementById("liitemflash" + i);
		if (i==idx)
		{
			obj_taimg.className = "selected";
		}
		else
		{
			obj_taimg.className = "notselected";
		}
	}
}

function getMovie(movieName) {
    if (navigator.appName.indexOf("Microsoft") != -1) {
        return window[movieName]
    }
    else {
        return document[movieName]
    }
}


// felicitari 
function felicitari_modifica()
{
	$("preview_hide").show();
	$("pform").mesaj.focus();
}

function felicitari_alterDestinatari(el)
{
	var d_el_name;
	for(i=1;i<=10;i++)
	{
		d_el_name="destinatar_row_"+i;
		
		if (i<=el.value) $(d_el_name).show();
		else $(d_el_name).hide();
	}
}
// end felicitari

// newsletter 
function newsletter_checkValue(el)
{
	if (el.value=="adresa de mail")
	{
			el.value="";
	} else
	if (el.value=="")
	{
		el.value="adresa de mail";
	}
}

function doNewsletterAbonare()
{
	var el=$("newsletterField");
	$("newsletter_loading").hide();
	
	if (el.value=="adresa de mail")
	{
		el.addClassName("form_error");
		el.focus();
		return false;
	} else
	if (el.value.length<1)
	{
		el.addClassName("form_error");
		el.focus();
		return false;
	} else
	{
		el.removeClassName("form_error");
	}
	
	$("newsletter_loading").show();
	return true;
	
	// do the ajax shit now
	f=$("newsletterForm");
	new Ajax.Request(f.action, 
	{
		method: f.method,
		parameters: f.serialize(true),
		onSuccess: function(transport) {
			var json=transport.responseText.evalJSON(); 
			if (json==false)
			{
				alert("Eroare Ajax!");
			} else
			{
				if (json.err) 
				{
					el.addClassName("form_error");
					alert("EROARE: "+json.msg);
					el.focus();
				}
				else
				{
					el.value="";
					alert(json.msg);
				}
			}
			$("newsletter_loading").hide();
		},
		onFailure: function() {
			$("newsletter_loading").hide();
			alert("Eroare Ajax!");
		}
	});
	
	return false;
}

function newsletter_Abonare()
{
	ret = doNewsletterAbonare();
	if (ret) $("newsletterForm").submit();
}
// newsletter end



//curs valutar begin

function setExchangeFlag()
{
	var _from_currency = document.getElementById("cv_currency_exchange_from");
	var _from_value = document.getElementById("cv_currency_value_from");
	_from_value.style.background = "url(/images/curs/" + _from_currency.value.toLowerCase() + ".gif) no-repeat top right";
	_from_value.style.backgroundColor = "#ffffff";

	var _to_currency = document.getElementById("cv_currency_exchange_to");
	var _to_value = document.getElementById("cv_currency_value_to");
	_to_value.style.background = "url(/images/curs/" + _to_currency.value.toLowerCase() + ".gif) no-repeat top right";
	_to_value.style.backgroundColor = "#ffffff";
}

function computeExchange()
{
	var _from_currency = document.getElementById("cv_currency_exchange_from");
	var _from_value = document.getElementById("cv_currency_value_from");
	
	var divider = 1;
	if ((_from_currency.value == "HUF") || (_from_currency.value == "JPY") || (_from_currency.value == "KRW"))
	{
		divider = 100;
	}
	
	var _to_currency = document.getElementById("cv_currency_exchange_to");
	var _to_value = document.getElementById("cv_currency_value_to");
	
	var nr_from_value = Number(_from_value.value.replace(",", "."));
	
	document.getElementById("div_currency_value_tva").style.display = (_to_currency.value != "RON") ? "none" : "";

	var currencyResult = (Number(currencyData[_from_currency.value]) / divider) / Number(currencyData[_to_currency.value]) * nr_from_value;
	_to_value.value = formatNumber(currencyResult, 2, ".", "");
	document.getElementById("cv_currency_value_to_tva").value = formatNumber(currencyResult * 0.19, 2, ".", "");
	document.getElementById("cv_currency_value_to_total").value = formatNumber(currencyResult * 1.19, 2, ".", "");
}

function repairCurrencyField(field)
{
	var i = 0;
	while (i<field.value.length)
	{
		if ((field.value.charCodeAt(i)<48 || field.value.charCodeAt(i)>57) && (field.value.substr(i,1)!=",") && (field.value.substr(i,1)!="."))
		{
			field.value = field.value.substr(0,i) + field.value.substr(i+1);
		}
		i++;
	}
}

function formatNumber(nr, decimals, dec_point, sep) {
	if (decimals == undefined) {
		decimals = 2;
	}else if(decimals<0){
		decimals=Math.abs(decimals);
		var exact_decimals=1;
	}else{
		var exact_decimals=0;
	}
	if (dec_point == undefined) {
		dec_point = ",";
	}
	if (sep == undefined) {
		sep = ".";
	}
	nr = String(nr).split(".");
	nat = nr[0];
	nr[0] = "";
	if(nr[1]==null){
		nr[1]="";
	}
	if(exact_decimals){
		for(var i=nr[1].length;i<decimals;i++){
			nr[1]+="0";
		}
	}
	for (var i = nat.length; i>0; i -= 3) {
		nr[0] = nat.substr(Math.max(i-3, 0), 3+Math.min(i-3, 0))+sep+nr[0];
	}
	nr[0] = nr[0].substr(0, nr[0].length-sep.length);
	return nr[0]+(nr[1] != "" ? dec_point+nr[1].substr(0, decimals) : "");
}

//curs valutar end



//tell friend begin

function switchDisplay(container_id)
{
	var d = document.getElementById(container_id);
	d.style.display = (d.style.display == "none" ? "" : "none");
	
	if (container_id == "tellfriend_container" && d.style.display != "none")
	{
		document.getElementById("tellfriend_responder").style.display = "none";
	}
	
	if (container_id == "tellfriend_container")
	{
		if (current_comment_form != null)
		{
			current_comment_form.style.display = "none";
		}
	}
}

function addFriend()
{
	var f = document.getElementById("tellfriend_toall");
	var d = document.createElement("DIV");
	d.id = "tellfriend_todata";
	d.innerHTML = '<input type="text" name="tf_name_to"><input type="text" name="tf_email_to" style="margin-left:12px; margin-right:10px"> <a href="javascript:void(0);" onmousedown="javascript:removeFriend(this);" style="position:relative; top:-6px">Sterge</a>';
	f.appendChild(d);
}

function removeFriend(linkObj)
{
	var pNode = linkObj.parentNode;
	pNode.style.display = "none";
}

function trimAll(sString) 
{
    while (sString.substring(0,1) == ' ') {
        sString = sString.substring(1, sString.length);
    }
    while (sString.substring(sString.length-1, sString.length) == ' ') {
        sString = sString.substring(0,sString.length-1);
    }
    return sString;
}

function validateField(f, isemail)
{
	var ok = true;
	if (trimAll(f.value) == "") ok = false;
	if (isemail)
	{
		email_regex = /^((\"[^\"\f\n\r\t\v\b]+\")|([\w\!\#\$\%\&\'\*\+\-\~\/\^\`\|\{\}]+(\.[\w\!\#\$\%\&\'\*\+\-\~\/\^\`\|\{\}]+)*))@((\[(((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9])))\])|(((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9])))|((([A-Za-z0-9\-])+\.)+[A-Za-z\-]+))$/;
		if (!f.value.match(email_regex)) ok = false;
	}
	
	if (!ok)
	{
		f.style.border = "1px solid #ff0000";
	}
	else
	{
		f.style.border = "1px solid #ffd9f1";
	}
	
	return ok;
}

function getTagsinTag(srcTag, soughtTag)
{
	var items = [];
	for (var i=0; i<srcTag.childNodes.length; i++)
	{
		if (srcTag.childNodes[i].nodeType != 3)
			if (srcTag.childNodes[i].tagName.toLowerCase() == soughtTag)
				if (srcTag.childNodes[i].style.display != "none")
					items.push(srcTag.childNodes[i]);
	}
	return items;
}

var callback_tf =
{
    success: function(o) {
    	document.getElementById("tellfriend_loading").style.display = "none";
			document.getElementById("tellfriend_responder").style.display = "";

			var inputs = getTagsinTag(document.getElementById("tellfriend_fromdata"), "input");
			inputs[0].value = "";
			inputs[1].value = "";

			var items = getTagsinTag(document.getElementById("tellfriend_toall"), "div");
			var inputs = getTagsinTag(items[0], "input");
			inputs[0].value = "";
			inputs[1].value = "";
			
			var frm = document.forms["send_to_friend"];
			frm.tf_message.value = "";
			
			for (var i=1; i<items.length; i++)
			{
				items[i].style.display = "none";
			}
			
			var tf = document.getElementById("tellfriend_container");
			tf.style.display = "none";
    },
    failure: function(o) {
    		document.getElementById("tellfriend_loading").style.display = "none";
    		document.getElementById("tellfriend_container").style.display = "";
        alert("Momentan aceasta functionalitate este indisponibila.");
    },
    argument: ['send','friend']
};

function sendtoFriends()
{
	var ok = true;
	var frm = document.forms["send_to_friend"];

	//get fields
	var fields = getTagsinTag(document.getElementById("tellfriend_fromdata"), "input");
	
	var divs = getTagsinTag(document.getElementById("tellfriend_toall"), "div");
	for (var i=0; i<divs.length; i++)
	{
		var inputs = getTagsinTag(divs[i], "input");
		fields.push(inputs[0]);
		fields.push(inputs[1]);
	}
	
	if (!validateField(frm.tf_message, false)) ok = false;

	var fnames = [];
	var femails = [];
	for (var i=0; i<fields.length; i++)
	{
		if (!validateField(fields[i], i%2!=0)) ok = false;
		if (i%2==0) fnames.push(fields[i].value);
		else femails.push(fields[i].value);
	}
	
	if (ok)
	{
		frm.fnames.value = fnames.join("__NS__");
		frm.femails.value = femails.join("__ES__");
		frm.page_url.value = document.location.href;

		document.getElementById("tellfriend_loading").style.display = "";
		document.getElementById("tellfriend_container").style.display = "none";


		var is_test = 1;
		//if (document.location.href == 'http://www.egirl.ro/Invata-sa-il-seduci-prin-gesturi,a1608.html?clean=true') is_test = 1;	


/*
		if (is_test == 0)
		{
			YAHOO.util.Connect.setForm("send_to_friend");
			YAHOO.util.Connect.asyncRequest("POST", "/send-to-friend2", callback_tf);
		}
*/


		if (is_test == 1)
		{
			var f = $("send_to_friend");
			new Ajax.Request("/send-to-friend2",
			{
				method: f.action,
				parameters: f.serialize(true),
				onSuccess: function(transport) {
		    	document.getElementById("tellfriend_loading").style.display = "none";
					document.getElementById("tellfriend_responder").style.display = "";
		
					var inputs = getTagsinTag(document.getElementById("tellfriend_fromdata"), "input");
					inputs[0].value = "";
					inputs[1].value = "";
		
					var items = getTagsinTag(document.getElementById("tellfriend_toall"), "div");
					var inputs = getTagsinTag(items[0], "input");
					inputs[0].value = "";
					inputs[1].value = "";
					
					var frm = document.forms["send_to_friend"];
					frm.tf_message.value = "";
					
					for (var i=1; i<items.length; i++)
					{
						items[i].style.display = "none";
					}
					
					var tf = document.getElementById("tellfriend_container");
					tf.style.display = "none";
				},
				onFailure: function() {
	    		document.getElementById("tellfriend_loading").style.display = "none";
	    		document.getElementById("tellfriend_container").style.display = "";
	        alert("Momentan aceasta functionalitate este indisponibila.");
				}
			});
		}


	}
}

//tell friend end



function popup(target_url, w, h)
{
 var ret = window.open(target_url, "egirl_popup" , "location=0,status=0,scrollbars=0,width="+w+",height="+h);
 ret.focus();
}

function drawPercentBar(width, percent, color, background) 
{ 
var pixels = width * (percent / 100); 
if (!background) { background = "none"; }

document.write("<div style=\"position: relative;color: #534741;float: left;line-height: 1em; background-color: " 

               + background + "; border: 1px solid #a0824b; width: " 
               + width + "px\">"); 
document.write("<div style=\"height: 1.5em;color: #534741;width: " + pixels + "px; background-color: "
               + color + ";\"></div>"); 
document.write("<div style=\"position: absolute;color: #534741;text-align: center; padding-top: .25em; width: " 
               + width + "px; top: 0; left: 0\">" + percent + "%</div>"); 

document.write("</div>"); 
} 

function boxLogin(box_id, btn_id)
{
	var b = document.getElementById(box_id);
	var btn = document.getElementById(btn_id);
	
	if (b.style.display == "none" || b.style.display == "")
	{
		b.style.display = "block";
		btn.style.display = "none";
	}
	else
	{
		b.style.display = "none";
		btn.style.display = "block";
	}
}

function goToLogin(box_id)
{
	var b = document.getElementById(box_id);
	b.style.display = "block";
	document.getElementById('username').focus();
	
	return false;
	//location.href = "http://www.egirl.ro/lookbook/add#username";
		
}
function add_error_validare(id,msg) 
{
	document.getElementById(id + "_error").innerHTML = msg + "<br />";
	document.getElementById(id + "_error").style.display = "block";
	return false;
}
function no_error_validare(id) 
{
	document.getElementById(id + "_error").style.display = "none";
}
function validateLook(f_name)
{
	var ok = true;
	with(f_name)
	{
		if(f_name["titlu"].value == "") 
		{
			ok = add_error_validare("titlu", "Nu ai completat campul \"Denumeste-ti look-ul\"!");
		}
		else
		{
			no_error_validare("titlu");
		}	
		if(f_name["descriere"].value.length < 10) 
		{	
			ok = add_error_validare("descriere", "Trebuie sa introduci minim 10 caractere la campul \"Detalii despre tinuta postata\"!");
		}
		else
		{
			no_error_validare("descriere");
		}		
		if(f_name["img_loaded"].value != "yes") 
		{
			ok = add_error_validare("photo", "Lipseste poza !");
		}
		else
		{	
			no_error_validare("photo");		
		}
		if((ok) && (f_name["loggedin"].value == ""))
		{
			var cf = confirm("Atentie! In acest moment nu esti logat. Apasand pe butonul OK look-ul va fi adaugat drept Fashionista Anonima. Da click pe Cancel pentru a salva look-ul sub userul tau. ");
			if(cf == false)
			{
				var b = document.getElementById("container_login");
				b.style.display = "block";
				document.getElementById("username").focus();
				document.getElementById("vreau_logare").value = 1;
				document.getElementById("send_personal").style.display = "block";
				document.getElementById("send_login").style.display = "none";
			}
			else
			{
				document.getElementById("vreau_logare").value = 0;
			}
		}
		
		return ok;
	}
}

function validateLook2(f_name)
{
	var ok = true;
	with(f_name)
	{
		if(f_name["titlu"].value == "") 
		{
			ok = add_error_validare("titlu", "Nu ai completat campul \"Denumeste-ti look-ul\"!");
		}
		else
		{
			no_error_validare("titlu");
		}	
		if(f_name["descriere"].value.length < 10) 
		{	
			ok = add_error_validare("descriere", "Trebuie sa introduci minim 10 caractere la campul \"Detalii despre tinuta postata\"!");
		}
		else
		{
			no_error_validare("descriere");
		}		
		if(f_name["img_loaded"].value != "yes") 
		{
			ok = add_error_validare("photo", "Lipseste poza !");
		}
		else
		{	
			no_error_validare("photo");		
		}
		
		if(f_name["captcha"].value == "") 
		{
			ok = add_error_validare("captcha", "Nu ai scris codul de verificare!");
		}
		else
		{
			no_error_validare("captcha");
		}	
		
		return ok;
	}
}
function validateLook3(f_name)
{
	var ok = true;
	with(f_name)
	{
		if(f_name["titlu"].value == "") 
		{
			ok = add_error_validare("titlu", "Nu ai completat campul \"Denumeste-ti look-ul\"!");
		}
		else
		{
			no_error_validare("titlu");
		}	
		if(f_name["descriere"].value.length < 10) 
		{	
			ok = add_error_validare("descriere", "Trebuie sa introduci minim 10 caractere la campul \"Detalii despre tinuta postata\"!");
		}
		else
		{
			no_error_validare("descriere");
		}		
		if(f_name["doChange"].value == 1)
		{
			if(f_name["img_loaded"].value != "yes") 
			{
				ok = add_error_validare("photo", "Lipseste poza !");
			}
			else
			{	
				no_error_validare("photo");		
			}
		}
		//if(f_name["captcha"].value == "") 
		//{
		//	ok = add_error_validare("captcha", "Nu ai scris codul de verificare!");
		//}
		//else
		//{
		//	no_error_validare("captcha");
		//}	
		
		return ok;
	}
}
// gallery popup
function showRules()
{
	newwindow=window.open('/lookbook/reguli','reguli','height=400,width=450');
	if (window.focus) {newwindow.focus()}
}


//added by iulia (slide gallery for lookbook)
scrollStep=7
timerLeft=""
timerRight=""
function scrollDivLeft(id){
  clearTimeout(timerRight) 
  document.getElementById(id).scrollLeft+=scrollStep
  timerRight=setTimeout("scrollDivLeft('"+id+"')",5)
}
function scrollDivRight(id){
  clearTimeout(timerLeft)
  document.getElementById(id).scrollLeft-=scrollStep
  timerLeft=setTimeout("scrollDivRight('"+id+"')",5)
}
function stopMe(){
  clearTimeout(timerRight) 
  clearTimeout(timerLeft)
}

function zoomImage() {
	var ajaxRequest;  // The variable that makes Ajax possible!
	
	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("Your browser broke!");
				return false;
			}
		}
	}

}

function changePhoto(id_d, id_link, val_do, val_dont, do_change)
{
	if(document.getElementById(id_link).innerHTML == val_do)
	{
		document.getElementById(id_d).style.display = "block";
		document.getElementById(id_link).innerHTML = val_dont;
		document.getElementById(do_change).value = "1";
	}
	else
		if(document.getElementById(id_link).innerHTML == val_dont)
		{
			document.getElementById(id_d).style.display = "none";
			document.getElementById(id_link).innerHTML = val_do;
			document.getElementById(do_change).value = "0";
		}	
}

function checkUncheckAll(theElement) {
     var theForm = theElement.form, z = 0;
	 for(z=0; z<theForm.length;z++){
      if(theForm[z].type == 'checkbox' && theForm[z].name != 'checkall'){
	  theForm[z].checked = theElement.checked;
	  }
     }
    }









function getWindowWidth()
{
	var w;
	if (window.innerWidth)
	{
		w = window.innerWidth;
	}
	else if (document.documentElement && document.documentElement.clientWidth)
	{
		w = document.documentElement.clientWidth;
	}
	else if (document.body)
	{
		w = document.body.clientWidth;
	}
	return w;
}

function getWindowHeight()
{
	var h;
	if (window.innerHeight)
	{
		h = window.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
	{
		h = document.documentElement.clientHeight;
	}
	else if (document.body)
	{
		h = document.body.clientHeight;
	}
	return h;
}

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;
}
