var Tout;

function getO(id){
  return document.getElementById(id);
}

function Toggle(id1,id2,obj,color){
  setTimeout(function(){ChangeColor(obj,color);},0);
  document.getElementById(id1).style.display ="none";
  document.getElementById(id2).style.display ="block";  
}

function ChangeColor(obj,color){
 obj.style.backgroundColor ="#"+color;
}


function linkMouseOver(obj){
 obj.style.backgroundColor ="#A33905";
 obj.style.color  = "#fff";

}


function showActiveMenu(id){
 if(document.getElementById(id))
   document.getElementById(id).style.display="block"; 
}


function expand(id,level){
   menu_td = document.getElementById('left_menu');	
   element = document.getElementById(id); 
   if(element.style.display=="block"){
      element.style.display = "none";
   }
   else{
   	  walkChildNodes(menu_td,level);
      element.style.display = "block";
   }   
}



function walkChildNodes(obj,level) {

  var i, group, txt;
  group = obj.childNodes;
  for (i = 0; i < group.length; i++) {
   
     if(group[i].nodeType ==1 && group[i].tagName=='DIV'){
        if(group[i].style.display=='block'&& group[i].attributes['level'].value>=level){
           group[i].style.display ='none';
        }
     }

     if (group[i].childNodes.length > 0) {
        walkChildNodes(group[i],level);
     }
  }
}



function linkMouseOut(obj){
 obj.style.backgroundColor="#fff";
 obj.style.color="#000";
}


function show(id,num){
 for(i=0;i<num;i++){
   if(document.getElementById('subm'+i)){
     document.getElementById('subm'+i).style.display="none";}
 }
 if(Tout!="") clearTimeout(Tout); 
   document.getElementById('subm'+id+'').style.display="block";
}

function hide(id){
 if(Tout!="") clearTimeout(Tout); 
   Tout=setTimeout("document.getElementById('subm"+id+"').style.display='none'",500);
}



function removeClass(oElement,sClassName){
 if(oElement && oElement.className) {
    oElement.className = oElement.className.replace(new RegExp('(.*)(^|\\s+)(' + sClassName + ')($|\\s+)(.*)'), '$1$4$5').replace(/(^)\s/, '$1');
 }
}

function lng(lng) {
	var current=window.location.href;
	var new_loc;
	if(/lng=/.test(current))
		new_loc=current.replace(/lng=\w*/,"lng="+lng);
	else if(/\?/.test(current))
		new_loc=current+"&lng="+lng;
	else 
		new_loc=current+"?lng="+lng;  
	window.location.replace(new_loc);
}

function default_currency(objSelect) {
	var new_loc;
	var current=window.location.href;
	var value = objSelect.options[objSelect.selectedIndex].value;
	
	if(/default_currency=/.test(current))
		new_loc=current.replace(/default_currency=\w*/,"default_currency="+value);
	else if(/\?/.test(current))
		new_loc=current+"&default_currency="+value;
	else 
		new_loc=current+"?default_currency="+value;  
	window.location.replace(new_loc);
}

var send = function(link){
 var dataToSend =document.getElementById('calendar_y').value+"."+document.getElementById('calendar_m').value;
 var ajax = new httpAjaxRequest(); 
 if(ajax)
  {  
     ajax.prepareHash({date:dataToSend}); 
     ajax.open("GET", "ajax.php");
     ajax.send();
     ajax.getData();
     ajax.onreadystatechange = function(){ 
           data =ajax.getResult();
           document.getElementById("calendar_div").innerHTML =data.calendar; 
     }  
  }
 else window.location =link + document.getElementById('calendar_y').value+"."+document.getElementById('calendar_m').value; 
}
 
function showPic(pic,width,height) { 
  var sDesrc = "width=" + width + ", height=" + height +
  		", status=no, toolbar=no, menubar=no, scrollbars=no";
  win=window.open("Gallery", "", sDesrc);
  win.document.write("<html><style>body{padding:0px;margin:0px;}</style><body>");
  win.document.write("<img src='uploads_script/gallery/"+pic+"' width='"+width+"' height='"+height+"'><br>");
  win.document.write("</body></html>");
  win.document.close();
} 


function showMap() {
    var map_file;
    map_file = lang == 'geo'? 'map1_GEO.html' : 'map1_ENG.html';
    map_file = "/map/"+map_file;
	sDesrc = "width=950, height=600, left="+(screen.availWidth/2-475)+", top="+(screen.availHeight/2-300)+", status=no, toolbar=no, menubar=no, scrollbars=no, resizable=no";
	win = window.open(map_file, "", sDesrc); 
	win.document.close();
	win.focus();
}


function drawOptions(arr,selected){
  selected = selected||"";
  for(var i in arr){
     select = (i==selected)?"selected":'';
     document.write('<option value="'+i+'" '+select+'>'+arr[i]+'</option>');
   }
}

function switchCountry(obj,selected){
  selected = selected||'';
  var ind = obj.options[obj.selectedIndex].value; 
  var selbox2 = document.getElementById('city');
  delOptions(selbox2);
  for(var i in city){
    if(ind==city[i][0])
      createOption(selbox2,i,city[i][1],selected);
   } 
}

function createOption(parent,val,text,selected){
  var opt = document.createElement("option");
  opt.value = val;
  opt.innerHTML = text;
  if(val==selected)
	 opt.selected = true;
  parent.appendChild(opt);
}

function delOptions(obj){
  var count = obj.options.length-1; 
  for(i=count; i>0; i--)
	obj.removeChild(obj.options[i]);
}


function showVideo(name, width, height, autostart, showcontrols, showstatusbar, showdisplay, autorewind){
	autostart = autostart ? autostart : 'True'; 
	showcontrols = showcontrols ? showcontrols : 'True';
	showstatusbar = showstatusbar ? showstatusbar : 'False';
	showdisplay = showdisplay ? showdisplay : 'False';						
	autorewind = autorewind ? autorewind : 'True';
	
	height = parseInt(height) + 50;
	
	var html = '';
	
    html += '<object width="' + width + '" height="' + height + '" classid="CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95">\n';
    html += '<param name="Filename"      value="' + name + '">\n'; //+ path 
    html += '<param name="AutoStart"     value="' + autostart + '">\n';
    html += '<param name="ShowControls"  value="' + showcontrols + '">\n';
    html += '<param name="ShowStatusBar" value="' + showstatusbar + '">\n';
    html += '<param name="ShowDisplay"   value="' + showdisplay + '">\n';
    html += '<param name="AutoRewind"    value="' + autorewind + '">\n';

    html += '<embed type="application/x-mplayer2"\n'; 
    html += ' pluginspage="http://www.microsoft.com/Windows/Downloads/Contents/MediaPlayer/"'; 
    html += ' width="'         + width + '"\n'; 
    html += ' height="'        + height + '"\n'; 
    html += ' src="'           + name + '"\n';//path + 
    html += ' filename="'      + name + '"\n';
    html += ' autostart="'     + autostart + '"\n';
    html += ' showcontrols="'  + showcontrols + '"\n';
    html += ' showstatusbar="' + showstatusbar + '"\n'; 
    html += ' showdisplay="'   + showdisplay + '"\n';
    html += ' autorewind="'    + autorewind + '">\n';
    html += '</embed>'; 
    html += '</object>\n';

	sDesrc = "width=" + width + ", height=" + height + ", status=no, toolbar=no, menubar=no, scrollbars=no";
	win = window.open("Video", "", sDesrc);
	win.document.write("<html>\n<style>\nbody{padding:0px;margin:0px;}</style>\n<body>\n");
	win.document.write(html);
	win.document.write("</body>\n</html>\n");
	win.document.close();
}


// ### COOKIE
function writeCookie(cookieName, cookieContent, cookieExpireTime){
	var cookiePath = '/';
	if(cookieExpireTime>0){
		var expDate=new Date()
		expDate.setTime(expDate.getTime()+cookieExpireTime*1000*60*60)
		var expires=expDate.toGMTString()
		document.cookie=cookieName+"="+escape(cookieContent)+";path="+escape(cookiePath)+";expires="+expires+";";
	}
    else
		document.cookie=cookieName+"="+escape(cookieContent)+";path="+escape(cookiePath)+";";
}

function readCookie(cookieName){
	var ourCookie=document.cookie;
	if(!ourCookie || ourCookie=="")return ""
	ourCookie=ourCookie.split(";")
	var i=0;
	var Cookie;
	while(i<ourCookie.length){
		Cookie=ourCookie[i].split("=")[0];
		if(Cookie.charAt(0)==" ")
		Cookie=Cookie.substring(1);
		if(Cookie==cookieName){
			return unescape(ourCookie[i].split("=")[1])
		}
		i++;
	}
	return "";
}

function deleteCookie(cookieName){
	var cookiePath = '/';
	document.cookie=cookieName+"="+readCookie(cookieName)+";path="+escape(cookiePath)+";expires=Thu, 01-Jan-1970 00:00:01 GMT;";
}


function previewPic(table_id,pic){
	document.getElementById(table_id).innerHTML = '<img src="'+pic+'">';
}

function CheckAll(formname,groupname) {
	for(i=0;i<document.forms[formname].elements.length;i++) {
		if(document.forms[formname].elements[i].name == groupname+"[]") {
			document.forms[formname].elements[i].checked = document.forms[formname].elements[i].checked?false:true;
		}
    }
}


function SaveGeo(){
 deleteCookie("geo");
 if(document.getElementById('geo').checked)
   writeCookie('geo',"1", 77777);
 else
   writeCookie('geo',"0", 77777);
}

function showMenu(id)
 {
 if(element=document.getElementById(id))
   {
   if(element.style.display!="block") element.style.display="block";
   else element.style.display="none";
   }
 }
function showHide(elmID,param) {
	var obj = document.getElementById(elmID);
	if (param == 'show' || param == 'block') {
		obj.style.display = 'block';
	}
	else if (param == 'hide' || param == 'none') {
		obj.style.display = 'none';
	}
	else {
		obj.style.display = (obj.style.display == 'none')?'block':'none';
	}
}

function CheckIsIE() {
	if (navigator.appName.toUpperCase() == 'MICROSOFT INTERNET EXPLORER') 
		return true;
	else
		return false;
}

// For printing iframes
function print_innerHTML(parseFrom,iframeTo,css_src) {
	document.getElementById(iframeTo).contentWindow.document.body.innerHTML = document.getElementById(parseFrom).innerHTML;
	if (CheckIsIE()==true) {
		iframe = eval ('document.'+iframeTo);
		iframe.close();
		importCSS(get_DOC(document.getElementById(iframeTo)),css_src);
		iframe.focus();
		iframe.print();
	}
	else {
		importCSS(get_DOC(document.getElementById(iframeTo)),css_src);
		document.getElementById(iframeTo).contentWindow.print();
	}
}

function importCSS(doc, css) {
	var css_ary = css.replace(/\s+/, '').split(',');
	var csslen, elm, headArr, x, css_file;

	for (x = 0, csslen = css_ary.length; x<csslen; x++) {
		css_file = css_ary[x];

		if (css_file != null && css_file != 'null' && css_file.length > 0) {
			// Is relative, make absolute
			if (css_file.indexOf('://') == -1 && css_file.charAt(0) != '/')
				css_file = this.documentBasePath + "/" + css_file;

			if (typeof(doc.createStyleSheet) == "undefined") {
				elm = doc.createElement("link");

				elm.rel = "stylesheet";
				elm.href = css_file;

				if ((headArr = doc.getElementsByTagName("head")) != null && headArr.length > 0)
					headArr[0].appendChild(elm);
			} else
				doc.createStyleSheet(css_file);
		}
	}
}

function get_DOC(iframe) {
	if(iframe.contentWindow) 
		return iframe.contentWindow.document;
	else if (iframe.contentDocument) 
		return iframe.contentDocument; 
	else 
		return iframe.document;
}



function ajax_pslt(cat_id,lang,m_id) {
	div_id = 'prts';
	if (cat_id) {
		document.getElementById(div_id).innerHTML = '<select Style="width:100%;"><option>loading..</option></select>';
		var url = 'ajax_psls.php?cat_id='+cat_id+'&lng='+lang+'&m='+m_id;
	    var myAjax = new Ajax.Updater	(div_id,url,{method: 'GET',evalScripts: true, onException:function () {window.location.href='index.php?m='+m_id+'&cat_id='+cat_id} });
	}
}

function prts_redirect(m_id,cat_id,pid) {
	window.location.href='index.php?m='+m_id+'&cat_id='+cat_id+'&pid='+pid;
}


function validateForm(form){
  var errstr = '';
  var validator = new Validator(form);
  validator.validateLength('from_name',3,255);
  validator.validateString('from_email','EMAIL',5,60);
  validator.validateString('to_email','EMAIL',5,60);
  
  var errors = validator.getErrors();
  for(var ind in errors){
     errstr += TEXT[ind] +' - ' + TEXT[errors[ind]]+ "\n";
  }
  
  if(errstr!=''){
     alert(errstr);
     return false;
  }
  return true;
}

function ShowHide(div){
  var o =document.getElementById(div);
  o.style.display = o.style.display=='block'?'none':'block';
}

function Calculate(){
  var form =document.calculator;
  if(form.from.value==""||form.into.value==""||!/\d+(\.d+)?/.test(form.amount.value))
     form.result.value = "Invalid parameters";
  else if(form.from.value==form.into.value)
     form.result.value =form.amount.value;   
  else
    {  
      form.result.value = "";
      for(i=0;i<currency.length;i++)
         { 
           if(currency[i][0]==form.from.value&&currency[i][1]==form.into.value)
             {
               var calculation = form.amount.value*currency[i][2];
               var re = /(\d+(\.\d{0,4})?)/;
               var res = re.exec(calculation);
               form.result.value = RegExp.$1;
               break;
             }  
         } 
      if(form.result.value == "")   
        form.result.value = "no data available";   
    }
}
