// JavaScript Document: (c) GigaTel Technologies S.R.L.

window.onload=function() {
	doCookie();
}

// AJAX POPUP WINDOWS
function setOpacity(value,d) {
	document.getElementById(d).style.opacity = value / 10;	// Mozilla
	document.getElementById(d).style.filter = 'alpha(opacity=' + value * 10 + ')';	// IEXPLORER
}
function fadeInMyPopup(d) {
 for( var i = 0 ; i <= 100 ; i++ )	{
	 var x = i / 10;
   setTimeout( 'setOpacity(' + (i / 10) + ',"' + d + '")' , 4 * i );	// ultimul numar este factorul de viteza "alert(i);"
 }
}
function fadeOutMyPopup(d) {
	for( var i = 0 ; i <= 100 ; i++ ) {
		setTimeout( 'setOpacity(' + (10 - i / 10) + ',' + '"' + d + '"' + ')' , 2 * i );
		}
	setTimeout('HideContent("' + d + '")', 200 );
}
function HideContent(d) {
	document.getElementById(d).style.display = "none";
}
function ShowContent(d) {
	if(document.getElementById(d).style.display == "none") { 
	setOpacity( 0,d );
	document.getElementById(d).style.display = "block";
	fadeInMyPopup(d);
	}
}
function ToggleDiv(d) {
	if(document.getElementById(d).style.display == "none") {
		ShowContent(d);
	}
	else {
		HideContent(d);
	}
}

// Arata/sterge continutul in div-ul cu iframe form_tel
function showTelForm(language)	{
	if (document.getElementById('tel_form').innerHTML == "") {
	// Facem query ca sa obtinem continutul
	document.getElementById('tel_form').innerHTML = "<div id=\"close\"><a href=\"javascript:fadeOutMyPopup('tel_form');\">&nbsp;</a></div><iframe name=\"form_tel\" id=\"form_tel\" src=\"http://www.gigatel.ro/forms/telform.php?lang=" + language + "&referer=" + location.href + "\" scrolling=\"no\" frameborder=\"0\"></iframe>";
	}
	ShowContent('tel_form');
}

// AJAX SEND
var xmlHttp;
function sendMsg(parameters,but_id,progress_id)	{ 
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)	{
		alert ("Browser does not support HTTP Request");
 		return;
	}
	document.getElementById(but_id).style.display = "none";	// Facem butonul invizibil dupa apasare
	if(document.getElementById("ssl")) {	// Facem SSL invizibil dupa apasare
		document.getElementById("ssl").style.display = "none";
	}
	document.getElementById(progress_id).style.display = "block"; // Facem progresul vizibil :)
	// In progres bagam iconul miscator
	document.getElementById(progress_id).innerHTML = "<img src=\"/images/public/ICO-ajax-loader.gif\" alt=\"Sending...\" />";
	var url="/includes/_formsubmit.php";
	// Daca primim raspuns de la formsubmit.php, il afisam in loc de iconul de progres
	xmlHttp.onreadystatechange=function()	{
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")	{
			// Inlocuim continutul progresului cu responseText
			document.getElementById(progress_id).innerHTML=xmlHttp.responseText;
			// Facem butonul vizibil numai in caz ca apare clasa .error in progress_id
			var classes=getElementsByClassName('success', 'div', document.getElementById(progress_id));
			if (classes.length == 0)	{
				document.getElementById(but_id).style.display = "block";	// Facem butonul vizibil dupa ce primim rezultat
				}																													// daca nu avem succes
			}
		}
	xmlHttp.open('POST', url, true);
  xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  xmlHttp.setRequestHeader("Content-length", parameters.length);
  xmlHttp.setRequestHeader("Connection", "close");
  xmlHttp.send(parameters);
}
function GetXmlHttpObject()	{
	var xmlHttp=null;
	try	{
		xmlHttp=new XMLHttpRequest();
	}
	catch (e)	{
		try	{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)	{
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}
// Contact functions ----------------------------------------------------------------------------------------
function telContact(obj,but_id,progress_id) {
	var poststr = "formtype=telform" +
								"&page_url=" + encodeURI( document.getElementById("page_url").value ) +
								"&nume=" + encodeURI( document.getElementById("nume").value ) +
								"&telefon=" + encodeURI( document.getElementById("telefon").value ) +
								"&interval=" + encodeURI( document.getElementById("interval").value ) +
								"&mesaj=" + encodeURI( document.getElementById("mesaj").value );
	sendMsg(poststr,but_id,progress_id);
}
function formContact(obj,but_id,progress_id) {
	var poststr = "formtype=contactform" +
								"&page_url=" + encodeURI( document.getElementById("page_url").value ) +
								"&nume=" + encodeURI( document.getElementById("nume").value ) +
								"&organizatie=" + encodeURI( document.getElementById("organizatie").value ) +
								"&orgAddr=" + encodeURI( document.getElementById("orgAddr").value ) +
								"&telefon=" + encodeURI( document.getElementById("telefon").value ) +
								"&email=" + encodeURI( document.getElementById("email").value ) +
								"&contactPrefs=" + encodeURI( document.getElementById("contactPrefs").value ) +
								"&interval=" + encodeURI( document.getElementById("interval").value ) +
								"&website=" + encodeURI( document.getElementById("website").value ) +
								"&mesaj=" + encodeURI( document.getElementById("mesaj").value );
	sendMsg(poststr,but_id,progress_id);
}
function rfpContact(obj,but_id,progress_id) {
	var poststr = "formtype=rfpform" +
								"&page_url=" + encodeURI( document.getElementById("page_url").value ) +
								"&nume=" + encodeURI( document.getElementById("nume").value ) +
								"&organizatie=" + encodeURI( document.getElementById("organizatie").value ) +
								"&orgAddr=" + encodeURI( document.getElementById("orgAddr").value ) +
								"&telefon=" + encodeURI( document.getElementById("telefon").value ) +
								"&email=" + encodeURI( document.getElementById("email").value ) +
								"&contactPrefs=" + encodeURI( document.getElementById("contactPrefs").value ) +
								"&interval=" + encodeURI( document.getElementById("interval").value ) +
								"&website=" + encodeURI( document.getElementById("website").value ) +
								"&mesaj=" + encodeURI( document.getElementById("mesaj").value );
	// Trimitem si checkboxes-urile bifate
	var checkboxes = ["dontknow","consulting","implement","techsupport","solutions","products"];
	for ( var i=0; i<checkboxes.length; i++ ) {
		if(document.getElementById( checkboxes[i] ).checked == true) {
			poststr += "&" + checkboxes[i] + "=" + encodeURI( document.getElementById( checkboxes[i] ).value);
		}
	}
	sendMsg(poststr,but_id,progress_id);
}

function newsLetter(obj,but_id,progress_id) {
	var poststr = "formtype=newsletter" +
								"&lang=" + encodeURI( document.getElementById("lang").value ) +
								"&nume=" + encodeURI( document.getElementById("nume").value ) +
								"&email=" + encodeURI( document.getElementById("email").value );
	sendMsg(poststr,but_id,progress_id);
}

// getElementsByClassName ---------------------------------------------------------------------------------
function getElementsByClassName(strClass, strTag, objContElm) {
  strTag = strTag || "*";
  objContElm = objContElm || document;    
  var objColl = objContElm.getElementsByTagName(strTag);
  if (!objColl.length &&  strTag == "*" &&  objContElm.all) objColl = objContElm.all;
  var arr = new Array();                              
  var delim = strClass.indexOf('|') != -1  ? '|' : ' ';   
  var arrClass = strClass.split(delim);    
  for (var i = 0, j = objColl.length; i < j; i++) {                         
    var arrObjClass = objColl[i].className.split(' ');   
    if (delim == ' ' && arrClass.length > arrObjClass.length) continue;
    var c = 0;
    comparisonLoop:
    for (var k = 0, l = arrObjClass.length; k < l; k++) {
      for (var m = 0, n = arrClass.length; m < n; m++) {
        if (arrClass[m] == arrObjClass[k]) c++;
        if ((delim == '|' && c == 1) || (delim == ' ' && c == arrClass.length)) {
          arr.push(objColl[i]); 
          break comparisonLoop;
        }
      }
    }
  }
  return arr; 
}
// Scrie formularul de contact, ca sa fim in acord cu XHTML
function write_contactform(language) {
	document.write('<iframe id="form_contact" frameborder="0" src="http://www.gigatel.ro/forms/contact.php?lang=' + language + '&referer=' + location.href + '">Browser-ul Dvs nu accepta iFrames. Formularul se gaseste <a href="http://www.gigatel.ro/forms/contact.php?lang=' + language + '&referer=' + location.href + '">aici</a></iframe>');
}
function write_pricecalc(language) {
	document.write('<iframe id="form_pricecalc" frameborder="0" scrolling="no" src="/forms/pricecalc/pricecalc.php?lang=' + language + '&referer=' + location.href + '">Browser-ul Dvs nu accepta iFrames. Formularul se gaseste <a href="http://www.gigatel.ro/forms/pricecalc/pricecalc.php?lang=' + language + '&referer=' + location.href + '">aici</a></iframe>');
}
function resize_height(id,height) {
	parent.document.getElementById(id).style.height = height + "px";
}
function write_rfpform(language) {
	document.write('<iframe id="form_rfp" frameborder="0" src="http://www.gigatel.ro/forms/rfp.php?lang=' + language + '&referer=' + location.href + '">Browser-ul Dvs nu accepta iFrames. Formularul se gaseste <a href="http://www.gigatel.ro/forms/rfp.php?lang=' + language + '&referer=' + location.href + '">aici</a></iframe>');
}
function write_newsletter(language) {
	document.write('<iframe id="newsletter" frameborder="0" src="http://www.gigatel.ro/forms/newsletter.php?lang=' + language + '">Browser-ul Dvs nu accepta iFrames. Formularul se gaseste <a href="http://www.gigatel.ro/forms/newsletter.php?lang=' + language + '">aici</a></iframe>');
}
// To cover IE 5 Mac lack of the push method
Array.prototype.push = function(value) {this[this.length] = value; };

function doCookie() {
  if (document.getElementsByTagName) {
    var links = document.getElementsByTagName("a");
    for (var i=0; i < links.length; i++) {
      if (links[i].className.match("deflang")) {
        links[i].onclick = function() {
					var name = 'LAST_LANG';
					var value = 'ro';
					var days = 30;
          var date = new Date();
					date.setTime(date.getTime()+(days*24*60*60*1000));
					var expires = "; expires="+date.toGMTString();
					document.cookie = name+"="+value+expires+"; path=/";
					window.location.href(this.getAttribute("href"));
          return false;
				};
      }
    }
  }
}
