﻿
function fieldCheck(id) {
		var str = document.getElementById(id).value;
		if (!str){
			document.getElementById(id).focus();
			document.getElementById(id).style.background = 'red';
			return false;
		}
		document.getElementById(id).style.background = 'white';
		return true;
}

function emailCheck(id) {
		var str = document.getElementById(id).value;
		var at="@";
		var dot=".";
		var lat=str.indexOf(at);
		var lstr=str.length;
		var ldot=str.indexOf(dot);
		if (str.indexOf(at)==-1){
			document.getElementById(id).focus();
			document.getElementById(id).style.background = 'red';
		   return false;
		}
		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
			document.getElementById(id).focus();
			document.getElementById(id).style.background = 'red';
		   return false;
		}
		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		document.getElementById(id).focus();
			document.getElementById(id).style.background = 'red';
		   return false;
		}
		 if (str.indexOf(at,(lat+1))!=-1){
		 document.getElementById(id).focus();
			document.getElementById(id).style.background = 'red';
		   return false;
		 }
		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
			document.getElementById(id).focus();
			document.getElementById(id).style.background = 'red';
		   return false;
		 }
		 if (str.indexOf(dot,(lat+2))==-1){		 
			document.getElementById(id).focus();
			document.getElementById(id).style.background = 'red';
		   return false;
		 }		
		 if (str.indexOf(" ")!=-1){
			document.getElementById(id).focus();
			document.getElementById(id).style.background = 'Yellow';
		   return false;
		 }
		 document.getElementById(id).style.background = 'white';		 
 		 return true;				
}

function phoneCheck(id) {
   var stripped = document.getElementById(id).value.replace(/[\(\)\.\-\ ]/g, '');    
   var error = "";

   if (document.getElementById(id) == "") {
	document.getElementById(id).focus();
        document.getElementById(id).style.background = 'Yellow';
	return false;
    } else if (isNaN(parseInt(stripped))) {
		document.getElementById(id).focus();
        document.getElementById(id).style.background = 'Yellow';
        alert("Непозволени символи за телефонен номер.\n");
	return false;
    } else if ((stripped.length < 6)) {
        alert("Моля добавете и код на област за телефонен номер\n");
		document.getElementById(id).focus();
        document.getElementById(id).style.background = 'Yellow';
	return false;
    }	 
	document.getElementById(id).style.background = 'white';
    return true;
}

function checkboxCheck(id,label) {
		if (!document.getElementById(id).selected){
			document.getElementById(id).focus();
			document.getElementById(label).style.background = 'red';
			return false;
		}
		document.getElementById(label).style.background = 'none';
		return true;
}

function registrationCheck() {
	var f1 = fieldCheck('Name');
	var f2 = emailCheck('Email');	
	return f1 && f2 ;
}

function contactCheck() {
	var f1 = fieldCheck('Name');
	var f2 = emailCheck('Email');
	return f1 && f2;
}



