﻿//==========================================================
// BODY ONLOAD CALL
//==========================================================

function body_onload()
{
    if(typeof window.alertIt == 'function')
    alertIt();
}

//==========================================================
// CHANGE LOADING BUTTON
//==========================================================
function MakeLoading(obj)
{

    r = true;
    i=0;
    while(i<document.forms[0].elements.length)
    {
        c = document.forms[0].elements[i];
        if(c.id.indexOf("btnChooseFile")!=-1)
        {
            if(c.value=="")
            {
                r = false;
            }
        }
        i++;
    }
    
    if(!r)
    {
        alert("יש לבחור קובץ");
        return false;
    }
    else
    {
        obj.style.display="none";
        document.getElementById("btnloading").style.display="block";
        __doPostBack(obj.name,'');
    }
    
}


//==========================================================
// THIS FUNCTION ADDS TO FAVORITE
//==========================================================

function AddToFavorites() 
{
    if (window.sidebar) 
    { // Mozilla Firefox Bookmark
        window.sidebar.addPanel('Picards', 'http://www.picards.co.il', "");
    } 
    else
        if (window.external) // IE Favorite
            window.external.AddFavorite('http://www.picards.co.il', 'Picards');
}

//==========================================================
// CHECK IF FIELD IS NUMBER
//==========================================================

function IsNumeric(obj,dval) 
{
    if(isNaN(obj.value))
    {
        obj.value=dval;
        alert("ערך שגוי");
        return false;
    }
    else
        return true;
}

//==========================================================
// FAILED TO SAVE FLASH IMAGE
//==========================================================
function Fpop()
{
    
    alert("עקב בעיית תקשורת נכשלה שמירת התמונה\nאנא נסו שנית");
}


//==========================================================
// THIS FUNCTION SHOW HIND GIVE OBJECT
//==========================================================
function ShowHideObject(obj)
{
    O = document.getElementById(obj);
    if(O.style.display=="none")
        O.style.display="block";
    else
        O.style.display="none";
        
}

//==========================================================
// THIS FUNCTION SWITCH BETWEEN 2 IMAGES OF THE GIVEN OBJECT
//==========================================================
function SwitchImgs(obj,img1,img2)
{
    if(obj.src.indexOf(img1)!=-1)
        obj.src=img2;
    else
        obj.src=img1;
}



//==========================================================
// DESIGN FORM
//==========================================================

function checkDesignForm()
{
    r = true;
    i=0;
    while(i<document.forms[0].elements.length)
    {
        c = document.forms[0].elements[i];
        if(c.id.indexOf("btnChooseFile")!=-1)
        {
            if(c.value=="")
            {
                alert("יש לבחור קובץ");
                r=false;
            }
            else
            {
                ex = c.value.toLowerCase();
                ex = ex.substring(ex.length-3,ex.length);
                if(ex!="gif" && ex!="jpg" && ex!="bmp")
                {
                    alert("קובץ בסיומת שגויה");
                    r=false;
                }
            }
        }
        if(!r) return r;
        i++;
     }
     return r;
}


//==========================================================
// FORGOT PASSWORD FORM
//==========================================================

function checkForgotPasswordForm()
{
    r = true;
    i=0;
    while(i<document.forms[0].elements.length)
    {
        c = document.forms[0].elements[i];
        if(c.id.indexOf("txtUserName")!=-1)
        {
            if(c.value=="")
            {
                alert("יש למלא שם משתמש");
                r=false;
            }
        }
        if(!r) return r;
        i++;
    }
    return r;
}   




//==========================================================
// LOGIN FORM
//==========================================================

function checkLoginForm()
{
    r = true;
    i=0;
    while(i<document.forms[0].elements.length)
    {
        c = document.forms[0].elements[i];
        if(c.id.indexOf("txtUserName")!=-1)
        {
            if(c.value=="")
            {
                alert("יש למלא שם משתמש");
                r=false;
            }
        }
        if(c.id.indexOf("txtPwd")!=-1)
        {
            if(c.value=="")
            {
                alert("יש למלא סיסמה");
                r=false;
            }
        }
        if(!r) return r;
        i++;
    }
    return r;
}   

//==========================================================
// REGISTER FORM
//==========================================================

function copyAddress(obj)
{
    i=0;
    LvalFN = "";
    LvalLN = "";
    LvalA =  "";
    LvalN =  "";
    LvalC =  "";
    LvalD = "";
    
    if(obj.checked==true)
    {
        while(i<document.forms[0].elements.length)
        {
            c = document.forms[0].elements[i];
            if(c.id.indexOf("txtFirstName")!=-1)
            {
                LvalFN = c.value;
              
            }
            if(c.id.indexOf("txtLastName")!=-1)
            {
                LvalLN = c.value;
              
            }
            if(c.id.indexOf("txtStreetName")!=-1)
            {
                LvalA = c.value;
              
            }
            if(c.id.indexOf("txtHouseNumber")!=-1)
            {
                LvalN = c.value;
               
            }
            if(c.id.indexOf("txtCityName")!=-1)
            {
                LvalC = c.value;
                
            }
            if(c.id.indexOf("txtZip")!=-1)
            {
                LvalD = c.value;
                
            }
            i++;
        }
        i=0;
        while(i<document.forms[0].elements.length)
        {
            c = document.forms[0].elements[i];
            if(c.id.indexOf("txtAddressShipping")!=-1)
            {
                c.value=LvalFN + " " + LvalLN + "\n"+  LvalA + "\n" + LvalN + "\n" +   LvalC + "\n"+ LvalD;
                break;
            }
            i++;
        }
    }
}



function checkRegisterForm()
{
    r = true;
    i=0;
    while(i<document.forms[0].elements.length)
    {
        c = document.forms[0].elements[i];
        //alert(c.type);
        switch(c.type)
        {
            case "text":
            case "password":
            {
                if(c.id.indexOf("txtUserName")!=-1)
                {
                    if(c.value=="")
                    {
                        alert("יש למלא שם משתמש");
                        r=false;
                    }
                }
                if(c.id.indexOf("txtPwd")!=-1 && c.id.indexOf("txtPwdCompare")==-1)
                {
                    if(c.value=="")
                    {
                        alert("יש למלא סיסמה");
                        r=false;
                    }
                }
                if(c.id.indexOf("txtPwdCompare")!=-1)
                {
                    if(c.value=="")
                    {
                        alert("יש למלא אישור סיסמה");
                        r=false;
                    }
                }
                if(c.id.indexOf("txtFirstName")!=-1)
                {
                    if(c.value=="")
                    {
                        alert("יש למלא שם פרטי");
                        r=false;
                    }
                }
                if(c.id.indexOf("txtLastName")!=-1)
                {
                    if(c.value=="")
                    {
                        alert("יש למלא שם משפחה");
                        r=false;
                    }
                }
                if(c.id.indexOf("txtEmail")!=-1)
                {
                    if(c.value=="")
                    {
                        alert("יש למלא דואל");
                        r=false;
                    }
                }
                if(c.id.indexOf("txtPhone")!=-1)
                {
                    if(c.value=="")
                    {
                        alert("יש למלא מספר טלפון");
                        r=false;
                    }
                    else
                    {
                         k = "0123456789-";
                         for(v=0;v<c.value.length;v++)
                         {
                            if(k.indexOf(c.value.substring(v,v+1))==-1)
                            {
                                alert("מספר טלפון לא תקין");
                                r=false;
                            }
                         }
                    }
                }
                if(c.id.indexOf("txtCellPhone")!=-1)
                {
                    if(c.value=="")
                    {
                        alert("יש למלא מספר טלפון נייד");
                        r=false;
                    }
                    else
                    {
                         k = "0123456789-";
                         for(v=0;v<c.value.length;v++)
                         {
                            if(k.indexOf(c.value.substring(v,v+1))==-1)
                            {
                                alert("מספר טלפון נייד לא תקין");
                                r=false;
                            }
                         }
                    }
                }
                if(c.id.indexOf("txtStreetName")!=-1)
                {
                    if(c.value=="")
                    {
                        alert("יש למלא כתובת מגורים");
                        r=false;
                    }
                }
                if(c.id.indexOf("txtHouseNumber")!=-1)
                {
                    if(c.value=="")
                    {
                        alert("יש למלא מספר בית");
                        r=false;
                    }
                }
                if(c.id.indexOf("txtCityName")!=-1)
                {
                    if(c.value=="")
                    {
                        alert("יש למלא עיר");
                        r=false;
                    }
                }
                 if(c.id.indexOf("txtZip")!=-1)
                {
                    if(c.value=="")
                    {
                        //alert("יש למלא מיקוד");
                       // r=false;
                    }
                }
             }   
             break;
            case "textarea":
            {
                 if(c.value=="")
                    {
                        alert("יש למלא כתובת למשלוח");
                        r=false;
                    }
            }
            break;
            case "checkbox":
            {
                if(c.id.indexOf("chkIAgree")!=-1 && !c.checked)
                {
                    alert("יש לאשר את התקנון");
                    r=false;
                }
            }
            break;
        }
       
        if(!r) return r;
        i++;
    }
    
    return r;
    
}

//==========================================================
// THIS FUNCTION CHECKS CREDIT CARD NUMBER
//==========================================================


function validateCreditCard(s) 
{
    // remove non-numerics
    var v = "0123456789";
    var w = "";
    for (i=0; i < s.length; i++) 
    {
        x = s.charAt(i);
        if (v.indexOf(x,0) != -1)
            w += x;
    }
    // validate number
    j = w.length / 2;
    if (j < 6.5 || j > 8 || j == 7) return false;
    var k = Math.floor(j);
    var m = Math.ceil(j) - k;
    var c = 0;
    for (i=0; i<k; i++) 
    {
        a = w.charAt(i*2+m) * 2;
        c += a > 9 ? Math.floor(a/10 + a%10) : a;
    }
    for (i=0; i<k+m; i++) c += w.charAt(i*2+1-m) * 1;
    return (c%10 == 0);
}

//==========================================================
// CHECK CREDIT CARD FORM
//==========================================================
function checkCreditCardForm()
{
    var m = "";
    var y = "";
    r = true;
    i=0;
    while(i<document.forms[0].elements.length)
    {
        c = document.forms[0].elements[i];
        switch(c.type)
        {
            case "text":
            case "password":
            {
                if(c.id.indexOf("txtCCHolderName")!=-1)
                {
                    if(c.value=="")
                    {
                        alert("יש למלא שם בעל כרטיס");
                        r=false;
                    }
                }
                if(c.id.indexOf("txtCCNumber")!=-1)
                {
                    if(c.value=="")
                    {
                        alert("יש למלא שם מספר כרטיס אשראי");
                        r=false;
                    }
                    else
                    {
                        //if(!validateCreditCard(c.value))
                        //{
                        //    alert("מספר כרטיס אשראי לא תקין");
                        //    return false;
                        //}
                    }
                }
                if(c.id.indexOf("txtCCHolderTZ")!=-1)
                {
                    if(c.value=="")
                    {
                        alert("יש למלא ת.ז בעל הכרטיס");
                        r=false;
                    }
                    else
                    {
                        if(!isSSN(c))
                        {
                            alert("ת.ז לא תקין");
                            r=false;
                        }
                    }
                }
                
                if(c.id.indexOf("txtCCHolderTZ")!=-1)
                {
                    if(c.value=="")
                    {
                        alert("יש למלא ת.ז בעל הכרטיס");
                        r=false;
                    }
                }
               } // CLOSE CASE
               case "select-one":
               {
                    if(c.id.indexOf("ddlCCPeriodMonth")!=-1)
                    {
                       m = c.value;
                    }
                    
                    if(c.id.indexOf("ddlCCPeriodYear")!=-1)
                    {
                       y = c.value; 
                    }
               }
            
        } //CLOSE SWITCH
        
      
        
        if(!r) return r;
        i++;
    }
    
      var myDate=new Date();
      mm = myDate.getMonth()+1;
      yy = myDate.getFullYear();
      if(y==yy && parseInt(m)<parseInt(mm))
        {
            alert("תוקף לא תקין");
            r=false;
        }
    return r;

}


//==========================================================
// THESE FUNCTION CHECK TZ
//==========================================================

function isSSN( field )
	{  
	
	  
	  var iCharCount = 0;
	  var SSNCheckSum = 0;
	  var tempSSN = "";
	  var digitsInSocialSecurityNumber = 9;
	  if ( (field.value.length>digitsInSocialSecurityNumber) || (field.value.length < 6) )
	    return false;
	  SSNCheckSum = CalculateSSNCheckSum( field.value );
 
	  /* If checksum is valid and SSN length is 9 then return true */
	  if ( (CalculateSSNCheckSum( field.value )==0) && (field.value.length==digitsInSocialSecurityNumber) )
	    return true;
   
	  tempSSN = AddLeadingChar( field.value ,"0", digitsInSocialSecurityNumber );
	  if (CalculateSSNCheckSum(tempSSN)==0)
	  { 
	    field.value = tempSSN;
	    return 2; 
	  }  
	  tempSSN = AddLeadingChar( field.value ,"0", digitsInSocialSecurityNumber-1);
	  if (tempSSN.length > 8) 
	    return false;
	  SSNCheckSum = CalculateSSNCheckSum(tempSSN);  
	  tempSSN = tempSSN + CalculateSifratBikoret( SSNCheckSum );
	  if (CalculateSSNCheckSum(tempSSN)==0)
	  {
	    field.value = tempSSN;
	    return 2; 
	  }  
	} 
	
function CalculateSSNCheckSum( inputvalue )
	{
	  var ColumnTotal = 0, CheckSum = 0;
	  for (var iCharCount=0; iCharCount<inputvalue.length; iCharCount++)
	  {
	    ColumnTotal = parseInt( inputvalue.charAt(iCharCount) );    
 			/* if number in odd position then mul it to 2 */
 			ColumnTotal = ColumnTotal * ( ( (iCharCount) % 2 ) + 1 );

	    /* if number more than 10 then number = (number - 10) + 1 */
	    if ( ColumnTotal >= 10 ) ColumnTotal = (ColumnTotal - 10) + 1;
	      CheckSum += ColumnTotal;  
	  }      
	  return ( CheckSum % 10 );
	}  

	function CalculateSifratBikoret( inputvalue )
	{
	    return ( 10 - inputvalue );    
	} 

function AddLeadingChar( iString, iChar, iCount )
	{
	  var iCharCount = 0;
	  var ReturnValue = "";
	  
	  for (iCharCount=iString.length; iCharCount<iCount; iCharCount++)
	    ReturnValue = iChar + ReturnValue;
	    
	  return( ReturnValue + iString );
	}