// JavaScript Document

/**
 * DHTML email validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */

function echeck(str) {
	//var alert_str='Adresa de email invalida';
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	  // alert(alert_str)
	   return false
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   //alert(alert_str)
	   return false
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		//alert(alert_str)
		return false
	}

	 if (str.indexOf(at,(lat+1))!=-1){
		//alert(alert_str)
		return false
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		//alert(alert_str)
		return false
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
		//alert(alert_str)
		return false
	 }
	
	 if (str.indexOf(" ")!=-1){
		//alert(alert_str)
		return false
	 }

	 return true					
}


/**
 * DHTML phone number validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */

// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 10;

function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}
function trim(s)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not a whitespace, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (c != " ") returnString += c;
    }
    return returnString;
}
function stripCharsInBag(s, bag)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function checkInternationalPhone(strPhone){
	var bracket=3
	strPhone=trim(strPhone)
	if(strPhone.indexOf("+")>1) return false
	if(strPhone.indexOf("-")!=-1)bracket=bracket+1
	if(strPhone.indexOf("(")!=-1 && strPhone.indexOf("(")>bracket)return false
	var brchr=strPhone.indexOf("(")
	if(strPhone.indexOf("(")!=-1 && strPhone.charAt(brchr+2)!=")")return false
	if(strPhone.indexOf("(")==-1 && strPhone.indexOf(")")!=-1)return false
	s=stripCharsInBag(strPhone,validWorldPhoneChars);
	return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}



function show_popup(id){
	$(id).style.display="inline";
}

function hide_popup(id){
	$(id).style.display="none";
}

/* trim */

function trim(str, chars) {
    return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function check_form_contact(){
    var err="";
    if (!$F("nume") > " ") err += " - Numele dvs\n";
    if ((!$F("telefon") > " ") || (checkInternationalPhone($('telefon').value)==false)) err += " - Numarul dvs de telefon\n";
    if ((!$F("email") > " ") || (echeck($('email').value)==false)) err += " - Email-ul dvs\n";	
    if (!$F("comentariu") > " ") err += " - Mesajul dvs\n";		
	if (err >" "){
        alert("Urmatoarele campuri nu sunt completate corect: \n"+err);
        return false;
    } else{
		$('date_valide').value="1";	
	}
    return true;
}

function check_form_inscriere(){
    var err="";
    if (!$F("denumire") > " ") err += " - Denumirea tipografiei\n";
    if ((!$F("telefon") > " ") || (checkInternationalPhone($('telefon').value)==false)) err += " - Numarul de telefon\n";
    if ((!$F("email") > " ") || (echeck($('email').value)==false)) err += " - Email-ul\n";	
    if (!$F("descriere") > " ") err += " - Descrierea tipografiei\n";
    if (!$F("oras") > " ") err += " - Orasul\n";
    if (!$F("pers_contact") > " ") err += " - Persoana de contact\n";	
	if (err >" "){
        alert("Urmatoarele campuri nu sunt completate corect: \n"+err);
        return false;
    } else{
		$('date_valide').value="1";	
	}
    return true;
}

function open_url_center(url,width,height)
{
	xpoz=Math.round((screen.width-width)/2);
	ypoz=Math.round((screen.height-height)/2);
	if (xpoz<0) xpoz=10;
	if (ypoz<0) ypoz=10;
	newWindow=window.open (url ,'pozaWindow','toolbar=no,scrollbars=yes ,width='+width+',height='+height+', left='+xpoz+', top='+ypoz);
	newWindow.focus();
}

function verifica_form_trimite()
{
	mail=document.getElementById('email_to').value;
	nume=document.getElementById('name_from').value;
	if ((mail.length<5) || (mail.search('@') == -1) || (mail.search('.') == -1)) 
	{
		alert ("Emailul prietenului este invalid");
		return false;
	}
	else if (nume.length==0) 
	{
		alert ("Introduceti numele dumneavoastra");
		return false;
	}
	return true;
}

function ver_login()
{
	if($("user_id_login").value=="")
	{
		alert("Nu ati introdus adresa email.");
		return false;
	}
	if($("pass_login").value=="")
	{
		alert("Nu ati introdus parola");
		return false;
	}
	return true;
}