﻿function GetXmlHttpObject()
{
  var xmlHttp=null;
  try
    {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
    }
  catch (e)
    {
    // Internet Explorer
    try
      {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
    catch (e)
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    }
  return xmlHttp;
}


function sendComment(){
		
	var xmlHttp = GetXmlHttpObject();
	var name = document.getElementById('name').value;
	var email = document.getElementById('email').value;
	var product = document.getElementById('product').value;
	var text = document.getElementById('text1').value;
	var site = document.getElementById('site').value;
	
	//SWthetext = thetext.replace(/(<([^>]+)>)/ig,"");
 
	text = text.replace(/\=/g, "<eq>");
    text = text.replace(/\$/g, "<dolar>");
    text = text.replace(/\&/g, "<amp>");
    text = text.replace(/ /g, "<sp>");
    text = text.replace(/\#/g, "<diez>");
    text = text.replace(/\?/g, "<quest>");
	
    var params = "idproduct=" + product + "&name=" + name + "&email="+email+"&comment="+text+"&site="+site;
    xmlHttp.open("POST", "/comments/addtoproduct", true);

    //Send the proper header information along with the request
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", params.length);
    xmlHttp.setRequestHeader("Connection", "close");
	  xmlHttp.onreadystatechange = function() { 
 	    if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
		
			var response = new String(xmlHttp.responseText);
			document.getElementById("icomment").innerHTML = "Вашия коментар към продукта е добавен успешно и ще бъде видим веднага след като бъде одобрен от администратор.<br><br>";
		}
	}
	xmlHttp.send(params);
}

function checkcomment(){
	
	var name = document.getElementById('name');
	var email = document.getElementById('email');
	var text = document.getElementById('text');
	
	if(name.value == ''){
		alert("Моля въведете Име");
		return false;
	}
	else if(email.value == ''){
		alert("Моля въведете Имейл");
		return false;
	}
	else if(text.value == ''){
		
		alert("Моля въведете Коментар");
		return false;
		
	} else return true;	
}

function checkCaptcha(){

	var xmlHttp = GetXmlHttpObject();
	var ccode = document.getElementById('ccode').value;
	var ccheck = false;
	
    var params = "ccode=" + ccode;
    xmlHttp.open("POST", "/comments/ccode", true);

    //Send the proper header information along with the request
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", params.length);
    xmlHttp.setRequestHeader("Connection", "close");
	  xmlHttp.onreadystatechange = function() { 
 	    if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
		
			var response = new String(xmlHttp.responseText);

			response = response.replace(/^\s+|\s+$/g,"");
			
			if(response == 'OK'){
				sendComment();
			} else {
				alert("Грешен Код");
			}			
		}
	}
	xmlHttp.send(params);
}
