﻿var Name = '', Surname = '', Company = '', Email = '', MailForNewFunctions = '', MailForAdvantages = '';
var BirthDay = '', BirthMonth = '', BirthYear = '', Gender = '', Country = '', City = '', Work = '', ProfessionalState = '';
var Position = '', Sector = '', EducationLevel = '', EducationState = '', GraduationSchool = '', SchoolLocationType = '', Agree = '', InvitationId = '';
var ErrorDesc = '', IsOtherSchool = '', OtherSchoolText = '', IsLogin = '', IsFromMainPage = '', Town = '', WorkArea = '';
		
function loadCombos(){
    var genList = new GeneralLists();
    
    getDayList('ddlBirthDay', Select_BirthDay, BirthDay);
    getMonthList('ddlBirthMonth', Select_BirthMonth, BirthMonth);
    getYearList('ddlBirthYear', Select_BirthYear, BirthYear, 1930, new Date().getFullYear() - 15);

    genList.ListItemAdd('ddlCountry', Select_Country, 'Country', Country);
    genList.ListItemAdd('ddlProfessionalState', Select_ProfessionalState, 'ProfessionalState', ProfessionalState);
    genList.ListItemAdd('ddlPosition', Select_Position, 'Position', Position);
    genList.ListItemAdd('ddlSector', Select_Sector, 'Sector', Sector);
    genList.ListItemAdd('ddlWorkArea', Select_WorkArea, 'WorkArea', WorkArea);
    genList.ListItemAdd('ddlEducationLevel', Select_EducationLevel, 'EducationLevel', EducationLevel);
    //genList.ListItemAdd('ddlEducationState', Select_EducationState, 'EducationState', EducationState);
    
    if(IsFromMainPage == '1'){
        /*if(Company==''){
           document.getElementById('rbWorkU').checked=true;
           setWorkObjects();
        }
        else{
            document.getElementById('rbWorkW').checked=true;
            setWorkObjects();
        }*/
    }
    
    if(Country != "0" && Country != ""){
	    genList.KeyValue('CountryId', Country);
	    genList.ListItemAdd('ddlCity', Select_City, 'City', City);
	}
	
	if(City != "0" && City != ""){
	    genList.KeyValue('CityId', City);
	    genList.ListItemAdd('ddlTown', Select_Town, 'Town', Town);
	}
	
	if(EducationLevel != "0" && EducationLevel != ""){
	    if(EducationLevel != "3"){
	        genList.KeyValue('UniversityTypeId', SchoolLocationType);
	        genList.ListItemAdd('ddlGraduationSchool', Select_GraduationSchool, 'University', GraduationSchool);
	    }
	    else{
	        genList.ListItemAdd('ddlGraduationSchool', Select_GraduationSchool, 'HighSchoolType', GraduationSchool);
	    }
	}
	
    genList.GetLists();
}

function isCityExist(objId, objCount){
    if(objId == 'ddlCity'){
        if(objCount > 0)document.getElementById('dvCityTown').style.display='block';
        else document.getElementById('dvCityTown').style.display='none';
    }
}
function getCity(country){
    var countryId = country.options[country.selectedIndex].value;
 
    if(countryId == 0)
    {
        var obj = document.getElementById("ddlCity");
        obj.options.length = 0;
    }
    else{
        //document.getElementById('dvCityTown').style.display='';
        var genList = new GeneralLists();
        GeneralLists_prototype_delegateFunctionName = "isCityExist";
        genList.KeyValue('CountryId', countryId);
        genList.ListItemAdd('ddlCity', Select_City, 'City','');
        genList.GetLists();
    }
//    if (countryId!=1)
//    {
//    document.getElementById('dvCityTown').style.display='none';
//    }
}

function getTown(city){
    var cityId = city.options[city.selectedIndex].value;
    if(cityId == 0)
    {
        var obj = document.getElementById("ddlTown");
        obj.options.length = 0;
    }
    else{
        var genList = new GeneralLists();
        genList.KeyValue('CityId', cityId);
        genList.ListItemAdd('ddlTown', Select_Town, 'Town','');
        genList.GetLists();
    }
}

function getGraduationSchool(){
    var objMan = new ObjectManager();
    var educationLevelId = objMan.getDdlSelectedValue('ddlEducationLevel');
    
    if(educationLevelId == '0' || educationLevelId == '')
    {
        var obj = document.getElementById("ddlGraduationSchool");
        obj.options.length = 0;
    }
    else{
        var genList = new GeneralLists();
        if(educationLevelId != "3"){
	        genList.KeyValue('UniversityTypeId', '');
	        genList.ListItemAdd('ddlGraduationSchool', Select_GraduationSchool, 'University', GraduationSchool);
	    }
	    else{
	        genList.ListItemAdd('ddlGraduationSchool', Select_GraduationSchool, 'HighSchoolType', GraduationSchool);
	    }
	    
        genList.GetLists();
    }
}
function setValueToObjects(){
    var objMan = new ObjectManager();
    
    if(InvitationId != "")objMan.setTextBoxValue('hdnInvId', InvitationId);
    if(Name != "")objMan.setTextBoxValue('txtName', Name);
    if(Surname != "")objMan.setTextBoxValue('txtSurname', Surname);
    if(Gender != "")objMan.setSelectedRadioButtonListValue('rbGender', Gender);
    if(Email != "")objMan.setTextBoxValue('txtEmail', Email);
    if(Work != "")objMan.setSelectedRadioButtonListValue('rbWork', Work);
    if(Company != "")objMan.setTextBoxValue('txtCompany', Company);
    if(SchoolLocationType != "")objMan.setSelectedRadioButtonListValue('rbSchool', SchoolLocationType);
    if(IsOtherSchool != "")objMan.setCheckBoxChecked('chkOtherSchool');
    if(OtherSchoolText != "")objMan.setTextBoxValue('txtOtherSchool', OtherSchoolText);
    if(Agree != "")objMan.setCheckBoxChecked('chkAgree');
    if(MailForNewFunctions != "")objMan.setCheckBoxChecked('chkMailForNewFunctions');
    if(MailForAdvantages != "")objMan.setCheckBoxChecked('chkMailForAdvantages');
    
    setWorkObjects();
    otherSchoolCheck(document.getElementById('chkOtherSchool'));
}

function checkRegisterForm()
{
    var objMan = new ObjectManager();
    var validation = new Validation();
    validation.add(new ValidationObject('txtName', validation.typeTextBox, validation.validationTypeReqFieldVal, '', Member_Error_Name));
    validation.add(new ValidationObject('txtSurname', validation.typeTextBox, validation.validationTypeReqFieldVal, '', Member_Error_Surname));
    //validation.add(new ValidationObject('ddlGender', validation.typeDropDownList, validation.validationTypeReqFieldVal, '0', Member_Error_Gender));
    validation.add(new ValidationObject('txtEmail', validation.typeTextBox, validation.validationTypeReqFieldVal, '', Member_Error_Email));
    validation.add(new ValidationObject('txtPassword', validation.typeTextBox, validation.validationTypeReqFieldVal, '', Member_Error_Password));
    validation.add(new ValidationObject('txtRePassword', validation.typeTextBox, validation.validationTypeReqFieldVal, '', Member_Error_RePassword));
   
    validation.add(new ValidationObject('ddlBirthDay', validation.typeDropDownList, validation.validationTypeReqFieldVal, '0', Member_Error_BirthDay));
    validation.add(new ValidationObject('ddlBirthMonth', validation.typeDropDownList, validation.validationTypeReqFieldVal, '0', Member_Error_BirthMonth));
    validation.add(new ValidationObject('ddlBirthYear', validation.typeDropDownList, validation.validationTypeReqFieldVal, '0', Member_Error_BirthYear));
   
    validation.add(new ValidationObject('ddlCountry', validation.typeDropDownList, validation.validationTypeReqFieldVal, '0', Member_Error_Country));
   
    if(document.getElementById("ddlCity").options.length > 0){
        validation.add(new ValidationObject('ddlCity', validation.typeDropDownList, validation.validationTypeReqFieldVal, '0', Member_Error_City));
        
        if(document.getElementById("ddlTown").options.length > 0)
            validation.add(new ValidationObject('ddlTown', validation.typeDropDownList, validation.validationTypeReqFieldVal, '0', Member_Error_Town));        
    }    
    
    validation.add(new ValidationObject('ddlProfessionalState', validation.typeDropDownList, validation.validationTypeReqFieldVal, '0', Member_Error_ProfessionalState));
    
    var profState = objMan.getDdlSelectedValue('ddlProfessionalState');
    if(profState == '1' || profState == '2' || profState == '3' || profState == '7')
    {
        validation.add(new ValidationObject('txtCompany', validation.typeTextBox, validation.validationTypeReqFieldVal, '', Member_Error_CompanyName));
        validation.add(new ValidationObject('ddlSector', validation.typeDropDownList, validation.validationTypeReqFieldVal, '0', Member_Error_Sector));
        validation.add(new ValidationObject('ddlWorkArea', validation.typeDropDownList, validation.validationTypeReqFieldVal, '0', Member_Error_WorkArea));
        validation.add(new ValidationObject('ddlPosition', validation.typeDropDownList, validation.validationTypeReqFieldVal, '0', Member_Error_Position));
        validation.add(new ValidationObject('txtCompany', validation.typeTextBox, validation.validationTypeOnlyTextAndSpaceAndNumberAndPunctuationVal, '', Member_Error_CompanyName));
    }
    
    validation.add(new ValidationObject('ddlEducationLevel', validation.typeDropDownList, validation.validationTypeReqFieldVal, '0', Member_Error_EducationState));
    //validation.add(new ValidationObject('ddlEducationState', validation.typeDropDownList, validation.validationTypeReqFieldVal, '0', Member_Error_GraduationState));
   
    if(objMan.getDdlSelectedValue('ddlGraduationSchool') == '-99')
        validation.add(new ValidationObject('txtOtherSchool', validation.typeTextBox, validation.validationTypeReqFieldVal, '', Member_Error_GraduateSchool));
    else 
        validation.add(new ValidationObject('ddlGraduationSchool', validation.typeDropDownList, validation.validationTypeReqFieldVal, '0', Member_Error_GraduateSchool));
   
    validation.add(new ValidationObject('chkAgree', validation.typeCheckBox, validation.validationTypeReqFieldVal, '', Member_Error_MembershipAggrement));
    //validation.add(new ValidationObject('txtCaptcha', validation.typeTextBox, validation.validationTypeReqFieldVal, '', Member_Error_Captcha));
        
    var bool = validation.validate();
   
    if(validation.pageIsValid){
        validation.clear();
        
        var objPass = document.getElementById("txtPassword");
        var objRePass = document.getElementById("txtRePassword");
        
        if(objPass.value != objRePass.value){
            validation.pageIsValid = false;
            bool = false;
            validation.addErrorMessages(Member_Error_Compare_Password);
        }
        
        if(bool && validation.pageIsValid){
            if(objPass.value.length < 6){
                validation.pageIsValid = false;
                bool = false;
                validation.addErrorMessages(Member_Error_Length_Password);
            }
        }
   }
   
   if(validation.pageIsValid){
        validation.clear();
        validation.add(new ValidationObject('txtName', validation.typeTextBox, validation.validationTypeOnlyTextAndSpaceVal, '', Member_Error_Type_Name));
        validation.add(new ValidationObject('txtSurname', validation.typeTextBox, validation.validationTypeOnlyTextAndSpaceVal, '', Member_Error_Type_Surname));
        validation.add(new ValidationObject('txtEmail', validation.typeTextBox, validation.validationTypeEmailVal, '', Member_Error_Type_Email));
        
        bool = validation.validate();
    }
    
   if(!validation.pageIsValid)validation.alertErrorMessages();
    
   return validation.pageIsValid;
}

function setWorkObjects(){
    var objMan = new ObjectManager();
    var profState = objMan.getDdlSelectedValue('ddlProfessionalState');
    
    if(profState == '0' || profState == '')
        profState = ProfessionalState;
    
    var hasUserWork;    
    if(profState == '1' || profState == '2' || profState == '3' || profState == '7')
        hasUserWork = true;
    else
        hasUserWork = false;
    
    //if(profState == '4' || profState == '5' || profState == '6')
      //  hasUserWork = false;
        
    if(hasUserWork){
        if(window.navigator.appName == "Microsoft Internet Explorer"){
            //Sector
            document.getElementById("liSector").style.setAttribute("cssText", "");
            document.getElementById("liSectorData").style.setAttribute("cssText", "");
            document.getElementById("liSectorClear").style.setAttribute("cssText", "");
            document.getElementById("ddlSector").style.display = 'block';
            //WorkArea
            document.getElementById("liWorkArea").style.setAttribute("cssText", "");
            document.getElementById("liWorkAreaData").style.setAttribute("cssText", "");
            document.getElementById("liWorkAreaClear").style.setAttribute("cssText", "");
            document.getElementById("ddlWorkArea").style.display = 'block';
            //Company
            document.getElementById("liCompany").style.setAttribute("cssText", "");
            document.getElementById("liCompanyData").style.setAttribute("cssText", "");
            document.getElementById("liCompanyClear").style.setAttribute("cssText", "");
            document.getElementById("txtCompany").style.setAttribute("cssText", "");
            //Position
            document.getElementById("liPosition").style.setAttribute("cssText", "");
            document.getElementById("liPositionData").style.setAttribute("cssText", "");
            document.getElementById("liPositionClear").style.setAttribute("cssText", "");
            document.getElementById("ddlPosition").style.display = 'block';
            
        }
        else{
            //Sector
            document.getElementById("liSector").setAttribute("style", "");
            document.getElementById("liSectorData").setAttribute("style", "");
            document.getElementById("liSectorClear").setAttribute("style", "");
            document.getElementById("ddlSector").style.display = 'block';
            //WorkArea
            document.getElementById("liWorkArea").setAttribute("style", "");
            document.getElementById("liWorkAreaData").setAttribute("style", "");
            document.getElementById("liWorkAreaClear").setAttribute("style", "");
            document.getElementById("ddlWorkArea").style.display = 'block';
            //Company
            document.getElementById("liCompany").setAttribute("style", "");
            document.getElementById("liCompanyData").setAttribute("style", "");
            document.getElementById("liCompanyClear").setAttribute("style", "");
            document.getElementById("txtCompany").setAttribute("style", "");
            //Position
            document.getElementById("liPosition").setAttribute("style", "");
            document.getElementById("liPositionData").setAttribute("style", "");
            document.getElementById("liPositionClear").setAttribute("style", "");
            document.getElementById("ddlPosition").style.display = 'block';
        }
    }
    else{
        if(window.navigator.appName == "Microsoft Internet Explorer"){
            //Sector
            document.getElementById("liSector").style.setAttribute("cssText", "display:none;");
            document.getElementById("liSectorData").style.setAttribute("cssText", "display:none;");
            document.getElementById("liSectorClear").style.setAttribute("cssText", "display:none;");
            document.getElementById("ddlSector").style.display = 'none';
            //WorkArea
            document.getElementById("liWorkArea").style.setAttribute("cssText", "display:none;");
            document.getElementById("liWorkAreaData").style.setAttribute("cssText", "display:none;");
            document.getElementById("liWorkAreaClear").style.setAttribute("cssText", "display:none;");
            document.getElementById("ddlWorkArea").style.display = 'none';
            //Company
            document.getElementById("liCompany").style.setAttribute("cssText", "display:none;");
            document.getElementById("liCompanyData").style.setAttribute("cssText", "display:none;");
            document.getElementById("liCompanyClear").style.setAttribute("cssText", "display:none;");
            document.getElementById("txtCompany").style.display = 'none';
            //Position
            document.getElementById("liPosition").style.setAttribute("cssText", "display:none;");
            document.getElementById("liPositionData").style.setAttribute("cssText", "display:none;");
            document.getElementById("liPositionClear").style.setAttribute("cssText", "display:none;");
            document.getElementById("ddlPosition").style.display = 'none';
        }
        else{
            //Sector
            document.getElementById("liSector").setAttribute("style", "display:none;");
            document.getElementById("liSectorData").setAttribute("style", "display:none;");
            document.getElementById("liSectorClear").setAttribute("style", "display:none;");
            document.getElementById("ddlSector").style.display = 'none';
            //WorkArea
            document.getElementById("liWorkArea").setAttribute("style", "display:none;");
            document.getElementById("liWorkAreaData").setAttribute("style", "display:none;");
            document.getElementById("liWorkAreaClear").setAttribute("style", "display:none;");
            document.getElementById("ddlWorkArea").style.display = 'none';
            //Company
            document.getElementById("liCompany").setAttribute("style", "display:none;");
            document.getElementById("liCompanyData").setAttribute("style", "display:none;");
            document.getElementById("liCompanyClear").setAttribute("style", "display:none;");
            document.getElementById("txtCompany").style.display = 'none';
            //Position
            document.getElementById("liPosition").setAttribute("style", "display:none;");
            document.getElementById("liPositionData").setAttribute("style", "display:none;");
            document.getElementById("liPositionClear").setAttribute("style", "display:none;");
            document.getElementById("ddlPosition").style.display = 'none';
        }
    }
}

function otherSchoolCheck(obj){
    var objMan = new ObjectManager();
    var schoolId = objMan.getDdlSelectedValue('ddlGraduationSchool');
    if(schoolId == '0' || schoolId == '')schoolId = GraduationSchool;
    
    if(schoolId == '-99'){
        if(window.navigator.appName == "Microsoft Internet Explorer"){
            document.getElementById("txtOtherSchool").style.setAttribute("cssText", "");
            document.getElementById("spnOtherSchool").style.setAttribute("cssText", "");
        }
        else{
            document.getElementById("txtOtherSchool").setAttribute("style", "");
            document.getElementById("spnOtherSchool").setAttribute("style", "");
        }
    }
    else{
        document.getElementById("txtOtherSchool").value = '';
        
        if(window.navigator.appName == "Microsoft Internet Explorer"){
            document.getElementById("txtOtherSchool").style.setAttribute("cssText", "display:none;");
            document.getElementById('spnOtherSchool').style.setAttribute("cssText", "display:none;");
         }
        else{
            document.getElementById("txtOtherSchool").setAttribute("style", "display:none;");
            document.getElementById('spnOtherSchool').setAttribute("style", "display:none;");
        }
    }
}

//source: http://snipplr.com/view.php?codeview&id=561
// Cross-browser implementation of element.addEventListener()
//function addListener(element, type, expression, bubbling)
//{
//  bubbling = bubbling || false;
//  if(window.addEventListener)	{ // Standard
//    element.addEventListener(type, expression, bubbling);
//    return true;
//  } else if(window.attachEvent) { // IE
//    element.attachEvent('on' + type, expression);
//    return true;
//  } else return false;
//}

//var ImageLoader = function(url){
//  this.url = url;
//  this.image = null;
//  this.loadEvent = null;
//};

//ImageLoader.prototype = {
//  load:function(){
//    this.image = document.createElement('img');
//    var url = this.url;
//    var image = this.image;
//    var loadEvent = this.loadEvent;
//    addListener(this.image, 'load', function(e){
//      if(loadEvent != null){
//        loadEvent(url, image);
//      }
//    }, false);
//    this.image.src = this.url;
//  },
//  getImage:function(){
//    return this.image;
//  }
//};

//var imgObj;
//function refreshCaptcha(){
//    var loader = new ImageLoader("Captcha.aspx?v=" + new Date().getTime().toString());
//    loader.loadEvent = function(url, image){
//        var dtImg = document.getElementById("liCaptcha");
//        image.width = 145;
//        image.height = 57;
//        image.className = "captcha";
//       //actions to perform when the image is loaded
//       //alert(url + ' is loaded');
//       while(dtImg.firstChild){
//            dtImg.removeChild(dtImg.firstChild);
//       }
//       dtImg.appendChild(image);
//      //hide loading indicator if applicable
//    }
//    
//    loader.load();
//}

