$(function() {
	ServusTV.flushDefault();
});

ServusTV.flushDefault = function() {
	$(".default_value").each(function() {
         var default_value = this.value;
   
		$(this).focus(function() {
			if(this.value == default_value) {  
				this.value = '';
			}   
		});
   
		$(this).blur(function() {   
			if(this.value == '') {
				this.value = default_value;
			}
		});
	});
};

function maskEmail(email) {	
    if (trim(email) != "" ) {
	var abc = email.split("|");
	var xyz = abc[0]+"@"+abc[1]+"."+abc[2];
	var temp = "<a href='mailto:"+xyz+"'>"+xyz+"</a>";
	return temp;
    }
}



function checkWholeForm(theForm) {
	var why = "";
	why += checkBlank(theForm.fName.value,"First Name");
	why += checkBlank(theForm.lName.value,"Last Name");
       why += checkEmail(theForm.email.value);    
       //why += checkAlphaChar(theForm.location.value,"Location");    
       why += checkBlank(theForm.message.value,"Message");    
	why += checkBlank(theForm.captcha_response_field.value,"Captcha text");
	
    if (why != "") {
	   document.getElementById("errorMsg").style.display="block";
	   document.getElementById("errorMsg").innerHTML=why;
       return false;
    }
	else
	{
		document.getElementById("errorMsg").innerHTML="";
		document.getElementById("errorMsg").style.display="none";
	   
	}

return true;
}

//added by Raman for recommended article validations

function checkRecommendArticleForm(theForm) {
	
      var error = "";
      error += checkEmail(theForm.toEmail.value,"Reciever's Email Address");
      error += checkEmail(theForm.fromEmail.value,"Sender's Email Address");    
      error += checkBlank(theForm.captcha_response.value,"Captcha text");  
    if (error != "") {
	
   	$('.lightbox_contents').find('#errorMsg').css({display :'block'});
	$('.lightbox_contents').find('#errorMsg').html(error);
  	return false;
    } else {
	$('.lightbox_contents').find('#errorMsg').html("");
	$('.lightbox_contents').find('#errorMsg').css({display :'none'});
    }
return true;
}

function checkNewsletterForm(theForm) {
	var why = "";
	why += checkEmail(theForm.email.value);    
    why += checkBlank(theForm.captcha_response_field.value,"Captcha text");
	
    if (why != "") {
	   document.getElementById("errorMsg").style.display="block";
	   document.getElementById("errorMsg").innerHTML=why;
       return false;
    }
	
return true;
}

function checkCommentsForm(theForm) {
	var why = "";

	why += checkBlank(theForm.commentTitle.value,"Comment Title");
	why += checkBlank(theForm.comments.value,"Comment Text");	    
	if(trim(theForm.commentTitle.value)== trim(commentTitle)) {
		why += "<p>Bitte geben Sie einen Titel ein.</p>";
	}
	if(trim(theForm.comments.value)== trim(commentText)) {
		why += "<p>Bitte geben Sie einen Text ein.</p>";
	}
    	if (why != "") {
	   document.getElementById("errorMsg").style.display="block";
	   document.getElementById("errorMsg").innerHTML=why;
    	   return false;
    	} else {
		document.getElementById("errorMsg").innerHTML="";
		document.getElementById("errorMsg").style.display="none";
	}
	ServusTV.openLayer(".commentsUserInfo");
	
	var commentTxt = theForm.comments.value.replace('\'',"&#146;");
	var commentTtl = theForm.commentTitle.value.replace('\'',"&#146;");
	var hiddenFields = "<input type='hidden' name='comments' value='"+commentTxt+"'><input type='hidden' name='commentTitle' value='"+commentTtl+"'>";

	$(".lightbox_contents").find("#hiddenFields").html(hiddenFields);
	return false;
}

function checkUserInfo(theForm) {
	var why = "";
	why += checkBlank(theForm.userName.value,"User Name");
	why += checkEmail(theForm.emailAdd.value);
	why += checkBlank(theForm.captcha_response_field.value,"Captcha text");
	if(trim(theForm.captcha_response_field.value) != "" && why == "") {
		why += ServusTV.validateCaptch();
	}
	if (why != "") {
	   $('.lightbox_contents').find('#overlayErrorMsg').css({display :'block'});
	   $('.lightbox_contents').find('#overlayErrorMsg').html(why);
       	   return false;
    	} else {
	   $('.lightbox_contents').find('#overlayErrorMsg').css({display :'none'});
	   $('.lightbox_contents').find('#overlayErrorMsg').html("");
	}
	return true;
}

function reverse(str) {
        if(!str) return str;
        return str.charAt(str.length-1) + reverse(str.substring(0,str.length-1));
    }

function trimLeft(str) {
 for (var i=0; str.charAt(i) == ' '; i++);
   return str.substring(i, str.length);
}
/*
var whitespace = " \t\n\r";
for (var i = 0; i < str.charAt(i) == ' '; i++)
           {
                // Check that current character isn't whitespace.
                var c = str.charAt(i);

                if (whitespace.indexOf(c) == -1) {
				return str.substring(i, str.length);
				}

           }
		   }*/
function trimRight(str) {
   return reverse(trimLeft(reverse(str)));
}
 
function trim(str) {
   return trimRight(trimLeft(str));
}


function checkBlank (strng,fieldName) {
 var error = "";
 strng=trim(strng);
 
 if (strng == null || strng.length==0 ) {
	if(fieldName=="First Name") {
    		error = "<p>Bitte geben Sie Ihren Vornamen ein.</p>";
    	} else if(fieldName=="Last Name") {
    		error = "<p>Bitte geben Sie Ihren Nachnamen ein.</p>";
    	} else if(fieldName=="Message") {
    		error = "<p>Tragen Sie bitte Ihren Text ein.</p>";
    	} else if(fieldName=="Captcha text") {
    		error = "<p>Geben Sie bitte die Zeichen ein.</p>";
    	} else if(fieldName=="User Name") {
    		error = "<p>Bitte geben Sie Benutzername ein.</p>";
    	}
     }
 return error;
 }
 
 function checkDropdown(choice,fieldName) {
    var error = "";
    if (choice == 0) {
       error = "<p>Wählen Sie bitte eine Option aus " + fieldName + ".</p>";
    }    
return error;
}    
/*
function checkAlphaChar(charStrng,label){
var error="";
var regExp = /^[a-zA-Z. 0-9]*$/;
var chk = checkBlank(charStrng,label)
  if (chk!==""){
  return chk;
 }
if (charStrng.search(regExp)==-1){ //if match failed
error=("<p>Bitte keine Sonderzeichen verwenden.</p>");
}
return error;
}

function checkChar(charStrng,label){
var error="";
//var regExp = /[a-zA-Z0-9äöüÄÖÜ]*$/;
var regExp =/[^a-z0-9_-äöüß]/
//var regExp = /[a-zA-Z0-9]*$/;
var chk = checkBlank(charStrng,label)
  if (chk!==""){
  return chk;
 }
if (charStrng.search(regExp)==-1){ //if match failed
error=("<p>You did not enter legal characters in " + label +".</p>");
}
return error;
}
*/
function checkNumber(numberStrng,label){
 var checkOK = "+-.0123456789/\\ ";
 var error="";
 var checkStr = numberStrng;
 var allValid = true;
 var decPoints = 0;
 var allNum = "";
 
 var chk = checkBlank(checkStr,label)
  if (chk!==""){
  return chk;
 }
 
 for (i = 0; i < checkStr.length; i++)
 {
 ch = checkStr.charAt(i);
 for (j = 0; j < checkOK.length; j++) {
 var test = checkOK.charAt(j);
 if (ch == test) {
 break;}
 }
 if (j == checkOK.length)
 {
 allValid = false;
 break;
 }
 allNum += ch;
}
 if (!allValid)
 {
 error="<p>Bitte nur Zahlen eingeben " + label + " field.</p>";
 //return (error);
}
 return error;
}

function checkEmail (strng) {
var error="";
var chk = checkBlank(strng,"Email")
  if (chk!==""){
  return chk;
 }
strng = trim(strng);
	//var emailFilter=/^.+@.+\..{2,3}$/;
	var emailFilter=/^(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6}$/;
    if (!(emailFilter.test(strng))) { 
       error = "<p>Bitte geben Sie eine g&uuml;ltige Email Adresse an.</p>";
    }
    
return error;    
}


function validateCaptcha(form,errNo){
	if(errNo=="true"){
	  document.getElementById("errorMsg").style.display="block";
	   document.getElementById("errorMsg").innerHTML="<p>Fehlerhafte Eingabe, bitte versuchen Sie es noch einmal.</p>";
}
	/*if(errNo=="true"){
		form = $(form);
		// 	add the Error box
		//form.prepend("<div class='error-explanation' style='display:none;'><p>"+translate("invalidcaptcha")+"</p></div>");
		//errors = form.find(".error-explanation")[0];
		errors = $(errors);
		fields = form.find(".field");
		
		$("#recaptcha_response_field").parent('.field').addClass('field-with-errors');
		errors.fadeIn();
	}
	else{
		return;
	}*/
}


