﻿
//debugger;
//alert(document.referrer);
//if   (document.referrer = "http://www.referringPageName.html") 
  //    location.href = "yourPage.html"; 


function SendLid(formUrl,fName, lName, areaCode, phone, email, subject, yesMore, noMore, clientId,source)
{
    
    try
    {
        var val;
           
        if(fName != null )
        {
            val = document.getElementById(fName).value;
            
            if(val == null || val == '')
            {
                alert('נא למלא את כל השדות');
                return false;
            }
            
              if(IsInvalidName(val))
            {
                alert('שם פרטי לא חוקי');
                return false;
            }
            
            document.getElementById(clientId + "Fname").value = val;
        }
        
        if(lName != null)
        {
            val = document.getElementById(lName).value;
            
            if(val == null || val == '')
            {
                alert('נא למלא את כל השדות');
                return false;
            }
             if(IsInvalidName(val))
            {
                alert('שם משפחה לא חוקי');
                return false;
            }
            document.getElementById(clientId + "Lname").value = val;
        }
        
        if(areaCode != null)
        {
            var areaCode_list= document.getElementById(areaCode);
           
            if(areaCode_list.value!='-1')
                document.getElementById(clientId + "AreaCode").value = areaCode_list.options[areaCode_list.selectedIndex].text;
            else
            {
                 alert('נא למלא את כל השדות');
                 return false;
            }
        }
            
        if(phone != null)
        {
            val = document.getElementById(phone).value;
            
            if(val == null || val == '')
            {
                alert('נא למלא את כל השדות');
                return false;
            }
            else
                {
                    if(CheckPhone(val) == false)
                    {
                        alert('מספר הטלפון שהוכנס אינו תקין');
                        return false;
                    }
                }
            document.getElementById(clientId + "Phone").value = val;
        }
         
        document.getElementById(clientId + "Phone").value = document.getElementById(clientId + "AreaCode").value + "-" + document.getElementById(phone).value;
         
        if(email != null)
        {
            val = document.getElementById(email).value;
            
            if(val == null || val == '')
            {
                alert('נא למלא את כל השדות');
                return false;
            }
            
			
			if(!CheckEmail(val))
			{
			    alert("שדה דוא''ל אינו חוקי");
			    return false;
			}
			
            document.getElementById(clientId + "Email").value = val;
        }
           
           
         if(yesMore != null && noMore != null)
        {
            var yesValue = document.getElementById(yesMore).checked;
            var NoValue = document.getElementById(noMore).checked;
                       
            if(yesValue == false && NoValue == false)
            {
                alert('נא למלא את כל השדות');
                return false;
            }
             if(yesValue)
            {
                val="כן";
            }
            else if(NoValue)
            {
                val = "לא";
            }                     
            document.getElementById(clientId + "approveMail").value = val;                      
        }
         
       
        if(subject != null)
        {
           var subject_list= document.getElementById(subject);

           if(subject_list.value!='0')
            {    document.getElementById(clientId + "Subject").value = subject_list.options[subject_list.selectedIndex].value;
}
           else 
           {
                alert('נא למלא את כל השדות');
                return false;
           }
        }
     
        try
        {
              	var theform = document.forms[0];

              
              	 theform.action = formUrl;
              
			if(document.all)
		        {
		        if(document.all('__VIEWSTATE')!=null)
			        document.all('__VIEWSTATE').outerHTML="";
		        if(document.all('__EVENTARGUMENT')!=null)
			        document.all('__EVENTARGUMENT').outerHTML="";
		        if(document.all('__EVENTTARGET')!=null)    
			        document.all('__EVENTTARGET').outerHTML="";	
        		
		        }
		        if(document.getElementById(clientId + "M2CID") !=  null)
		            document.getElementById(clientId + "M2CID").name = "M2CID";
		           
		       if(document.getElementById(clientId + "site") !=  null)
		            document.getElementById(clientId + "site").name = "site";
		            
		       if(document.getElementById(clientId + "ReturnTo") !=  null)
		            document.getElementById(clientId + "ReturnTo").name = "ReturnTo";

			 if(document.getElementById(clientId + "Fname") !=  null)
		            document.getElementById(clientId + "Fname").name = "Fname";

			 if(document.getElementById(clientId + "Lname") !=  null)
		            document.getElementById(clientId + "Lname").name = "Lname";

			 if(document.getElementById(clientId + "Phone") !=  null)
		            document.getElementById(clientId + "Phone").name = "Phone"; 

			if(document.getElementById(clientId + "AreaCode") !=  null)
		            document.getElementById(clientId + "AreaCode").name = "AreaCode"; 

			if(document.getElementById(clientId + "Email") !=  null)
		            document.getElementById(clientId + "Email").name = "Email";

			if(document.getElementById(clientId + "Subject") !=  null)
		            document.getElementById(clientId + "Subject").name = "OptionID";
		            
		    if(document.getElementById(clientId + "approveMail") !=  null)
		            document.getElementById(clientId + "approveMail").name = "approveMail";

//window.open("/ThankU.aspx?source=" + source,null,
  //  "height=400,width=400,modal=1,status=yes,toolbar=no,menubar=no,location=no,resizable=no");

		       
//window.showModalDialog("/ThankU.aspx?source=" + source);//, "name", "dialogWidth:200px;dialogHeight:200px");
		        theform.submit();

		      /*  if (location.href.indexOf("?") > -1) {
		            location.href = location.href + "&afterPniot=" + source;
		        }
		        else {
		            location.href = location.href + "?afterPniot=" + source;
		        }*/

		     //   alert(location.href);
		     //  msg();
                alert('הטופס נשלח בהצלחה');
		       window.open("/ThankU.aspx?source=" + source, 'name', "height=255,width=250,toolbar=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no ,modal=yes,dialog=yes ,minimizable =no");

		       //window.open("/ThankU.aspx?source=" + source,null,
    //"height=400,width=400,status=yes,toolbar=no,menubar=no,location=no,resizable=no");
		      
		            //
		       
		     
        }
        catch (e)
        {
            alert(e.message);
            return false;
        }
       
    
    }
    catch(e)
    {
        alert(e.message);
        return false;
    }
    return false;
}


function CheckPhone(sText) {
    var ValidChars = '0123456789';
    var IsNumber = true;
    var Char;

    if (sText.length == 7) {
        for (i = 0; i < sText.length && IsNumber == true; i++) {
            Char = sText.charAt(i);
            if (ValidChars.indexOf(Char) == -1) {
                IsNumber = false;
            }
        }
        return IsNumber;
    }
    else {
        return false;
    }
}


function CheckEmail(sText) {
    var filter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/;

    if (!filter.test(sText)) {
        return false;
    }
    else {
        return true;
    }
}

function CheckIsAlphabet(sText) {
    var filter = /^[a-zA-Z. \u0621-\u064A]{1,50}$/;

    if (!filter.test(sText)) {
        return false;
    }
    else {
        return true;
    }
}

function IsInvalidName(sText) {

    var InValidChars = "0123456789<>?/).,_-(*&^%$#@!~`;+=\"";
    var IsInValid = false;
    var Char;

    if (sText.length > 50)
        return true;
    // IsValidID = true;
    for (i = 0; i < sText.length && IsInValid == false; i++) {
        Char = sText.charAt(i);
        if (InValidChars.indexOf(Char) >= 0) {
            IsInValid = true;
        }
    }
    return IsInValid;
}

 
   