/* New function for Member Details Editing */
var nn4 = (document.layers) ? true : false
var ie = (document.all) ? true : false
var dom = (document.getElementById && !document.all) ? true : false

/*
*Accept only Number and Space
*/
function _jsNumberOnly(evt)
{
    var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode > 32 && (charCode < 48 || charCode > 57))
    {
        return false;
    }else{
        return true;
    }
}

/*
*Accept only valid characters for Phone (0..9 ()+) and Space
*/
function _jsPhoneNumber(evt)
{
    var charCode = (evt.which) ? evt.which : evt.keyCode
    if (charCode > 32 && (charCode < 48 || charCode > 57))
    {
        if (charCode == 43 || charCode == 40 || charCode == 41)
        {
            return true;
        }else{
           return false;
        }
    }else{
        
        return true;
    } 
}


/* 
Validate Abn No
*/
function _jsValidateAbnNo(abnNo)
{
    if (abnNo.length < 11 )
    {
        return false;
    }
    return true
}

/*
*Validate a Phone Number
*Return Formattee Phone Number XX XXXX XXXX
*/
function _jsValidatePhone(phone,checkEmpty)
{
    var hErr = {'err':false,'msg':'','fnum':''};
    if (checkEmpty ==undefined)
    {
        checkEmpty = false;
    }
    //Strip all non numeric char
    var theLen = phone.length;
    var newPhone = "";
    for ( var i = 0 ; i < theLen ; i++ )
    {
        // Character codes for ints 1 - 9 are 48 - 57 | 32 = space
        if (((phone.charCodeAt(i) >= 48 ) && (phone.charCodeAt(i) <= 57)) 
            || phone.charCodeAt(i) == 32
            || phone.charCodeAt(i) == 43
            || phone.charCodeAt(i)== 40
            || phone.charCodeAt(i) == 41)
        {
            newPhone = newPhone + phone.charAt(i);
        }   
    }
    //Everything is ok
    hErr.fnum = newPhone;
    return hErr;   
}

/*
*Validate a Email Address
*/
function _jsValidateEmail(email,checkEmpty)
{
    if (checkEmpty ==undefined)
    {
        checkEmpty = false;
    }
    //Empty 
    if (!checkEmpty && email.length==0)
    {
        return true;
    }
    var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    if (!filter.test(email)) 
    {
        return false;
    }
    return true;
}


function browser(id){
	if(nn4) {
		path = document.layers[id]
	}else if(ie) {
  	
			path = document.all[id]
	}else{
		path = document.getElementById(id)
	}
	return path  //return the path to the css layer depending on which browser is looking at the page
}//End Function browser


//Hide Div 
function jsHideDiv( whichLayer )
{
  var elem, vis;
  if( document.getElementById ) // this is the way the standards work
    elem = document.getElementById( whichLayer );
  else if( document.all ) // this is the way old msie versions work
      elem = document.all[whichLayer];
  else if( document.layers ) // this is the way nn4 works
    elem = document.layers[whichLayer];
  vis = elem.style;
  vis.display = 'none';(vis.display==''||vis.display=='block')?'none':'block';
}
//show Div 
function jsShowDiv( whichLayer )
{
  var elem, vis;
  if( document.getElementById ) // this is the way the standards work
    elem = document.getElementById( whichLayer );
  else if( document.all ) // this is the way old msie versions work
      elem = document.all[whichLayer];
  else if( document.layers ) // this is the way nn4 works
    elem = document.layers[whichLayer];
  vis = elem.style;
  vis.display = '';
}


/*Edit Section */
function jsSaveSection(iSectionId,sAction)
{
    if (typeof(sAction)=='undefined')
    {
        sAction='';
    }
    //Do Javascript Validation for Different Sections
    if (jsValidateSection(iSectionId)==false)
    {
        return false;
    }
    $('#txtSection').val(iSectionId);
    $('#txtAction').val(sAction);
    
    $.post('/members/save_member.isp', $('#editForm').serialize(), function(data, textStatus) {
        
        if (data.err == false) {
            jsRefreshSection(iSectionId);
            jsShowSection(iSectionId);
        } else {
            //alert('Error saving information');
            $('#idErrorSec9').html(data.err_html)
        }
        
    },'json');

	
	return false;
}



/* Cancel the Given Section 
*  Rest the Form
*   
*/
function jsCancelSection(iSectionId)
{
    
    //document.editForm.reset();
    jsRefreshSection(iSectionId);
    //alert(document.forms[0].name);
    jsShowSection(iSectionId);
     
}

/*
*Show the Section in Unedit format
*/
function jsShowSection(iSectionId)
{
    //Change All the Input Text Box to Normal (Readonly = true, class=input_readonly)
    idEditSection = '#idEditSection'+iSectionId;
    
    $(idEditSection).find('input').not('.input_readonly1').not('[type=button]').attr('readonly', true);
    $(idEditSection).find('input').not('.input_readonly1').not('[type=button]').addClass('input_readonly');    
    
    switch (iSectionId)
    {
        case 1:     //Top Details
            break;
        case 71:     //Employee Spesfic Work Address
            jsHideAddress('employeework');
            break;
        case 3: //Home and Other Address
            jsHideAddress('home');
            jsHideAddress('other');
            
            break;
        case 4: //Communcation Profile
            $('#idSection4a_0').show();
            $('#idSection4a_1').hide();
            break;
        case 6: //Pref Email Address
            //Pref Address
            $('#idSection1b_0').show();
            $('#idSection1b_1').hide();
            break;
        case 7: //Employment Category
            //Employment Category
            $('#idSection1a_0').show();
            $('#idSection1a_1').hide();
            break;
        case 9: //Credit Card Details
            toggleCC(1);
            break;
    }
    
    //Show  the Edit Button
    idEditBtn = '#idEditBtn'+iSectionId;
    $(idEditBtn).show();
    
    //Hide the Save Btm
    idSaveBtn = '#idSaveBtn'+iSectionId;
    $(idSaveBtn).hide(); 
    
    //Rest the Error Div
    var idError = '#idErrorSec'+iSectionId;
    var objError = $(idError);
    objError.text('');

}

function toggleCC(type)
{
    
    if (type ==0)
    {
        //Edit button pressed  hide text and show input 
        $('#idCreditCardType').hide();
        $('#idCreditCardNoText').hide();
        $('#idCcExpDate').hide();
        
        
        $('#idCreditCardTypeCombo').show();
        $('#idCreditCardNo').show();
        $('#idCcDateCombo').show();
        $('#idLabelCVV').show();
        $('#idCVVtxt').show();
        
        
    }else{
        //Cancel Button is pressed
        
        $('#idCreditCardType').show();
        $('#idCreditCardNoText').show();
        $('#idCcExpDate').show();
        
        
        $('#idCreditCardTypeCombo').hide();
        $('#idCreditCardNo').hide();
        $('#idCcDateCombo').hide();
        $('#idLabelCVV').hide();
        $('#idCVVtxt').hide();
        
    }
}

/*
*Toggle the Div
*/
function jsToggleDiv(whichLayer)
{
    var elem, vis;
    if( document.getElementById ) // this is the way the standards work
            elem = document.getElementById( whichLayer );
    else if( document.all ) // this is the way old msie versions work
            elem = document.all[whichLayer];
    else if( document.layers ) // this is the way nn4 works
            elem = document.layers[whichLayer];
    vis = elem.style;
    if (vis.display=='none')
    {
            jsShowDiv(whichLayer);
    }else{
            jsHideDiv(whichLayer);
    }
}


/* Window to Change Employer */
function jsChangeEmployer(iMemberNo)
{
     //sLoc = "change_employer.isp?iMemberNo="+iMemberNo;
     sLoc = "change_employer.isp";
     myWin = window.open(''+sLoc,'myWin','left=20,top=20,width=800,height=600,toolbar=0,resizable=0,scrollbars=1');
     myWin.focus();
}


function jsConfirmJoinEmployer(empname,employerid,memno)
{
    msg = "Are you sure you want to join ("+empname+")";
    if(confirm(msg))
    {
        $('#txtNewEmployerId').val(employerid); 
        jsSubmitForm(2);
        return true;
    }
    return false;
}

/*
*Member Changed the Employer so rest Employer Section in Main Screen
*/
function jsChangeEmployerDtl(iMemberNo)
{
    location.reload(true);

}


function jsSubmitForm(iAction)
{
  
  try {
    $('#txtAction').val(iAction);
    $('#editForm').submit();    
  } catch(e) {
    document.editForm.txtAction.value=iAction;
    document.editForm.submit();
  }

}


/*
*Edit the Given Section
*/
function jsEditSection(iSectionId)
{
    switch (iSectionId)
    {
        case 1:  
            break;
        case 71: //Employee Spesfic Work Address
            $('#idWEINotExists').hide();
            jsDisplayAddress('employeework');
            break;
        case 3: //Home and Other Address
            $('#idOtherAddNotExists').hide();
            jsDisplayAddress('home');
            jsDisplayAddress('other');
            break;
        case 4: //Communication Profile
            $('#idSection4a_0').hide();
            $('#idSection4a_1').show();
            break;
        case 5: //Email
        
            break;
        case 6: //Pref Mailing Address
            //Pref Address
            $('#idSection1b_0').hide();
            $('#idSection1b_1').show();
            break
        case 7: //Employment Category
            //Employment Category
            $('#idSection1a_0').hide();
            $('#idSection1a_1').show();
            break;
        case 9: //Credit Card Details
            toggleCC(0);
            break;
    }
    //Change All the Input Text Box to Normal (Readonly = false, class='')
    idEditSection = '#idEditSection'+iSectionId;

    $(idEditSection).find('input').not('.input_readonly1').attr('readonly', false);
    $(idEditSection).find('input').removeClass('input_readonly');
    
    //Hide the Edit Button
    idEditBtn = '#idEditBtn'+iSectionId;
    $(idEditBtn).hide();
    
    //Show the Save Btm
    idSaveBtn = '#idSaveBtn'+iSectionId;
    $(idSaveBtn).show(); 
}

/*
*Display the Address Table if Address Exists
*/
function jsDisplayAddress(sAddType)
{
    //Show the Address Table if Address does not exists
    if (!jsCheckAddressExists(sAddType))
    {
        idName = '#idtbl'+sAddType;
        $(idName).show();
    }
}

/*
*Hide the Address Table if Address Not Exists
*/
function jsHideAddress(sAddType)
{
    //Show the Address Table if Address does not exists
    if (!jsCheckAddressExists(sAddType))
    {
        idName = '#idtbl'+sAddType;
        $(idName).hide();
    }
    
    //Hide 'Not Specifeid Text'
    if (sAddType =='other')
    {
        if (!jsCheckAddressExists(sAddType))
        {
            $('#idOtherAddNotExists').show();       
        }else{
            $('#idOtherAddNotExists').hide();
        }
    }
    
    if (sAddType =='employeework')
    {
        if (!jsCheckAddressExists(sAddType))
        {
            $('#idWEINotExists').show();       
        }else{
            $('#idWEINotExists').hide();
        }
    }
    
    
}



/*
*Refresh a Given Section
*Get Response from Ajax 
*/
function jsRefreshSection(iSectionId)
{
    $.post('/members/ajax_function.isp', {'iSectionId' : iSectionId, 'iMemberNo' : $('#txtMemberNo').val()}, function(data, textStatus) {
        
        
        if (textStatus == 'success') {
        
            jsRefreshValue(iSectionId, data);
            
        } else {
            
            alert('Error with request');
            
        }
        
    }, 'json');
}

/*
*Rest the  Fields by Response that we got 
*/
function jsRefreshValue(iSectionId,objResponse)
{
    //Rest all the Text Boxes Fields that are there in the Section
    idEditSection = '#idEditSection'+iSectionId;
    
    $(idEditSection).find('input').not('.input_readonly1').not('[type=button]').attr('readonly', true);
    $(idEditSection).find('input').not('.input_readonly1').not('[type=button]').addClass('input_readonly');
    
    //Other Fields depending on Section
    switch (iSectionId)
    {
        case 1: //Top Details
            break;
        case 71: //Employee Work Individual Address
            document.editForm.txtWEIExists.value=objResponse.exists?1:0;
            break;
        case 3: //Employee Home and Other Address
            document.editForm.txtHExists.value=objResponse.hexists?1:0;
            document.editForm.txtOExists.value=objResponse.oexists?1:0;
            break;
        case 4: //Your Communication Profile
            //display Communication Profile
            $('#idSection4a_0').html(objResponse.commprofile);
            
            
            //Resest All the Check Boxes
            var sProfile  =objResponse.selprofile; 
            var aProfile = sProfile.split(',');
            $('.cb-commp').attr('checked', false);
            $('.cb-commp').filter(function () { return aProfile.indexOf($(this).val()) != -1  }).attr('checked', true);
            
            
            
            
            /*for (var i = 0; i < aProfile.length; i++) 
            {
                alert(aProfile[i]);
            }*/
             

            break;
        case 5: //Email Address
            break;
        case 6: //Pref Mail Address
            //Change Pref Address Type
            $('#idSection1b_0').html(objResponse.prefadd_type);
            //Imp to Get the Value of Radio button
            var item = $('input[name=optPrefAdd][checked]');
            //alert(item.value);
            $('input[name=optPrefAdd][value=' + objResponse.prefadd + ']').attr('checked',true);
            break;
        case 7: //Employment Category
            $('#idSection1a_0').html(objResponse.empcatname);
            $('#idCmbEmpCat').val(objResponse.empcat);
            break;
        case 9: //Credit Card 
            $('#IdNameOnCard').html(objResponse.type)
            $('#idCreditCardType').html(objResponse.type)
            $('#idCreditCardNoText').html(objResponse.display_cc_number)
            $('#idCcExpDate').html(objResponse.expirydate)
            $('#idCVV').html(objResponse.cvv)
            
            break;
     
    }
    
}

/* Check if a Given Address Type Exists */
function jsCheckAddressExists(sAddType)
{
    
    iValue=0;
    switch (sAddType)
    {
        case 'employeework':
            iValue =parseFloat(document.editForm.txtWEIExists.value);
            break;
        case 'home':
            iValue =parseFloat(document.editForm.txtHExists.value);
            break;
        case 'other':
            iValue =parseFloat(document.editForm.txtOExists.value);
            break;
    
    }
    if (iValue ==1)
    {
        return true;
    }else{
        return false;
    }            
}

/* Remove the Given Address */
function jsRemoveAddress(iSectionId,sAddType)
{
    //jsSaveSection(iSectionId)
    document.editForm.submit();   
    //alert (iSectionId);

}


function jsConfirmSubmit(msg,iAction,iId)
{
    if(confirm(msg))
    {
        document.editForm.txtAction.value=iAction;
        document.editForm.txtIid.value=iId;
        document.editForm.submit();
    }
}

function jsSubmitForm1(iAction,iId)
{
    document.editForm.txtAction.value=iAction;
    document.editForm.txtIid.value=iId;
    document.editForm.submit();
}

/*
*Javascript Validation on Different Section
*/
function jsValidateSection(iSectionId)
{
    //Top Section
    if (iSectionId ==1)
    {
        //All is Ok with Section so return True
        return true;
    }
    //Employer Work Address
    if (iSectionId==71)
    {
        //Phone
        var phone = document.editForm.txtWEIPhone.value;
        var hErr =_jsValidatePhone(phone); 
        if (hErr.err)
        {
            alert("Invalid Work Phone number !");
            document.editForm.txtWEIPhone.focus();
            return false;
        }else{
            document.editForm.txtWEIPhone.value = hErr.fnum;
        }
        //FAX
        var fax = document.editForm.txtWEIFax.value;
        var hErr =_jsValidatePhone(fax); 
        if (hErr.err)
        {
            alert("Invalid Work Fax number !");
            document.editForm.txtWEIFax.focus();
            return false;
        }else{
            document.editForm.txtWEIFax.value = hErr.fnum;
        }
        
        //Work Spesfic Email
        var email = document.editForm.txtWEIEmail.value;
        if (!_jsValidateEmail(email))
        {
            alert("Invalid Email Address");
            document.editForm.txtWEIEmail.focus();
            return false;
        }
        
    
        //All is Ok with Section so return True
        return true;
    }
    
    //Home and Other Address
    
    if (iSectionId ==3)
    {
        //Home Phone
        var phone = document.editForm.txtPhone.value;
        var hErr =_jsValidatePhone(phone); 
        if (hErr.err)
        {
            alert("Invalid Home Phone number !");
            document.editForm.txtPhone.focus();
            return false;
        }else{
            document.editForm.txtPhone.value = hErr.fnum;
        }
        //HOME FAX
        var fax = document.editForm.txtFax.value;
        var hErr =_jsValidatePhone(fax); 
        if (hErr.err)
        {
            alert("Invalid Home Fax number !");
            document.editForm.txtFax.focus();
            return false;
        }else{
            document.editForm.txtFax.value = hErr.fnum;
        }
        
        //Mobile
        var phone = document.editForm.txtMobile.value;
        var hErr =_jsValidatePhone(phone); 
        if (hErr.err)
        {
            alert("Invalid Mobile number !");
            document.editForm.txtMobile.focus();
            return false;
        }else{
            document.editForm.txtMobile.value = hErr.fnum;
        }
    
        //All is Ok with Section so return True
        return true;
    }
    
    //email Section
    if (iSectionId ==5)
    {
        //Email
        var email = document.editForm.txtEmail.value;
        if (!_jsValidateEmail(email,true))
        {
            alert("Invalid Email Address");
            document.editForm.txtEmail.focus();
            return false;
        }
        //All is Ok with Section so return True
        return true;
    }
    //Pref Email Address
    if (iSectionId ==6)
    {
        //All is Ok with Section so return True
        return true;
    }
    
    //Employment Category
    if (iSectionId ==7)
    {
        //All is Ok with Section so return True
        return true;
    }
    
    
    
    //All Check so Return True
    return true;
}

