// submit form
function saveSupplier() {
    $('SuppliersForm').method = 'POST';
    $('SuppliersForm').action = 'index?object=suppliers&action=supplierssave'; 
    $('SuppliersForm').submit();
}

// clear Supplier search criteria
function clearSupplierSearchCriteria(supplierFieldName,supplierIdFieldName){
    $(supplierFieldName).value='';
    $(supplierIdFieldName).value='';
}

// return supplier
function returnSupplierValue(namereturnfield, idreturnfield, name, id){
  if(namereturnfield =='serchcriteriaSupplierName' || namereturnfield =='sourceSupplier'){
    new Effect.Fade('fadebox', { duration: 0.4 });
  } else {
    myLightbox.end();
  }
  $(namereturnfield).value = name;
  $(idreturnfield).value = id;
}

// add favorite supplier
function submitFavoriteSupplier(){
    $('FavoriteSupplierForm').method = 'POST';
    $('FavoriteSupplierForm').action = 'index?object=favoritesuppliers&action=favoritesuppliersave'; 
    $('FavoriteSupplierForm').submit(); 
}

// show supplier div
function showSupplierDiv(value){
  if($('SupplierDiv').style.display == 'none') {
    $('moreless').innerHTML = 'Less';
    $('SupplierDiv').style.display = 'block';
    if(value == 2  && $('SupplierId').value == '0' ) {
        $('Supplier').value = 'Not Specified';
        $('SupplierId').value = '0';
    }
  }
  else {
    $('moreless').innerHTML = 'More';
    $('SupplierDiv').style.display = 'none';
    if(value == 2 && $('SupplierId').value == '0') {
        $('Supplier').value = '';
        $('SupplierId').value = '0';
        $('Catalogue').value = '';
        $('Purity').value = '';
        $('CatalogueId').value = '';
        $('CatalogueAvailabilityId').value = ''
        $('PackingUnit').value = '';
        $('UnitPrice').value = '';
        $('SupplierComment').value = '';
        $('CatalogueDiv').style.display = 'none';
    }
  }
}

// show search div select supplier form
function showDisabledSearch() {
  for(var i = 0; i < document.form.radio.length; i++) {
    if(document.form.radio[i].checked) {
        if(document.form.radio[i].value == 2) {
            document.getElementById('SearchDiv').style.display = 'block';
            searchSelectSupplier();
            break;
        }
    }
    else
        document.getElementById('SearchDiv').style.display = 'none';
  }
}


// search supplier
function searchSelectSuppliers() {
  var name = document.getElementById('SearchName').value;
  var address = document.getElementById('SearchAddress').value;
  var city = document.getElementById('SearchCity').value;
  var zip = document.getElementById('SearchZip').value;
  var country = document.getElementById('CountryAsText').value;
  var moleculeid = document.getElementById('MoleculeId').value;
  return '&SearchName='+name+'&SearchAddress='+address+'&SearchCity='+city+'&SearchZip='+zip+'&SearchCountryAsText='+country+'&MoleculeId='+moleculeid;
}

// cancel select supplier form
function cancelSelectSupplier() {
  myLightbox.end();
}

// search button select supplier form
function searchSelectSupplier() {
  var myAjaxRequest = new Ajax.Request('index?object=selectsuppliers&action=selectsuppliersearch', {method: 'post', parameters: searchSelectSuppliers(), onComplete: searchAJAXResponse});
}

function searchAJAXResponse(response) {  
  $('tableDiv').innerHTML = response.responseText;
}


// ok select supplier form user is autentificated
function okSelectSupplier(list) {
  var check = false;
        if(document.form.radio[0].checked) {
                check = true;
                $('Supplier').value = 'My favorite';
                $('SupplierId').value = '-1';
                $('CatalogueDiv').style.display = 'none';
                $('Catalogue').value = '';
                $('Purity').value = '';
                $('CatalogueId').value = '';
                $('CatalogueAvailabilityId').value = '';
                $('PackingUnit').value = '';
                $('UnitPrice').value = '';
                $('SupplierComment').value = '';
            } else {

                if(list.length!=undefined){
                //is array
                    for(var j = 0; j < list.length; j++) {
                        if(list[j].checked == true){
                            check = true;
                            if($('Supplier').value != $(list[j].value).innerHTML) {
                                $('SupplierId').value = list[j].value;
                                $('Supplier').value = $(list[j].value).innerHTML;
                                $('Catalogue').value = '';
                                $('Purity').value = '';
                                $('CatalogueId').value = '';
                                $('CatalogueAvailabilityId').value = '';
                                $('PackingUnit').value = '';
                                $('UnitPrice').value = '';
                                $('SupplierComment').value = '';
                                $('CatalogueDiv').style.display = 'block';
                                 break;
                            }
                        }
                    }
                }else{
                //not array, only one element in list      
                    if(list.value > 0) {
                        check = true;
                        if($('Supplier').value != $(list.value).innerHTML) {
                            $('SupplierId').value = list.value;
                            $('Supplier').value = $(list.value).innerHTML;
                            $('Catalogue').value = '';
                            $('Purity').value = '';
                            $('CatalogueId').value = '';
                            $('CatalogueAvailabilityId').value = '';
                            $('PackingUnit').value = '';
                            $('UnitPrice').value = '';
                            $('SupplierComment').value = '';
                            $('CatalogueDiv').style.display = 'block';
                        }
                    }    
                }
            }
  if(check == true) {
    myLightbox.end();
  } else {
    $('SupplierId').value = '0';
    $('Supplier').value = 'Not Specified';
    $('Catalogue').value = '';
    $('Purity').value = '';
    $('CatalogueId').value = '';
    $('CatalogueAvailabilityId').value = '';
    $('PackingUnit').value = '';
    $('UnitPrice').value = '';
    $('SupplierComment').value = '';
    $('CatalogueDiv').style.display = 'none';
    myLightbox.end();
  }
}

// ok select supplier form user not autentificated
function okSelectSupplier1(list) {
  var check = false;

        if(list.length!=undefined){
        //is array
            for(var j = 0; j < list.length; j++) {
                if(list[j].checked == true){
                    check = true;
                    if($('Supplier').value != $(list[j].value).innerHTML) {
                        $('SupplierId').value = list[j].value;
                        $('Supplier').value = $(list[j].value).innerHTML;
                        $('Catalogue').value = '';
                        $('Purity').value = '';
                        $('CatalogueId').value = '';
                        $('CatalogueAvailabilityId').value = '';
                        $('PackingUnit').value = '';
                        $('UnitPrice').value = '';
                        $('SupplierComment').value = '';
                        $('CatalogueDiv').style.display = 'block';
                        break;
                    }
                }
            }
        }else{
         //not array, only one element in list 
            if(list.value > 0) {
                check = true;
                 if($('Supplier').value != $(list.value).innerHTML) {
                    $('SupplierId').value = list.value;
                    $('Supplier').value = $(list.value).innerHTML;
                    $('Catalogue').value = '';
                    $('Purity').value = '';
                    $('CatalogueId').value = '';
                    $('CatalogueAvailabilityId').value = '';
                    $('PackingUnit').value = '';
                    $('UnitPrice').value = '';
                    $('SupplierComment').value = '';
                    $('CatalogueDiv').style.display = 'block';
                }
             }
        }
  if(check == true) {
    myLightbox.end();
  } else {
    $('SupplierId').value = '0';
    $('Supplier').value = 'Not Specified';
    $('Catalogue').value = '';
    $('Purity').value = '';
    $('CatalogueId').value = '';
    $('CatalogueAvailabilityId').value = '';
    $('PackingUnit').value = '';
    $('UnitPrice').value = '';
    $('SupplierComment').value = '';
    $('CatalogueDiv').style.display = 'none';
    myLightbox.end();
  }
}


// ok select supplier form user not autentificated
function okSelectSupplierFavorite(list) {

  var check = false;
    for(var j = 0; j < list.length; j++) {
        if(list[j].checked == true){
            check = true;
            if($('Supplier').value != $(list[j].value).innerHTML) {
                $('SupplierId').value = list[j].value;
                $('Supplier').value = $(list[j].value).innerHTML;
            }
        }
        if(list.value > 0) {
            check = true;
            if($('Supplier').value != $(list[j].value).innerHTML) {
                $('SupplierId').value = list[j].value;
                $('Supplier').value = $(list[j].value).innerHTML;
            }
        }
    }
  if(check == true) {
    myLightbox.end();
  } else {
    $('SupplierId').value = '';
    $('Supplier').value = '';
    myLightbox.end();
  }
  
}

// set group supplier
function setSupplier( value , start) {
            if(value == 1) {
                $('Name').readOnly = true;
                $('Name').className ='textdisabled';
                $('Code').readOnly = true;
                $('Code').className ='textdisabled';
                $('Des').readOnly = true;
                $('Des').className ='textdisabled';
                $('Phone').readOnly = true;
                $('Phone').className ='textdisabled';
                $('Fax').readOnly = true;
                $('Fax').className ='textdisabled';
                $('Address1').readOnly = true;
                $('Address1').className ='textdisabled';
                $('Address2').readOnly = true;
                $('Address2').className ='textdisabled';
                $('City').readOnly = true;
                $('City').className ='textdisabled';
                $('State').readOnly = true;
                $('State').className ='textdisabled';
                $('Zip').readOnly = true;
                $('Zip').className ='textdisabled';
                $('CountryAsText').readOnly = true;
                $('CountryAsText').className ='textdisabled';
                $('Email').readOnly = true;
                $('Email').className ='textdisabled';
                $('WebSite').readOnly = true;
                $('WebSite').className ='textdisabled';
                $('ContactFirstName').readOnly = true;
                $('ContactFirstName').className ='textdisabled';
                $('ContactMiddleName').readOnly = true;
                $('ContactMiddleName').className ='textdisabled';
                $('ContactLastName').readOnly = true;
                $('ContactLastName').className ='textdisabled';
                $('ContactTitle').readOnly = true;
                $('ContactTitle').className ='textdisabled';
                $('Notes').readOnly = true;
                $('Notes').className ='textdisabled';
                $('CountryDiv').style.display = 'none';
                $('SupplierAdd').style.display = 'block';
                document.SuppliersForm.radio[0].checked = true;
                document.SuppliersForm.radio[1].checked = false;
            }else if (value == 2) {
                $('Name').readOnly = false;
                $('Name').className ='text';
                $('Code').readOnly = false;
                $('Code').className ='text';
                $('Des').readOnly = false;
                $('Des').className ='text';
                $('Phone').readOnly = false;
                $('Phone').className ='text';
                $('Fax').readOnly = false;
                $('Fax').className ='text';
                $('Address1').readOnly = false;
                $('Address1').className ='text';
                $('Address2').readOnly = false;
                $('Address2').className ='text';
                $('City').readOnly = false;
                $('City').className ='text';
                $('State').readOnly = false;
                $('State').className ='text';
                $('Zip').readOnly = false;
                $('Zip').className ='text';
                $('CountryAsText').readOnly = false;
                $('CountryAsText').className ='text';
                $('Email').readOnly = false;
                $('Email').className ='text';
                $('WebSite').readOnly = false;
                $('WebSite').className ='text';
                $('ContactFirstName').readOnly = false;
                $('ContactFirstName').className ='text';
                $('ContactMiddleName').readOnly = false;
                $('ContactMiddleName').className ='text';
                $('ContactLastName').readOnly = false;
                $('ContactLastName').className ='text';
                $('ContactTitle').readOnly = false;
                $('ContactTitle').className ='text';
                $('Notes').readOnly = false;
                $('Notes').className ='text';
                if( $('CountryDiv') != undefined ) { 
                    $('CountryDiv').style.display = 'block';
                }
                if(start != 1) {
                    if( $('SupplierAdd') != undefined ) { 
                        $('SupplierAdd').style.display = 'none';
                        document.SuppliersForm.radio[0].checked = false;
                        document.SuppliersForm.radio[1].checked = true;
                    }
                }
            }
}


function submitTradeRules() {
    
    $('SupplierRules').method = 'POST';
    $('SupplierRules').action = 'index?object=suppliers&action=suppliersrulessubmit'; 
    $('SupplierRules').submit();

}
