//validate email address and form feilds in conact






String.prototype.stripTags = function () {
   return this.replace(/<([^>]+)>/g,'');
}

function validate() {
    if (document.getElementById("email").value == "") { return (false); }
    if (document.getElementById("author").value == "") { return (false); }
    if (document.getElementById("comment").value == "") { return (false); }
    if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(document.getElementById("email").value))
    { return (true); }
    else { return (false); }
}


//################ AJAX
function ajaxObject(url, callbackFunction) {
  var that=this;      
  this.updating = false;
  this.abort = function() {
    if (that.updating) {
      that.updating=false;
      that.AJAX.abort();
      that.AJAX=null;
    }
  }
  this.update = function(passData,postMethod) { 
    if (that.updating) { return false; }
    that.AJAX = null;                          
    if (window.XMLHttpRequest) {              
      that.AJAX=new XMLHttpRequest();              
    } else {                                  
      that.AJAX=new ActiveXObject("Microsoft.XMLHTTP");
    }                                             
    if (that.AJAX==null) {                             
      return false;                               
    } else {
      that.AJAX.onreadystatechange = function() {  
        if (that.AJAX.readyState==4) {             
          that.updating=false;                
          that.callback(that.AJAX.responseText,that.AJAX.status,that.AJAX.responseXML);        
          that.AJAX=null;                                         
        }                                                      
      }                                                        
      that.updating = new Date();                              
      if (/post/i.test(postMethod)) {
        var uri=urlCall+'?'+that.updating.getTime();
        that.AJAX.open("POST", uri, true);
        that.AJAX.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        that.AJAX.send(passData);
      } else {
        var uri=urlCall+'?'+passData+'&timestamp='+(that.updating.getTime()); 
        that.AJAX.open("GET", uri, true);                             
        that.AJAX.send(null);                                         
      }              
      return true;                                             
    }                                                                           
  }
  var urlCall = url;        
  this.callback = callbackFunction || function () { };
}


function set_version(versija){
	var hashish=location.href;
	var iesetotCuuku = new ajaxObject('save_version.php');
	iesetotCuuku.callback = function(responseText) {
		 	eval(responseText);
	}
	iesetotCuuku.update('action=iesetotCuuku&versija='+versija+'&href='+hashish,'POST');
	
}

function show_aiziet(){
	
	var contents = "<tr><td>Vēlies iegādāties kādu no mūsu produktiem, vai vienkārši uzdot kādu jautājumu? Sazinies ar mums, <b>atstājot savu kontaktinformāciju</b>, lai varam tev atbildēt!<br></td></tr><tr><td><a id=\"aiziet_button\" href=\"#\" onclick=\"show_sutit_zinju(); return false;\"><img src=\"/images/aiziet_big.gif\" border=\"0\" height=\"29\" width=\"91\"></a><div id=\"sutit_zinju\"></div></td></tr>";
	$("#aiziet").html(contents);
	$("#sutit_zinju").hide();
}

function sutit_zinju(){
	
	if($("#zinja").val().stripTags()!=""){
		var url = location.href;
		var nosutit_zinju = new ajaxObject('sutit_zinju.php');
		nosutit_zinju.callback = function(responseText) {
			if(responseText!="ok"){
		  	alert(responseText);
			} else {
					
				/*
				$("#sutit_zinju").slideToggle("fast", function(){
					$("#sutit_zinju").removeAttr("style");
					$("#sutit_zinju").hide();
					$("#sutit_zinju").html("Paldies, ziņa tika nosūtīta!");
			
				});	
				*/
			}
		}
		nosutit_zinju.update('action=sutit&zinja='+$("#zinja").val().stripTags()+'&url='+url.stripTags(),'POST');
		$("#sutit_zinju").removeAttr("style");
		$("#sutit_zinju").html("Paldies, ziņa tika nosūtīta!");
	} else  {
		alert("Ievadi takš kautko!");
	}
	
}

function show_sutit_zinju(){
	var contents = "<textarea name=\"zinja\" id=\"zinja\" onKeyDown=\"textCounter(this,'remLen',700);\" onKeyUp=\"textCounter(this,'remLen',700);\" ></textarea><p style=\"float:right;\"><input readonly type=text name=\"remLen\" id=\"remLen\" size=3 maxlength=3 value=\"700\"> simboli atlikuši</p><p><input type=\"button\" value=\"Nosūtīt\" onclick=\"sutit_zinju();\"></p>";
	$("#sutit_zinju").html(contents);
	$("#sutit_zinju").hide();
	$("#sutit_zinju").slideToggle("fast");

	//$("#aiziet_button").attr("onclick", "togle_sutit_zinju(); return false;");
	
	
//	$("#aiziet[a]").attr("onclick", "togle_sutit_zinju(); return false;");
}
function togle_sutit_zinju(){
	$("#sutit_zinju").slideToggle("fast");
}

function textCounter(field, countfield, maxlimit) {
if (field.value.length > maxlimit) 
field.value = field.value.substring(0, maxlimit);
else 
$("#"+countfield).val(maxlimit - field.value.length);
}



function show_spam_input(){
$("#ievadi_epastu").slideToggle("fast");	
}


function add_to_spamlist(epasts){
	if(epasts!=""){
	$("#epasta_button").attr("disabled","disabled");
	var add_spam = new ajaxObject('spam_list.php');
	add_spam.callback = function(responseText) {
		if(responseText!="ok"){
	  	alert(responseText);
		} else {
			alert("Paldies! Jums tika nosūtīts apstiprinājuma epasts!");
			$("#ievadi_epastu").slideToggle("fast");	
		}
	}
	add_spam.update('action=pierakstities&epasts='+epasts.stripTags(),'POST');
	} else {
		alert("Ievadi epastu!");
		
	}	
	
}




function shwrng() {
    if (document.getElementById("email").value == "") {
        document.getElementById("cne").style.display = "inline";
        document.getElementById("cie").style.display = "none";
    }
    else {
        if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(document.getElementById("email").value)) {
            document.getElementById("cie").style.display = "none";
            document.getElementById("cne").style.display = "none";
        }
        else {
            document.getElementById("cie").style.display = "inline";
            document.getElementById("cne").style.display = "none";
        }
    }

    if (document.getElementById("author").value == "")
    { document.getElementById("cns").style.display = "inline"; }
    else
    { document.getElementById("cns").style.display = "none"; }

    if (document.getElementById("comment").value == "")
    { document.getElementById("cnm").style.display = "inline"; }
    else
    { document.getElementById("cnm").style.display = "none"; }
}


function reset_comments(){
		document.getElementById('cne').style.display = 'none';
		document.getElementById('cie').style.display = 'none';
		document.getElementById('cns').style.display = 'none';
		document.getElementById('cnm').style.display = 'none';
		document.getElementById('comment').value = "";
		$("#submit").removeAttr("disabled");

}

function insertAfter(parent, node, referenceNode){

	if(referenceNode.nextSibling){
		parent.insertBefore(node, referenceNode.nextSibling);
	} else {
		parent.appendChild(node);
	}
}




function send() {
	
	 $("#submit").attr("disabled","disabled");


var HTML_request = null; 
if(window.XMLHttpRequest)
	HTML_request = new XMLHttpRequest(); 
else if (window.ActiveXObject)
	HTML_request  = new ActiveXObject(Microsoft.XMLHTTP); 
if (!HTML_request) {
	alert('Giving up :( Cannot create an XMLHTTP instance');
}
    document.getElementById("comments_body_loading").style.visibility = "visible";
    if (HTML_request.readyState == 0 || HTML_request.readyState == 4) {
        try {
            HTML_request.open("POST", "comments-post.php", true);
            HTML_request.onreadystatechange=function() {
                if (HTML_request.readyState==4) { 
										if (HTML_request.status == 200) {					 
					 	document.getElementById("comments_body").innerHTML += HTML_request.responseText;
						execJS('comments_body');
						document.getElementById("comments_body_loading").style.visibility = "hidden";
            			reset_comments();					 
					} else {						
							//errors
					}				                        
        }
      }
      HTML_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
      HTML_request.send("email=" +
			document.getElementById("email").value + "&author=" +
			document.getElementById("author").value + "&comment=" +
			document.getElementById("comment").value + "&nr=" +
			document.getElementById("nr").value + "&alt=" +
			document.getElementById("alt").value + "&id=" +
			document.getElementById("object_id").value);
        }
        catch(e) {
            //doInnerHTML("emailform", e);
        }
    }


}